-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add export module add methods supporting excel export add types * add small tweaks * fix yarn.lock * yarn lock * update yarn lock * update yarn lock * Update yarn.lock * Fix unit tests * add ExportableData type * Use prisma validator to create the input type Co-authored-by: Andrey <[email protected]> Co-authored-by: Ilko Kacharov <[email protected]>
- Loading branch information
1 parent
07ff323
commit dbc3a45
Showing
15 changed files
with
538 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Prisma } from '@prisma/client' | ||
|
||
export const donationWithPerson = Prisma.validator<Prisma.DonationFindManyArgs>()({ | ||
include: { | ||
person: { | ||
select: { | ||
firstName: true, | ||
lastName: true, | ||
}, | ||
}, | ||
targetVault: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: [{ createdAt: 'desc' }], | ||
}) | ||
|
||
export type DonationWithPerson = Prisma.DonationGetPayload<typeof donationWithPerson> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common' | ||
import { ExportService } from './export.service' | ||
import { PrismaService } from '../prisma/prisma.service' | ||
|
||
@Module({ | ||
providers: [ExportService, PrismaService], | ||
exports: [ExportService], | ||
}) | ||
export class ExportModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { PrismaService } from '../prisma/prisma.service' | ||
import { Response } from 'express' | ||
import { createWorkbook } from './helpers/createWorkbook' | ||
import { ExportableData } from './helpers/exportableData' | ||
import { Template } from './helpers/exportableData' | ||
|
||
@Injectable() | ||
export class ExportService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
exportToExcel = async (res: Response, data: ExportableData, template: Template) => { | ||
if (!data.length) res.status(404).end('No data to export') | ||
|
||
try { | ||
const workbook = createWorkbook(data, template) | ||
|
||
res.set({ | ||
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
'Content-Disposition': 'attachment;', | ||
}) | ||
|
||
workbook.xlsx.write(res).then(() => { | ||
res.status(200).end() | ||
}) | ||
} catch (err) { | ||
throw new Error(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import excelJs from 'exceljs' | ||
import { ExportableData } from './exportableData' | ||
import { Template } from './exportableData' | ||
|
||
const applySheetDataToRows = (sheet, data: ExportableData) => { | ||
data.forEach((el) => { | ||
sheet.addRow(el) | ||
}) | ||
} | ||
|
||
const handleHeadersRowStyle = (header, style) => { | ||
header.font = style?.font | ||
header.alignment = style?.alignment | ||
header.height = style?.height | ||
} | ||
|
||
const handleBodyRowsStyle = (sheet, style) => { | ||
let rowIndex = 2 | ||
for (rowIndex; rowIndex <= sheet.rowCount; rowIndex++) { | ||
const currentRow = sheet.getRow(rowIndex) | ||
currentRow.alignment = style?.alignment | ||
} | ||
} | ||
|
||
export const createWorkbook = (data: ExportableData, template: Template) => { | ||
const workbook = new excelJs.Workbook() | ||
template.sheets.forEach((sheet) => { | ||
const { title, columns, style } = sheet | ||
|
||
const currentSheet = workbook.addWorksheet(title) | ||
currentSheet.columns = columns | ||
|
||
applySheetDataToRows(currentSheet, data) | ||
|
||
const headerRow = currentSheet.getRow(1) | ||
handleHeadersRowStyle(headerRow, style.header) | ||
|
||
handleBodyRowsStyle(currentSheet, style.body) | ||
}) | ||
|
||
return workbook | ||
} |
Oops, something went wrong.