creating line scrappy works sort of
This commit is contained in:
@@ -28,7 +28,7 @@ app.get("/", (req: Request, res: Response) => {
|
|||||||
|
|
||||||
app.use("/api/user", getUserRoutes());
|
app.use("/api/user", getUserRoutes());
|
||||||
app.use("/api/search", getSearchRoutes());
|
app.use("/api/search", getSearchRoutes());
|
||||||
app.use("/api/conversations", getLineRoutes());
|
app.use("/api/lines", getLineRoutes());
|
||||||
|
|
||||||
const PORT = 5000;
|
const PORT = 5000;
|
||||||
var server = app.listen(PORT, () => console.log("express running"));
|
var server = app.listen(PORT, () => console.log("express running"));
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import GetDmConversationByOtherUserIdResponse from "@nirvana/core/responses/getD
|
|||||||
import GetUserLinesResponse from "@nirvana/core/responses/getUserLines.response";
|
import GetUserLinesResponse from "@nirvana/core/responses/getUserLines.response";
|
||||||
import { LineService } from "../services/line.service";
|
import { LineService } from "../services/line.service";
|
||||||
import MasterLineData from "@nirvana/core/models/masterLineData.model";
|
import MasterLineData from "@nirvana/core/models/masterLineData.model";
|
||||||
|
import NirvanaResponse from "../../core/responses/nirvanaResponse";
|
||||||
import { ObjectId } from "mongodb";
|
import { ObjectId } from "mongodb";
|
||||||
import Relationship from "@nirvana/core/models/relationship.model";
|
import Relationship from "@nirvana/core/models/relationship.model";
|
||||||
import { UserService } from "../services/user.service";
|
import { UserService } from "../services/user.service";
|
||||||
@@ -70,7 +71,15 @@ async function createLine(req: Request, res: Response) {
|
|||||||
|
|
||||||
const userInfo = res.locals.userInfo as JwtClaims;
|
const userInfo = res.locals.userInfo as JwtClaims;
|
||||||
|
|
||||||
const newLine = new Line(new ObjectId(), new Date());
|
const newLine = new Line(
|
||||||
|
new ObjectId(userInfo.userId),
|
||||||
|
reqObj.lineName,
|
||||||
|
new Date(),
|
||||||
|
new Date(),
|
||||||
|
new ObjectId()
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: validate that users exists before creating line members
|
||||||
const lineMembers: LineMember[] =
|
const lineMembers: LineMember[] =
|
||||||
reqObj.otherMemberIds.map((memId) => {
|
reqObj.otherMemberIds.map((memId) => {
|
||||||
const newLineMember = new LineMember(
|
const newLineMember = new LineMember(
|
||||||
@@ -96,8 +105,12 @@ async function createLine(req: Request, res: Response) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
transactionResult
|
transactionResult
|
||||||
? res.status(200).json(newLine)
|
? res.status(200).json(new NirvanaResponse(newLine))
|
||||||
: res.status(400).json("unable to create line");
|
: res
|
||||||
|
.status(400)
|
||||||
|
.json(
|
||||||
|
new NirvanaResponse(undefined, new Error("unable to create line"))
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(500).json(error);
|
res.status(500).json(error);
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ import { ObjectId } from "mongodb";
|
|||||||
|
|
||||||
export class Line {
|
export class Line {
|
||||||
constructor(
|
constructor(
|
||||||
public _id?: ObjectId,
|
public createdByUserId: ObjectId,
|
||||||
|
public name?: string,
|
||||||
public createdDate: Date = new Date(),
|
public createdDate: Date = new Date(),
|
||||||
public lastUpdatedDate: Date = new Date(),
|
public lastUpdatedDate: Date = new Date(),
|
||||||
public name?: string
|
public _id?: ObjectId
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// TODO: enforce this or the other sort of thing for the class
|
||||||
|
// type INirvanaResponse<T> =
|
||||||
|
// | {
|
||||||
|
// data: T;
|
||||||
|
// error?: Error;
|
||||||
|
// }
|
||||||
|
// | { data?: T; error: Error };
|
||||||
|
|
||||||
|
export default class NirvanaResponse<T> {
|
||||||
|
data?: T;
|
||||||
|
error?: Error;
|
||||||
|
|
||||||
|
constructor(_data: T, _error?: Error) {
|
||||||
|
this.data = _data;
|
||||||
|
this.error = _error;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -48,6 +48,6 @@ export function useGetDmByUserId() {
|
|||||||
return useMutation(ApiCalls.getDmByUserId);
|
return useMutation(ApiCalls.getDmByUserId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useCreateConvo() {
|
export function useCreateLine() {
|
||||||
return useMutation(ApiCalls.createConversation);
|
return useMutation(ApiCalls.createLine);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import CreateLineRequest from "@nirvana/core/requests/createLine.request";
|
|||||||
import { Line } from "@nirvana/core/models/line.model";
|
import { Line } from "@nirvana/core/models/line.model";
|
||||||
import LoginResponse from "../../../core/responses/login.response";
|
import LoginResponse from "../../../core/responses/login.response";
|
||||||
import MasterConversation from "@nirvana/core/models/masterLineData.model";
|
import MasterConversation from "@nirvana/core/models/masterLineData.model";
|
||||||
|
import NirvanaResponse from "../../../core/responses/nirvanaResponse";
|
||||||
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";
|
||||||
import UserSearchResponse from "../../../core/responses/userSearch.response";
|
import UserSearchResponse from "../../../core/responses/userSearch.response";
|
||||||
@@ -82,19 +83,17 @@ async function userSearch(searchQuery: string): Promise<UserSearchResponse> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getUserConversations(): Promise<MasterConversation[]> {
|
async function getUserConversations(): Promise<MasterConversation[]> {
|
||||||
return await NirvanaApi.fetch(`/conversations`, "GET", true);
|
return await NirvanaApi.fetch(`/lines`, "GET", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDmByUserId(otherUserId: string): Promise<Line> {
|
async function getDmByUserId(otherUserId: string): Promise<Line> {
|
||||||
return await NirvanaApi.fetch(
|
return await NirvanaApi.fetch(`/lines/dm/${otherUserId}`, "GET", true);
|
||||||
`/conversations/dm/${otherUserId}`,
|
|
||||||
"GET",
|
|
||||||
true
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createConversation(request: CreateLineRequest): Promise<void> {
|
async function createLine(
|
||||||
return await NirvanaApi.fetch(`/conversations`, "POST", true, request);
|
request: CreateLineRequest
|
||||||
|
): Promise<NirvanaResponse<Line>> {
|
||||||
|
return await NirvanaApi.fetch(`/lines`, "POST", true, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ApiCalls = {
|
export const ApiCalls = {
|
||||||
@@ -104,5 +103,5 @@ export const ApiCalls = {
|
|||||||
userSearch,
|
userSearch,
|
||||||
getUserConversations,
|
getUserConversations,
|
||||||
getDmByUserId,
|
getDmByUserId,
|
||||||
createConversation,
|
createLine,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Avatar, Modal } from "antd";
|
import { Avatar, Modal } from "antd";
|
||||||
import { HotKeys, KeyMap } from "react-hotkeys";
|
import { HotKeys, KeyMap } from "react-hotkeys";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { useCreateConvo, useUserSearch } from "../../../controller/index";
|
import { useCreateLine, useUserSearch } from "../../../controller/index";
|
||||||
|
|
||||||
import BasicUserRow from "../../../components/User/basicUserDetailsRow";
|
import BasicUserRow from "../../../components/User/basicUserDetailsRow";
|
||||||
import { FiSearch } from "react-icons/fi";
|
import { FiSearch } from "react-icons/fi";
|
||||||
@@ -23,7 +23,7 @@ export default function NewLineModal({
|
|||||||
const [searchQuery, setSearchQuery] = useState<string>("");
|
const [searchQuery, setSearchQuery] = useState<string>("");
|
||||||
const { refetch, data: searchRes } = useUserSearch(searchQuery);
|
const { refetch, data: searchRes } = useUserSearch(searchQuery);
|
||||||
|
|
||||||
const { mutateAsync, isError, isLoading } = useCreateConvo();
|
const { mutateAsync, isLoading } = useCreateLine();
|
||||||
|
|
||||||
const [lineName, setLineName] = useState<string>("");
|
const [lineName, setLineName] = useState<string>("");
|
||||||
|
|
||||||
@@ -71,6 +71,7 @@ export default function NewLineModal({
|
|||||||
);
|
);
|
||||||
|
|
||||||
const handleSubmit = useCallback(async () => {
|
const handleSubmit = useCallback(async () => {
|
||||||
|
// TODO: prevent creating one-on-one line if already exists with x person?
|
||||||
// ensure that we don't have a one on one chat already with x person if it's one person selected
|
// ensure that we don't have a one on one chat already with x person if it's one person selected
|
||||||
|
|
||||||
// upon success,
|
// upon success,
|
||||||
@@ -89,7 +90,12 @@ export default function NewLineModal({
|
|||||||
selectedPerson._id.toString()
|
selectedPerson._id.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
await mutateAsync({ lineName, otherMemberIds: selectedMemberIds });
|
const res = await mutateAsync({
|
||||||
|
lineName,
|
||||||
|
otherMemberIds: selectedMemberIds,
|
||||||
|
});
|
||||||
|
|
||||||
|
toast.success("created line!");
|
||||||
|
|
||||||
// handle close once the new line is created
|
// handle close once the new line is created
|
||||||
handleClose();
|
handleClose();
|
||||||
|
|||||||
Reference in New Issue
Block a user