Skip to content

Commit a60d2ac

Browse files
authored
Merge pull request #3531 from Northeastern-Electric-Racing/lead-manager-changes
added lead and manager defaults for wps
2 parents d66dd8d + d9ab632 commit a60d2ac

2 files changed

Lines changed: 79 additions & 8 deletions

File tree

src/backend/src/services/work-packages.services.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
import { getBlockingWorkPackages, validateBlockedBys } from '../utils/work-packages.utils';
3535
import { getDescriptionBulletQueryArgs } from '../prisma-query-args/description-bullets.query-args';
3636
import { userHasPermission } from '../utils/users.utils';
37-
import ProjectsService from './projects.services';
3837

3938
/** Service layer containing logic for work package controller functions. */
4039
export default class WorkPackagesService {
@@ -173,13 +172,37 @@ export default class WorkPackagesService {
173172
// and what number work package this should be
174173
const { carNumber, projectNumber } = projectWbsNum;
175174

176-
const project = await ProjectsService.getSingleProject(projectWbsNum, organization);
175+
const project = await prisma.project.findFirst({
176+
where: {
177+
wbsElement: {
178+
carNumber,
179+
projectNumber,
180+
organizationId: organization.organizationId,
181+
dateDeleted: null
182+
}
183+
},
184+
include: {
185+
workPackages: {
186+
where: { wbsElement: { dateDeleted: null } },
187+
include: {
188+
wbsElement: true
189+
}
190+
},
191+
wbsElement: true
192+
}
193+
});
194+
195+
if (!project) {
196+
throw new NotFoundException('Project', `${carNumber}.${projectNumber}.0`);
197+
}
198+
199+
const { projectId } = project;
177200

178-
const { id: projectId } = project;
201+
const { leadId, managerId } = project.wbsElement;
179202

180203
const newWorkPackageNumber: number =
181204
project.workPackages
182-
.map((element) => element.wbsNum.workPackageNumber)
205+
.map((element) => element.wbsElement.workPackageNumber)
183206
.reduce((prev, curr) => Math.max(prev, curr), 0) + 1;
184207

185208
// make the date object but add 12 hours so that the time isn't 00:00 to avoid timezone problems
@@ -208,7 +231,9 @@ export default class WorkPackagesService {
208231
changes: {
209232
createMany: { data: changesToCreate }
210233
},
211-
organizationId: organization.organizationId
234+
organizationId: organization.organizationId,
235+
leadId,
236+
managerId
212237
}
213238
},
214239
stage,
@@ -233,9 +258,9 @@ export default class WorkPackagesService {
233258
[],
234259
blockedByElements,
235260
null,
261+
leadId,
236262
null,
237-
null,
238-
null,
263+
managerId,
239264
[],
240265
descriptionBullets,
241266
crId,

src/backend/src/utils/projects.utils.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export const updateProjectAndCreateChanges = async (
7070
links: { where: { dateDeleted: null }, ...getLinkQueryArgs(organizationId) },
7171
descriptionBullets: { where: { dateDeleted: null }, ...getDescriptionBulletQueryArgs(organizationId) }
7272
}
73+
},
74+
workPackages: {
75+
where: { wbsElement: { dateDeleted: null } },
76+
include: { wbsElement: true }
7377
}
7478
}
7579
});
@@ -79,7 +83,7 @@ export const updateProjectAndCreateChanges = async (
7983
if (originalProject.wbsElement.dateDeleted) throw new DeletedException('Project', projectId);
8084
if (originalProject.wbsElement.organizationId !== organizationId) throw new InvalidOrganizationException('Project');
8185

82-
const { wbsElementId } = originalProject;
86+
const { wbsElementId, workPackages: originalWorkPackages } = originalProject;
8387

8488
const nameChangeJson = createChange(
8589
'name',
@@ -158,6 +162,48 @@ export const updateProjectAndCreateChanges = async (
158162

159163
changesJson = changesJson.concat(descriptionBulletChanges.changes).concat(linkChanges.changes);
160164

165+
// if the project has no managerId and a managerId is provided, we need to update the work packages
166+
// that do not have a managerId and create changes for them
167+
// this is so that project manager is the default manager for all work packages
168+
if (!originalProject.wbsElement.managerId && managerId) {
169+
const wpToUpdate = originalWorkPackages.filter((wp) => !wp.wbsElement.managerId);
170+
171+
const wpChanges = (
172+
await Promise.all(
173+
wpToUpdate.map(async (wp) =>
174+
createChange('manager', null, await getUserFullName(managerId), crId, implementerId, wp.wbsElementId, null, null)
175+
)
176+
)
177+
).filter((change) => change !== undefined);
178+
179+
changesJson = changesJson.concat(wpChanges);
180+
181+
await prisma.wBS_Element.updateMany({
182+
where: { wbsElementId: { in: wpToUpdate.map((wp) => wp.wbsElementId) } },
183+
data: { managerId }
184+
});
185+
}
186+
187+
// same goes for the project lead
188+
if (!originalProject.wbsElement.leadId && leadId) {
189+
const wpToUpdate = originalWorkPackages.filter((wp) => !wp.wbsElement.leadId);
190+
191+
const wpChanges = (
192+
await Promise.all(
193+
wpToUpdate.map(async (wp) =>
194+
createChange('lead', null, await getUserFullName(leadId), crId, implementerId, wp.wbsElementId, null, null)
195+
)
196+
)
197+
).filter((change) => change !== undefined);
198+
199+
changesJson = changesJson.concat(wpChanges);
200+
201+
await prisma.wBS_Element.updateMany({
202+
where: { wbsElementId: { in: wpToUpdate.map((wp) => wp.wbsElementId) } },
203+
data: { leadId }
204+
});
205+
}
206+
161207
// update the project with the input fields
162208
const updatedProject = await prisma.project.update({
163209
where: {

0 commit comments

Comments
 (0)