@@ -30,6 +30,7 @@ import {
30
30
import { Assessment , AssessmentModel } from "../models/assessment.model" ;
31
31
import { TopicModel } from "../models/topic.model" ;
32
32
import { SubtopicModel } from "../models/subtopic.model" ;
33
+ import { sequelize } from "../database/db" ;
33
34
34
35
export async function getAllAssessments (
35
36
req : Request ,
@@ -70,6 +71,7 @@ export async function createAssessment(
70
71
req : Request ,
71
72
res : Response
72
73
) : Promise < any > {
74
+ const transaction = await sequelize . transaction ( ) ;
73
75
try {
74
76
const newAssessment : Assessment = req . body ;
75
77
@@ -80,14 +82,16 @@ export async function createAssessment(
80
82
} )
81
83
) ;
82
84
}
83
- const createdAssessment = await createNewAssessmentQuery ( newAssessment , false ) ;
85
+ const createdAssessment = await createNewAssessmentQuery ( newAssessment , false , transaction ) ;
84
86
85
87
if ( createdAssessment ) {
88
+ await transaction . commit ( ) ;
86
89
return res . status ( 201 ) . json ( STATUS_CODE [ 201 ] ( createdAssessment ) ) ;
87
90
}
88
91
89
92
return res . status ( 503 ) . json ( STATUS_CODE [ 503 ] ( { } ) ) ;
90
93
} catch ( error ) {
94
+ await transaction . rollback ( ) ;
91
95
return res . status ( 500 ) . json ( STATUS_CODE [ 500 ] ( ( error as Error ) . message ) ) ;
92
96
}
93
97
}
@@ -96,6 +100,7 @@ export async function updateAssessmentById(
96
100
req : Request ,
97
101
res : Response
98
102
) : Promise < any > {
103
+ const transaction = await sequelize . transaction ( ) ;
99
104
try {
100
105
const assessmentId = parseInt ( req . params . id ) ;
101
106
const updatedAssessment : Assessment = req . body ;
@@ -109,15 +114,18 @@ export async function updateAssessmentById(
109
114
}
110
115
const assessment = await updateAssessmentByIdQuery (
111
116
assessmentId ,
112
- updatedAssessment
117
+ updatedAssessment ,
118
+ transaction
113
119
) ;
114
120
115
121
if ( assessment ) {
122
+ await transaction . commit ( ) ;
116
123
return res . status ( 202 ) . json ( STATUS_CODE [ 202 ] ( assessment ) ) ;
117
124
}
118
125
119
126
return res . status ( 404 ) . json ( STATUS_CODE [ 404 ] ( { } ) ) ;
120
127
} catch ( error ) {
128
+ await transaction . rollback ( ) ;
121
129
return res . status ( 500 ) . json ( STATUS_CODE [ 500 ] ( ( error as Error ) . message ) ) ;
122
130
}
123
131
}
@@ -126,91 +134,23 @@ export async function deleteAssessmentById(
126
134
req : Request ,
127
135
res : Response
128
136
) : Promise < any > {
137
+ const transaction = await sequelize . transaction ( ) ;
129
138
try {
130
139
const assessmentId = parseInt ( req . params . id ) ;
131
- const deletedAssessment = await deleteAssessmentByIdQuery ( assessmentId ) ;
140
+ const deletedAssessment = await deleteAssessmentByIdQuery ( assessmentId , transaction ) ;
132
141
133
142
if ( deletedAssessment ) {
143
+ await transaction . commit ( ) ;
134
144
return res . status ( 202 ) . json ( STATUS_CODE [ 202 ] ( deletedAssessment ) ) ;
135
145
}
136
146
137
147
return res . status ( 404 ) . json ( STATUS_CODE [ 404 ] ( { } ) ) ;
138
148
} catch ( error ) {
149
+ await transaction . rollback ( ) ;
139
150
return res . status ( 500 ) . json ( STATUS_CODE [ 500 ] ( ( error as Error ) . message ) ) ;
140
151
}
141
152
}
142
153
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
-
214
154
export async function getAnswers ( req : Request , res : Response ) : Promise < any > {
215
155
try {
216
156
const assessmentId = parseInt ( req . params . id ) ;
@@ -233,67 +173,6 @@ export async function getAnswers(req: Request, res: Response): Promise<any> {
233
173
}
234
174
}
235
175
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
-
297
176
export async function getAssessmentByProjectId ( req : Request , res : Response ) {
298
177
const projectId = parseInt ( req . params . id ) ;
299
178
console . log ( "projectId : " , projectId ) ;
0 commit comments