Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented member ledger file endpoint #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"@fast-csv/format": "^4.3.5",
"@types/morgan": "^1.9.3",
"@types/mysql": "^2.15.21",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-validator": "^6.14.2",
"fast-csv": "^4.3.6",
"morgan": "^1.10.0",
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
Expand Down
6 changes: 6 additions & 0 deletions src/models/member-service.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface LedgerOptions {
field: string;
header?: string;
}

export { LedgerOptions };
30 changes: 29 additions & 1 deletion src/routes/member.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from "express";
import { query, validationResult } from "express-validator";
import { body, query, validationResult } from "express-validator";
import * as path from "path";

import MemberService from "../services/member-service";

Expand Down Expand Up @@ -35,4 +36,31 @@ router.get(
}
);

/* POST /member/ledger */

router.post(
"/ledger",
body("options").isArray().notEmpty(),
body("options.*.field").isString().notEmpty(),
body("options.*.header").isString().optional(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

const { options } = req.body;

try {
await MemberService.writeLedgerFile(options);
return res.status(200).download(path.resolve(__dirname, "../services/member-ledger.csv"));
} catch (err) {
logger.error("MemberService.getLedgerFile failed. Error =");
logger.error(err);
}

return res.status(500).json({ message: "Unable to load resource" });
}
);

export default router;
18 changes: 17 additions & 1 deletion src/services/member-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// import { type } from "os";
import { writeToPath } from "fast-csv";
import logger from "../utils/logger/logger";
import * as path from "path";

import SQLService from "./sql-service";
import { LedgerOptions as LedgerOption } from "src/models/member-service.model";

class MemberService {
static async isMember(uh_id = "", email = "") {
Expand All @@ -24,6 +26,20 @@ class MemberService {
end_date: result[0].end_date,
};
}

static async writeLedgerFile(options: Array<LedgerOption>) {
logger.info("MemberService.getLedgerFile invoked! Options = " + options);

const fields = options.map((option) => { return option.field; });
const headers = options.map((option) => { return option.header ? option.header : option.field; });

const ledger_data = await SQLService.select("contact", { fields: fields });
const rows = ledger_data.map(Object.values);

writeToPath(path.resolve(__dirname, "member-ledger.csv"), rows, { headers: headers })
.on("error", (err) => console.error(err))
.on("finish", () => console.log("Done writing."));
}
}

export default MemberService;