From ab57061f9d16da3f6587265f04b1153e84d35bc2 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 3 Oct 2023 13:57:58 -0700 Subject: [PATCH] Fixing CSL Export --- .../admin/csl-certificate-export-router.ts | 120 +++++++----------- .../views/CslCertificateExport.vue | 26 ++-- 2 files changed, 59 insertions(+), 87 deletions(-) diff --git a/src/api/routes/admin/csl-certificate-export-router.ts b/src/api/routes/admin/csl-certificate-export-router.ts index 3b47db0a..863f8ab0 100644 --- a/src/api/routes/admin/csl-certificate-export-router.ts +++ b/src/api/routes/admin/csl-certificate-export-router.ts @@ -49,24 +49,20 @@ cslCertificateExportRouter.get( ) ); - if (results.length) { - const results2 = await db.raw("SELECT sfa.fn_cert_data(?, ?, ?) as fileText", [ - CSL_CERT_SEQ_P, - FROM_DATE_P, - TO_DATE_P, - ]); - - if (results2[0].fileText) { - return res.status(200).json({ success: true, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); - } else { - return res.status(200).json({ success: false, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); - } + const results2 = await db.raw("SELECT sfa.fn_cert_data(?, ?, ?) as fileText", [ + CSL_CERT_SEQ_P, + FROM_DATE_P, + TO_DATE_P, + ]); + + await db("sfa.disbursement") + .where({ csl_cert_seq_number: CSL_CERT_SEQ_P }) + .update({ ecert_sent_date: new Date() }); + + if (results2[0].fileText) { + return res.status(200).json({ success: true, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); } else { - return res.status(200).json({ - success: false, - batch: CSL_CERT_SEQ_P, - message: "There are no records between " + FROM_DATE_P + " and " + TO_DATE_P, - }); + return res.status(200).json({ success: false, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); } } catch (error: any) { console.log(error); @@ -76,17 +72,11 @@ cslCertificateExportRouter.get( ); cslCertificateExportRouter.get( - "/:FROM_DATE_P/:TO_DATE_P/:CSL_CERT_SEQ_P/:IS_PREVIEW/regenerate", - [ - param("CSL_CERT_SEQ_P").isInt().notEmpty(), - param("FROM_DATE_P").notEmpty(), - param("TO_DATE_P").notEmpty(), - param("IS_PREVIEW").notEmpty(), - ], + "/regenerate/:CSL_CERT_SEQ_P", + [param("CSL_CERT_SEQ_P").isInt().notEmpty()], //ReturnValidationErrors, async (req: Request, res: Response) => { - const { filter = true } = req.query; - const { CSL_CERT_SEQ_P, FROM_DATE_P, TO_DATE_P, IS_PREVIEW } = req.params; + const { CSL_CERT_SEQ_P } = req.params; try { const results = await db @@ -106,9 +96,7 @@ cslCertificateExportRouter.get( .join("sfa.disbursement AS d", "fr.id", "=", "d.funding_request_id") .join("sfa.request_type AS r", "fr.request_type_id", "=", "r.id") .join("sfa.msfaa AS m", "s.id", "=", "m.student_id") - .where(IS_PREVIEW === "1" ? "d.csl_cert_seq_number_prev" : "d.csl_cert_seq_number", "=", CSL_CERT_SEQ_P) - .andWhere("d.issue_date", ">=", FROM_DATE_P) - .andWhere("d.issue_date", "<=", TO_DATE_P) + .where("d.csl_cert_seq_number", "=", CSL_CERT_SEQ_P) .andWhere("m.msfaa_status", "=", "Received") .andWhere( db.raw( @@ -116,24 +104,12 @@ cslCertificateExportRouter.get( ) ); - if (results.length) { - const results2 = await db.raw("SELECT sfa.fn_cert_data_regen(?, ?, ?) as fileText", [ - CSL_CERT_SEQ_P, - FROM_DATE_P, - TO_DATE_P, - ]); - - if (results2[0].fileText) { - return res.status(200).json({ success: true, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); - } else { - return res.status(200).json({ success: false, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); - } + const results2 = await db.raw("SELECT sfa.fn_cert_data_regen(?) as fileText", [CSL_CERT_SEQ_P]); + + if (results2[0].fileText) { + return res.status(200).json({ success: true, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); } else { - return res.status(200).json({ - success: false, - batch: CSL_CERT_SEQ_P, - message: "There are no records between " + FROM_DATE_P + " and " + TO_DATE_P, - }); + return res.status(200).json({ success: false, data1: results, data2: results2, batch: CSL_CERT_SEQ_P }); } } catch (error: any) { console.log(error); @@ -151,41 +127,41 @@ cslCertificateExportRouter.put( const { FROM_DATE_P, TO_DATE_P, PREVIEW } = req.params; try { - const results = await db.select( + /* const results = await db.select( db.raw(`sfa.fn_get_count_disbursement_ecerts('${FROM_DATE_P}', '${TO_DATE_P}') AS result`) - ); + ); */ - if (results[0].result > 0) { - const nextVal = await db.select( - db.raw(`NEXT VALUE FOR ${PREVIEW === "1" ? "sfa.csl_cert_seq_prev" : "sfa.csl_cert_seq"} AS nextVal;`) - ); + //if (results[0].result > 0) { + const nextVal = await db.select( + db.raw(`NEXT VALUE FOR ${PREVIEW === "1" ? "sfa.csl_cert_seq_prev" : "sfa.csl_cert_seq"} AS nextVal;`) + ); - const innerSelect = await db.raw( - `EXEC ${ - PREVIEW === "1" ? "sfa.sp_get_and_update_csl_cert_seq_num_prev" : "sfa.sp_get_and_update_csl_cert_seq_num" - } '${FROM_DATE_P}','${TO_DATE_P}', ${nextVal[0].nextVal};` - ); + await db.raw( + `EXEC ${ + PREVIEW === "1" ? "sfa.sp_get_and_update_csl_cert_seq_num_prev" : "sfa.sp_get_and_update_csl_cert_seq_num" + } '${FROM_DATE_P}','${TO_DATE_P}', ${nextVal[0].nextVal};` + ); - const resultsCheck = await db.raw( - `SELECT count(id) as count FROM sfa.disbursement d where ${ - PREVIEW === "1" ? "csl_cert_seq_number_prev" : "csl_cert_seq_number" - } = ${nextVal[0].nextVal}` - ); + /* const resultsCheck = await db.raw( + `SELECT count(id) as count FROM sfa.disbursement d where ${ + PREVIEW === "1" ? "csl_cert_seq_number_prev" : "csl_cert_seq_number" + } = ${nextVal[0].nextVal}` + ); */ - if (resultsCheck[0].count) { - return res.json({ flag: 1, data: nextVal[0].nextVal }); - } else { - return res.json({ - flag: 0, - data: `Something went wrong! No records have been modified from ${FROM_DATE_P} to ${TO_DATE_P}`, - }); - } - } else { + //if (resultsCheck[0].count) { + return res.json({ flag: 1, data: nextVal[0].nextVal }); + /* } else { + return res.json({ + flag: 0, + data: `Something went wrong! No records have been modified from ${FROM_DATE_P} to ${TO_DATE_P}`, + }); + } */ + /* } else { return res.json({ flag: 0, data: `There are no certificates with received MSFAA between ${FROM_DATE_P} and ${TO_DATE_P}`, }); - } + } */ } catch (error: any) { console.log(error); return res.status(404).send(); diff --git a/src/web/src/modules/csl-certificate-export/views/CslCertificateExport.vue b/src/web/src/modules/csl-certificate-export/views/CslCertificateExport.vue index 5fa09987..62a3ee50 100644 --- a/src/web/src/modules/csl-certificate-export/views/CslCertificateExport.vue +++ b/src/web/src/modules/csl-certificate-export/views/CslCertificateExport.vue @@ -197,30 +197,28 @@ export default { this.isLoading = newFlag; let resInsert; if (isPreview) { - resInsert = await axios.put(CSL_CERTIFICATE_EXPORT + `/${this.from.date}/${this.to.date}/1`); + resInsert = await axios.put(`${CSL_CERTIFICATE_EXPORT}/${this.from.date}/${this.to.date}/1`); } else { - resInsert = await axios.put(CSL_CERTIFICATE_EXPORT + `/${this.from.date}/${this.to.date}/0`); + resInsert = await axios.put(`${CSL_CERTIFICATE_EXPORT}/${this.from.date}/${this.to.date}/0`); } + let newSequenceNumber = resInsert.data.data; + this.sequence = newSequenceNumber; + if (resInsert.data.flag === 0 || !resInsert.data.data) { let newFlag = { flag: false }; this.disabled = newFlag; - let newLoading = { flag: false }; this.isLoading = newFlag; this.$emit("showError", resInsert.data.data); } else { let resInsert2; if (isPreview === 1) { - this.sequence = resInsert.data.data; - resInsert2 = await axios.get( - CSL_CERTIFICATE_EXPORT + `/${this.from.date}/${this.to.date}/${resInsert.data.data}/1` + `${CSL_CERTIFICATE_EXPORT}/${this.from.date}/${this.to.date}/${newSequenceNumber}/1` ); } else { - this.sequence = resInsert.data.data; - resInsert2 = await axios.get( - CSL_CERTIFICATE_EXPORT + `/${this.from.date}/${this.to.date}/${resInsert.data.data}/0` + `${CSL_CERTIFICATE_EXPORT}/${this.from.date}/${this.to.date}/${newSequenceNumber}/0` ); } @@ -241,8 +239,6 @@ export default { FileSaver.saveAs(blob, `${isPreview === 1 ? "PREVIEW_" : ""}${resultado}.txt`); let newFlag = { flag: false }; this.disabled = newFlag; - - let newLoading = { flag: false }; this.isLoading = newFlag; } else { this.$emit("showError", resInsert2.data.message); @@ -260,7 +256,7 @@ export default { this.icon = "99"; // no toggle }); - if (this.from.date === "" || this.from.date === null || this.to.date === "" || this.to.date === null) { + if (!this.sequence) { this.modalTitle = "Error"; this.modalText = "Please fill in all the fields"; this.openModal(); @@ -270,13 +266,13 @@ export default { this.isLoading = newFlag; let resInsert2 = await axios.get( - `${CSL_CERTIFICATE_EXPORT}/${this.from.date}/${this.to.date}/${this.sequence}/0/regenerate` + `${CSL_CERTIFICATE_EXPORT}/regenerate/${this.sequence}` ); if (resInsert2.data.success) { this.tableData = resInsert2.data.data1; this.batch = resInsert2.data.batch; - this.generatePDF(isPreview); + this.generatePDF(0); let FileSaver = require("file-saver"); const regex = /PPYT\.EDU\.CERTS\.D\d+\.001/; @@ -287,7 +283,7 @@ export default { let blob = new Blob([resInsert2.data.data2[0]["fileText"].replace(/PPYT\.EDU\.CERTS\.D\d+\.001/, "")], { type: "text/plain;charset=utf-8", }); - FileSaver.saveAs(blob, `${isPreview === 1 ? "PREVIEW_" : ""}${resultado}.txt`); + FileSaver.saveAs(blob, `${resultado}.txt`); let newFlag = { flag: false }; this.disabled = newFlag; this.isLoading = newFlag;