diff --git a/packages/api/middleware/auth.ts b/packages/api/middleware/auth.ts index 492848f..cbd4fa8 100644 --- a/packages/api/middleware/auth.ts +++ b/packages/api/middleware/auth.ts @@ -11,7 +11,7 @@ export const authCheck = async (req: Request, res: Response, next: NextFunction) const { authorization } = req.headers; if (!authorization) { - throw Error('No provided header'); + throw Error('No provided auth header'); } // verify jwt token with our api secret diff --git a/packages/api/routes/conversations.ts b/packages/api/routes/conversations.ts index e1f8b44..2c49e89 100644 --- a/packages/api/routes/conversations.ts +++ b/packages/api/routes/conversations.ts @@ -4,7 +4,7 @@ import Conversation, { MemberRole, } from '@nirvana/core/models/conversation.model'; import { JwtClaims, authCheck } from '../middleware/auth'; -import express, { NextFunction, Request, Response } from 'express'; +import express, { NextFunction, Request, Response, response } from 'express'; import ConversationService from '../services/conversation.service'; import CreateConversationRequest from '@nirvana/core/requests/CreateConversationRequest.request'; @@ -23,11 +23,13 @@ export default function getConversationRoutes() { router.get('/:conversationId', authCheck); // get all conversations that I am in - router.get('/', authCheck); + router.get('/', authCheck, getAllConversations); // get a one on one conversation based on other user Id + router.get('/check/:otherUserId', authCheck); // get a conversation's content + router.get('/:conversationId/content', authCheck); // create a conversation router.post('/', authCheck, createConversation); @@ -35,10 +37,33 @@ export default function getConversationRoutes() { return router; } +const getAllConversations = async (req: Request, res: Response, next: NextFunction) => { + const userInfo = res.locals.userInfo as JwtClaims; + + try { + const resultConversations = await ConversationService.getAllConversationsForUser( + userInfo.userId, + ); + + const responseObj = new NirvanaResponse( + resultConversations ?? [], + undefined, + 'here are all of the conversations for user', + ); + + return res.json(responseObj); + } catch (error) { + return next(Error('unable to get conversations')); + } +}; + const createConversation = async (req: Request, res: Response, next: NextFunction) => { const createRequest = req.body as CreateConversationRequest; const userInfo = res.locals.userInfo as JwtClaims; + // todo: do quick check to make sure that we don't already have a conversation + // as last resort if client didn't already do this + if (!createRequest.otherUsers || createRequest.otherUsers.length === 0) { return next(new Error('must provide who you want to talk to')); } diff --git a/packages/api/routes/user.ts b/packages/api/routes/user.ts index 6dab422..94355cf 100644 --- a/packages/api/routes/user.ts +++ b/packages/api/routes/user.ts @@ -23,6 +23,9 @@ export default function getUserRoutes() { router.get('/login', login); + // get user public information by id + router.get('/:userId', authCheck, getPublicUser); + router.get('/authcheck', authCheck, handleAuthCheck); return router; @@ -45,6 +48,19 @@ async function getUserDetails(req: Request, res: Response, next: NextFunction) { next(Error('No user found')); } +async function getPublicUser(req: Request, res: Response, next: NextFunction) { + const { userId } = req.params; + + const user = await UserService.getUserById(userId as string); + + if (user) { + return res.status(200).json(new UserDetailsResponse(user)); + } + + res.status(404); + next(Error('No user found')); +} + /** Create user if doesn't exist * Returns jwt token for client and user details */ diff --git a/packages/api/services/conversation.service.ts b/packages/api/services/conversation.service.ts index bdc8f6e..a35a5ff 100644 --- a/packages/api/services/conversation.service.ts +++ b/packages/api/services/conversation.service.ts @@ -1,8 +1,22 @@ import Conversation from '@nirvana/core/models/conversation.model'; +import { ObjectId } from 'mongodb'; import { collections } from './database.service'; export default class ConversationService { static async createConversation(newConversation: Conversation) { return await collections.conversations?.insertOne(newConversation); } + + static async getAllConversationsForUser(userId: string) { + const query = { 'members._id': new ObjectId(userId) }; + + const res = await collections.conversations?.find(query).toArray(); + + // exists + if (res?.length) { + return res as Conversation[]; + } + + return undefined; + } } diff --git a/packages/core/models/conversation.model.ts b/packages/core/models/conversation.model.ts index 12036af..00bb097 100644 --- a/packages/core/models/conversation.model.ts +++ b/packages/core/models/conversation.model.ts @@ -13,7 +13,7 @@ export default class Conversation { public name?: string, - public id: ObjectId = new ObjectId(), + public _id: ObjectId = new ObjectId(), /** * last time there was new content for everyone