mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
refactor(nestjs): export module
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
import { Response } from 'express';
|
||||
import { convertAcceptFormatToFormat } from './_utils';
|
||||
import { Controller, Headers, Query, Res } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Controller, Get, Headers, Query, Res } from '@nestjs/common';
|
||||
import { AcceptType } from '@/constants/accept-type';
|
||||
import { ExportQuery } from './dtos/ExportQuery.dto';
|
||||
import { ExportResourceService } from './ExportService';
|
||||
import { AcceptType } from '@/constants/accept-type';
|
||||
import { convertAcceptFormatToFormat } from './Export.utils';
|
||||
|
||||
@Controller('/export')
|
||||
@ApiTags('export')
|
||||
export class ExportController {
|
||||
constructor(private readonly exportResourceApp: ExportResourceService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Retrieves exported the given resource.' })
|
||||
async export(
|
||||
@Query() query: ExportQuery,
|
||||
@Res() res: Response,
|
||||
@Headers('accept') acceptHeader: string,
|
||||
) {
|
||||
const applicationFormat = convertAcceptFormatToFormat(acceptType);
|
||||
const applicationFormat = convertAcceptFormatToFormat(acceptHeader);
|
||||
|
||||
const data = await this.exportResourceApp.export(
|
||||
query.resource,
|
||||
|
||||
13
packages/server/src/modules/Export/Export.utils.ts
Normal file
13
packages/server/src/modules/Export/Export.utils.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ACCEPT_TYPE } from '@/common/constants/http.constants';
|
||||
import { ExportFormat } from './common';
|
||||
|
||||
export const convertAcceptFormatToFormat = (accept: string): ExportFormat => {
|
||||
switch (accept) {
|
||||
case ACCEPT_TYPE.APPLICATION_CSV:
|
||||
return ExportFormat.Csv;
|
||||
case ACCEPT_TYPE.APPLICATION_PDF:
|
||||
return ExportFormat.Pdf;
|
||||
case ACCEPT_TYPE.APPLICATION_XLSX:
|
||||
return ExportFormat.Xlsx;
|
||||
}
|
||||
};
|
||||
@@ -1,22 +1,3 @@
|
||||
// import Container, { Service } from 'typedi';
|
||||
// import { AccountsExportable } from '../Accounts/AccountsExportable';
|
||||
// import { ExportableRegistry } from './ExportRegistery';
|
||||
// import { ItemsExportable } from '../Items/ItemsExportable';
|
||||
// import { CustomersExportable } from '../Contacts/Customers/CustomersExportable';
|
||||
// import { VendorsExportable } from '../Contacts/Vendors/VendorsExportable';
|
||||
// import { ExpensesExportable } from '../Expenses/ExpensesExportable';
|
||||
// import { SaleInvoicesExportable } from '../Sales/Invoices/SaleInvoicesExportable';
|
||||
// import { SaleEstimatesExportable } from '../Sales/Estimates/SaleEstimatesExportable';
|
||||
// import { SaleReceiptsExportable } from '../Sales/Receipts/SaleReceiptsExportable';
|
||||
// import { BillsExportable } from '../Purchases/Bills/BillsExportable';
|
||||
// import { PaymentsReceivedExportable } from '../Sales/PaymentReceived/PaymentsReceivedExportable';
|
||||
// import { BillPaymentExportable } from '../Purchases/BillPayments/BillPaymentExportable';
|
||||
// import { ManualJournalsExportable } from '../ManualJournals/ManualJournalExportable';
|
||||
// import { CreditNotesExportable } from '../CreditNotes/CreditNotesExportable';
|
||||
// import { VendorCreditsExportable } from '../Purchases/VendorCredits/VendorCreditsExportable';
|
||||
// import { ItemCategoriesExportable } from '../ItemCategories/ItemCategoriesExportable';
|
||||
// import { TaxRatesExportable } from '../TaxRates/TaxRatesExportable';
|
||||
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ExportableRegistry } from "./ExportRegistery";
|
||||
import { AccountsExportable } from "../Accounts/AccountsExportable.service";
|
||||
@@ -33,7 +14,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 },
|
||||
|
||||
@@ -22,7 +22,6 @@ export class ExportResourceService {
|
||||
) {}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} resourceName
|
||||
* @param {ExportFormat} format
|
||||
* @returns
|
||||
@@ -46,15 +45,14 @@ export class ExportResourceService {
|
||||
format: ExportFormat = ExportFormat.Csv
|
||||
) {
|
||||
const resource = sanitizeResourceName(resourceName);
|
||||
const resourceMeta = this.getResourceMeta(tenantId, resource);
|
||||
const resourceMeta = this.getResourceMeta(resource);
|
||||
const resourceColumns = this.resourceService.getResourceColumns(
|
||||
tenantId,
|
||||
resource
|
||||
);
|
||||
this.validateResourceMeta(resourceMeta);
|
||||
|
||||
const data = await this.getExportableData(tenantId, resource);
|
||||
const transformed = this.transformExportedData(tenantId, resource, data);
|
||||
const data = await this.getExportableData(resource);
|
||||
const transformed = this.transformExportedData(resource, data);
|
||||
|
||||
// Returns the csv, xlsx format.
|
||||
if (format === ExportFormat.Csv || format === ExportFormat.Xlsx) {
|
||||
@@ -67,7 +65,6 @@ export class ExportResourceService {
|
||||
const printableColumns = this.getPrintableColumns(resourceMeta);
|
||||
|
||||
return this.exportPdf.pdf(
|
||||
tenantId,
|
||||
printableColumns,
|
||||
transformed,
|
||||
resourceMeta?.print?.pageTitle
|
||||
|
||||
@@ -6,7 +6,7 @@ export class Exportable {
|
||||
*/
|
||||
public async exportable(
|
||||
query: Record<string, any>,
|
||||
): Promise<Array<Record<string, any>>> {
|
||||
): Promise<any> {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
const exportableModels = new Map<string, boolean>();
|
||||
const exportableService = new Map<string, any>()
|
||||
|
||||
/**
|
||||
* Decorator that marks a model as exportable and registers its metadata.
|
||||
* @param metadata Model metadata configuration for export
|
||||
*/
|
||||
export function ExportableModel() {
|
||||
return function (target: any) {
|
||||
const modelName = target.name;
|
||||
exportableModels.set(modelName, true);
|
||||
};
|
||||
}
|
||||
|
||||
export function ExportableService({ name }: { name: string }) {
|
||||
return function (target: any) {
|
||||
exportableService.set(name, target);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the registered exportable model metadata
|
||||
* @param modelName Name of the model class
|
||||
*/
|
||||
export function getExportableModelMeta(modelName: string): boolean | undefined {
|
||||
return exportableModels.get(modelName);
|
||||
}
|
||||
|
||||
|
||||
export function getExportableService(modelName: string) {
|
||||
return exportableService.get(modelName);
|
||||
}
|
||||
Reference in New Issue
Block a user