UNPKG

@tucmc/hazel

Version:
139 lines (138 loc) 4.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvaluationDocument = void 0; const path_1 = __importDefault(require("path")); const DMap_1 = require("../../util/data/DMap"); const IDUtil_1 = require("../data/ID/IDUtil"); const pdf = require('pdf-node'); class EvaluationDocument { clubID; clubMemberData; docInfo; userMap; constructor(clubID, docInfo, clubMemberData, uMap) { this.clubID = clubID; this.clubMemberData = clubMemberData; this.docInfo = docInfo; this.userMap = uMap; } createDocumentHeading() { return { club: IDUtil_1.IDUtil.translateToMainClubName(this.clubID), clubId: IDUtil_1.IDUtil.applyOverriddenLayer(this.clubID), sem: this.docInfo.semester, year: this.docInfo.year, count: this.clubMemberData.all.size(), pass: (this.clubMemberData.passed?.length || 0).toString(), failed: (this.clubMemberData.failed?.length || 0).toString(), exc: ((this.clubMemberData.break?.length || 0) + (this.clubMemberData.resign?.length || 0)).toString() }; } createSortedDetailedList(memberData) { return new DMap_1.DMap(memberData) .map((k, v) => { const userData = this.userMap.get(k); if (!userData) throw Error(`missing user id:${k}`); return { name: `${userData.get('title')}${userData.get('firstname')} ${userData.get('lastname')}`, grade: parseInt(userData.get('level'), 10), room: parseInt(userData.get('room'), 10), e: v.action === 'break' ? 'ลาพัก' : v.action === 'resign' ? 'ลาออก' : '' }; }) .sort((a, b) => a.grade - b.grade || a.room - b.room) .map((d, i) => { return { c: (i + 1).toString(), name: d.name, grade: `ม.${d.grade}`, room: d.room.toString(), e: d.e }; }); } fillBlank(d) { if (d.length === 0) { return [ { c: '', name: '', grade: '', room: '', e: '' } ]; } return d; } sliceToPrintableChunk(d, f = 15) { function chunk(arr, c) { const chunks = []; let i = 0; const n = arr.length; while (i < n) { chunks.push(arr.slice(i, (i += c))); } return chunks; } let sliced = [d]; if (d.length > f) { const firstChunk = d.slice(0, f); const rest = chunk(d.slice(f, d.length), 27); sliced = [firstChunk, ...rest]; } return sliced; } async generate(docTemplate, fileName) { const heading = this.createDocumentHeading(); let failed = this.clubMemberData.failed ? this.createSortedDetailedList(this.clubMemberData.failed) : []; let exc = this.clubMemberData.resign || this.clubMemberData.break ? this.createSortedDetailedList([ ...(this.clubMemberData.resign || []), ...(this.clubMemberData.break || []) ]) : []; failed = this.fillBlank(failed); exc = this.fillBlank(exc); const slicedExc = this.sliceToPrintableChunk(exc, 6); let excd; if (slicedExc.length > 1) { excd = slicedExc.slice(0, slicedExc.length - 1); } else { excd = [slicedExc[0] || []]; } const documentProps = { html: docTemplate.template, data: { general: heading, failed: this.sliceToPrintableChunk(failed), excdata: slicedExc.length > 1 ? excd : [], lastTable: slicedExc.length > 1 ? [slicedExc[slicedExc.length - 1]] : excd }, path: path_1.default.join('./out/evaluate/', `${fileName}.pdf`), type: 'pdf' }; const options = { format: 'A4', orientation: 'portrait', border: '10mm', footer: { height: '28mm' } }; await pdf(documentProps, options); } } exports.EvaluationDocument = EvaluationDocument;