refactor: subscriptions to nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-03-24 23:38:43 +02:00
parent 4c42515613
commit ef22b9ddaf
87 changed files with 3949 additions and 92 deletions

View File

@@ -0,0 +1,46 @@
import { Response } from 'express';
import { convertAcceptFormatToFormat } from './_utils';
import { Controller, Headers, Query, Res } from '@nestjs/common';
import { ExportQuery } from './dtos/ExportQuery.dto';
import { ExportResourceService } from './ExportService';
import { AcceptType } from '@/constants/accept-type';
@Controller('/export')
export class ExportController {
constructor(private readonly exportResourceApp: ExportResourceService) {}
async export(
@Query() query: ExportQuery,
@Res() res: Response,
@Headers('accept') acceptHeader: string,
) {
const applicationFormat = convertAcceptFormatToFormat(acceptType);
const data = await this.exportResourceApp.export(
query.resource,
applicationFormat,
);
// Retrieves the csv format.
if (acceptHeader.includes(AcceptType.ApplicationCsv)) {
res.setHeader('Content-Disposition', 'attachment; filename=output.csv');
res.setHeader('Content-Type', 'text/csv');
return res.send(data);
// Retrieves the xlsx format.
} else if (acceptHeader.includes(AcceptType.ApplicationXlsx)) {
res.setHeader('Content-Disposition', 'attachment; filename=output.xlsx');
res.setHeader(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
);
return res.send(data);
// Retrieve the pdf format.
} else if (acceptHeader.includes(AcceptType.ApplicationPdf)) {
res.set({
'Content-Type': 'application/pdf',
'Content-Length': data.length,
});
res.send(data);
}
}
}

View File

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { ExportController } from './Export.controller';
import { ExportResourceService } from './ExportService';
import { ExportPdf } from './ExportPdf';
import { ExportAls } from './ExportAls';
import { ExportApplication } from './ExportApplication';
@Module({
providers: [ExportResourceService, ExportPdf, ExportAls, ExportApplication],
controllers: [ExportController],
})
export class ExportModule {}

View File

@@ -16,7 +16,7 @@ export class ExportApplication {
* @param {string} reosurce
* @param {ExportFormat} format
*/
public export(tenantId: number, resource: string, format: ExportFormat) {
return this.exportResource.export(tenantId, resource, format);
public export(resource: string, format: ExportFormat) {
return this.exportResource.export(resource, format);
}
}

View File

@@ -1,8 +1,7 @@
// @ts-nocheck
// import { ChromiumlyTenancy } from '../ChromiumlyTenancy/ChromiumlyTenancy';
// import { TemplateInjectable } from '../TemplateInjectable/TemplateInjectable';
import { mapPdfRows } from './utils';
import { Injectable } from '@nestjs/common';
import { ChromiumlyTenancy } from '../ChromiumlyTenancy/ChromiumlyTenancy.service';
import { TemplateInjectable } from '../TemplateInjectable/TemplateInjectable.service';
import { mapPdfRows } from './utils';
@Injectable()
export class ExportPdf {
@@ -13,7 +12,6 @@ export class ExportPdf {
/**
* Generates the pdf table sheet for the given data and columns.
* @param {number} tenantId
* @param {} columns
* @param {Record<string, string>} data
* @param {string} sheetTitle
@@ -21,7 +19,6 @@ export class ExportPdf {
* @returns
*/
public async pdf(
tenantId: number,
columns: { accessor: string },
data: Record<string, any>,
sheetTitle: string = '',
@@ -30,7 +27,6 @@ export class ExportPdf {
const rows = mapPdfRows(columns, data);
const htmlContent = await this.templateInjectable.render(
tenantId,
'modules/export-resource-table',
{
table: { rows, columns },
@@ -39,7 +35,7 @@ export class ExportPdf {
}
);
// Convert the HTML content to PDF
return this.chromiumlyTenancy.convertHtmlContent(tenantId, htmlContent, {
return this.chromiumlyTenancy.convertHtmlContent(htmlContent, {
margins: { top: 0.2, bottom: 0.2, left: 0.2, right: 0.2 },
landscape: true,
});

View File

@@ -1,4 +1,3 @@
// @ts-nocheck
// import Container, { Service } from 'typedi';
// import { AccountsExportable } from '../Accounts/AccountsExportable';
// import { ExportableRegistry } from './ExportRegistery';
@@ -20,10 +19,10 @@
import { Injectable } from "@nestjs/common";
import { ExportableRegistry } from "./ExportRegistery";
import { AccountsExportable } from "../Accounts/AccountsExportable.service";
@Injectable()
export class ExportableResources {
constructor(
private readonly exportRegistry: ExportableRegistry,
) {
@@ -34,7 +33,7 @@ export class ExportableResources {
* Importable instances.
*/
private importables = [
// { resource: 'Account', exportable: AccountsExportable },
{ resource: 'Account', exportable: AccountsExportable },
// { resource: 'Item', exportable: ItemsExportable },
// { resource: 'ItemCategory', exportable: ItemCategoriesExportable },
// { resource: 'Customer', exportable: CustomersExportable },

View File

@@ -23,29 +23,25 @@ export class ExportResourceService {
/**
*
* @param {number} tenantId
* @param {string} resourceName
* @param {ExportFormat} format
* @returns
*/
public async export(
tenantId: number,
resourceName: string,
format: ExportFormat = ExportFormat.Csv
) {
return this.exportAls.run(() =>
this.exportAlsRun(tenantId, resourceName, format)
this.exportAlsRun(resourceName, format)
);
}
/**
* Exports the given resource data through csv, xlsx or pdf.
* @param {number} tenantId - Tenant id.
* @param {string} resourceName - Resource name.
* @param {ExportFormat} format - File format.
*/
public async exportAlsRun(
tenantId: number,
resourceName: string,
format: ExportFormat = ExportFormat.Csv
) {
@@ -85,8 +81,8 @@ export class ExportResourceService {
* @param {string} resource - The name of the resource.
* @returns The metadata of the resource.
*/
private getResourceMeta(tenantId: number, resource: string) {
return this.resourceService.getResourceMeta(tenantId, resource);
private getResourceMeta(resource: string) {
return this.resourceService.getResourceMeta(resource);
}
/**
@@ -104,18 +100,15 @@ export class ExportResourceService {
* Transforms the exported data based on the resource metadata.
* If the resource metadata specifies a flattening attribute (`exportFlattenOn`),
* the data will be flattened based on this attribute using the `flatDataCollections` utility function.
*
* @param {number} tenantId - The tenant identifier.
* @param {string} resource - The name of the resource.
* @param {Array<Record<string, any>>} data - The original data to be transformed.
* @returns {Array<Record<string, any>>} - The transformed data.
*/
private transformExportedData(
tenantId: number,
resource: string,
data: Array<Record<string, any>>
): Array<Record<string, any>> {
const resourceMeta = this.getResourceMeta(tenantId, resource);
const resourceMeta = this.getResourceMeta(resource);
return R.when<Array<Record<string, any>>, Array<Record<string, any>>>(
R.always(Boolean(resourceMeta.exportFlattenOn)),
@@ -129,11 +122,11 @@ export class ExportResourceService {
* @param {string} resource - The name of the resource.
* @returns A promise that resolves to the exportable data.
*/
private async getExportableData(tenantId: number, resource: string) {
private async getExportableData(resource: string) {
const exportable =
this.exportableResources.registry.getExportable(resource);
return exportable.exportable(tenantId, {});
return exportable.exportable({});
}
/**

View File

@@ -0,0 +1,11 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class ExportQuery {
@IsString()
@IsNotEmpty()
resource: string;
@IsString()
@IsNotEmpty()
format: string;
}