creating convo working with content type stupid thing
This commit is contained in:
@@ -12,6 +12,7 @@ import getUserRoutes from "./routes/user";
|
|||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
app.use((req: Request, res: Response, next: NextFunction) => {
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
||||||
console.log("Time: ", Date.now());
|
console.log("Time: ", Date.now());
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ async function getDmByOtherUserId(req: Request, res: Response) {
|
|||||||
async function createConversation(req: Request, res: Response) {
|
async function createConversation(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const reqObj: CreateConvoRequest = req.body as CreateConvoRequest;
|
const reqObj: CreateConvoRequest = req.body as CreateConvoRequest;
|
||||||
|
console.log(req.body);
|
||||||
|
|
||||||
if (!reqObj?.otherMemberIds.length) {
|
if (!reqObj?.otherMemberIds.length) {
|
||||||
res.status(400).json("must provide member Ids");
|
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 userInfo = res.locals.userInfo as JwtClaims;
|
||||||
|
|
||||||
const newConvo = new Conversation();
|
const newConvo = new Conversation(new ObjectId());
|
||||||
const convoMembers: ConversationMember[] =
|
const convoMembers: ConversationMember[] =
|
||||||
reqObj.otherMemberIds.map((memId) => {
|
reqObj.otherMemberIds.map((memId) => {
|
||||||
const newConvoMember = new ConversationMember(
|
const newConvoMember = new ConversationMember(
|
||||||
newConvo._id,
|
newConvo._id!,
|
||||||
new ObjectId(memId),
|
new ObjectId(memId),
|
||||||
ConversationMemberState.INVITED
|
ConversationMemberState.INVITED
|
||||||
);
|
);
|
||||||
@@ -79,7 +80,7 @@ async function createConversation(req: Request, res: Response) {
|
|||||||
|
|
||||||
convoMembers.push(
|
convoMembers.push(
|
||||||
new ConversationMember(
|
new ConversationMember(
|
||||||
newConvo._id,
|
newConvo._id!,
|
||||||
new ObjectId(userInfo.userId),
|
new ObjectId(userInfo.userId),
|
||||||
ConversationMemberState.INBOX
|
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() {
|
export function useGetDmByUserId() {
|
||||||
return useMutation(ApiCalls.getDmByUserId);
|
return useMutation(ApiCalls.getDmByUserId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useCreateConvo() {
|
||||||
|
return useMutation(ApiCalls.createConversation);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import axios, { AxiosRequestConfig, AxiosResponse, Method } from "axios";
|
import axios, { AxiosRequestConfig, AxiosResponse, Method } from "axios";
|
||||||
|
|
||||||
import { Conversation } from "../../../core/models/conversation.model";
|
import { Conversation } from "../../../core/models/conversation.model";
|
||||||
|
import CreateConvoRequest from "../../../core/requests/createConvo.request";
|
||||||
import LoginResponse from "../../../core/responses/login.response";
|
import LoginResponse from "../../../core/responses/login.response";
|
||||||
import { User } from "@nirvana/core/models";
|
import { User } from "@nirvana/core/models";
|
||||||
import UserDetailsResponse from "../../../core/responses/userDetails.response";
|
import UserDetailsResponse from "../../../core/responses/userDetails.response";
|
||||||
@@ -14,7 +15,12 @@ export default class NirvanaApi {
|
|||||||
// auth token from google that our backend will use
|
// auth token from google that our backend will use
|
||||||
static _jwtToken?: string;
|
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
|
// use the auth token if it's a private route
|
||||||
// error if no auth token and 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
|
// 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) {
|
if (privateRoute && this._jwtToken) {
|
||||||
res = await fetch(fullUrl, {
|
res = await fetch(fullUrl, {
|
||||||
method: method,
|
method: method,
|
||||||
headers: { Authorization: this._jwtToken },
|
headers: {
|
||||||
|
Authorization: this._jwtToken,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: body ? JSON.stringify(body) : null,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res = await fetch(fullUrl);
|
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 = {
|
export const ApiCalls = {
|
||||||
login,
|
login,
|
||||||
authCheck,
|
authCheck,
|
||||||
getUserDetails,
|
getUserDetails,
|
||||||
userSearch,
|
userSearch,
|
||||||
getDmByUserId,
|
getDmByUserId,
|
||||||
|
createConversation,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ import {
|
|||||||
TextField,
|
TextField,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { Check, Search } from "@mui/icons-material";
|
import { Check, Search } from "@mui/icons-material";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import {
|
import {
|
||||||
|
useCreateConvo,
|
||||||
useGetDmByUserId,
|
useGetDmByUserId,
|
||||||
useGetUserDetails,
|
useGetUserDetails,
|
||||||
useUserSearch,
|
useUserSearch,
|
||||||
} from "../../../controller/index";
|
} from "../../../controller/index";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { $newConvoPage } from "../../../controller/recoil";
|
import { $newConvoPage } from "../../../controller/recoil";
|
||||||
import { ApiCalls } from "../../../controller/nirvanaApi";
|
import { ApiCalls } from "../../../controller/nirvanaApi";
|
||||||
@@ -110,6 +111,7 @@ export default function NewConvo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { mutateAsync } = useGetDmByUserId();
|
const { mutateAsync } = useGetDmByUserId();
|
||||||
|
const { mutateAsync: createConvoMutateAsync } = useCreateConvo();
|
||||||
|
|
||||||
const createConvo = async () => {
|
const createConvo = async () => {
|
||||||
if (!selectedUsers?.length) {
|
if (!selectedUsers?.length) {
|
||||||
@@ -120,18 +122,24 @@ export default function NewConvo() {
|
|||||||
|
|
||||||
// consolidate the user Ids into an array
|
// consolidate the user Ids into an array
|
||||||
// make sure that there are no duplicates
|
// make sure that there are no duplicates
|
||||||
// const userIds = selectedUsers.map((selUser) => selUser._id.toString());
|
const otherMemberIds = selectedUsers.map((selUser) =>
|
||||||
// userIds.push(userDetails.user._id.toString());
|
selUser._id.toString()
|
||||||
|
);
|
||||||
|
|
||||||
// IF it's a one on one chat
|
// IF it's a one on one chat
|
||||||
// check backend with one route if there is an existing conversation
|
// check backend with one route if there is an existing conversation
|
||||||
if (selectedUsers?.length === 1) {
|
if (selectedUsers?.length === 1) {
|
||||||
let existingConvo;
|
let existingConvoRes;
|
||||||
try {
|
try {
|
||||||
existingConvo = await mutateAsync(selectedUsers[0]._id.toString());
|
existingConvoRes = await mutateAsync(selectedUsers[0]._id.toString());
|
||||||
|
|
||||||
console.log("there is an existing convo!");
|
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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -139,8 +147,18 @@ export default function NewConvo() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a conversation object in db with two members, me and this other person
|
// create a conversation object in db with convoMembers
|
||||||
// IF it's a group convo/room/channel, create a channel with these people
|
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 (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user