Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 52 additions & 60 deletions src/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,52 @@
import mongoose, { Document, PopulatedDoc, Schema } from 'mongoose';

import TextService from '../services/TextService';
import { Model } from '../utils/constants';
import { BaseModel, ID } from '../utils/types';
import Post, { PostDocument } from './Post';
import User, { UserDocument } from './User';

/**
* TODO: (5.01)
* - Read this interface.
* - Delete this comment once you've done so.
*/
interface IComment extends BaseModel {
/**
* User that is associated with the creation of the comment.
*/
author: PopulatedDoc<UserDocument>;

/**
* Text content of the comment.
*/
content: string;

/**
* Post that the comment was created on.
*/
post: PopulatedDoc<PostDocument>;
}

export type CommentDocument = Document<{}, {}, IComment> & IComment;

const commentSchema: Schema<CommentDocument> = new Schema<CommentDocument>(
{
/**
* (5.02) TODO:
* - Create the schema for the Comments that we'll save in the database using
* the interface above as a reference.
* - Delete this comment and the example field.
* - Add comment(s) to explain your work.
*/
exampleField: { ref: Model.USER, required: false, type: ID, unique: false }
},
{ timestamps: true }
);

commentSchema.pre('save', function () {
if (this.isNew) {
/**
* TODO: (6.05)
* - Send a text to the author of the post notifying them that a podmate
* commented under it!
*/
}
});

const Comment: mongoose.Model<CommentDocument> =
mongoose.model<CommentDocument>(Model.COMMENT, commentSchema);

export default Comment;
import mongoose, { Document, PopulatedDoc, Schema } from 'mongoose';

import TextService from '../services/TextService';
import { Model } from '../utils/constants';
import { BaseModel, ID } from '../utils/types';
import Post, { PostDocument } from './Post';
import User, { UserDocument } from './User';

interface IComment extends BaseModel {
/**
* User that is associated with the creation of the comment.
*/
author: PopulatedDoc<UserDocument>;

/**
* Text content of the comment.
*/
content: string;

/**
* Post that the comment was created on.
*/
post: PopulatedDoc<PostDocument>;
}

export type CommentDocument = Document<{}, {}, IComment> & IComment;

const commentSchema: Schema<CommentDocument> = new Schema<CommentDocument>(
{
author: { ref: Model.USER, required: true, type: ID },
content: { required: true, type: String },
post: { ref: Model.POST, required: true, type: ID }
},
{ timestamps: true }
);

commentSchema.pre('save', async function () {
if (this.isNew) {
const post: PostDocument = await Post.findById(this.post);
const postAuthor: UserDocument = await User.findById(post.author);

TextService.sendText({
message: 'One of your podmates commented on your post',
to: postAuthor.phoneNumber
});
}
});

const Comment: mongoose.Model<CommentDocument> =
mongoose.model<CommentDocument>(Model.COMMENT, commentSchema);

export default Comment;
230 changes: 116 additions & 114 deletions src/models/Post.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,116 @@
import mongoose, { Document, PopulatedDoc, Schema } from 'mongoose';

import TextService from '../services/TextService';
import { Model } from '../utils/constants';
import { BaseModel, ID } from '../utils/types';
import { CommentDocument } from './Comment';
import { ReactionDocument } from './Reaction';
import User, { UserDocument } from './User';

/**
* TODO: (3.01)
* - Read this enum.
* - Delete this comment.
*/
export enum PostType {
HELP = 'HELP', // Asking for help...
TIL = 'TIL', // Today I learned...
WIN = 'WIN' // Sharing a win...
}

/**
* TODO: (3.02)
* - Read this interface.
* - Delete this comment once you've done so.
*/
interface IPost extends BaseModel {
/**
* User that is associated with the creation of the post.
*/
author: PopulatedDoc<UserDocument>;

/**
* List of comments that were created on the post.
*/
comments: PopulatedDoc<CommentDocument>[];

/**
* Text content of the post.
*/
content: string;

/**
* List of reactions that were created on the reaction.
*/
reactions: PopulatedDoc<ReactionDocument>[];

/**
* Type of the post that was created. This can be null, if no PostType
* if specified.
*/
type?: PostType;
}

export type PostDocument = Document<{}, {}, IPost> & IPost;

const postSchema: Schema<PostDocument> = new Schema<PostDocument>(
{
/**
* TODO: (3.03)
* - Create the schema for the Posts that we'll save in the database using
* the interface above as a reference.
* - Delete this comment and the example field.
* - Add comment(s) to explain your work.
*/
exampleField: { required: true, type: String }
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);

const sendNotification = async function (
author: PopulatedDoc<UserDocument, {} & string>
) {
/**
* TODO: (6.04)
* - Send a text to all the users except for the author of this post letting
* them know that their podmate shared an update!
*/
};

postSchema.pre('save', function () {
if (this.isNew) {
sendNotification(this.author);
}
});

// Creates a "virtual" property on the Post model called 'comments'. By
// default, this sorts comments by the createdAt in ascending order (AKA we
// want to see newer comments last).
postSchema.virtual('comments', {
foreignField: 'post',
localField: '_id',
options: { sort: { createdAt: 1 } },
ref: Model.COMMENT
});

// Similar to above, creates a "virtual" property called 'reactions' and we
// want to sort these in ascending order by their creation date/time.
postSchema.virtual('reactions', {
foreignField: 'post',
localField: '_id',
options: { sort: { createdAt: 1 } },
ref: Model.REACTION
});

const Post: mongoose.Model<PostDocument> = mongoose.model<PostDocument>(
Model.POST,
postSchema
);

export default Post;
import mongoose, { Document, PopulatedDoc, Schema } from 'mongoose';
import { MemberContext } from 'twilio/lib/rest/api/v2010/account/queue/member';

import TextService from '../services/TextService';
import { Model } from '../utils/constants';
import { BaseModel, ID } from '../utils/types';
import { CommentDocument } from './Comment';
import { ReactionDocument } from './Reaction';
import User, { UserDocument } from './User';

export enum PostType {
HELP = 'HELP', // Asking for help...
TIL = 'TIL', // Today I learned...
WIN = 'WIN' // Sharing a win...
}
interface IPost extends BaseModel {
/**
* User that is associated with the creation of the post.
*/
author: PopulatedDoc<UserDocument>;

/**
* List of comments that were created on the post.
*/
comments: PopulatedDoc<CommentDocument>[];

/**
* Text content of the post.
*/
content: string;

/**
* List of reactions that were created on the reaction.
*/
reactions: PopulatedDoc<ReactionDocument>[];

/**
* Type of the post that was created. This can be null, if no PostType
* if specified.
*/
type?: PostType;
}

export type PostDocument = Document<{}, {}, IPost> & IPost;

const postSchema: Schema<PostDocument> = new Schema<PostDocument>(
{
/**
* TODO: (3.03)
* - Create the schema for the Posts that we'll save in the database using
* the interface above as a reference.
* - Delete this comment and the example field.
* - Add comment(s) to explain your work.
*/
author: { ref: Model.USER, required: true, type: ID },
content: { required: true, type: String },
type: { required: false, type: string }
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);

const sendNotification = async function (
author: PopulatedDoc<UserDocument, {} & string>
) {
/**
* TODO: (6.04)
* - Send a text to all the users except for the author of this post letting
* them know that their podmate shared an update!
*/
const allUsers: UserDocument[] = await User.find();

allUsers.map((user) => {
if (user !== author) {
TextService.sendText({
message: 'One of your podmates has shared a post',
to: user.phoneNumber
});
}
});
};

postSchema.pre('save', function () {
if (this.isNew) {
sendNotification(this.author);
}
});

// Creates a "virtual" property on the Post model called 'comments'. By
// default, this sorts comments by the createdAt in ascending order (AKA we
// want to see newer comments last).
postSchema.virtual('comments', {
foreignField: 'post',
localField: '_id',
options: { sort: { createdAt: 1 } },
ref: Model.COMMENT
});

// Similar to above, creates a "virtual" property called 'reactions' and we
// want to sort these in ascending order by their creation date/time.
postSchema.virtual('reactions', {
foreignField: 'post',
localField: '_id',
options: { sort: { createdAt: 1 } },
ref: Model.REACTION
});

const Post: mongoose.Model<PostDocument> = mongoose.model<PostDocument>(
Model.POST,
postSchema
);

export default Post;
Loading