cleaning up api and testing getting conversations and such

This commit is contained in:
talksik
2022-06-11 10:18:27 -05:00
parent e39316eeff
commit c898402b31
5 changed files with 59 additions and 4 deletions
+1 -1
View File
@@ -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
+27 -2
View File
@@ -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<Conversation[]>(
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'));
}
+16
View File
@@ -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
*/
@@ -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;
}
}
+1 -1
View File
@@ -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