Skip to content

Commit

Permalink
Merge pull request #169 from icefoganalytics/test
Browse files Browse the repository at this point in the history
Fixing CSL Export when Empty
  • Loading branch information
datajohnson authored Oct 3, 2023
2 parents b84259a + 3d35091 commit 046a6db
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 87 deletions.
120 changes: 48 additions & 72 deletions src/api/routes/admin/csl-certificate-export-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -106,34 +96,20 @@ 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(
"((m.is_full_time = CASE d.disbursement_type_id WHEN 4 THEN 1 ELSE 0 END) OR hd.academic_year_id <= 2012) "
)
);

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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
}
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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/;
Expand All @@ -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;
Expand Down

0 comments on commit 046a6db

Please sign in to comment.