fixing things regarding bson error with node and such...now working?
This commit is contained in:
@@ -4,7 +4,6 @@ import { NextFunction } from "express";
|
||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
||||
import { UserService } from "./services/user.service";
|
||||
import { UserStatus } from "@nirvana/core/models";
|
||||
import { connectToDatabase } from "./services/database.service";
|
||||
import cors from "cors";
|
||||
import getConversationRoutes from "./routes/conversation";
|
||||
import getSearchRoutes from "./routes/search";
|
||||
@@ -29,7 +28,6 @@ app.use("/api/conversations", getConversationRoutes());
|
||||
|
||||
const PORT = 5000;
|
||||
var server = app.listen(PORT, () => console.log("express running"));
|
||||
connectToDatabase();
|
||||
|
||||
// socket IO stuff
|
||||
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
import {
|
||||
Conversation,
|
||||
ConversationMember,
|
||||
ConversationMemberState,
|
||||
} from "../../core/models/conversation.model";
|
||||
import { JwtClaims, authCheck } from "../middleware/auth";
|
||||
import express, { Application, Request, Response } from "express";
|
||||
|
||||
import Content from "@nirvana/core/models/content.model";
|
||||
import { ConversationService } from "../services/conversation.service";
|
||||
import CreateConvoRequest from "../../core/requests/createConvo.request";
|
||||
import GetConversationDetailsResponse from "@nirvana/core/responses/getConversationDetails.response";
|
||||
import GetDmConversationByOtherUserIdResponse from "../../core/responses/getDmConversationByOtherUserId.response";
|
||||
import MasterConversation from "../../core/models/masterConversation.model";
|
||||
import { ObjectId } from "mongodb";
|
||||
import Relationship from "@nirvana/core/models/relationship.model";
|
||||
import { UserService } from "../services/user.service";
|
||||
import { authCheck } from "../middleware/auth";
|
||||
import { collections } from "../services/database.service";
|
||||
|
||||
export default function getConversationRoutes() {
|
||||
@@ -16,5 +25,76 @@ export default function getConversationRoutes() {
|
||||
// get data for a one on one conversation
|
||||
// router.get("/:otherUserGoogleUserId", authCheck, getConversationDetails);
|
||||
|
||||
// create a convo
|
||||
router.post("/", authCheck, createConversation);
|
||||
|
||||
// get conversation between user and other user
|
||||
router.get("/dm/:otherUserId", authCheck, getDmByOtherUserId);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
async function getDmByOtherUserId(req: Request, res: Response) {
|
||||
try {
|
||||
const { otherUserId } = req.params;
|
||||
const userInfo = res.locals.userInfo as JwtClaims;
|
||||
|
||||
console.log(otherUserId);
|
||||
|
||||
// check db for convos between me and this other person
|
||||
// if there is one, then return it with 200 status
|
||||
// else return it with custom status that frontend will read
|
||||
|
||||
res.status(200).json();
|
||||
return;
|
||||
|
||||
res.status(205).json("no such conversation between you two");
|
||||
} catch (error) {
|
||||
res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function createConversation(req: Request, res: Response) {
|
||||
try {
|
||||
const reqObj: CreateConvoRequest = req.body as CreateConvoRequest;
|
||||
|
||||
if (!reqObj?.otherMemberIds.length) {
|
||||
res.status(400).json("must provide member Ids");
|
||||
return;
|
||||
}
|
||||
|
||||
const userInfo = res.locals.userInfo as JwtClaims;
|
||||
|
||||
const newConvo = new Conversation();
|
||||
const convoMembers: ConversationMember[] =
|
||||
reqObj.otherMemberIds.map((memId) => {
|
||||
const newConvoMember = new ConversationMember(
|
||||
newConvo._id,
|
||||
new ObjectId(memId),
|
||||
ConversationMemberState.INVITED
|
||||
);
|
||||
|
||||
return newConvoMember;
|
||||
}) ?? [];
|
||||
|
||||
convoMembers.push(
|
||||
new ConversationMember(
|
||||
newConvo._id,
|
||||
new ObjectId(userInfo.userId),
|
||||
ConversationMemberState.INBOX
|
||||
)
|
||||
);
|
||||
|
||||
const transactionResult = await ConversationService.createConversation(
|
||||
newConvo,
|
||||
convoMembers
|
||||
);
|
||||
|
||||
transactionResult
|
||||
? res.status(200).json(newConvo)
|
||||
: res.status(400).json("unable to create convo");
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,77 @@
|
||||
import {
|
||||
Conversation,
|
||||
ConversationMember,
|
||||
} from "../../core/models/conversation.model";
|
||||
import { client, collections } from "./database.service";
|
||||
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
export class ConversationService {
|
||||
// static async getUserById(userId: string) {
|
||||
// const query = { googleId: userId };
|
||||
// const res = await collections.users?.findOne(query);
|
||||
// // exists
|
||||
// if (res?._id) {
|
||||
// return res as User;
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
static async getConversationByOtherUserId(otherUserId: ObjectId) {
|
||||
// get all of the conversations for this user that have exactly two conversation members
|
||||
// get all of the conversationMembers for this user
|
||||
// get all of the conversations for this user
|
||||
// get all of the conversations
|
||||
// const query = { googleId: userId };
|
||||
// const res = await collections.users?.findOne(query);
|
||||
// // exists
|
||||
// if (res?._id) {
|
||||
// return res as User;
|
||||
// }
|
||||
// return null;
|
||||
}
|
||||
|
||||
static async createConversation(
|
||||
convo: Conversation,
|
||||
convoMembers: ConversationMember[]
|
||||
) {
|
||||
const session = client.startSession();
|
||||
try {
|
||||
const transactionResults = await session.withTransaction(async () => {
|
||||
// todo: check if convoMembers userId's actually exist
|
||||
|
||||
const insertConvoRes = await collections.conversations?.insertOne(
|
||||
convo
|
||||
);
|
||||
if (!insertConvoRes?.insertedId) {
|
||||
await session.abortTransaction();
|
||||
console.error("failed to create convo");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const insertConvoMembersRes =
|
||||
await collections.conversationMembers?.insertMany(convoMembers);
|
||||
if (!insertConvoMembersRes?.insertedCount) {
|
||||
await session.abortTransaction();
|
||||
console.error("failed to create conversation members");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("success");
|
||||
return insertConvoMembersRes;
|
||||
});
|
||||
|
||||
console.log(transactionResults);
|
||||
|
||||
return "success";
|
||||
|
||||
// if (transactionResults) {
|
||||
// console.log("The convo was successfully created.");
|
||||
// return transactionResults;
|
||||
// } else {
|
||||
// console.log("The convo was intentionally aborted.");
|
||||
// return null;
|
||||
// }
|
||||
} catch (e) {
|
||||
console.log(
|
||||
"The transaction was aborted due to an unexpected error: " + e
|
||||
);
|
||||
} finally {
|
||||
await session.endSession();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,25 +6,34 @@ import { loadConfig } from "../config";
|
||||
// Global Variables
|
||||
export const collections: {
|
||||
users?: mongoDB.Collection;
|
||||
conversations?: mongoDB.Collection;
|
||||
conversationMembers?: mongoDB.Collection;
|
||||
audioClips?: mongoDB.Collection;
|
||||
} = {};
|
||||
|
||||
// Initialize Connection
|
||||
export async function connectToDatabase() {
|
||||
const config = loadConfig();
|
||||
const config = loadConfig();
|
||||
|
||||
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
|
||||
config.MONGO_CONNECTION_STRING
|
||||
);
|
||||
export const client: mongoDB.MongoClient = new mongoDB.MongoClient(
|
||||
config.MONGO_CONNECTION_STRING
|
||||
);
|
||||
|
||||
await client.connect();
|
||||
client.connect();
|
||||
|
||||
const db: mongoDB.Db = client.db(process.env.DB_NAME);
|
||||
const db: mongoDB.Db = client.db(process.env.DB_NAME);
|
||||
|
||||
const usersCollection: mongoDB.Collection = db.collection("users");
|
||||
const usersCollection: mongoDB.Collection = db.collection("users");
|
||||
const convoCollection: mongoDB.Collection = db.collection("conversations");
|
||||
const convoMembersCollection: mongoDB.Collection = db.collection(
|
||||
"conversationMembers"
|
||||
);
|
||||
const audioClipsCollection: mongoDB.Collection = db.collection("audioClips");
|
||||
|
||||
collections.users = usersCollection;
|
||||
collections.users = usersCollection;
|
||||
collections.conversations = convoCollection;
|
||||
collections.conversationMembers = convoMembersCollection;
|
||||
collections.audioClips = audioClipsCollection;
|
||||
|
||||
console.log(
|
||||
`Successfully connected to database: ${db.databaseName} and collections`
|
||||
);
|
||||
}
|
||||
console.log(
|
||||
`Successfully connected to database: ${db.databaseName} and collections`
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user