-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…oring-desc-bullets #487: put changes on new branch
- Loading branch information
Showing
8 changed files
with
112 additions
and
86 deletions.
There are no files selected for viewing
76 changes: 14 additions & 62 deletions
76
src/backend/src/controllers/description-bullets.controllers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,16 @@ | ||
import { WBS_Element_Status } from '@prisma/client'; | ||
import { Request, Response } from 'express'; | ||
import prisma from '../prisma/prisma'; | ||
import { hasBulletCheckingPermissions } from '../utils/description-bullets.utils'; | ||
|
||
export const checkDescriptionBullet = async (req: Request, res: Response) => { | ||
const { body } = req; | ||
const { userId, descriptionId } = body; | ||
|
||
const originalDB = await prisma.description_Bullet.findUnique({ | ||
where: { descriptionId }, | ||
include: { | ||
workPackageDeliverables: { include: { wbsElement: true } }, | ||
workPackageExpectedActivities: { include: { wbsElement: true } } | ||
import { NextFunction, Request, Response } from 'express'; | ||
import DescriptionBulletsService from '../services/description-bullets.services'; | ||
import { getCurrentUser } from '../utils/utils'; | ||
|
||
export default class DescriptionBulletsController { | ||
static async checkDescriptionBullet(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const { descriptionId } = req.body; | ||
const user = await getCurrentUser(res); | ||
const updatedDB = await DescriptionBulletsService.checkDescriptionBullet(user, descriptionId); | ||
res.status(200).json(updatedDB); | ||
} catch (error: unknown) { | ||
next(error); | ||
} | ||
}); | ||
|
||
if (!originalDB) { | ||
return res.status(404).json({ message: `Description Bullet with id ${descriptionId} not found` }); | ||
} | ||
|
||
if (originalDB.dateDeleted) { | ||
return res.status(400).json({ message: 'Cant edit a deleted Description Bullet' }); | ||
} | ||
|
||
const workPackage = originalDB.workPackageDeliverables || originalDB.workPackageExpectedActivities; | ||
if (!workPackage) { | ||
return res.status(400).json({ | ||
message: 'This description bullet is not tied to a workpackage deliverable or expected activity!' | ||
}); | ||
} | ||
|
||
if (workPackage.wbsElement.status !== WBS_Element_Status.ACTIVE) { | ||
return res.status(400).json({ message: 'Cannot check a description bullet on an inactive work package!' }); | ||
} | ||
|
||
const hasPerms = await hasBulletCheckingPermissions(userId, descriptionId); | ||
|
||
if (!hasPerms) { | ||
return res.status(403).json({ message: 'Access Denied' }); | ||
} | ||
|
||
let updatedDB; | ||
|
||
if (originalDB.userCheckedId) { | ||
updatedDB = await prisma.description_Bullet.update({ | ||
where: { descriptionId }, | ||
data: { | ||
userCheckedId: null, | ||
dateTimeChecked: null | ||
} | ||
}); | ||
} else { | ||
updatedDB = await prisma.description_Bullet.update({ | ||
where: { descriptionId }, | ||
data: { | ||
userCheckedId: userId, | ||
dateTimeChecked: new Date() | ||
} | ||
}); | ||
} | ||
|
||
return res.status(200).json(updatedDB); | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/backend/src/prisma-query-args/description-bullets.query-args.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Prisma } from '@prisma/client'; | ||
|
||
const descriptionBulletQueryArgs = Prisma.validator<Prisma.Description_BulletArgs>()({ | ||
include: { userChecked: true } | ||
}); | ||
|
||
export default descriptionBulletQueryArgs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
import express from 'express'; | ||
import { body } from 'express-validator'; | ||
import { checkDescriptionBullet } from '../controllers/description-bullets.controllers'; | ||
import DescriptionBulletsController from '../controllers/description-bullets.controllers'; | ||
import { validateInputs } from '../utils/utils'; | ||
|
||
const descriptionBulletsRouter = express.Router(); | ||
|
||
descriptionBulletsRouter.post( | ||
'/check', | ||
body('userId').isInt({ min: 0 }).not().isString(), | ||
body('descriptionId').isInt({ min: 0 }).not().isString(), | ||
validateInputs, | ||
checkDescriptionBullet | ||
DescriptionBulletsController.checkDescriptionBullet | ||
); | ||
|
||
export default descriptionBulletsRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { User, WBS_Element_Status } from '@prisma/client'; | ||
import prisma from '../prisma/prisma'; | ||
import { hasBulletCheckingPermissions } from '../utils/description-bullets.utils'; | ||
import { AccessDeniedException, HttpException, NotFoundException } from '../utils/errors.utils'; | ||
import descriptionBulletTransformer from '../transformers/description-bullets.transformer'; | ||
import descriptionBulletQueryArgs from '../prisma-query-args/description-bullets.query-args'; | ||
import { DescriptionBullet } from 'shared'; | ||
|
||
export default class DescriptionBulletsService { | ||
/** | ||
* Checks the description bullet | ||
* @param user user that checks the description bullet | ||
* @param descriptionId description of bullet that is being checked | ||
* @throws if bullet doesn't exist or if the bullet is not linked to anything valid | ||
* @returns a checked description bullet | ||
*/ | ||
static async checkDescriptionBullet(user: User, descriptionId: number): Promise<DescriptionBullet> { | ||
const originalDB = await prisma.description_Bullet.findUnique({ | ||
where: { descriptionId }, | ||
include: { | ||
workPackageDeliverables: { include: { wbsElement: true } }, | ||
workPackageExpectedActivities: { include: { wbsElement: true } } | ||
} | ||
}); | ||
if (!originalDB) throw new NotFoundException('Description Bullet', descriptionId); | ||
|
||
if (originalDB.dateDeleted) throw new HttpException(400, 'Cant edit a deleted Description Bullet!'); | ||
|
||
const workPackage = originalDB.workPackageDeliverables || originalDB.workPackageExpectedActivities; | ||
|
||
if (!workPackage) | ||
throw new HttpException(400, 'This description bullet is not tied to a workpackage deliverable or expected activity'); | ||
|
||
if (workPackage.wbsElement.status !== WBS_Element_Status.ACTIVE) | ||
throw new HttpException(400, 'Cannot check a description bullet on an inactive work package!'); | ||
|
||
const hasPerms = await hasBulletCheckingPermissions(user.userId, descriptionId); | ||
|
||
if (!hasPerms) throw new AccessDeniedException(); | ||
|
||
let updatedDB; | ||
|
||
if (originalDB.userCheckedId) { | ||
updatedDB = await prisma.description_Bullet.update({ | ||
where: { descriptionId }, | ||
data: { | ||
userCheckedId: null, | ||
dateTimeChecked: null | ||
}, | ||
...descriptionBulletQueryArgs | ||
}); | ||
} else { | ||
updatedDB = await prisma.description_Bullet.update({ | ||
where: { descriptionId }, | ||
data: { | ||
userCheckedId: user.userId, | ||
dateTimeChecked: new Date() | ||
}, | ||
...descriptionBulletQueryArgs | ||
}); | ||
} | ||
|
||
return descriptionBulletTransformer(updatedDB); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/backend/src/transformers/description-bullets.transformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import { DescriptionBullet } from 'shared'; | ||
import descriptionBulletQueryArgs from '../prisma-query-args/description-bullets.query-args'; | ||
|
||
const descriptionBulletTransformer = ( | ||
descBullet: Prisma.Description_BulletGetPayload<typeof descriptionBulletQueryArgs> | ||
): DescriptionBullet => { | ||
return { | ||
id: descBullet.descriptionId, | ||
detail: descBullet.detail, | ||
dateAdded: descBullet.dateAdded, | ||
dateDeleted: descBullet.dateDeleted ?? undefined, | ||
userChecked: descBullet.userChecked ?? undefined | ||
}; | ||
}; | ||
|
||
export default descriptionBulletTransformer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters