adding all logic for sending request and should work?

This commit is contained in:
talksik
2022-03-19 22:49:43 -04:00
parent def2cd2085
commit 0faacbab73
7 changed files with 102 additions and 13 deletions
+30 -6
View File
@@ -1,7 +1,10 @@
import Relationship, {
RelationshipState,
} from "@nirvana/core/models/relationship.model";
import express, { Application, Request, Response } from "express";
import { ContactService } from "../services/contact.service";
import { ObjectId } from "mongodb";
import Relationship from "@nirvana/core/models/relationship.model";
import { authCheck } from "../middleware/auth";
import { collections } from "../services/database.service";
@@ -14,7 +17,7 @@ export default function getContactsRoutes() {
router.get("/", authCheck, getAllContacts);
// send a friend request
router.post("/:userId", authCheck, addContact);
router.post("/:otherUserGoogleId", authCheck, addContact);
return router;
}
@@ -38,18 +41,39 @@ async function getAllContacts(req: Request, res: Response) {
async function addContact(req: Request, res: Response) {
try {
// get the userId of the person to add as a friend/contact
const { userId } = req.params;
const { otherUserGoogleId } = req.params;
const { userId } = res.locals;
if (!userId) {
if (!otherUserGoogleId) {
res.status(400).send("No contact id provided!");
return;
}
if (otherUserGoogleId === userId) {
res.status(400).send("Can't add yourself!");
return;
}
// validations
// make sure that we are not adding a friend that already has added us
// add a many to many table of friends
// const newRelationship = new Relationship();
const newRelationship = new Relationship(
userId,
otherUserGoogleId,
RelationshipState.PENDING
);
const insertResult = await ContactService.createRelationship(
newRelationship
);
newRelationship._id = insertResult?.insertedId;
insertResult
? res.status(200).send(newRelationship)
: res.status(500).send("Failed to create account, already exists");
return;
} catch (error) {
console.log(error);
res.status(500).send(`something went wrong`);
+2
View File
@@ -65,6 +65,8 @@ async function getUserDetails(req: Request, res: Response) {
// create user if not exists
const insertResult = await UserService.createUserIfNotExists(newUser);
newUser._id = insertResult?.insertedId;
insertResult
? res.status(200).send(newUser)
: res.status(500).send("Failed to create account, already exists");