refactor(nestjs): add importable service to other modules

This commit is contained in:
Ahmed Bouhuolia
2025-04-12 19:26:15 +02:00
parent 51de3631fc
commit 1d53063e09
30 changed files with 1666 additions and 139 deletions

View File

@@ -25,6 +25,7 @@ import { AccountsModule } from '../Accounts/Accounts.module';
import { GetCreditNotesService } from './queries/GetCreditNotes.service';
import { DynamicListModule } from '../DynamicListing/DynamicList.module';
import { CreditNotesExportable } from './commands/CreditNotesExportable';
import { CreditNotesImportable } from './commands/CreditNotesImportable';
@Module({
imports: [
@@ -55,6 +56,7 @@ import { CreditNotesExportable } from './commands/CreditNotesExportable';
CreditNoteGLEntries,
CreditNoteGLEntriesSubscriber,
CreditNotesExportable,
CreditNotesImportable,
],
exports: [
CreateCreditNoteService,
@@ -69,6 +71,7 @@ import { CreditNotesExportable } from './commands/CreditNotesExportable';
CreditNoteApplication,
CreditNoteBrandingTemplate,
CreditNotesExportable,
CreditNotesImportable,
],
controllers: [CreditNotesController],
})

View File

@@ -1,44 +1,45 @@
// import { Inject, Service } from 'typedi';
// import { Knex } from 'knex';
// import { ICreditNoteNewDTO } from '@/interfaces';
// import { Importable } from '../Import/Importable';
// import CreateCreditNote from './commands/CreateCreditNote.service';
import { Knex } from 'knex';
import { Injectable } from '@nestjs/common';
import { CreateCreditNoteService } from './CreateCreditNote.service';
import { Importable } from '@/modules/Import/Importable';
import { CreateCreditNoteDto } from '../dtos/CreditNote.dto';
import { ImportableService } from '@/modules/Import/decorators/Import.decorator';
import { CreditNote } from '../models/CreditNote';
// @Service()
// export class CreditNotesImportable extends Importable {
// @Inject()
// private createCreditNoteImportable: CreateCreditNote;
@Injectable()
@ImportableService({ name: CreditNote.name })
export class CreditNotesImportable extends Importable {
constructor(
private readonly createCreditNoteImportable: CreateCreditNoteService,
) {
super();
}
// /**
// * Importing to account service.
// * @param {number} tenantId
// * @param {IAccountCreateDTO} createAccountDTO
// * @returns
// */
// public importable(
// tenantId: number,
// createAccountDTO: ICreditNoteNewDTO,
// trx?: Knex.Transaction
// ) {
// return this.createCreditNoteImportable.newCreditNote(
// tenantId,
// createAccountDTO,
// trx
// );
// }
/**
* Importing to credit note service.
*/
public importable(
createAccountDTO: CreateCreditNoteDto,
trx?: Knex.Transaction,
) {
return this.createCreditNoteImportable.creditCreditNote(
createAccountDTO,
trx,
);
}
// /**
// * Concurrrency controlling of the importing process.
// * @returns {number}
// */
// public get concurrency() {
// return 1;
// }
/**
* Concurrrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 1;
}
// /**
// * Retrieves the sample data that used to download accounts sample sheet.
// */
// public sampleData(): any[] {
// return [];
// }
// }
/**
* Retrieves the sample data that used to download accounts sample sheet.
*/
public sampleData(): any[] {
return [];
}
}

View File

@@ -0,0 +1,249 @@
import { Features } from "@/common/types/Features";
function StatusFieldFilterQuery(query, role) {
query.modify('filterByStatus', role.value);
}
function StatusFieldSortQuery(query, role) {
query.modify('sortByStatus', role.order);
}
export const CreditNoteMeta = {
defaultFilterField: 'name',
defaultSort: {
sortOrder: 'DESC',
sortField: 'name',
},
exportable: true,
exportFlattenOn: 'entries',
importable: true,
importAggregator: 'group',
importAggregateOn: 'entries',
importAggregateBy: 'creditNoteNumber',
print: {
pageTitle: 'Credit Notes',
},
fields: {
customer: {
name: 'credit_note.field.customer',
column: 'customer_id',
fieldType: 'relation',
relationType: 'enumeration',
relationKey: 'customer',
relationEntityLabel: 'display_name',
relationEntityKey: 'id',
},
credit_date: {
name: 'credit_note.field.credit_note_date',
column: 'credit_note_date',
fieldType: 'date',
},
credit_number: {
name: 'credit_note.field.credit_note_number',
column: 'credit_note_number',
fieldType: 'text',
},
reference_no: {
name: 'credit_note.field.reference_no',
column: 'reference_no',
fieldType: 'text',
},
amount: {
name: 'credit_note.field.amount',
column: 'amount',
fieldType: 'number',
},
currency_code: {
name: 'credit_note.field.currency_code',
column: 'currency_code',
fieldType: 'number',
},
note: {
name: 'credit_note.field.note',
column: 'note',
fieldType: 'text',
},
terms_conditions: {
name: 'credit_note.field.terms_conditions',
column: 'terms_conditions',
fieldType: 'text',
},
status: {
name: 'credit_note.field.status',
fieldType: 'enumeration',
options: [
{ key: 'draft', label: 'credit_note.field.status.draft' },
{ key: 'published', label: 'credit_note.field.status.published' },
{ key: 'open', label: 'credit_note.field.status.open' },
{ key: 'closed', label: 'credit_note.field.status.closed' },
],
filterCustomQuery: StatusFieldFilterQuery,
sortCustomQuery: StatusFieldSortQuery,
},
created_at: {
name: 'credit_note.field.created_at',
column: 'created_at',
fieldType: 'date',
},
},
columns: {
customer: {
name: 'Customer',
accessor: 'customer.displayName',
},
exchangeRate: {
name: 'Exchange Rate',
printable: false,
},
creditNoteDate: {
name: 'Credit Note Date',
accessor: 'formattedCreditNoteDate',
},
referenceNo: {
name: 'Reference No.',
},
note: {
name: 'Note',
},
termsConditions: {
name: 'Terms & Conditions',
printable: false,
},
creditNoteNumber: {
name: 'Credit Note Number',
printable: false,
},
open: {
name: 'Open',
type: 'boolean',
printable: false,
},
entries: {
name: 'Entries',
type: 'collection',
collectionOf: 'object',
columns: {
itemName: {
name: 'Item Name',
accessor: 'item.name',
},
rate: {
name: 'Item Rate',
accessor: 'rateFormatted',
},
quantity: {
name: 'Item Quantity',
accessor: 'quantityFormatted',
},
description: {
name: 'Item Description',
},
amount: {
name: 'Item Amount',
accessor: 'totalFormatted',
},
},
},
branch: {
name: 'Branch',
type: 'text',
accessor: 'branch.name',
features: [Features.BRANCHES],
},
warehouse: {
name: 'Warehouse',
type: 'text',
accessor: 'warehouse.name',
features: [Features.BRANCHES],
},
},
fields2: {
customerId: {
name: 'Customer',
fieldType: 'relation',
relationModel: 'Contact',
relationImportMatch: 'displayName',
required: true,
},
exchangeRate: {
name: 'Exchange Rate',
fieldType: 'number',
},
creditNoteDate: {
name: 'Credit Note Date',
fieldType: 'date',
required: true,
},
referenceNo: {
name: 'Reference No.',
fieldType: 'text',
},
note: {
name: 'Note',
fieldType: 'text',
},
termsConditions: {
name: 'Terms & Conditions',
fieldType: 'text',
},
creditNoteNumber: {
name: 'Credit Note Number',
fieldType: 'text',
},
open: {
name: 'Open',
fieldType: 'boolean',
},
entries: {
name: 'Entries',
fieldType: 'collection',
collectionOf: 'object',
collectionMinLength: 1,
fields: {
itemId: {
name: 'Item',
fieldType: 'relation',
relationModel: 'Item',
relationImportMatch: ['name', 'code'],
required: true,
importHint: 'Matches the item name or code.',
},
rate: {
name: 'Rate',
fieldType: 'number',
required: true,
},
quantity: {
name: 'Quantity',
fieldType: 'number',
required: true,
},
description: {
name: 'Description',
fieldType: 'text',
},
},
},
branchId: {
name: 'Branch',
fieldType: 'relation',
relationModel: 'Branch',
relationImportMatch: ['name', 'code'],
features: [Features.BRANCHES],
required: true,
},
warehouseId: {
name: 'Warehouse',
fieldType: 'relation',
relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'],
features: [Features.WAREHOUSES],
required: true,
},
},
};

View File

@@ -3,12 +3,17 @@ import { BaseModel } from '@/models/Model';
import { Branch } from '@/modules/Branches/models/Branch.model';
import { Customer } from '@/modules/Customers/models/Customer';
import { ExportableModel } from '@/modules/Export/decorators/ExportableModel.decorator';
import { ImportableModel } from '@/modules/Import/decorators/Import.decorator';
import { TenantBaseModel } from '@/modules/System/models/TenantBaseModel';
import { InjectModelMeta } from '@/modules/Tenancy/TenancyModels/decorators/InjectModelMeta.decorator';
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
import { Warehouse } from '@/modules/Warehouses/models/Warehouse.model';
import { mixin, Model, raw } from 'objection';
import { CreditNoteMeta } from './CreditNote.meta';
@ExportableModel()
@ImportableModel()
@InjectModelMeta(CreditNoteMeta)
export class CreditNote extends TenantBaseModel {
public amount: number;
public exchangeRate: number;