Skip to content

Commit 2c54bd7

Browse files
committed
#1417 - get many work packages endpoint
1 parent 5c87627 commit 2c54bd7

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ export default class WorkPackagesController {
2929
}
3030
}
3131

32+
// Fetch the work packages for the specified WBS numbers
33+
static async getManyWorkPackages(req: Request, res: Response, next: NextFunction) {
34+
try {
35+
const pastedWbs: string = req.body.wbsNums;
36+
const wbsNums: WbsNumber[] = pastedWbs.split('\n').map((wbsNum) => validateWBS(wbsNum));
37+
const workPackages: WorkPackage[] = await WorkPackagesService.getManyWorkPackages(wbsNums);
38+
res.send(200).json(workPackages);
39+
} catch (error: unknown) {
40+
next(error);
41+
}
42+
}
43+
3244
// Create a work package with the given details
3345
static async createWorkPackage(req: Request, res: Response, next: NextFunction) {
3446
try {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const workPackagesRouter = express.Router();
77

88
workPackagesRouter.get('/', WorkPackagesController.getAllWorkPackages);
99
workPackagesRouter.get('/:wbsNum', WorkPackagesController.getSingleWorkPackage);
10+
workPackagesRouter.get('/get-many', WorkPackagesController.getManyWorkPackages);
1011
workPackagesRouter.post(
1112
'/create',
1213
intMinZero(body('crId')),

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,45 @@ export default class WorkPackagesService {
108108
return workPackageTransformer(wp);
109109
}
110110

111+
/**
112+
* Retrieve a subset of work packages.
113+
* @param wbsNums the WBS numbers of the work packages to retrieve
114+
* @returns the work packages with the given WBS numbers
115+
* @throws if any of the work packages are not found
116+
*/
117+
static async getManyWorkPackages(wbsNums: WbsNumber[]): Promise<WorkPackage[]> {
118+
for (const wbsNum of wbsNums) {
119+
if (isProject(wbsNum)) {
120+
throw new HttpException(
121+
404,
122+
'WBS Number ' +
123+
`${wbsNum.carNumber}.${wbsNum.projectNumber}.${wbsNum.workPackageNumber}` +
124+
' is a project WBS#, not a Work Package WBS#'
125+
);
126+
}
127+
}
128+
129+
const workPackages = await prisma.work_Package.findMany({
130+
where: {
131+
wbsElement: {
132+
dateDeleted: null,
133+
OR: wbsNums.map((wbsNum) => ({
134+
carNumber: wbsNum.carNumber,
135+
projectNumber: wbsNum.projectNumber,
136+
workPackageNumber: wbsNum.workPackageNumber
137+
}))
138+
}
139+
},
140+
...workPackageQueryArgs
141+
});
142+
143+
if (!workPackages) {
144+
throw new NotFoundException('Work Package', wbsNums.map((wbsNum) => wbsPipe(wbsNum)).join(', '));
145+
}
146+
147+
return workPackages.map(workPackageTransformer);
148+
}
149+
111150
/**
112151
* Creates a Work_Package in the database
113152
* @param user the user creating the work package

0 commit comments

Comments
 (0)