From 4225a5a0296ac9d064acddf311c8517ef1ea8278 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 11 Jun 2022 09:00:49 -0500 Subject: [PATCH] create conversation should work with frontend objectIds --- packages/api/routes/conversations.ts | 61 ++++++++++++++++--- packages/api/services/user.service.ts | 13 ++++ packages/core/models/conversation.model.ts | 14 +++-- .../CreateConversationRequest.request.ts | 3 +- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/packages/api/routes/conversations.ts b/packages/api/routes/conversations.ts index a9e7084..1b592b8 100644 --- a/packages/api/routes/conversations.ts +++ b/packages/api/routes/conversations.ts @@ -1,11 +1,17 @@ +import Conversation, { + ConversationMember, + ConversationUserMember, + MemberRole, +} from '@nirvana/core/models/conversation.model'; +import { JwtClaims, authCheck } from '../middleware/auth'; import express, { NextFunction, Request, Response } from 'express'; -import Conversation from '@nirvana/core/models/conversation.model'; import ConversationService from '../services/conversation.service'; import CreateConversationRequest from '@nirvana/core/requests/CreateConversationRequest.request'; import CreateConversationResponse from '@nirvana/core/responses/CreateConversationResponse.response'; +import { MemberState } from '../../core/models/conversation.model'; import NirvanaResponse from '@nirvana/core/responses/nirvanaResponse'; -import { authCheck } from '../middleware/auth'; +import { UserService } from '../services/user.service'; export default function getConversationRoutes() { const router = express.Router(); @@ -30,17 +36,57 @@ export default function getConversationRoutes() { const createConversation = async (req: Request, res: Response, next: NextFunction) => { const createRequest = req.body as CreateConversationRequest; + const userInfo = res.locals.userInfo as JwtClaims; - console.log(createRequest.conversation); + console.log('other members', createRequest.otherMemberIds); - if (!createRequest.conversation) { - return next(new Error('must pass in a conversation')); + if (!createRequest.otherMemberIds || createRequest.otherMemberIds.length === 0) { + return next(new Error('must provide who you want to talk to')); } - const insertResult = await ConversationService.createConversation(createRequest.conversation); + // process of creating other conversation user members with their user objects + // for cached data purposes + const otherUserMembers = await UserService.getUsersByIds(createRequest.otherMemberIds); + if (!otherUserMembers) { + return next(new Error('unable to find other conversation members')); + } + const conversationUserMembers: ConversationUserMember[] = []; + createRequest.otherMemberIds.forEach(async (memberId) => { + const userObject = otherUserMembers.find((userObj) => userObj._id?.equals(memberId)); + + if (!userObject) { + return next(new Error('unable to find a user that was passed in')); + } + + const newConversationMember = new ConversationMember(MemberRole.regular, MemberState.inbox); + + conversationUserMembers.push({ + ...userObject, + ...newConversationMember, + }); + }); + + // adding in the admin user which is the user who started the conversation + const adminMember = new ConversationMember(MemberRole.admin, MemberState.inbox); + const currentUser = await UserService.getUserById(userInfo.userId); + if (!currentUser) { + return next(new Error('unable to find your user object for caching')); + } + conversationUserMembers.push({ + ...currentUser, + ...adminMember, + }); + + // make the insert of the overall conversation document + const newConversation = new Conversation( + userInfo.userId, + conversationUserMembers, + createRequest.conversationName, + ); + const insertResult = await ConversationService.createConversation(newConversation); if (!insertResult) { - return next(Error('unale to create a conversation')); + return next(Error('unable to create a conversation')); } const responseObj = new NirvanaResponse( @@ -48,6 +94,5 @@ const createConversation = async (req: Request, res: Response, next: NextFunctio undefined, 'created conversation!', ); - return res.json(responseObj); }; diff --git a/packages/api/services/user.service.ts b/packages/api/services/user.service.ts index 52cd135..5df9ee8 100644 --- a/packages/api/services/user.service.ts +++ b/packages/api/services/user.service.ts @@ -5,6 +5,19 @@ import axios from 'axios'; import { collections } from './database.service'; export class UserService { + static async getUserByObjectId(userId: ObjectId) { + const query = { _id: userId }; + + const res = await collections.users?.findOne(query); + + // exists + if (res?._id) { + return res as User; + } + + return null; + } + static async getUserById(userId: string) { const query = { _id: new ObjectId(userId) }; diff --git a/packages/core/models/conversation.model.ts b/packages/core/models/conversation.model.ts index dc6a5ab..12036af 100644 --- a/packages/core/models/conversation.model.ts +++ b/packages/core/models/conversation.model.ts @@ -9,25 +9,31 @@ export default class Conversation { * includes basic user info as well as their information * for this particular conversation */ - public members: (ConversationMember & User)[], + public members: ConversationUserMember[], public name?: string, + public id: ObjectId = new ObjectId(), + /** * last time there was new content for everyone */ public lastActivityDate = new Date(), /** - * when name was changed or member list updated + * when the conversation document was created */ - public lastUpdatedDate = new Date(), public createdDate = new Date(), - public id: ObjectId = new ObjectId(), + /** + * when name was changed or member list updated + */ + public lastUpdatedDate?: Date, ) {} } +export type ConversationUserMember = ConversationMember & User; + export class ConversationMember { constructor( public role: MemberRole, diff --git a/packages/core/requests/CreateConversationRequest.request.ts b/packages/core/requests/CreateConversationRequest.request.ts index de29875..ba8ac7d 100644 --- a/packages/core/requests/CreateConversationRequest.request.ts +++ b/packages/core/requests/CreateConversationRequest.request.ts @@ -1,5 +1,6 @@ import Conversation from '@nirvana/core/models/conversation.model'; +import { ObjectId } from 'mongodb'; export default class CreateConversationRequest { - constructor(public conversation: Conversation) {} + constructor(public otherMemberIds: ObjectId[], public conversationName?: string) {} }