Skip to content

Commit 32cec27

Browse files
committed
Issue #1527 initial push
1 parent 56aa6d8 commit 32cec27

6 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/backend/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import changeRequestsRouter from './src/routes/change-requests.routes';
1111
import descriptionBulletsRouter from './src/routes/description-bullets.routes';
1212
import tasksRouter from './src/routes/tasks.routes';
1313
import reimbursementRequestsRouter from './src/routes/reimbursement-requests.routes';
14+
import materialsController from './src/routes/materials.routes';
1415

1516
const app = express();
1617
const port = process.env.PORT || 3001;
@@ -52,6 +53,7 @@ app.use('/change-requests', changeRequestsRouter);
5253
app.use('/description-bullets', descriptionBulletsRouter);
5354
app.use('/tasks', tasksRouter);
5455
app.use('/reimbursement-requests', reimbursementRequestsRouter);
56+
app.use('/materials', materialsController);
5557
app.use('/', (_req, res) => {
5658
res.json('Welcome to FinishLine');
5759
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NextFunction, Request, Response } from 'express';
2+
import { getCurrentUser } from '../utils/auth.utils';
3+
import MaterialService from '../services/materials.services';
4+
import { User } from '@prisma/client';
5+
6+
export default class MaterialsController {
7+
static async deleteMaterial(req: Request, res: Response, next: NextFunction) {
8+
try {
9+
const { materialId } = req.params;
10+
11+
const user: User = await getCurrentUser(res);
12+
13+
const updatedMaterial = await MaterialService.deleteMaterial(user, materialId);
14+
15+
res.status(200).json(updatedMaterial);
16+
} catch (error: unknown) {
17+
next(error);
18+
}
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from 'express';
2+
import MaterialsController from '../controllers/materials.controllers';
3+
4+
const materialsRouter = express.Router();
5+
6+
materialsRouter.post('/:materialId/delete', MaterialsController.deleteMaterial);
7+
8+
export default materialsRouter;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { User } from '@prisma/client';
2+
import { isLeadership } from 'shared';
3+
import prisma from '../prisma/prisma';
4+
import { NotFoundException, AccessDeniedException, DeletedException } from '../utils/errors.utils';
5+
6+
export default class MaterialService {
7+
/**
8+
* Delete material in the database
9+
* @param materialId the id number of the given material
10+
* @param currentUser the current user currently accessing the material
11+
* @returns the deleted material
12+
* @throws if the user does not have permission, or materidal already deleted
13+
*/
14+
static async deleteMaterial(currentUser: User, materialId: string): Promise<string> {
15+
if (!isLeadership(currentUser.role)) {
16+
throw new AccessDeniedException('Only Leadership can delete materials');
17+
}
18+
19+
const material = await prisma.material.findUnique({ where: { materialId } });
20+
21+
if (!material) throw new NotFoundException('Material', materialId);
22+
23+
if (material.dateDeleted) throw new DeletedException('Material', materialId);
24+
25+
const deletedMaterial = await prisma.material.update({
26+
where: { materialId },
27+
data: { dateDeleted: new Date(), userDeletedId: currentUser.userId }
28+
});
29+
30+
return deletedMaterial.materialId;
31+
}
32+
}

src/backend/src/utils/errors.utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class DeletedException extends HttpException {
2424
* @param id the id of the thing that is deleted
2525
*/
2626
constructor(name: ExceptionObjectNames, id: number | string) {
27-
super(404, `${name} with id: ${id} has been deleted already!`);
27+
super(400, `${name} with id: ${id} has been deleted already!`);
2828
}
2929
}
3030

@@ -111,6 +111,7 @@ type ExceptionObjectNames =
111111
| 'Expense Type'
112112
| 'Reimbursement Request'
113113
| 'User Secure Settings'
114+
| 'Material'
114115
| 'Image File'
115116
| 'Assembly'
116117
| 'Material Type'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const request = require('supertest');
2+
const express = require('express');
3+
const app = express();
4+
5+
app.use(express.urlencoded({ extended: false }));
6+
7+
test('materisal not found', async () => {
8+
const response = await request(app).post('/materials/10/delete').set('Authorization', 3);
9+
10+
expect(response.statusCode).toBe(404);
11+
});

0 commit comments

Comments
 (0)