mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat: wip export resource data
This commit is contained in:
@@ -1,13 +1,27 @@
|
||||
import Container, { Service } from 'typedi';
|
||||
import { AccountsExportable } from '../Accounts/AccountsExportable';
|
||||
import { ExportableRegistry } from './ExportRegistery';
|
||||
import { ItemsImportable } from '../Items/ItemsImportable';
|
||||
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/PaymentReceives/PaymentsReceivedExportable';
|
||||
import { BillPaymentExportable } from '../Purchases/BillPayments/BillPaymentExportable';
|
||||
import { ManualJournalsExportable } from '../ManualJournals/ManualJournalExportable';
|
||||
import { CreditNotesExportable } from '../CreditNotes/CreditNotesExportable';
|
||||
import { VendorCreditsExportable } from '../Purchases/VendorCredits/VendorCreditsExportable';
|
||||
|
||||
@Service()
|
||||
export class ExportableResources {
|
||||
private static registry: ExportableRegistry;
|
||||
|
||||
/**
|
||||
* Consttuctor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.boot();
|
||||
}
|
||||
@@ -18,6 +32,18 @@ export class ExportableResources {
|
||||
private importables = [
|
||||
{ resource: 'Account', exportable: AccountsExportable },
|
||||
{ resource: 'Item', exportable: ItemsExportable },
|
||||
{ resource: 'Customer', exportable: CustomersExportable },
|
||||
{ resource: 'Vendor', exportable: VendorsExportable },
|
||||
{ resource: 'Expense', exportable: ExpensesExportable },
|
||||
{ resource: 'SaleInvoice', exportable: SaleInvoicesExportable },
|
||||
{ resource: 'SaleEstimate', exportable: SaleEstimatesExportable },
|
||||
{ resource: 'SaleReceipt', exportable: SaleReceiptsExportable },
|
||||
{ resource: 'Bill', exportable: BillsExportable },
|
||||
{ resource: 'PaymentReceive', exportable: PaymentsReceivedExportable },
|
||||
{ resource: 'BillPayment', exportable: BillPaymentExportable },
|
||||
{ resource: 'ManualJournal', exportable: ManualJournalsExportable },
|
||||
{ resource: 'CreditNote', exportable: CreditNotesExportable },
|
||||
{ resource: 'VendorCredit', exportable: VendorCreditsExportable }
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,8 @@ import xlsx from 'xlsx';
|
||||
import { sanitizeResourceName } from '../Import/_utils';
|
||||
import ResourceService from '../Resource/ResourceService';
|
||||
import { ExportableResources } from './ExportResources';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import { Errors } from './common';
|
||||
|
||||
@Service()
|
||||
export class ExportResourceService {
|
||||
@@ -13,10 +15,10 @@ export class ExportResourceService {
|
||||
private exportableResources: ExportableResources;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} tenantId
|
||||
* @param {string} resourceName
|
||||
* @param {string} format
|
||||
* Exports the given resource data through csv, xlsx or pdf.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {string} resourceName - Resource name.
|
||||
* @param {string} format - File format.
|
||||
*/
|
||||
async export(tenantId: number, resourceName: string, format: string = 'csv') {
|
||||
const resource = sanitizeResourceName(resourceName);
|
||||
@@ -27,24 +29,25 @@ export class ExportResourceService {
|
||||
const exportable =
|
||||
this.exportableResources.registry.getExportable(resource);
|
||||
|
||||
if (!resourceMeta.exportable) {
|
||||
throw new ServiceError(Errors.RESOURCE_NOT_EXPORTABLE);
|
||||
}
|
||||
const data = await exportable.exportable(tenantId, {});
|
||||
|
||||
const exportableColumns = [
|
||||
{
|
||||
label: 'Account Normal',
|
||||
accessor: 'accountNormalFormatted',
|
||||
},
|
||||
{
|
||||
label: 'Account Type',
|
||||
accessor: 'accountTypeFormatted',
|
||||
},
|
||||
];
|
||||
const exportableColumns = Object.entries(resourceMeta.columns)
|
||||
.filter(([_, value]) => value.exportable)
|
||||
.map(([key, value]) => ({
|
||||
name: value.name,
|
||||
type: value.type,
|
||||
accessor: value.accessor || key,
|
||||
}));
|
||||
|
||||
const workbook = xlsx.utils.book_new();
|
||||
const worksheetData = data.map((item) =>
|
||||
exportableColumns.map((col) => item[col.accessor])
|
||||
);
|
||||
worksheetData.unshift(exportableColumns.map((col) => col.label)); // Add header row
|
||||
worksheetData.unshift(exportableColumns.map((col) => col.name)); // Add header row
|
||||
|
||||
const worksheet = xlsx.utils.aoa_to_sheet(worksheetData);
|
||||
xlsx.utils.book_append_sheet(workbook, worksheet, 'Exported Data');
|
||||
|
||||
|
||||
3
packages/server/src/services/Export/common.ts
Normal file
3
packages/server/src/services/Export/common.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export enum Errors {
|
||||
RESOURCE_NOT_EXPORTABLE = 'RESOURCE_NOT_EXPORTABLE',
|
||||
}
|
||||
Reference in New Issue
Block a user