Skip to content

Commit

Permalink
[BE] Exam-upload-flow-modifications (#1494)
Browse files Browse the repository at this point in the history
* Exam-upload-flow-modifications

* Conflicts resolved

* Duplication code resolved

* Uploaded by added to the api for manual and file upload
  • Loading branch information
GitVivekHub authored Sep 10, 2024
1 parent 32cfaa0 commit 1e71d80
Showing 1 changed file with 60 additions and 32 deletions.
92 changes: 60 additions & 32 deletions src/src/exam/exam.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,16 +641,34 @@ export class ExamService {
async createExamResult(body: any, request: any, response: any) {
let program_id = request?.mw_program_id;
let academic_year_id = request?.mw_academic_year_id;
let role = request?.mw_roles;
let examResultBody = body;
let examResult;
let uploaded_by;

//add upload type to the result body

examResultBody.upload_type = 'manual';

let examResult = await this.ExamResultUpsert(
//add uploaded_by to the result body

if (role?.includes('staff')) {
uploaded_by = 'IP';
} else if (role?.includes('facilitator')) {
uploaded_by = 'facilitator';
}

examResultBody.uploaded_by = uploaded_by;

examResult = await this.ExamResultUpsertNew(
examResultBody,
academic_year_id,
program_id,
);

let response_status = examResult?.status == 422 ? 422 : 200;
if (examResult) {
return response.status(200).json({
return response.status(response_status).json({
data: examResult,
});
} else {
Expand All @@ -660,7 +678,7 @@ export class ExamService {
}
}

async ExamResultUpsert(examResultBody, academic_year_id, program_id) {
async ExamResultUpsertNew(examResultBody, academic_year_id, program_id) {
let data;
let vquery;
let vresponse;
Expand Down Expand Up @@ -693,10 +711,11 @@ export class ExamService {
set_update = exam_result_id ? 1 : 0; // Set the update flag

if (set_update == 1) {
mutation_query = `
mutation UpdateExamResults {
update_exam_results_by_pk(pk_columns: {id: ${exam_result_id}}, _set: {
`;
return {
status: 422,
message: 'Exam data already exists',
data: exam_result_id,
};
} else {
mutation_query = `
mutation CreateExamResults {
Expand Down Expand Up @@ -736,11 +755,11 @@ export class ExamService {
exam_year
total_marks
final_result
upload_type
}
}
`;

console.log('mutation_query', mutation_query);
data = {
query: `${mutation_query}`,
variables: {},
Expand All @@ -749,17 +768,11 @@ export class ExamService {
const query_response =
await this.hasuraServiceFromServices.queryWithVariable(data);

console.log('query_response', query_response?.data);

exam_result_id =
set_update == 1
? query_response?.data?.data?.update_exam_results_by_pk?.id
: query_response?.data?.data?.insert_exam_results_one?.id;
query_response?.data?.data?.insert_exam_results_one?.id;

exam_result_response =
set_update == 1
? query_response?.data?.data?.update_exam_results_by_pk
: query_response?.data?.data?.insert_exam_results_one;
query_response?.data?.data?.insert_exam_results_one;

result = { ...exam_result_response }; // Set exam result data directly

Expand Down Expand Up @@ -787,20 +800,11 @@ export class ExamService {
exam_result_subjects_id =
vresponse?.data?.data?.exam_subject_results?.[0]?.id;

let query;

if (exam_result_subjects_id) {
query = `
mutation UpdateExamResultSubjects {
update_exam_subject_results_by_pk(pk_columns: {id: ${exam_result_subjects_id}}, _set: {
`;
} else {
query = `
let query = `
mutation CreateExamResultSubjects {
insert_exam_subject_results_one(object: {
exam_results_id:${exam_result_id},
`;
}

Object.keys(schedule).forEach((key) => {
if (schedule[key] !== null && schedule[key] !== '') {
Expand Down Expand Up @@ -842,9 +846,7 @@ export class ExamService {

const updatedOrCreatedEvent =
query_response?.data?.data?.[
exam_result_subjects_id
? 'update_exam_subject_results_by_pk'
: 'insert_exam_subject_results_one'
'insert_exam_subject_results_one'
];

if (updatedOrCreatedEvent) {
Expand Down Expand Up @@ -1250,9 +1252,12 @@ export class ExamService {
const user_id = request?.body?.user_id;
const board_id = request?.body?.board_id;
const enrollment = request?.body?.enrollment;
let role = request?.mw_roles;
let uploaded_by;
let validation_result;
let result_upload_status;
let update_status_body = {};
let examResult;

try {
const result = await this.examResultPattern.extractResultFromPDF(
Expand Down Expand Up @@ -1290,12 +1295,36 @@ export class ExamService {
//add document id in results
let document_id = document?.document_id;
result.document_id = document_id;
let examResult = await this.ExamResultUpsert(

// add upload_type to
result.upload_type = 'file';

//add uploaded_by to the result body

if (role?.includes('staff')) {
uploaded_by = 'IP';
} else if (role?.includes('facilitator')) {
uploaded_by = 'facilitator';
}

result.uploaded_by = uploaded_by;

examResult = await this.ExamResultUpsertNew(
result,
academic_year_id,
program_id,
);
if (examResult) {

if (examResult?.status == 422) {
return response.status(422).json({
success: false,
data: [],
document: [],
extracted_data: {
result,
},
});
} else if (examResult) {
return response.status(200).json({
success: true,
data: examResult,
Expand Down Expand Up @@ -1820,7 +1849,6 @@ export class ExamService {
newQdata?.enrollment_middle_name,
enrollment_dob: newQdata?.enrollment_dob,
user: newQdata?.user,

subjectsArray,
},
});
Expand Down

0 comments on commit 1e71d80

Please sign in to comment.