|
| 1 | +// src/excel/excel.service.ts |
| 2 | +import { Injectable } from "@nestjs/common"; |
| 3 | +import * as ExcelJS from "exceljs"; |
| 4 | +import { |
| 5 | + UserExcelDto, |
| 6 | + WorkspaceExcelDto, |
| 7 | +} from "../payloads/downgrade-user.payload"; |
| 8 | + |
| 9 | +@Injectable() |
| 10 | +export class ExcelEmailService { |
| 11 | + /** |
| 12 | + * Generate downgrade summary Excel with deleted workspaces and members |
| 13 | + */ |
| 14 | + async generateDowngradeSummaryExcel( |
| 15 | + workspaces: WorkspaceExcelDto[], |
| 16 | + users: UserExcelDto[], |
| 17 | + ): Promise<Buffer> { |
| 18 | + const workbook = new ExcelJS.Workbook(); |
| 19 | + // Set workbook properties |
| 20 | + workbook.creator = "Sparrow"; |
| 21 | + workbook.created = new Date(); |
| 22 | + workbook.modified = new Date(); |
| 23 | + // ==================== Deleted Workspaces Sheet ==================== |
| 24 | + const workspacesSheet = workbook.addWorksheet("Deleted Workspaces", { |
| 25 | + properties: { tabColor: { argb: "FF316CF6" } }, |
| 26 | + }); |
| 27 | + // Define columns for workspaces |
| 28 | + workspacesSheet.columns = [ |
| 29 | + { header: "Workspace Name", key: "name", width: 35 }, |
| 30 | + { header: "Created At", key: "created_at", width: 20 }, |
| 31 | + { header: "Collections", key: "collections", width: 15 }, |
| 32 | + { header: "Test Flows", key: "testflow", width: 15 }, |
| 33 | + ]; |
| 34 | + // Style header row for workspaces |
| 35 | + const workspaceHeaderRow = workspacesSheet.getRow(1); |
| 36 | + workspaceHeaderRow.font = { |
| 37 | + bold: true, |
| 38 | + color: { argb: "FFFFFFFF" }, |
| 39 | + size: 12, |
| 40 | + }; |
| 41 | + workspaceHeaderRow.fill = { |
| 42 | + type: "pattern", |
| 43 | + pattern: "solid", |
| 44 | + fgColor: { argb: "FF316CF6" }, |
| 45 | + }; |
| 46 | + workspaceHeaderRow.alignment = { |
| 47 | + vertical: "middle", |
| 48 | + horizontal: "center", |
| 49 | + }; |
| 50 | + workspaceHeaderRow.height = 25; |
| 51 | + // Add border to header |
| 52 | + workspaceHeaderRow.eachCell((cell) => { |
| 53 | + cell.border = { |
| 54 | + top: { style: "thin" }, |
| 55 | + left: { style: "thin" }, |
| 56 | + bottom: { style: "thin" }, |
| 57 | + right: { style: "thin" }, |
| 58 | + }; |
| 59 | + }); |
| 60 | + // Add workspace data |
| 61 | + workspaces.forEach((workspace, index) => { |
| 62 | + const row = workspacesSheet.addRow({ |
| 63 | + name: workspace.name, |
| 64 | + created_at: workspace.created_at || "N/A", |
| 65 | + collections: workspace.collections, |
| 66 | + testflow: workspace.testflow, |
| 67 | + }); |
| 68 | + // Alternate row colors for better readability |
| 69 | + if (index % 2 === 0) { |
| 70 | + row.fill = { |
| 71 | + type: "pattern", |
| 72 | + pattern: "solid", |
| 73 | + fgColor: { argb: "FFF8F9FA" }, |
| 74 | + }; |
| 75 | + } |
| 76 | + // Add borders to data cells |
| 77 | + row.eachCell((cell) => { |
| 78 | + cell.border = { |
| 79 | + top: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 80 | + left: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 81 | + bottom: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 82 | + right: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 83 | + }; |
| 84 | + cell.alignment = { vertical: "middle" }; |
| 85 | + }); |
| 86 | + }); |
| 87 | + // ==================== Deleted Members Sheet ==================== |
| 88 | + const membersSheet = workbook.addWorksheet("Deleted Members", { |
| 89 | + properties: { tabColor: { argb: "FF316CF6" } }, |
| 90 | + }); |
| 91 | + // Define columns for members |
| 92 | + membersSheet.columns = [ |
| 93 | + { header: "Name", key: "name", width: 30 }, |
| 94 | + { header: "Email", key: "email", width: 40 }, |
| 95 | + { header: "Role", key: "role", width: 15 }, |
| 96 | + ]; |
| 97 | + // Style header row for members |
| 98 | + const memberHeaderRow = membersSheet.getRow(1); |
| 99 | + memberHeaderRow.font = { |
| 100 | + bold: true, |
| 101 | + color: { argb: "FFFFFFFF" }, |
| 102 | + size: 12, |
| 103 | + }; |
| 104 | + memberHeaderRow.fill = { |
| 105 | + type: "pattern", |
| 106 | + pattern: "solid", |
| 107 | + fgColor: { argb: "FF316CF6" }, |
| 108 | + }; |
| 109 | + memberHeaderRow.alignment = { |
| 110 | + vertical: "middle", |
| 111 | + horizontal: "center", |
| 112 | + }; |
| 113 | + memberHeaderRow.height = 25; |
| 114 | + |
| 115 | + // Add border to header |
| 116 | + memberHeaderRow.eachCell((cell) => { |
| 117 | + cell.border = { |
| 118 | + top: { style: "thin" }, |
| 119 | + left: { style: "thin" }, |
| 120 | + bottom: { style: "thin" }, |
| 121 | + right: { style: "thin" }, |
| 122 | + }; |
| 123 | + }); |
| 124 | + |
| 125 | + // Add member data |
| 126 | + users.forEach((user, index) => { |
| 127 | + const row = membersSheet.addRow({ |
| 128 | + name: user.name, |
| 129 | + email: user.email, |
| 130 | + }); |
| 131 | + |
| 132 | + // Alternate row colors |
| 133 | + if (index % 2 === 0) { |
| 134 | + row.fill = { |
| 135 | + type: "pattern", |
| 136 | + pattern: "solid", |
| 137 | + fgColor: { argb: "FFF8F9FA" }, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + // Add borders to data cells |
| 142 | + row.eachCell((cell) => { |
| 143 | + cell.border = { |
| 144 | + top: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 145 | + left: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 146 | + bottom: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 147 | + right: { style: "thin", color: { argb: "FFE0E0E0" } }, |
| 148 | + }; |
| 149 | + cell.alignment = { vertical: "middle" }; |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + // Generate and return buffer |
| 154 | + const buffer = await workbook.xlsx.writeBuffer(); |
| 155 | + return Buffer.from(buffer); |
| 156 | + } |
| 157 | +} |
0 commit comments