feat: wip configure resources to be exportable

This commit is contained in:
Ahmed Bouhuolia
2024-05-01 12:45:24 +02:00
parent 7e89966f20
commit fab71d2b65
25 changed files with 295 additions and 39 deletions

View File

@@ -16,7 +16,11 @@ export class CreditNotesExportable extends Exportable {
*/
public exportable(tenantId: number, query: ICreditNotesQueryDTO) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
page: 1,
pageSize: 12000,
} as ICreditNotesQueryDTO;
return this.getCreditNotes

View File

@@ -15,7 +15,11 @@ export class ExpensesExportable extends Exportable {
*/
public exportable(tenantId: number, query: IExpensesFilter) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
page: 1,
pageSize: 12000,
} as IExpensesFilter;
return this.expensesApplication

View File

@@ -14,6 +14,7 @@ import { BillPaymentExportable } from '../Purchases/BillPayments/BillPaymentExpo
import { ManualJournalsExportable } from '../ManualJournals/ManualJournalExportable';
import { CreditNotesExportable } from '../CreditNotes/CreditNotesExportable';
import { VendorCreditsExportable } from '../Purchases/VendorCredits/VendorCreditsExportable';
import { ItemCategoriesExportable } from '../ItemCategories/ItemCategoriesExportable';
@Service()
export class ExportableResources {
@@ -32,6 +33,7 @@ export class ExportableResources {
private importables = [
{ resource: 'Account', exportable: AccountsExportable },
{ resource: 'Item', exportable: ItemsExportable },
{ resource: 'ItemCategory', exportable: ItemCategoriesExportable },
{ resource: 'Customer', exportable: CustomersExportable },
{ resource: 'Vendor', exportable: VendorsExportable },
{ resource: 'Expense', exportable: ExpensesExportable },
@@ -43,7 +45,7 @@ export class ExportableResources {
{ resource: 'BillPayment', exportable: BillPaymentExportable },
{ resource: 'ManualJournal', exportable: ManualJournalsExportable },
{ resource: 'CreditNote', exportable: CreditNotesExportable },
{ resource: 'VendorCredit', exportable: VendorCreditsExportable }
{ resource: 'VendorCredit', exportable: VendorCreditsExportable },
];
/**

View File

@@ -5,6 +5,7 @@ import ResourceService from '../Resource/ResourceService';
import { ExportableResources } from './ExportResources';
import { ServiceError } from '@/exceptions';
import { Errors } from './common';
import { IModelMeta } from '@/interfaces';
@Service()
export class ExportResourceService {
@@ -22,40 +23,94 @@ export class ExportResourceService {
*/
async export(tenantId: number, resourceName: string, format: string = 'csv') {
const resource = sanitizeResourceName(resourceName);
const resourceMeta = this.resourceService.getResourceMeta(
tenantId,
resource
);
const exportable =
this.exportableResources.registry.getExportable(resource);
const resourceMeta = this.getResourceMeta(tenantId, resource);
if (!resourceMeta.exportable) {
this.validateResourceMeta(resourceMeta);
const data = await this.getExportableData(tenantId, resource);
const exportableColumns = this.getExportableColumns(resourceMeta);
const workbook = this.createWorkbook(data, exportableColumns);
return this.exportWorkbook(workbook, format);
}
/**
* Retrieves metadata for a specific resource.
* @param {number} tenantId - The tenant identifier.
* @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);
}
/**
* Validates if the resource metadata is exportable.
* @param {any} resourceMeta - The metadata of the resource.
* @throws {ServiceError} If the resource is not exportable or lacks columns.
*/
private validateResourceMeta(resourceMeta: any) {
if (!resourceMeta.exportable || !resourceMeta.columns) {
throw new ServiceError(Errors.RESOURCE_NOT_EXPORTABLE);
}
const data = await exportable.exportable(tenantId, {});
}
const exportableColumns = Object.entries(resourceMeta.columns)
.filter(([_, value]) => value.exportable)
/**
* Fetches exportable data for a given resource.
* @param {number} tenantId - The tenant identifier.
* @param {string} resource - The name of the resource.
* @returns A promise that resolves to the exportable data.
*/
private async getExportableData(tenantId: number, resource: string) {
const exportable =
this.exportableResources.registry.getExportable(resource);
return exportable.exportable(tenantId, {});
}
/**
* Extracts columns that are marked as exportable from the resource metadata.
* @param {IModelMeta} resourceMeta - The metadata of the resource.
* @returns An array of exportable columns.
*/
private getExportableColumns(resourceMeta: IModelMeta) {
return Object.entries(resourceMeta.columns)
.filter(([_, value]) => value.exportable !== false)
.map(([key, value]) => ({
name: value.name,
type: value.type,
accessor: value.accessor || key,
}));
}
/**
* Creates a workbook from the provided data and columns.
* @param {any[]} data - The data to be included in the workbook.
* @param {any[]} exportableColumns - The columns to be included in the workbook.
* @returns The created workbook.
*/
private createWorkbook(data: any[], exportableColumns: any[]) {
const workbook = xlsx.utils.book_new();
const worksheetData = data.map((item) =>
exportableColumns.map((col) => item[col.accessor])
);
worksheetData.unshift(exportableColumns.map((col) => col.name)); // Add header row
worksheetData.unshift(exportableColumns.map((col) => col.name));
const worksheet = xlsx.utils.aoa_to_sheet(worksheetData);
xlsx.utils.book_append_sheet(workbook, worksheet, 'Exported Data');
return workbook;
}
/**
* Exports the workbook in the specified format.
* @param {any} workbook - The workbook to be exported.
* @param {string} format - The format to export the workbook in.
* @returns The exported workbook data.
*/
private exportWorkbook(workbook: any, format: string) {
if (format.toLowerCase() === 'csv') {
// Convert to CSV using the xlsx package
return xlsx.write(workbook, { type: 'buffer', bookType: 'csv' });
} else if (format.toLowerCase() === 'xlsx') {
// Write to XLSX format
return xlsx.write(workbook, { type: 'buffer', bookType: 'xlsx' });
}
}

View File

@@ -39,7 +39,7 @@ export class GetManualJournals {
tenantId: number,
filterDTO: IManualJournalsFilter
): Promise<{
manualJournals: IManualJournal;
manualJournals: IManualJournal[];
pagination: IPaginationMeta;
filterMeta: IFilterMeta;
}> => {

View File

@@ -9,7 +9,7 @@ export class ManualJournalsExportable extends Exportable {
private manualJournalsApplication: ManualJournalsApplication;
/**
* Retrieves the accounts data to exportable sheet.
* Retrieves the manual journals data to exportable sheet.
* @param {number} tenantId
* @returns
*/

View File

@@ -14,7 +14,11 @@ export class BillPaymentExportable extends Exportable {
*/
public exportable(tenantId: number, query: any) {
const parsedQuery = {
page: 1,
pageSize: 12,
...query,
sortOrder: 'desc',
columnSortBy: 'created_at',
} as any;
return this.billPaymentsApplication

View File

@@ -31,7 +31,7 @@ export class GetBillPayments {
tenantId: number,
filterDTO: IBillPaymentsFilter
): Promise<{
billPayments: IBillPayment;
billPayments: IBillPayment[];
pagination: IPaginationMeta;
filterMeta: IFilterMeta;
}> {

View File

@@ -99,7 +99,7 @@ export class BillsApplication {
tenantId: number,
filterDTO: IBillsFilter
): Promise<{
bills: IBill;
bills: IBill[];
pagination: IPaginationMeta;
filterMeta: IFilterMeta;
}> {

View File

@@ -15,7 +15,11 @@ export class BillsExportable extends Exportable {
*/
public exportable(tenantId: number, query: IBillsFilter) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
page: 1,
pageSize: 12000,
} as IBillsFilter;
return this.billsApplication

View File

@@ -9,14 +9,18 @@ export class VendorCreditsExportable extends Exportable {
private getVendorCredits: ListVendorCredits;
/**
* Retrieves the accounts data to exportable sheet.
* Retrieves the vendor credits data to exportable sheet.
* @param {number} tenantId -
* @param {IVendorCreditsQueryDTO} query -
* @returns {}
*/
public exportable(tenantId: number, query: IVendorCreditsQueryDTO) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
page: 1,
pageSize: 12000,
} as IVendorCreditsQueryDTO;
return this.getVendorCredits

View File

@@ -15,7 +15,11 @@ export class SaleEstimatesExportable extends Exportable {
*/
public exportable(tenantId: number, query: ISalesInvoicesFilter) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
page: 1,
pageSize: 12000,
} as ISalesInvoicesFilter;
return this.saleEstimatesApplication

View File

@@ -15,7 +15,11 @@ export class SaleInvoicesExportable extends Exportable {
*/
public exportable(tenantId: number, query: ISalesInvoicesFilter) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
page: 1,
pageSize: 120000,
} as ISalesInvoicesFilter;
return this.saleInvoicesApplication

View File

@@ -1,5 +1,5 @@
import { Inject, Service } from 'typedi';
import { ISalesInvoicesFilter, ISalesReceiptsFilter } from '@/interfaces';
import { ISalesReceiptsFilter } from '@/interfaces';
import { Exportable } from '@/services/Export/Exportable';
import { SaleReceiptApplication } from './SaleReceiptApplication';
@@ -15,8 +15,12 @@ export class SaleReceiptsExportable extends Exportable {
*/
public exportable(tenantId: number, query: ISalesReceiptsFilter) {
const parsedQuery = {
sortOrder: 'desc',
columnSortBy: 'created_at',
...query,
} as ISalesInvoicesFilter;
page: 1,
pageSize: 12,
} as ISalesReceiptsFilter;
return this.saleReceiptsApp
.getSaleReceipts(tenantId, parsedQuery)