You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.9 KiB
118 lines
2.9 KiB
// 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;
|
|
|
|
await project.save();
|
|
|
|
return project.toObject();
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|