// client.js // Copyright (C) 2024 DTP Technologies, LLC // All Rights Reserved 'use strict'; import mongoose from 'mongoose'; const Client = mongoose.model('Client'); const ClientProject = mongoose.model('ClientProject'); import { SiteService/*, SiteError*/ } from '../../lib/site-lib.js'; export default class ClientService extends SiteService { static get name ( ) { return 'ClientService'; } static get slug () { return 'client'; } constructor (dtp) { super(dtp, ClientService); } async start ( ) { const { user: userService } = this.dtp.services; this.populateClient = [ { path: 'user', select: userService.USER_SELECT, }, ]; this.populateClientProject = [ { path: 'user', select: userService.USER_SELECT, }, { path: 'client', populate: this.populateClient, }, ]; } async createClient (user, clientDefinition) { const { text: textService } = this.dtp.services; const client = new Client(); client.user = user._id; client.name = textService.filter(clientDefinition.name); if (clientDefinition.description && clientDefinition.description.length > 0) { client.description = textService.filter(clientDefinition.description); } await client.save(); return client.toObject(); } async getClientById (clientId) { const client = Client .findOne({ _id: clientId }) .populate(this.populateClient) .lean(); return client; } async getClientsForUser (user) { const clients = Client .find({ user: user._id }) .sort({ name: 1 }) .populate(this.populateClient) .lean(); return clients; } async createProject (client, projectDefinition) { const { text: textService } = this.dtp.services; const project = new ClientProject(); project.user = client.user._id; project.client = client._id; project.name = textService.filter(projectDefinition.name); if (projectDefinition.description && (projectDefinition.description.length > 0)) { project.description = textService.filter(projectDefinition.description); } project.hourlyRate = projectDefinition.hourlyRate; project.hoursLimit = projectDefinition.hoursLimit; await project.save(); return project.toObject(); } async updateProject (project, projectDefinition) { const { text: textService } = this.dtp.services; const update = { $set: { }, $unset: { } }; if (projectDefinition.name !== project.name) { update.$set.name = textService.filter(projectDefinition.name); } if (projectDefinition.description !== project.description) { update.$set.description = textService.filter(projectDefinition.description); } update.$set.hourlyRate = projectDefinition.hourlyRate; update.$set.hoursLimit = projectDefinition.hoursLimit; await ClientProject.updateOne({ _id: project._id }, update); } async getProjectById (projectId) { const project = ClientProject .findOne({ _id: projectId }) .populate(this.populateClientProject) .lean(); return project; } async getProjectsForUser (user) { const projects = ClientProject .find({ user: user._id }) .sort({ created: -1 }) .populate(this.populateClientProject) .lean(); return projects; } async getProjectsForClient (client) { const projects = ClientProject .find({ client: client._id }) .sort({ created: -1 }) .populate(this.populateClientProject) .lean(); return projects; } }