feat: download csv report, download file, delete file

This commit is contained in:
izzy
2025-12-01 14:20:38 +00:00
parent fec8923431
commit 06fcd54b9f
10 changed files with 572 additions and 28 deletions
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Insertable, Kysely } from 'kysely';
import { InjectKysely } from 'nestjs-kysely';
import { Readable } from 'node:stream';
import {
MaintenanceGetIntegrityReportDto,
MaintenanceIntegrityReportResponseDto,
@@ -23,8 +24,16 @@ export class IntegrityReportRepository {
.executeTakeFirst();
}
async getIntegrityReportSummary(): Promise<MaintenanceIntegrityReportSummaryResponseDto> {
return await this.db
getById(id: string) {
return this.db
.selectFrom('integrity_report')
.selectAll('integrity_report')
.where('id', '=', id)
.executeTakeFirstOrThrow();
}
getIntegrityReportSummary(): Promise<MaintenanceIntegrityReportSummaryResponseDto> {
return this.db
.selectFrom('integrity_report')
.select((eb) =>
eb.fn
@@ -58,6 +67,28 @@ export class IntegrityReportRepository {
};
}
getIntegrityReportCsv(type: IntegrityReportType): Readable {
const items = this.db
.selectFrom('integrity_report')
.select(['id', 'type', 'path'])
.where('type', '=', type)
.orderBy('createdAt', 'desc')
.stream();
// very rudimentary csv serialiser
async function* generator() {
yield 'id,type,path\n';
for await (const item of items) {
// no expectation of particularly bad filenames
// but they could potentially have a newline or quote character
yield `${item.id},${item.type},"${item.path.replace(/"/g, '\\"')}"\n`;
}
}
return Readable.from(generator());
}
deleteById(id: string) {
return this.db.deleteFrom('integrity_report').where('id', '=', id).execute();
}