more logic for getting conversation if it's not there but should be
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user