Skip to content

Commit c180c40

Browse files
Merge pull request #1240 from bluewave-labs/hp-apr-24-convert-eu-to-a-framework
Convert EU to a framework
2 parents 26db9ee + 043e08f commit c180c40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3802
-1194
lines changed

Servers/controllers/assessment.ctrl.ts

+14-135
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
import { Assessment, AssessmentModel } from "../models/assessment.model";
3131
import { TopicModel } from "../models/topic.model";
3232
import { SubtopicModel } from "../models/subtopic.model";
33+
import { sequelize } from "../database/db";
3334

3435
export async function getAllAssessments(
3536
req: Request,
@@ -70,6 +71,7 @@ export async function createAssessment(
7071
req: Request,
7172
res: Response
7273
): Promise<any> {
74+
const transaction = await sequelize.transaction();
7375
try {
7476
const newAssessment: Assessment = req.body;
7577

@@ -80,14 +82,16 @@ export async function createAssessment(
8082
})
8183
);
8284
}
83-
const createdAssessment = await createNewAssessmentQuery(newAssessment, false);
85+
const createdAssessment = await createNewAssessmentQuery(newAssessment, false, transaction);
8486

8587
if (createdAssessment) {
88+
await transaction.commit();
8689
return res.status(201).json(STATUS_CODE[201](createdAssessment));
8790
}
8891

8992
return res.status(503).json(STATUS_CODE[503]({}));
9093
} catch (error) {
94+
await transaction.rollback();
9195
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
9296
}
9397
}
@@ -96,6 +100,7 @@ export async function updateAssessmentById(
96100
req: Request,
97101
res: Response
98102
): Promise<any> {
103+
const transaction = await sequelize.transaction();
99104
try {
100105
const assessmentId = parseInt(req.params.id);
101106
const updatedAssessment: Assessment = req.body;
@@ -109,15 +114,18 @@ export async function updateAssessmentById(
109114
}
110115
const assessment = await updateAssessmentByIdQuery(
111116
assessmentId,
112-
updatedAssessment
117+
updatedAssessment,
118+
transaction
113119
);
114120

115121
if (assessment) {
122+
await transaction.commit();
116123
return res.status(202).json(STATUS_CODE[202](assessment));
117124
}
118125

119126
return res.status(404).json(STATUS_CODE[404]({}));
120127
} catch (error) {
128+
await transaction.rollback();
121129
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
122130
}
123131
}
@@ -126,91 +134,23 @@ export async function deleteAssessmentById(
126134
req: Request,
127135
res: Response
128136
): Promise<any> {
137+
const transaction = await sequelize.transaction();
129138
try {
130139
const assessmentId = parseInt(req.params.id);
131-
const deletedAssessment = await deleteAssessmentByIdQuery(assessmentId);
140+
const deletedAssessment = await deleteAssessmentByIdQuery(assessmentId, transaction);
132141

133142
if (deletedAssessment) {
143+
await transaction.commit();
134144
return res.status(202).json(STATUS_CODE[202](deletedAssessment));
135145
}
136146

137147
return res.status(404).json(STATUS_CODE[404]({}));
138148
} catch (error) {
149+
await transaction.rollback();
139150
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
140151
}
141152
}
142153

143-
// export async function saveAnswers(req: RequestWithFile, res: Response): Promise<any> {
144-
// try {
145-
// const requestBody = req.body as {
146-
// assessmentId: number;
147-
// topic: string;
148-
// topicId: number;
149-
// subtopic: string;
150-
// };
151-
// const assessmentId = requestBody.assessmentId;
152-
// // now, create a topic using the assessmentId and the topic
153-
// const topic: any = await updateTopicByIdQuery(requestBody.topicId, {
154-
// assessmentId,
155-
// title: requestBody.topic,
156-
// });
157-
158-
// // now iterate over the subtopics, create a subtopic using topic id and the subtopic
159-
// const subtopics = JSON.parse(requestBody.subtopic) as {
160-
// id: number;
161-
// name: string;
162-
// questions: {
163-
// id: number;
164-
// subtopicId: number;
165-
// questionText: string;
166-
// answerType: string;
167-
// evidenceFileRequired: boolean;
168-
// hint: string;
169-
// isRequired: boolean;
170-
// priorityLevel: string;
171-
// answer: string;
172-
// evidenceFiles: [];
173-
// }[];
174-
// }[];
175-
// const subTopicResp = []
176-
// for (const subtopic of subtopics) {
177-
// const subtopicToUpdate: any = await updateSubtopicByIdQuery(subtopic.id, {
178-
// topicId: topic.id,
179-
// name: subtopic.name,
180-
// });
181-
// const subtopicId = subtopicToUpdate.id;
182-
// const questions = subtopic.questions;
183-
// // now iterate over the questions, create a question using subtopic id and the question
184-
// const questionResp = []
185-
// for (const question of questions) {
186-
// console.log(req.files);
187-
// const questionSaved = await updateQuestionByIdQuery(
188-
// question.id,
189-
// {
190-
// subtopicId,
191-
// questionText: question.questionText,
192-
// answerType: question.answerType,
193-
// evidenceFileRequired: question.evidenceFileRequired,
194-
// hint: question.hint,
195-
// isRequired: question.isRequired,
196-
// priorityLevel: question.priorityLevel,
197-
// answer: question.answer,
198-
// },
199-
// req.files as UploadedFile[]
200-
// );
201-
// questionResp.push(questionSaved)
202-
// }
203-
// subtopicToUpdate["questions"] = questionResp
204-
// subTopicResp.push(subtopicToUpdate)
205-
// }
206-
// const response = { ...topic, subTopics: subTopicResp }
207-
// res.status(200).json(STATUS_CODE[200]({ message: response }));
208-
// }
209-
// catch (error) {
210-
// return res.status(500).json(STATUS_CODE[500]((error as Error).message));
211-
// }
212-
// }
213-
214154
export async function getAnswers(req: Request, res: Response): Promise<any> {
215155
try {
216156
const assessmentId = parseInt(req.params.id);
@@ -233,67 +173,6 @@ export async function getAnswers(req: Request, res: Response): Promise<any> {
233173
}
234174
}
235175

236-
// export async function updateAnswers(req: Request, res: Response): Promise<any> {
237-
// const requestBody = req.body as {
238-
// assessmentId: number;
239-
// topic: string;
240-
// topicId: number;
241-
// subtopic: {
242-
// id: number;
243-
// name: string;
244-
// questions: {
245-
// id: number;
246-
// subtopicId: number;
247-
// question: string;
248-
// answerType: string;
249-
// evidenceFileRequired: boolean;
250-
// hint: string;
251-
// isRequired: boolean;
252-
// priorityLevel: string;
253-
// answer: string;
254-
// evidenceFiles: [];
255-
// }[];
256-
// }[];
257-
// };
258-
// const assessmentId = requestBody.assessmentId;
259-
260-
// const topicId = requestBody.topicId;
261-
// // now, update the topic using the assessmentId and the topic
262-
// updateTopicByIdQuery(topicId, {
263-
// assessmentId,
264-
// title: requestBody.topic,
265-
// });
266-
267-
// // now iterate over the subtopics, update the subtopic using topic id and the subtopic
268-
// const subtopics = requestBody.subtopic;
269-
// for (const subtopic of subtopics) {
270-
// const subtopicId = subtopic.id;
271-
// updateSubtopicByIdQuery(subtopicId, {
272-
// topicId,
273-
// name: subtopic.name,
274-
// });
275-
// const questions = subtopic.questions;
276-
// // now iterate over the questions, update the question using subtopic id and the question
277-
// for (const question of questions) {
278-
// const questionId = question.id;
279-
// updateQuestionByIdQuery(
280-
// questionId,
281-
// {
282-
// subtopicId,
283-
// questionText: question.question,
284-
// answerType: question.answerType,
285-
// evidenceFileRequired: question.evidenceFileRequired,
286-
// hint: question.hint,
287-
// isRequired: question.isRequired,
288-
// priorityLevel: question.priorityLevel,
289-
// answer: question.answer,
290-
// },
291-
// question.evidenceFiles
292-
// );
293-
// }
294-
// }
295-
// }
296-
297176
export async function getAssessmentByProjectId(req: Request, res: Response) {
298177
const projectId = parseInt(req.params.id);
299178
console.log("projectId : ", projectId);

Servers/controllers/autoDriver.ctrl.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { STATUS_CODE } from "../utils/statusCode.utils";
44

55
export async function postAutoDriver(req: Request, res: Response) {
66
try {
7-
insertMockData()
7+
await insertMockData()
88
res.status(201).json(STATUS_CODE[201]("Mock data inserted"))
99
} catch(error) {
1010
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
@@ -13,7 +13,7 @@ export async function postAutoDriver(req: Request, res: Response) {
1313

1414
export async function deleteAutoDriver(req: Request, res: Response) {
1515
try {
16-
deleteMockData()
16+
await deleteMockData()
1717
res.status(200).json(STATUS_CODE[200]("Mock data deleted"))
1818
} catch(error) {
1919
return res.status(500).json(STATUS_CODE[500]((error as Error).message));

Servers/controllers/control.ctrl.ts

+26-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Control, ControlModel } from "../models/control.model";
1818
import { deleteFileById, uploadFile } from "../utils/fileUpload.utils";
1919
import { FileType } from "../models/file.model";
2020
import { updateProjectUpdatedByIdQuery } from "../utils/project.utils";
21+
import { sequelize } from "../database/db";
2122

2223
export async function getAllControls(
2324
req: Request,
@@ -56,17 +57,20 @@ export async function getControlById(
5657
}
5758

5859
export async function createControl(req: Request, res: Response): Promise<any> {
60+
const transaction = await sequelize.transaction();
5961
try {
6062
const newControl: Control = req.body;
6163

62-
const createdControl = await createNewControlQuery(newControl);
64+
const createdControl = await createNewControlQuery(newControl, transaction);
6365

6466
if (createdControl) {
67+
await transaction.commit();
6568
return res.status(201).json(STATUS_CODE[201](createdControl));
6669
}
6770

6871
return res.status(400).json(STATUS_CODE[400]({}));
6972
} catch (error) {
73+
await transaction.rollback();
7074
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
7175
}
7276
}
@@ -75,18 +79,21 @@ export async function updateControlById(
7579
req: Request,
7680
res: Response
7781
): Promise<any> {
82+
const transaction = await sequelize.transaction();
7883
try {
7984
const controlId = parseInt(req.params.id);
8085
const updatedControl: Control = req.body;
8186

82-
const control = await updateControlByIdQuery(controlId, updatedControl);
87+
const control = await updateControlByIdQuery(controlId, updatedControl, transaction);
8388

8489
if (control) {
90+
await transaction.commit();
8591
return res.status(200).json(STATUS_CODE[200](control));
8692
}
8793

8894
return res.status(400).json(STATUS_CODE[400](control));
8995
} catch (error) {
96+
await transaction.rollback();
9097
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
9198
}
9299
}
@@ -95,17 +102,20 @@ export async function deleteControlById(
95102
req: Request,
96103
res: Response
97104
): Promise<any> {
105+
const transaction = await sequelize.transaction();
98106
try {
99107
const controlId = parseInt(req.params.id);
100108

101-
const control = await deleteControlByIdQuery(controlId);
109+
const control = await deleteControlByIdQuery(controlId, transaction);
102110

103111
if (control) {
112+
await transaction.commit();
104113
return res.status(200).json(STATUS_CODE[200](control));
105114
}
106115

107116
return res.status(400).json(STATUS_CODE[400](control));
108117
} catch (error) {
118+
await transaction.rollback();
109119
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
110120
}
111121
}
@@ -114,6 +124,7 @@ export async function saveControls(
114124
req: RequestWithFile,
115125
res: Response
116126
): Promise<any> {
127+
const transaction = await sequelize.transaction();
117128
try {
118129
const controlId = parseInt(req.params.id);
119130
const Control = req.body as Control & {
@@ -136,11 +147,11 @@ export async function saveControls(
136147
due_date: Control.due_date,
137148
implementation_details: Control.implementation_details,
138149
control_category_id: Control.control_category_id,
139-
});
150+
}, transaction);
140151

141152
const filesToDelete = JSON.parse(Control.delete || "[]") as number[];
142153
for (let f of filesToDelete) {
143-
await deleteFileById(f);
154+
await deleteFileById(f, transaction);
144155
}
145156

146157
// now we need to iterate over subcontrols inside the control, and create a subcontrol for each subcontrol
@@ -160,7 +171,8 @@ export async function saveControls(
160171
f,
161172
Control.user_id,
162173
Control.project_id,
163-
"Compliance tracker group"
174+
"Compliance tracker group",
175+
transaction
164176
);
165177
evidenceUploadedFiles.push({
166178
id: evidenceUploadedFile.id!.toString(),
@@ -179,7 +191,8 @@ export async function saveControls(
179191
f,
180192
Control.user_id,
181193
Control.project_id,
182-
"Compliance tracker group"
194+
"Compliance tracker group",
195+
transaction
183196
);
184197
feedbackUploadedFiles.push({
185198
id: feedbackUploadedFile.id!.toString(),
@@ -217,9 +230,10 @@ export async function saveControls(
217230
feedback_description: subcontrol.feedback_description,
218231
control_id: subcontrol.control_id,
219232
},
233+
transaction,
220234
evidenceUploadedFiles,
221235
feedbackUploadedFiles,
222-
filesToDelete
236+
filesToDelete,
223237
);
224238
subControlResp.push(subcontrolToSave);
225239
}
@@ -228,10 +242,13 @@ export async function saveControls(
228242
...{ control, subControls: subControlResp },
229243
};
230244
// Update the project's last updated date
231-
await updateProjectUpdatedByIdQuery(controlId, "controls");
245+
await updateProjectUpdatedByIdQuery(controlId, "controls", transaction);
246+
247+
await transaction.commit();
232248

233249
return res.status(200).json(STATUS_CODE[200]({ response }));
234250
} catch (error) {
251+
await transaction.rollback();
235252
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
236253
}
237254
}

0 commit comments

Comments
 (0)