diff --git a/packages/api/routes/contacts.ts b/packages/api/routes/contacts.ts index 2f1336d..5c73c15 100644 --- a/packages/api/routes/contacts.ts +++ b/packages/api/routes/contacts.ts @@ -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`); + } +} diff --git a/packages/api/services/contact.service.ts b/packages/api/services/contact.service.ts index 72be4c6..c41f7d9 100644 --- a/packages/api/services/contact.service.ts +++ b/packages/api/services/contact.service.ts @@ -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; + } } diff --git a/packages/core/requests/updateRelationshipState.request.ts b/packages/core/requests/updateRelationshipState.request.ts new file mode 100644 index 0000000..5c6afa2 --- /dev/null +++ b/packages/core/requests/updateRelationshipState.request.ts @@ -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 + ) {} +} diff --git a/packages/desktop/package.json b/packages/desktop/package.json index 09b0a3a..1c7d7b9 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -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": { diff --git a/packages/desktop/src/controller/index.tsx b/packages/desktop/src/controller/index.tsx index 3f9b853..7d3cc9b 100644 --- a/packages/desktop/src/controller/index.tsx +++ b/packages/desktop/src/controller/index.tsx @@ -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( diff --git a/packages/desktop/src/pages/Home/selectedConversation/index.tsx b/packages/desktop/src/pages/Home/selectedConversation/index.tsx index 3926fb0..2b6f733 100644 --- a/packages/desktop/src/pages/Home/selectedConversation/index.tsx +++ b/packages/desktop/src/pages/Home/selectedConversation/index.tsx @@ -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 Please wait; + 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 ( - please accept this request by clicking here: + please accept this request by clicking here:{" "} + ); } diff --git a/packages/desktop/src/pages/Login/index.tsx b/packages/desktop/src/pages/Login/index.tsx index 24d8ba3..2e39de7 100644 --- a/packages/desktop/src/pages/Login/index.tsx +++ b/packages/desktop/src/pages/Login/index.tsx @@ -100,8 +100,6 @@ export default function Login({ onReady }: { onReady: Function }) { Continue with Google )} - - ); }