create conversation should work with frontend objectIds
This commit is contained in:
@@ -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<CreateConversationResponse>(
|
||||
@@ -48,6 +94,5 @@ const createConversation = async (req: Request, res: Response, next: NextFunctio
|
||||
undefined,
|
||||
'created conversation!',
|
||||
);
|
||||
|
||||
return res.json(responseObj);
|
||||
};
|
||||
|
||||
@@ -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) };
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user