more logic for getting conversation if it's not there but should be

This commit is contained in:
talksik
2022-06-11 12:40:17 -05:00
parent 298a989cd9
commit 10ac1626ab
5 changed files with 80 additions and 16 deletions
+20 -2
View File
@@ -19,8 +19,8 @@ export default function getConversationRoutes() {
router.use(express.json());
// get a conversation
router.get('/:conversationId', authCheck);
// get a conversation by id
router.get('/:conversationId', authCheck, getConversationById);
// get all conversations that I am in
// todo: add in pagination for performance, while allowing getting long polling updates?
@@ -38,6 +38,24 @@ export default function getConversationRoutes() {
return router;
}
const getConversationById = async (req: Request, res: Response, next: NextFunction) => {
const { conversationId } = req.params;
if (!conversationId) {
return next(Error('Must provide a conversation Id'));
}
const conversationResult = await ConversationService.getConversationById(conversationId);
const responseObj = new NirvanaResponse(
conversationResult,
undefined,
conversationResult ? 'here is the conversation' : 'No conversation found',
);
return res.json(responseObj);
};
const getAllConversations = async (req: Request, res: Response, next: NextFunction) => {
const userInfo = res.locals.userInfo as JwtClaims;
@@ -3,6 +3,19 @@ import { ObjectId } from 'mongodb';
import { collections } from './database.service';
export default class ConversationService {
static async getConversationById(conversationId: string) {
const query = { _id: new ObjectId(conversationId) };
const res = await collections.conversations?.findOne(query);
// exists
if (res?._id) {
return res as Conversation;
}
return undefined;
}
static async createConversation(newConversation: Conversation) {
return await collections.conversations?.insertOne(newConversation);
}