working send request and such
This commit is contained in:
@@ -5,6 +5,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 { authCheck } from "../middleware/auth";
|
||||
import { collections } from "../services/database.service";
|
||||
|
||||
@@ -19,6 +20,8 @@ export default function getContactsRoutes() {
|
||||
// send a friend request
|
||||
router.post("/:otherUserGoogleId", authCheck, addContact);
|
||||
|
||||
router.put("/", authCheck, updateRelationshipState);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -79,3 +82,23 @@ async function addContact(req: Request, res: Response) {
|
||||
res.status(500).send(`something went wrong`);
|
||||
}
|
||||
}
|
||||
|
||||
async function updateRelationshipState(req: Request, res: Response) {
|
||||
try {
|
||||
// get the userId of the person to add as a friend/contact
|
||||
const requestObj = req.body as UpdateRelationshipStateRequest;
|
||||
|
||||
const updateResult = await ContactService.updateRelationshipState(
|
||||
requestObj.relationshipId,
|
||||
requestObj.newState
|
||||
);
|
||||
|
||||
updateResult?.acknowledged
|
||||
? res.status(200).send("Updated state of relationship")
|
||||
: res.status(500).send("Failed to update relationship");
|
||||
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send(`something went wrong`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { Db } from "mongodb";
|
||||
import Relationship from "@nirvana/core/models/relationship.model";
|
||||
import { Db, ObjectId } from "mongodb";
|
||||
import Relationship, {
|
||||
RelationshipState,
|
||||
} from "@nirvana/core/models/relationship.model";
|
||||
|
||||
import { collections } from "./database.service";
|
||||
|
||||
export class ContactService {
|
||||
@@ -50,4 +53,15 @@ export class ContactService {
|
||||
|
||||
return insertResult;
|
||||
}
|
||||
|
||||
static async updateRelationshipState(
|
||||
relationshipId: ObjectId,
|
||||
newState: RelationshipState
|
||||
) {
|
||||
const query = { _id: relationshipId };
|
||||
const updateDoc = { $set: { state: newState } };
|
||||
const result = await collections.relationships?.updateOne(query, updateDoc);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { ObjectId } from "mongodb";
|
||||
import { RelationshipState } from "../models/relationship.model";
|
||||
|
||||
export default class UpdateRelationshipStateRequest {
|
||||
constructor(
|
||||
public relationshipId: ObjectId,
|
||||
public newState: RelationshipState
|
||||
) {}
|
||||
}
|
||||
@@ -46,6 +46,8 @@
|
||||
[
|
||||
"@electron-forge/plugin-webpack",
|
||||
{
|
||||
"port": "3001",
|
||||
"loggerPort": "9001",
|
||||
"mainConfig": "./webpack.main.config.js",
|
||||
"devContentSecurityPolicy": "connect-src 'self' http://localhost:5000 'unsafe-eval'",
|
||||
"renderer": {
|
||||
|
||||
@@ -3,7 +3,10 @@ import axios, { AxiosResponse } from "axios";
|
||||
import { useMutation, useQuery } from "react-query";
|
||||
|
||||
import GetConversationDetailsResponse from "@nirvana/core/responses/getConversationDetails.response";
|
||||
import { ObjectId } from "mongodb";
|
||||
import { RelationshipState } from "@nirvana/core/models/relationship.model";
|
||||
import SearchResponse from "@nirvana/core/responses/search.response";
|
||||
import UpdateRelationshipStateRequest from "../../../core/requests/updateRelationshipState.request";
|
||||
import { User } from "@nirvana/core/models";
|
||||
import { nirvanaApi } from "./nirvanaApi";
|
||||
import { queryClient } from "../nirvanaApp";
|
||||
@@ -52,8 +55,6 @@ const sendContactRequest = async (
|
||||
idToken: string,
|
||||
otherUserGoogleId: string
|
||||
) => {
|
||||
console.log(idToken);
|
||||
|
||||
const response = await axios.post(
|
||||
localHost + `/contacts/${otherUserGoogleId}`,
|
||||
null,
|
||||
@@ -65,6 +66,17 @@ const sendContactRequest = async (
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateContactRequestState = async (
|
||||
idToken: string,
|
||||
reqObj: UpdateRelationshipStateRequest
|
||||
) => {
|
||||
const response = await axios.put(localHost + `/contacts`, reqObj, {
|
||||
headers: { Authorization: idToken },
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// ====== QUERIES
|
||||
export enum Querytypes {
|
||||
GET_USER_DETAILS = "GET_USER_DETAILS",
|
||||
@@ -110,9 +122,17 @@ export function useConversationDetails(otherUserGoogleId: string) {
|
||||
export function useSendContactRequest() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useMutation((otherGoogleUserId: string) =>
|
||||
sendContactRequest(authTokens.idToken, otherGoogleUserId)
|
||||
);
|
||||
}
|
||||
|
||||
export function useUpdateRelationshipState() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useMutation(
|
||||
(otherGoogleUserId: string) =>
|
||||
sendContactRequest(authTokens.idToken, otherGoogleUserId),
|
||||
(updateReqObj: UpdateRelationshipStateRequest) =>
|
||||
updateContactRequestState(authTokens.idToken, updateReqObj),
|
||||
{
|
||||
onSettled: (data, error) => {
|
||||
return queryClient.invalidateQueries(
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import {
|
||||
Querytypes,
|
||||
useConversationDetails,
|
||||
useGetUserDetails,
|
||||
useSendContactRequest,
|
||||
useUpdateRelationshipState,
|
||||
} from "../../../controller";
|
||||
|
||||
import { $selectedConversation } from "../../../controller/recoil";
|
||||
import { Dimensions } from "../../../electron/constants";
|
||||
import { FaWindowClose } from "react-icons/fa";
|
||||
import { RelationshipState } from "@nirvana/core/models/relationship.model";
|
||||
import UpdateRelationshipStateRequest from "@nirvana/core/requests/updateRelationshipState.request";
|
||||
import moment from "moment";
|
||||
import { queryClient } from "../../../nirvanaApp";
|
||||
import { useEffect } from "react";
|
||||
import { useRecoilState } from "recoil";
|
||||
|
||||
@@ -19,7 +23,8 @@ export default function SelectedConversation() {
|
||||
const { data: userDetailsData } = useGetUserDetails();
|
||||
const { data: convoDetailsResponse, isFetching } =
|
||||
useConversationDetails(selectedConvo);
|
||||
const { mutate } = useSendContactRequest();
|
||||
const { mutate: sendContactRequest } = useSendContactRequest();
|
||||
const { mutate: updateRelationshipState } = useUpdateRelationshipState();
|
||||
|
||||
useEffect(() => {
|
||||
// if selected, then change the bounds of this window as well
|
||||
@@ -46,9 +51,35 @@ export default function SelectedConversation() {
|
||||
const otherUserGoogleId = convoDetailsResponse.contactUser.googleId;
|
||||
|
||||
// todo mutation to create a user
|
||||
mutate(otherUserGoogleId);
|
||||
sendContactRequest(otherUserGoogleId, {
|
||||
onSettled: (data, error) => {
|
||||
return queryClient.invalidateQueries(
|
||||
Querytypes.GET_CONVERSATION_DETAILS + "/" + otherUserGoogleId
|
||||
);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const acceptRequest = () => {
|
||||
const otherUserGoogleId = convoDetailsResponse.contactUser.googleId;
|
||||
|
||||
updateRelationshipState(
|
||||
new UpdateRelationshipStateRequest(
|
||||
convoDetailsResponse.ourRelationship._id,
|
||||
RelationshipState.ACTIVE
|
||||
),
|
||||
{
|
||||
onSettled: (data, error) => {
|
||||
return queryClient.invalidateQueries(
|
||||
Querytypes.GET_CONVERSATION_DETAILS + "/" + otherUserGoogleId
|
||||
);
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
if (isFetching) return <span>Please wait</span>;
|
||||
|
||||
const renderMainContent = () => {
|
||||
// if there's no relationship, then don't show messages, show "send request" button
|
||||
if (!convoDetailsResponse?.ourRelationship) {
|
||||
@@ -64,7 +95,8 @@ export default function SelectedConversation() {
|
||||
) {
|
||||
return (
|
||||
<span>
|
||||
please accept this request by clicking here: <button>accept</button>
|
||||
please accept this request by clicking here:{" "}
|
||||
<button onClick={acceptRequest}>accept</button>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,8 +100,6 @@ export default function Login({ onReady }: { onReady: Function }) {
|
||||
<span>Continue with Google</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
<button onClick={logOut}>Log Out</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user