creating convo working with content type stupid thing

This commit is contained in:
talksik
2022-04-03 09:31:34 -04:00
parent 19b90b2196
commit d6eadab3c1
6 changed files with 62 additions and 13 deletions
+1
View File
@@ -12,6 +12,7 @@ import getUserRoutes from "./routes/user";
const app = express();
app.use(cors());
app.use(express.json());
app.use((req: Request, res: Response, next: NextFunction) => {
console.log("Time: ", Date.now());
+4 -3
View File
@@ -57,6 +57,7 @@ async function getDmByOtherUserId(req: Request, res: Response) {
async function createConversation(req: Request, res: Response) {
try {
const reqObj: CreateConvoRequest = req.body as CreateConvoRequest;
console.log(req.body);
if (!reqObj?.otherMemberIds.length) {
res.status(400).json("must provide member Ids");
@@ -65,11 +66,11 @@ async function createConversation(req: Request, res: Response) {
const userInfo = res.locals.userInfo as JwtClaims;
const newConvo = new Conversation();
const newConvo = new Conversation(new ObjectId());
const convoMembers: ConversationMember[] =
reqObj.otherMemberIds.map((memId) => {
const newConvoMember = new ConversationMember(
newConvo._id,
newConvo._id!,
new ObjectId(memId),
ConversationMemberState.INVITED
);
@@ -79,7 +80,7 @@ async function createConversation(req: Request, res: Response) {
convoMembers.push(
new ConversationMember(
newConvo._id,
newConvo._id!,
new ObjectId(userInfo.userId),
ConversationMemberState.INBOX
)
@@ -0,0 +1,5 @@
export default class ErrorResponse {
status: number;
message: string;
detail: string;
}
@@ -34,3 +34,7 @@ export function useUserSearch(searchQuery: string) {
export function useGetDmByUserId() {
return useMutation(ApiCalls.getDmByUserId);
}
export function useCreateConvo() {
return useMutation(ApiCalls.createConversation);
}
+22 -2
View File
@@ -1,6 +1,7 @@
import axios, { AxiosRequestConfig, AxiosResponse, Method } from "axios";
import { Conversation } from "../../../core/models/conversation.model";
import CreateConvoRequest from "../../../core/requests/createConvo.request";
import LoginResponse from "../../../core/responses/login.response";
import { User } from "@nirvana/core/models";
import UserDetailsResponse from "../../../core/responses/userDetails.response";
@@ -14,7 +15,12 @@ export default class NirvanaApi {
// auth token from google that our backend will use
static _jwtToken?: string;
static async fetch(url: string, method: Method, privateRoute = false) {
static async fetch(
url: string,
method: Method,
privateRoute = false,
body: object = null
) {
// use the auth token if it's a private route
// error if no auth token and it's a private route
// throw error and show message on anything that is an error from the backend
@@ -27,7 +33,11 @@ export default class NirvanaApi {
if (privateRoute && this._jwtToken) {
res = await fetch(fullUrl, {
method: method,
headers: { Authorization: this._jwtToken },
headers: {
Authorization: this._jwtToken,
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : null,
});
} else {
res = await fetch(fullUrl);
@@ -78,10 +88,20 @@ async function getDmByUserId(otherUserId: string): Promise<Conversation> {
);
}
async function createConversation(otherMemberIds: string[]): Promise<void> {
return await NirvanaApi.fetch(
`/conversations`,
"POST",
true,
new CreateConvoRequest(otherMemberIds)
);
}
export const ApiCalls = {
login,
authCheck,
getUserDetails,
userSearch,
getDmByUserId,
createConversation,
};
@@ -7,12 +7,13 @@ import {
TextField,
} from "@mui/material";
import { Check, Search } from "@mui/icons-material";
import { useEffect, useState } from "react";
import {
useCreateConvo,
useGetDmByUserId,
useGetUserDetails,
useUserSearch,
} from "../../../controller/index";
import { useEffect, useState } from "react";
import { $newConvoPage } from "../../../controller/recoil";
import { ApiCalls } from "../../../controller/nirvanaApi";
@@ -110,6 +111,7 @@ export default function NewConvo() {
}
const { mutateAsync } = useGetDmByUserId();
const { mutateAsync: createConvoMutateAsync } = useCreateConvo();
const createConvo = async () => {
if (!selectedUsers?.length) {
@@ -120,18 +122,24 @@ export default function NewConvo() {
// consolidate the user Ids into an array
// make sure that there are no duplicates
// const userIds = selectedUsers.map((selUser) => selUser._id.toString());
// userIds.push(userDetails.user._id.toString());
const otherMemberIds = selectedUsers.map((selUser) =>
selUser._id.toString()
);
// IF it's a one on one chat
// check backend with one route if there is an existing conversation
if (selectedUsers?.length === 1) {
let existingConvo;
let existingConvoRes;
try {
existingConvo = await mutateAsync(selectedUsers[0]._id.toString());
existingConvoRes = await mutateAsync(selectedUsers[0]._id.toString());
console.log("there is an existing convo!");
console.log(existingConvo);
if (existingConvoRes) {
toast.error("already have a convo with this person");
console.log(existingConvoRes);
return;
}
} catch (error) {
console.error(error);
} finally {
@@ -139,8 +147,18 @@ export default function NewConvo() {
}
}
// create a conversation object in db with two members, me and this other person
// IF it's a group convo/room/channel, create a channel with these people
// create a conversation object in db with convoMembers
try {
const createConvoRes = await createConvoMutateAsync(otherMemberIds);
toast.success("Successfully create conversation");
} catch (error) {
console.error(error);
} finally {
console.log("done creating convo");
}
// broadcast this to right sockets either here or serverside after convo creation
};
return (