setting up dotenv and stuff
This commit is contained in:
@@ -1,80 +0,0 @@
|
||||
import { Db, ObjectId } from "mongodb";
|
||||
import Relationship, {
|
||||
RelationshipState,
|
||||
} from "@nirvana/core/models/relationship.model";
|
||||
|
||||
import { collections } from "./database.service";
|
||||
|
||||
export class ContactService {
|
||||
static async getAllUserRelationships(userId: string) {
|
||||
const query = {
|
||||
$or: [{ senderUserId: userId }, { receiverUserId: userId }],
|
||||
};
|
||||
const res = (await collections.relationships
|
||||
?.find(query)
|
||||
.toArray()) as Relationship[];
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static async getRelationship(
|
||||
myGoogleUserId: string,
|
||||
otherGoogleUserId: string
|
||||
) {
|
||||
// i am the sender and they are the receiver
|
||||
const clauseOne = {
|
||||
$and: [
|
||||
{ senderUserId: myGoogleUserId },
|
||||
{ receiverUserId: otherGoogleUserId },
|
||||
],
|
||||
};
|
||||
// i could also be the receiver and them the sender
|
||||
const clauseTwo = {
|
||||
$and: [
|
||||
{ senderUserId: otherGoogleUserId },
|
||||
{ receiverUserId: myGoogleUserId },
|
||||
],
|
||||
};
|
||||
|
||||
const query = { $or: [clauseOne, clauseTwo] };
|
||||
const res = await collections.relationships?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
return res as Relationship;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static async updateRelationshipState(
|
||||
relationshipId: ObjectId,
|
||||
newState: RelationshipState
|
||||
) {
|
||||
const query = { _id: new ObjectId(relationshipId) };
|
||||
const updateDoc = {
|
||||
$set: { state: newState, lastUpdatedDate: new Date() },
|
||||
};
|
||||
const result = await collections.relationships?.updateOne(query, updateDoc);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
// External Dependencies
|
||||
import * as mongoDB from "mongodb";
|
||||
// import * as dotenv from "dotenv";
|
||||
|
||||
import { loadConfig } from "../config";
|
||||
|
||||
// Global Variables
|
||||
|
||||
export const collections: {
|
||||
users?: mongoDB.Collection;
|
||||
relationships?: mongoDB.Collection;
|
||||
} = {};
|
||||
|
||||
// Initialize Connection
|
||||
export async function connectToDatabase() {
|
||||
const config = loadConfig();
|
||||
|
||||
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
|
||||
"mongodb+srv://default:[email protected]/default?retryWrites=true&w=majority"
|
||||
config.MONGO_CONNECTION_STRING
|
||||
);
|
||||
|
||||
await client.connect();
|
||||
@@ -20,11 +21,8 @@ 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 collections`
|
||||
|
||||
Reference in New Issue
Block a user