import mongoose from "mongoose" ; import { ProjectInterface } from "../../types/project" ; import uniqid from "uniqid" ; const projectSchema = new mongoose . Schema ( { id : { type : String , unique : true , } , organization : { type : String , index : true , } , name : String , dateCreated : Date , dateUpdated : Date , } ) ; type ProjectDocument = mongoose . Document & ProjectInterface ; const ProjectModel = mongoose . model < ProjectDocument > ( "Project" , projectSchema ) ; function toInterface ( doc ) { return doc . toJSON ( ) ; } export async function createProject ( organization , data ) { const doc = await ProjectModel . create ( { ... data , organization , id : uniqid ( "prj_" ) , dateCreated : new Date ( ) , dateUpdated : new Date ( ) , } ) ; return toInterface ( doc ) ; } export async function findAllProjectsByOrganization ( organization ) { const docs = await ProjectModel . find ( { organization , } ) ; return docs . map ( toInterface ) ; } export async function findProjectById ( id , organization ) { const doc = await ProjectModel . findOne ( { id , organization } ) ; return doc ? toInterface ( doc ) : null ; } export async function deleteProjectById ( id , organization ) { await ProjectModel . deleteOne ( { id , organization , } ) ; } export async function updateProject ( id , organization , update ) { await ProjectModel . updateOne ( { id , organization , } , { $set : update , } ) ; }