adding route for the getting of conversation details

This commit is contained in:
talksik
2022-03-19 10:39:19 -04:00
parent 3bf3c586d1
commit 50c37776fd
15 changed files with 205 additions and 9 deletions
+2
View File
@@ -3,6 +3,7 @@ import express, { Application, Request, Response } from "express";
import { NextFunction } from "express";
import { connectToDatabase } from "./services/database.service";
import cors from "cors";
import getConversationRoutes from "./routes/conversation";
import getSearchRoutes from "./routes/search";
import getUserRoutes from "./routes/user";
@@ -21,6 +22,7 @@ app.get("/", (req: Request, res: Response) => {
app.use("/api/users", getUserRoutes());
app.use("/api/search", getSearchRoutes());
app.use("/api/conversations", getConversationRoutes());
app.listen(5000, () => console.log("Example app is listening on port 5000."));
+2 -1
View File
@@ -23,8 +23,9 @@ export const authCheck = async (
const userId = ticket.getPayload()?.sub;
const email = ticket.getPayload()?.email;
// used in subsequent handlers
if (!userId) throw new Error("No google user Id found");
// used in subsequent handlers
// todo: have to get our database id for the user instead of google's id
res.locals.userId = userId;
res.locals.email = email;
+61
View File
@@ -0,0 +1,61 @@
import express, { Application, Request, Response } from "express";
import { ContactService } from "../services/contact.service";
import Content from "@nirvana/core/models/content.model";
import GetConversationDetailsResponse from "@nirvana/core/responses/getConversationDetails.response";
import { ObjectId } from "mongodb";
import Relationship from "@nirvana/core/models/relationship.model";
import { UserService } from "../services/user.service";
import { authCheck } from "../middleware/auth";
import { collections } from "../services/database.service";
export default function getConversationRoutes() {
const router = express.Router();
router.use(express.json());
// get data for a one on one conversation
router.get("/:otherUserGoogleUserId", authCheck, getConversationDetails);
return router;
}
async function getConversationDetails(req: Request, res: Response) {
try {
// google user Id of current user
const { userId } = res.locals;
const { otherUserGoogleUserId } = req.params;
// get the "other" user's details
const otherUser = await UserService.getUserByGoogleId(
otherUserGoogleUserId
);
if (!otherUser) {
res.status(404).send("No such user found");
return;
}
// find the relationship between us...if not there just null
const ourRelationship = await ContactService.getRelationship(
userId,
otherUserGoogleUserId
);
// todo: get latest content based on limit count
const latestContent: Content[] = [];
const resObj = new GetConversationDetailsResponse(
otherUser,
false,
ourRelationship ?? undefined,
latestContent
);
res.status(200).send(resObj);
} catch (error) {
console.log(error);
res.status(500).send(`something went wrong`);
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ async function getUserDetails(req: Request, res: Response) {
try {
// return user details if it passed auth middleware
const user = await UserService.getUserById(userId);
const user = await UserService.getUserByGoogleId(userId);
// if no user found, then go ahead and create user
if (!user) {
+34
View File
@@ -0,0 +1,34 @@
import Relationship from "@nirvana/core/models/relationship.model";
import { collections } from "./database.service";
export class ContactService {
static async getRelationship(
myGoogleUserId: string,
otherGoogleUserId: string
) {
// i am the sender and they are the receiver
const clauseOne = {
$and: [
{ senderUserId: myGoogleUserId },
{ receiverUserId: otherGoogleUserId },
],
};
// i could also be the receiver and them the sender
const clauseTwo = {
$and: [
{ senderUserId: myGoogleUserId },
{ receiverUserId: otherGoogleUserId },
],
};
const query = { $or: [clauseOne, clauseTwo] };
const res = await collections.users?.findOne(query);
// exists
if (res?._id) {
return res as Relationship;
}
return null;
}
}
@@ -0,0 +1,11 @@
export class ConversationService {
// static async getUserById(userId: string) {
// const query = { googleId: userId };
// const res = await collections.users?.findOne(query);
// // exists
// if (res?._id) {
// return res as User;
// }
// return null;
// }
}
+1 -1
View File
@@ -5,7 +5,7 @@ import axios from "axios";
import { collections } from "./database.service";
export class UserService {
static async getUserById(userId: string) {
static async getUserByGoogleId(userId: string) {
const query = { googleId: userId };
const res = await collections.users?.findOne(query);