|
| 1 | +import { Injectable } from "@nestjs/common"; |
| 2 | +import ExcelJS from "exceljs"; |
| 3 | +import { type Response } from "express"; |
| 4 | +import { PrismaService } from "src/core/prisma/prisma.service"; |
| 5 | +import { S3Service } from "src/core/s3/s3.service"; |
| 6 | + |
| 7 | +@Injectable() |
| 8 | +export class StaffExportService { |
| 9 | + constructor( |
| 10 | + private readonly prisma: PrismaService, |
| 11 | + private readonly s3: S3Service, |
| 12 | + ) {} |
| 13 | + |
| 14 | + private decodeQueryStrings(data: Record<string, unknown> | null | undefined): Record<string, unknown> { |
| 15 | + if (!data) return {}; |
| 16 | + |
| 17 | + return Object.fromEntries( |
| 18 | + Object.entries(data).map(([key, value]) => { |
| 19 | + if (typeof value !== "string") return [key, value]; |
| 20 | + try { |
| 21 | + return [key, decodeURI(value)]; |
| 22 | + } catch { |
| 23 | + return [key, value]; |
| 24 | + } |
| 25 | + }), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + async exportAll(res: Response) { |
| 30 | + const applications = await this.prisma.studentApplication.findMany({ |
| 31 | + where: { |
| 32 | + std_application_confirm: true, |
| 33 | + }, |
| 34 | + select: { |
| 35 | + std_application_id: true, |
| 36 | + std_user: { |
| 37 | + select: { |
| 38 | + id: true, |
| 39 | + email: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + std_total_score: { |
| 43 | + select: { |
| 44 | + std_regis_score: true, |
| 45 | + std_academic_score: true, |
| 46 | + std_academic_chaos_score: true, |
| 47 | + std_total_score: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + std_file: { |
| 51 | + where: { |
| 52 | + std_file_disabled: false, |
| 53 | + }, |
| 54 | + select: { |
| 55 | + std_file_type: true, |
| 56 | + std_file_key: true, |
| 57 | + pe_payment_evidence: { |
| 58 | + select: { |
| 59 | + pe_transaction_actual_amount: true, |
| 60 | + pe_transaction_date: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + std_info: { |
| 66 | + select: { |
| 67 | + std_info_prefix: true, |
| 68 | + std_info_first_name: true, |
| 69 | + std_info_last_name: true, |
| 70 | + std_info_nick_name: true, |
| 71 | + std_info_age: true, |
| 72 | + std_info_birthdate: true, |
| 73 | + std_info_gender: true, |
| 74 | + std_info_sexuality: true, |
| 75 | + std_info_religion: true, |
| 76 | + std_info_phone_number: true, |
| 77 | + std_info_education_level: true, |
| 78 | + std_info_education_institute: true, |
| 79 | + std_info_education_plan: true, |
| 80 | + std_info_grade_gpax: true, |
| 81 | + std_info_grade_math: true, |
| 82 | + std_info_grade_sci: true, |
| 83 | + std_info_grade_eng: true, |
| 84 | + std_info_parent_fullname: true, |
| 85 | + std_info_parent_relation: true, |
| 86 | + std_info_parent_phone_number: true, |
| 87 | + std_info_have_participated: true, |
| 88 | + std_info_have_laptop: true, |
| 89 | + std_info_can_participate_every_day: true, |
| 90 | + std_info_medical_insurance: true, |
| 91 | + std_info_chronic_disease: true, |
| 92 | + std_info_drug_allergy: true, |
| 93 | + std_info_food_allergy: true, |
| 94 | + std_info_blood_group: true, |
| 95 | + std_info_address: true, |
| 96 | + std_info_shirt_size: true, |
| 97 | + std_info_travel_plan: true, |
| 98 | + std_info_laptop_os: true, |
| 99 | + std_info_have_tablet: true, |
| 100 | + std_info_have_mouse: true, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + const data = await Promise.all( |
| 107 | + applications.map(async (app) => { |
| 108 | + const entries = await Promise.all( |
| 109 | + app.std_file.map(async (file) => { |
| 110 | + return [file.std_file_type, `https://staff.comcamp.io/file/${file.std_file_key}` || ""] as const; |
| 111 | + }), |
| 112 | + ); |
| 113 | + const result = this.decodeQueryStrings(Object.fromEntries(entries) as Record<string, string>); |
| 114 | + const userData = this.decodeQueryStrings(app.std_user); |
| 115 | + const infoData = this.decodeQueryStrings(app.std_info); |
| 116 | + const scoreData = this.decodeQueryStrings(app.std_total_score); |
| 117 | + |
| 118 | + return { |
| 119 | + ...app, |
| 120 | + ...userData, |
| 121 | + ...infoData, |
| 122 | + ...scoreData, |
| 123 | + ...result, |
| 124 | + }; |
| 125 | + }), |
| 126 | + ); |
| 127 | + |
| 128 | + const workbook = new ExcelJS.Workbook(); |
| 129 | + const worksheetAll = workbook.addWorksheet("All"); |
| 130 | + const worksheetMaleM4 = workbook.addWorksheet("Male M4"); |
| 131 | + const worksheetMaleM5 = workbook.addWorksheet("Male M5"); |
| 132 | + const worksheetFemaleM4 = workbook.addWorksheet("Female M4"); |
| 133 | + const worksheetFemaleM5 = workbook.addWorksheet("FeMale M5"); |
| 134 | + |
| 135 | + const columns = [ |
| 136 | + { header: "Application ID", key: "std_application_id" }, |
| 137 | + { header: "User ID", key: "id" }, |
| 138 | + { header: "Email", key: "email" }, |
| 139 | + { header: "Prefix", key: "std_info_prefix" }, |
| 140 | + { header: "Firstname", key: "std_info_first_name" }, |
| 141 | + { header: "Lastname", key: "std_info_last_name" }, |
| 142 | + { header: "Nickname", key: "std_info_nick_name" }, |
| 143 | + { header: "Age", key: "std_info_age" }, |
| 144 | + { header: "Birthdate", key: "std_info_birthdate" }, |
| 145 | + { header: "Gender", key: "std_info_gender" }, |
| 146 | + { header: "Sexuality", key: "std_info_sexuality" }, |
| 147 | + { header: "Religion", key: "std_info_religion" }, |
| 148 | + { header: "Phone Number", key: "std_info_phone_number" }, |
| 149 | + { header: "Address", key: "std_info_address" }, |
| 150 | + { header: "Shirt Size", key: "std_info_shirt_size" }, |
| 151 | + { header: "Travel Plan", key: "std_info_travel_plan" }, |
| 152 | + { header: "Grade", key: "std_info_education_level" }, |
| 153 | + { header: "Institute", key: "std_info_education_institute" }, |
| 154 | + { header: "Plan", key: "std_info_education_plan" }, |
| 155 | + { header: "GPAX", key: "std_info_grade_gpax" }, |
| 156 | + { header: "GPA Math", key: "std_info_grade_math" }, |
| 157 | + { header: "GPA Science", key: "std_info_grade_sci" }, |
| 158 | + { header: "GPA English", key: "std_info_grade_eng" }, |
| 159 | + { header: "Medical Insurance", key: "std_info_medical_insurance" }, |
| 160 | + { header: "Chronic Disease", key: "std_info_chronic_disease" }, |
| 161 | + { header: "Drug Allergy", key: "std_info_drug_allergy" }, |
| 162 | + { header: "Food Allergy", key: "std_info_food_allergy" }, |
| 163 | + { header: "Parent Name", key: "std_info_parent_fullname" }, |
| 164 | + { header: "Parent Relation", key: "std_info_parent_relation" }, |
| 165 | + |
| 166 | + { header: "Have participated before", key: "std_info_have_participated" }, |
| 167 | + { header: "Have laptop", key: "std_info_have_laptop" }, |
| 168 | + { header: "Laptop OS", key: "std_info_laptop_os" }, |
| 169 | + { header: "Have mouse", key: "std_info_have_mouse" }, |
| 170 | + { header: "Have tablet", key: "std_info_have_tablet" }, |
| 171 | + { header: "Can participate every day", key: "std_info_can_participate_every_day" }, |
| 172 | + |
| 173 | + { header: "Regis Question Score", key: "std_regis_score" }, |
| 174 | + { header: "Academic Question Score", key: "std_academic_score" }, |
| 175 | + { header: "Academic Chaos Question Score", key: "std_academic_chaos_score" }, |
| 176 | + { header: "Total Score (Regis + Academic Chaos)", key: "std_total_score" }, |
| 177 | + |
| 178 | + { header: "Face", key: "file_face" }, |
| 179 | + { header: "National ID", key: "file_national_id" }, |
| 180 | + { header: "Transcript", key: "file_pp_1" }, |
| 181 | + { header: "Student Certification", key: "file_pp_7" }, |
| 182 | + { header: "Parent Permission", key: "file_parent_permission" }, |
| 183 | + ]; |
| 184 | + |
| 185 | + worksheetAll.columns = columns; |
| 186 | + worksheetMaleM4.columns = columns; |
| 187 | + worksheetMaleM5.columns = columns; |
| 188 | + worksheetFemaleM4.columns = columns; |
| 189 | + worksheetFemaleM5.columns = columns; |
| 190 | + |
| 191 | + worksheetAll.addRows(data); |
| 192 | + worksheetMaleM4.addRows(data.filter((d: any) => (d.std_info_education_level === "มัธยมศึกษาปีที่ 4" || d.std_info_education_level === "ปวช. 1") && d.std_info_gender === "male")); |
| 193 | + worksheetMaleM5.addRows(data.filter((d: any) => (d.std_info_education_level === "มัธยมศึกษาปีที่ 5" || d.std_info_education_level === "ปวช. 2") && d.std_info_gender === "male")); |
| 194 | + worksheetFemaleM4.addRows(data.filter((d: any) => (d.std_info_education_level === "มัธยมศึกษาปีที่ 4" || d.std_info_education_level === "ปวช. 1") && d.std_info_gender === "female")); |
| 195 | + worksheetFemaleM5.addRows(data.filter((d: any) => (d.std_info_education_level === "มัธยมศึกษาปีที่ 5" || d.std_info_education_level === "ปวช. 2") && d.std_info_gender === "female")); |
| 196 | + |
| 197 | + res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| 198 | + res.setHeader("Content-Disposition", "attachment; filename=ComCamp37-khomul-nongnong.xlsx"); |
| 199 | + |
| 200 | + // // asked chat dont ask me how it work |
| 201 | + worksheetAll.columns.forEach((column) => { |
| 202 | + let maxLength = 0; |
| 203 | + if (!column.eachCell) return; |
| 204 | + column.eachCell({ includeEmpty: true }, (cell) => { |
| 205 | + const value = cell.value ? cell.value.toString() : ""; |
| 206 | + maxLength = Math.max(maxLength, value.length); |
| 207 | + }); |
| 208 | + column.width = maxLength + 2; |
| 209 | + }); |
| 210 | + worksheetMaleM4.columns.forEach((column) => { |
| 211 | + let maxLength = 0; |
| 212 | + if (!column.eachCell) return; |
| 213 | + column.eachCell({ includeEmpty: true }, (cell) => { |
| 214 | + const value = cell.value ? cell.value.toString() : ""; |
| 215 | + maxLength = Math.max(maxLength, value.length); |
| 216 | + }); |
| 217 | + column.width = maxLength + 2; |
| 218 | + }); |
| 219 | + worksheetMaleM5.columns.forEach((column) => { |
| 220 | + let maxLength = 0; |
| 221 | + if (!column.eachCell) return; |
| 222 | + column.eachCell({ includeEmpty: true }, (cell) => { |
| 223 | + const value = cell.value ? cell.value.toString() : ""; |
| 224 | + maxLength = Math.max(maxLength, value.length); |
| 225 | + }); |
| 226 | + column.width = maxLength + 2; |
| 227 | + }); |
| 228 | + worksheetFemaleM4.columns.forEach((column) => { |
| 229 | + let maxLength = 0; |
| 230 | + if (!column.eachCell) return; |
| 231 | + column.eachCell({ includeEmpty: true }, (cell) => { |
| 232 | + const value = cell.value ? cell.value.toString() : ""; |
| 233 | + maxLength = Math.max(maxLength, value.length); |
| 234 | + }); |
| 235 | + column.width = maxLength + 2; |
| 236 | + }); |
| 237 | + worksheetFemaleM5.columns.forEach((column) => { |
| 238 | + let maxLength = 0; |
| 239 | + if (!column.eachCell) return; |
| 240 | + column.eachCell({ includeEmpty: true }, (cell) => { |
| 241 | + const value = cell.value ? cell.value.toString() : ""; |
| 242 | + maxLength = Math.max(maxLength, value.length); |
| 243 | + }); |
| 244 | + column.width = maxLength + 2; |
| 245 | + }); |
| 246 | + |
| 247 | + await workbook.xlsx.write(res); |
| 248 | + res.end(); |
| 249 | + } |
| 250 | +} |
0 commit comments