|
|
@ -8,7 +8,7 @@ import mongoose from 'mongoose'; |
|
|
|
const Client = mongoose.model('Client'); |
|
|
|
const ClientProject = mongoose.model('ClientProject'); |
|
|
|
|
|
|
|
import { SiteService/*, SiteError*/ } from '../../lib/site-lib.js'; |
|
|
|
import { SiteService, SiteError } from '../../lib/site-lib.js'; |
|
|
|
|
|
|
|
export default class ClientService extends SiteService { |
|
|
|
|
|
|
@ -38,6 +38,10 @@ export default class ClientService extends SiteService { |
|
|
|
path: 'client', |
|
|
|
populate: this.populateClient, |
|
|
|
}, |
|
|
|
{ |
|
|
|
path: 'managers', |
|
|
|
select: userService.USER_SELECT, |
|
|
|
}, |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
@ -119,6 +123,10 @@ export default class ClientService extends SiteService { |
|
|
|
project.description = textService.filter(projectDefinition.description); |
|
|
|
} |
|
|
|
|
|
|
|
if (projectDefinition.managers && (projectDefinition.managers.length > 0)) { |
|
|
|
project.managers = await this.parseManagerNames(projectDefinition.managers); |
|
|
|
} |
|
|
|
|
|
|
|
project.hourlyRate = projectDefinition.hourlyRate; |
|
|
|
|
|
|
|
await project.save(); |
|
|
@ -135,11 +143,19 @@ export default class ClientService extends SiteService { |
|
|
|
update.$set.name = textService.filter(projectDefinition.name); |
|
|
|
changed = true; |
|
|
|
} |
|
|
|
|
|
|
|
if (projectDefinition.description !== project.description) { |
|
|
|
update.$set.description = textService.filter(projectDefinition.description); |
|
|
|
changed = true; |
|
|
|
} |
|
|
|
|
|
|
|
if (projectDefinition.managers && (projectDefinition.managers.length > 0)) { |
|
|
|
update.$set.managers = await this.parseManagerNames(projectDefinition.managers); |
|
|
|
changed = true; |
|
|
|
} else { |
|
|
|
update.$unset.managers = 1; |
|
|
|
} |
|
|
|
|
|
|
|
if (projectDefinition.hourlyRate !== project.hourlyRate) { |
|
|
|
update.$set.hourlyRate = projectDefinition.hourlyRate; |
|
|
|
changed = true; |
|
|
@ -152,6 +168,22 @@ export default class ClientService extends SiteService { |
|
|
|
await ClientProject.updateOne({ _id: project._id }, update); |
|
|
|
} |
|
|
|
|
|
|
|
async parseManagerNames (managerNames) { |
|
|
|
const { user: userService } = this.dtp.services; |
|
|
|
const managers = [ ]; |
|
|
|
|
|
|
|
managerNames = managerNames.split(',').map((m) => m.trim().toLowerCase()); |
|
|
|
for (const name of managerNames) { |
|
|
|
const user = await userService.getByUsername(name); |
|
|
|
if (!user) { |
|
|
|
throw new SiteError(400, `Trying to set unknown user "${name}" as project manager`); |
|
|
|
} |
|
|
|
managers.push(user._id); |
|
|
|
} |
|
|
|
|
|
|
|
return managers; |
|
|
|
} |
|
|
|
|
|
|
|
async getProjectById (projectId) { |
|
|
|
const project = ClientProject |
|
|
|
.findOne({ _id: projectId }) |
|
|
@ -161,18 +193,27 @@ export default class ClientService extends SiteService { |
|
|
|
} |
|
|
|
|
|
|
|
async getProjectsForUser (user) { |
|
|
|
const projects = ClientProject |
|
|
|
const projects = await ClientProject |
|
|
|
.find({ user: user._id }) |
|
|
|
.sort({ created: -1 }) |
|
|
|
.sort({ name: 1 }) |
|
|
|
.populate(this.populateClientProject) |
|
|
|
.lean(); |
|
|
|
return projects; |
|
|
|
} |
|
|
|
|
|
|
|
async getProjectsForClient (client) { |
|
|
|
const projects = ClientProject |
|
|
|
const projects = await ClientProject |
|
|
|
.find({ client: client._id }) |
|
|
|
.sort({ created: -1 }) |
|
|
|
.sort({ name: 1 }) |
|
|
|
.populate(this.populateClientProject) |
|
|
|
.lean(); |
|
|
|
return projects; |
|
|
|
} |
|
|
|
|
|
|
|
async getProjectsForManager (manager) { |
|
|
|
const projects = await ClientProject |
|
|
|
.find({ managers: manager._id }) |
|
|
|
.sort({ name: 1 }) |
|
|
|
.populate(this.populateClientProject) |
|
|
|
.lean(); |
|
|
|
return projects; |
|
|
|