working displaying contacts, but of course not nice

This commit is contained in:
talksik
2022-03-20 18:24:50 -04:00
parent b491eee62c
commit b9f93d741d
5 changed files with 102 additions and 6 deletions
+29 -6
View File
@@ -1,3 +1,6 @@
import GetContactsResponse, {
ContactDetails,
} from "../../core/responses/getContacts.response";
import Relationship, {
RelationshipState,
} from "@nirvana/core/models/relationship.model";
@@ -6,6 +9,7 @@ import express, { Application, Request, Response } from "express";
import { ContactService } from "../services/contact.service";
import { ObjectId } from "mongodb";
import UpdateRelationshipStateRequest from "../../core/requests/updateRelationshipState.request";
import { UserService } from "../services/user.service";
import { authCheck } from "../middleware/auth";
import { collections } from "../services/database.service";
@@ -25,16 +29,35 @@ export default function getContactsRoutes() {
return router;
}
// todo optimization with mongo lookups and such
async function getAllContacts(req: Request, res: Response) {
try {
const { email } = res.locals;
const { userId } = res.locals;
if (!email) {
res.status(400).send("No email of user!");
return;
}
const resultRelationships = await ContactService.getAllUserRelationships(
userId
);
// todo: fetch all of my contacts
const responseObj = new GetContactsResponse();
// get all of the other users based on these relationships and join them
await Promise.all(
resultRelationships.map(async (relationship) => {
const otherUserId =
relationship.receiverUserId === userId
? relationship.senderUserId
: relationship.receiverUserId;
const otherUser = await UserService.getUserByGoogleId(otherUserId);
if (otherUser)
responseObj.contactsDetails.push(
new ContactDetails(otherUser, relationship)
);
})
);
res.status(200).send(responseObj);
} catch (error) {
console.log(error);
res.status(500).send(`something went wrong`);
+11
View File
@@ -6,6 +6,17 @@ import Relationship, {
import { collections } from "./database.service";
export class ContactService {
static async getAllUserRelationships(userId: string) {
const query = {
$or: [{ senderUserId: userId }, { receiverUserId: userId }],
};
const res = (await collections.relationships
?.find(query)
.toArray()) as Relationship[];
return res;
}
static async getRelationship(
myGoogleUserId: string,
otherGoogleUserId: string