adding all logic for sending request and should work?
This commit is contained in:
@@ -3,6 +3,7 @@ import express, { Application, Request, Response } from "express";
|
||||
import { NextFunction } from "express";
|
||||
import { connectToDatabase } from "./services/database.service";
|
||||
import cors from "cors";
|
||||
import getContactsRoutes from "./routes/contacts";
|
||||
import getConversationRoutes from "./routes/conversation";
|
||||
import getSearchRoutes from "./routes/search";
|
||||
import getUserRoutes from "./routes/user";
|
||||
@@ -23,6 +24,7 @@ app.get("/", (req: Request, res: Response) => {
|
||||
app.use("/api/users", getUserRoutes());
|
||||
app.use("/api/search", getSearchRoutes());
|
||||
app.use("/api/conversations", getConversationRoutes());
|
||||
app.use("/api/contacts", getContactsRoutes());
|
||||
|
||||
app.listen(5000, () => console.log("Example app is listening on port 5000."));
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import Relationship, {
|
||||
RelationshipState,
|
||||
} from "@nirvana/core/models/relationship.model";
|
||||
import express, { Application, Request, Response } from "express";
|
||||
|
||||
import { ContactService } from "../services/contact.service";
|
||||
import { ObjectId } from "mongodb";
|
||||
import Relationship from "@nirvana/core/models/relationship.model";
|
||||
import { authCheck } from "../middleware/auth";
|
||||
import { collections } from "../services/database.service";
|
||||
|
||||
@@ -14,7 +17,7 @@ export default function getContactsRoutes() {
|
||||
router.get("/", authCheck, getAllContacts);
|
||||
|
||||
// send a friend request
|
||||
router.post("/:userId", authCheck, addContact);
|
||||
router.post("/:otherUserGoogleId", authCheck, addContact);
|
||||
|
||||
return router;
|
||||
}
|
||||
@@ -38,18 +41,39 @@ async function getAllContacts(req: Request, res: Response) {
|
||||
async function addContact(req: Request, res: Response) {
|
||||
try {
|
||||
// get the userId of the person to add as a friend/contact
|
||||
const { userId } = req.params;
|
||||
const { otherUserGoogleId } = req.params;
|
||||
const { userId } = res.locals;
|
||||
|
||||
if (!userId) {
|
||||
if (!otherUserGoogleId) {
|
||||
res.status(400).send("No contact id provided!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (otherUserGoogleId === userId) {
|
||||
res.status(400).send("Can't add yourself!");
|
||||
return;
|
||||
}
|
||||
|
||||
// validations
|
||||
// make sure that we are not adding a friend that already has added us
|
||||
|
||||
// add a many to many table of friends
|
||||
// const newRelationship = new Relationship();
|
||||
const newRelationship = new Relationship(
|
||||
userId,
|
||||
otherUserGoogleId,
|
||||
RelationshipState.PENDING
|
||||
);
|
||||
|
||||
const insertResult = await ContactService.createRelationship(
|
||||
newRelationship
|
||||
);
|
||||
|
||||
newRelationship._id = insertResult?.insertedId;
|
||||
|
||||
insertResult
|
||||
? res.status(200).send(newRelationship)
|
||||
: res.status(500).send("Failed to create account, already exists");
|
||||
|
||||
return;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).send(`something went wrong`);
|
||||
|
||||
@@ -65,6 +65,8 @@ async function getUserDetails(req: Request, res: Response) {
|
||||
// create user if not exists
|
||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||
|
||||
newUser._id = insertResult?.insertedId;
|
||||
|
||||
insertResult
|
||||
? res.status(200).send(newUser)
|
||||
: res.status(500).send("Failed to create account, already exists");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Db } from "mongodb";
|
||||
import Relationship from "@nirvana/core/models/relationship.model";
|
||||
import { collections } from "./database.service";
|
||||
|
||||
@@ -22,7 +23,7 @@ export class ContactService {
|
||||
};
|
||||
|
||||
const query = { $or: [clauseOne, clauseTwo] };
|
||||
const res = await collections.users?.findOne(query);
|
||||
const res = await collections.relationships?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
@@ -31,4 +32,22 @@ export class ContactService {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async createRelationship(newRelationship: Relationship) {
|
||||
// i am the sender and they are the receiver
|
||||
const relationshipRes = await this.getRelationship(
|
||||
newRelationship.senderUserId,
|
||||
newRelationship.receiverUserId
|
||||
);
|
||||
|
||||
if (relationshipRes) {
|
||||
throw new Error("Already exists");
|
||||
}
|
||||
|
||||
const insertResult = await collections.relationships?.insertOne(
|
||||
newRelationship
|
||||
);
|
||||
|
||||
return insertResult;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ import * as mongoDB from "mongodb";
|
||||
|
||||
// Global Variables
|
||||
|
||||
export const collections: { users?: mongoDB.Collection } = {};
|
||||
export const collections: {
|
||||
users?: mongoDB.Collection;
|
||||
relationships?: mongoDB.Collection;
|
||||
} = {};
|
||||
|
||||
// Initialize Connection
|
||||
export async function connectToDatabase() {
|
||||
@@ -17,10 +20,13 @@ export async function connectToDatabase() {
|
||||
const db: mongoDB.Db = client.db(process.env.DB_NAME);
|
||||
|
||||
const usersCollection: mongoDB.Collection = db.collection("users");
|
||||
const relationshipCollection: mongoDB.Collection =
|
||||
db.collection("relationships");
|
||||
|
||||
collections.users = usersCollection;
|
||||
collections.relationships = relationshipCollection;
|
||||
|
||||
console.log(
|
||||
`Successfully connected to database: ${db.databaseName} and collection: ${usersCollection.collectionName}`
|
||||
`Successfully connected to database: ${db.databaseName} and collections`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -48,6 +48,20 @@ const getConversationDetails = async (
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const sendContactRequest = async (
|
||||
idToken: string,
|
||||
otherUserGoogleId: string
|
||||
) => {
|
||||
const response = await axios.post(
|
||||
localHost + `/contacts/${otherUserGoogleId}`,
|
||||
{
|
||||
headers: { Authorization: idToken },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// ====== QUERIES
|
||||
export enum Querytypes {
|
||||
GET_USER_DETAILS = "GET_USER_DETAILS",
|
||||
@@ -74,7 +88,7 @@ export function useSearch() {
|
||||
return useQuery(
|
||||
Querytypes.GET_SEARCH_RESULTS,
|
||||
() => search(authTokens.idToken, searchQuery),
|
||||
{ enabled: searchQuery ? true : false }
|
||||
{ enabled: searchQuery ? true : false, refetchOnWindowFocus: false }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -96,3 +110,19 @@ export function useCreateUser() {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useSendContactRequest() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useMutation(
|
||||
(otherGoogleUserId: string) =>
|
||||
sendContactRequest(authTokens.idToken, otherGoogleUserId),
|
||||
{
|
||||
onSettled: (data, error) => {
|
||||
return queryClient.invalidateQueries(
|
||||
Querytypes.GET_CONVERSATION_DETAILS + "/" + ""
|
||||
);
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { useConversationDetails, useGetUserDetails } from "../../../controller";
|
||||
import {
|
||||
useConversationDetails,
|
||||
useGetUserDetails,
|
||||
useSendContactRequest,
|
||||
} from "../../../controller";
|
||||
|
||||
import { $selectedConversation } from "../../../controller/recoil";
|
||||
import { Dimensions } from "../../../electron/constants";
|
||||
@@ -13,8 +17,9 @@ export default function SelectedConversation() {
|
||||
$selectedConversation
|
||||
);
|
||||
const { data: userDetailsData } = useGetUserDetails();
|
||||
const { data: convoDetailsResponse, isLoading } =
|
||||
const { data: convoDetailsResponse, isFetching } =
|
||||
useConversationDetails(selectedConvo);
|
||||
const { mutate } = useSendContactRequest();
|
||||
|
||||
useEffect(() => {
|
||||
// if selected, then change the bounds of this window as well
|
||||
@@ -31,7 +36,7 @@ export default function SelectedConversation() {
|
||||
return <></>;
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
if (isFetching) {
|
||||
return <span className="text-slate-200">Loading conversation details</span>;
|
||||
}
|
||||
|
||||
@@ -41,6 +46,7 @@ export default function SelectedConversation() {
|
||||
const otherUserGoogleId = convoDetailsResponse.contactUser.googleId;
|
||||
|
||||
// todo mutation to create a user
|
||||
mutate(otherUserGoogleId);
|
||||
};
|
||||
|
||||
const renderMainContent = () => {
|
||||
|
||||
Reference in New Issue
Block a user