Skip to content

Commit

Permalink
Merge pull request #2638 from Northeastern-Electric-Racing/moved-work…
Browse files Browse the repository at this point in the history
…-package-templates

Moved work package templates to their own file
  • Loading branch information
Peyton-McKee authored Jun 1, 2024
2 parents 7b8cde5 + be06732 commit 7133ccd
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 415 deletions.
118 changes: 118 additions & 0 deletions src/backend/src/controllers/work-package-templates.controllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { NextFunction, Request, Response } from 'express';
import { WorkPackageTemplate } from 'shared';
import { getCurrentUser } from '../utils/auth.utils';
import { getOrganizationId } from '../utils/utils';
import WorkPackageTemplatesService from '../services/work-package-template.services';

/** Controller for operations involving work packages templates. */
export default class WorkPackageTemplatesController {
// Create a work package template with the given details
static async createWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const { templateName, templateNotes, workPackageName, duration, descriptionBullets, blockedBy } = req.body;

let { stage } = req.body;
if (stage === 'NONE') {
stage = null;
}

const user = await getCurrentUser(res);
const organizationId = getOrganizationId(req.headers);

const workPackageTemplate: WorkPackageTemplate = await WorkPackageTemplatesService.createWorkPackageTemplate(
user,
templateName,
templateNotes,
workPackageName,
stage,
duration,
descriptionBullets,
blockedBy,
organizationId
);

res.status(200).json(workPackageTemplate);
} catch (error: unknown) {
next(error);
}
}

// Get a single work package template that corresponds to the given work package template id
static async getSingleWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const user = await getCurrentUser(res);
const { workPackageTemplateId } = req.params;
const organizationId = getOrganizationId(req.headers);

const workPackageTemplate: WorkPackageTemplate = await WorkPackageTemplatesService.getSingleWorkPackageTemplate(
user,
workPackageTemplateId,
organizationId
);

res.status(200).json(workPackageTemplate);
} catch (error: unknown) {
next(error);
}
}
// Get all work package templates
static async getAllWorkPackageTemplates(req: Request, res: Response, next: NextFunction) {
try {
const submitter = await getCurrentUser(res);
const organizationId = getOrganizationId(req.headers);

const workPackageTemplates: WorkPackageTemplate[] = await WorkPackageTemplatesService.getAllWorkPackageTemplates(
submitter,
organizationId
);

res.status(200).json(workPackageTemplates);
} catch (error: unknown) {
next(error);
}
}

static async editWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const { workpackageTemplateId } = req.params;
const { templateName, templateNotes, duration, blockedBy, descriptionBullets, workPackageName } = req.body;
const user = await getCurrentUser(res);
let { stage } = req.body;
if (stage === 'NONE') {
stage = null;
}
const organizationId = getOrganizationId(req.headers);

const updatedWorkPackageTemplate = await WorkPackageTemplatesService.editWorkPackageTemplate(
user,
workpackageTemplateId,
templateName,
templateNotes,
duration,
stage,
blockedBy,
descriptionBullets,
workPackageName,
organizationId
);

res.status(200).json(updatedWorkPackageTemplate);
} catch (error: unknown) {
next(error);
}
}

// Delete a work package template that corresponds to the given workPackageTemplateId
static async deleteWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const user = await getCurrentUser(res);
const { workPackageTemplateId } = req.params;
const organizationId = getOrganizationId(req.headers);

await WorkPackageTemplatesService.deleteWorkPackageTemplate(user, workPackageTemplateId, organizationId);
res.status(200).json({ message: `Successfully deleted work package template #${req.params.workPackageTemplateId}` });
} catch (error: unknown) {
next(error);
}
}
}
111 changes: 1 addition & 110 deletions src/backend/src/controllers/work-packages.controllers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextFunction, Request, Response } from 'express';
import { validateWBS, WbsNumber, WorkPackage, WorkPackageTemplate } from 'shared';
import { validateWBS, WbsNumber, WorkPackage } from 'shared';
import WorkPackagesService from '../services/work-packages.services';
import { getCurrentUser } from '../utils/auth.utils';
import { getOrganizationId } from '../utils/utils';
Expand Down Expand Up @@ -75,37 +75,6 @@ export default class WorkPackagesController {
}
}

// Create a work package template with the given details
static async createWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const { templateName, templateNotes, workPackageName, duration, descriptionBullets, blockedBy } = req.body;

let { stage } = req.body;
if (stage === 'NONE') {
stage = null;
}

const user = await getCurrentUser(res);
const organizationId = getOrganizationId(req.headers);

const workPackageTemplate: WorkPackageTemplate = await WorkPackagesService.createWorkPackageTemplate(
user,
templateName,
templateNotes,
workPackageName,
stage,
duration,
descriptionBullets,
blockedBy,
organizationId
);

res.status(200).json(workPackageTemplate);
} catch (error: unknown) {
next(error);
}
}

// Edit a work package to the given specifications
static async editWorkPackage(req: Request, res: Response, next: NextFunction) {
try {
Expand Down Expand Up @@ -177,82 +146,4 @@ export default class WorkPackagesController {
next(error);
}
}
// Get a single work package template that corresponds to the given work package template id
static async getSingleWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const user = await getCurrentUser(res);
const { workPackageTemplateId } = req.params;
const organizationId = getOrganizationId(req.headers);

const workPackageTemplate: WorkPackageTemplate = await WorkPackagesService.getSingleWorkPackageTemplate(
user,
workPackageTemplateId,
organizationId
);

res.status(200).json(workPackageTemplate);
} catch (error: unknown) {
next(error);
}
}
// Get all work package templates
static async getAllWorkPackageTemplates(req: Request, res: Response, next: NextFunction) {
try {
const submitter = await getCurrentUser(res);
const organizationId = getOrganizationId(req.headers);

const workPackageTemplates: WorkPackageTemplate[] = await WorkPackagesService.getAllWorkPackageTemplates(
submitter,
organizationId
);

res.status(200).json(workPackageTemplates);
} catch (error: unknown) {
next(error);
}
}

static async editWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const { workpackageTemplateId } = req.params;
const { templateName, templateNotes, duration, blockedBy, descriptionBullets, workPackageName } = req.body;
const user = await getCurrentUser(res);
let { stage } = req.body;
if (stage === 'NONE') {
stage = null;
}
const organizationId = getOrganizationId(req.headers);

const updatedWorkPackageTemplate = await WorkPackagesService.editWorkPackageTemplate(
user,
workpackageTemplateId,
templateName,
templateNotes,
duration,
stage,
blockedBy,
descriptionBullets,
workPackageName,
organizationId
);

res.status(200).json(updatedWorkPackageTemplate);
} catch (error: unknown) {
next(error);
}
}

// Delete a work package template that corresponds to the given workPackageTemplateId
static async deleteWorkPackageTemplate(req: Request, res: Response, next: NextFunction) {
try {
const user = await getCurrentUser(res);
const { workPackageTemplateId } = req.params;
const organizationId = getOrganizationId(req.headers);

await WorkPackagesService.deleteWorkPackageTemplate(user, workPackageTemplateId, organizationId);
res.status(200).json({ message: `Successfully deleted work package template #${req.params.workPackageTemplateId}` });
} catch (error: unknown) {
next(error);
}
}
}
8 changes: 4 additions & 4 deletions src/backend/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import DesignReviewsService from '../services/design-reviews.services';
import BillOfMaterialsService from '../services/boms.services';
import UsersService from '../services/users.services';
import { transformDate } from '../utils/datetime.utils';
import WorkPackagesService from '../services/work-packages.services';
import { writeFileSync } from 'fs';
import WorkPackageTemplatesService from '../services/work-package-template.services';

const prisma = new PrismaClient();

Expand Down Expand Up @@ -1906,7 +1906,7 @@ const performSeed: () => Promise<void> = async () => {
}
);

await WorkPackagesService.createWorkPackageTemplate(
await WorkPackageTemplatesService.createWorkPackageTemplate(
batman,
'Batmobile Config 1',
'This is the first Batmobile configuration',
Expand All @@ -1918,7 +1918,7 @@ const performSeed: () => Promise<void> = async () => {
organizationId
);

const schematicWpTemplate = await WorkPackagesService.createWorkPackageTemplate(
const schematicWpTemplate = await WorkPackageTemplatesService.createWorkPackageTemplate(
batman,
'Schematic',
'This is the schematic template',
Expand All @@ -1930,7 +1930,7 @@ const performSeed: () => Promise<void> = async () => {
organizationId
);

await WorkPackagesService.createWorkPackageTemplate(
await WorkPackageTemplatesService.createWorkPackageTemplate(
batman,
'Layout ',
'This is the Layout template',
Expand Down
15 changes: 9 additions & 6 deletions src/backend/src/routes/work-package-templates.routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import express from 'express';
import WorkPackagesController from '../controllers/work-packages.controllers';
import {
descriptionBulletsValidators,
intMinZero,
Expand All @@ -8,11 +7,12 @@ import {
validateInputs
} from '../utils/validation.utils';
import { body } from 'express-validator';
import WorkPackageTemplatesController from '../controllers/work-package-templates.controllers';

const workPackageTemplatesRouter = express.Router();

workPackageTemplatesRouter.get('/', WorkPackagesController.getAllWorkPackageTemplates);
workPackageTemplatesRouter.get('/:workPackageTemplateId', WorkPackagesController.getSingleWorkPackageTemplate);
workPackageTemplatesRouter.get('/', WorkPackageTemplatesController.getAllWorkPackageTemplates);
workPackageTemplatesRouter.get('/:workPackageTemplateId', WorkPackageTemplatesController.getSingleWorkPackageTemplate);

workPackageTemplatesRouter.post(
'/:workpackageTemplateId/edit',
Expand All @@ -25,7 +25,7 @@ workPackageTemplatesRouter.post(
nonEmptyString(body('workPackageName').optional()),
...descriptionBulletsValidators,
validateInputs,
WorkPackagesController.editWorkPackageTemplate
WorkPackageTemplatesController.editWorkPackageTemplate
);

workPackageTemplatesRouter.post(
Expand All @@ -39,9 +39,12 @@ workPackageTemplatesRouter.post(
nonEmptyString(body('blockedBy.*')),
...descriptionBulletsValidators,
validateInputs,
WorkPackagesController.createWorkPackageTemplate
WorkPackageTemplatesController.createWorkPackageTemplate
);

workPackageTemplatesRouter.delete('/:workPackageTemplateId/delete', WorkPackagesController.deleteWorkPackageTemplate);
workPackageTemplatesRouter.delete(
'/:workPackageTemplateId/delete',
WorkPackageTemplatesController.deleteWorkPackageTemplate
);

export default workPackageTemplatesRouter;
Loading

0 comments on commit 7133ccd

Please sign in to comment.