refactor: wip to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-25 00:43:55 +02:00
parent 336171081e
commit a6932d76f3
249 changed files with 21314 additions and 1616 deletions

View File

@@ -0,0 +1,71 @@
import { Inject, Injectable } from '@nestjs/common';
import { keyBy, sumBy } from 'lodash';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
import { TaxRateModel } from './models/TaxRate.model';
@Injectable()
export class ItemEntriesTaxTransactions {
constructor(
@Inject(ItemEntry.name) private itemEntryModel: typeof ItemEntry,
@Inject(TaxRateModel.name) private taxRateModel: typeof TaxRateModel,
) {}
/**
* Associates tax amount withheld to the model.
* @param model
* @returns
*/
public assocTaxAmountWithheldFromEntries(model: any) {
const entries = model.entries.map((entry) =>
this.itemEntryModel.fromJson(entry),
);
const taxAmountWithheld = sumBy(entries, 'taxAmount');
if (taxAmountWithheld) {
model.taxAmountWithheld = taxAmountWithheld;
}
return model;
}
/**
* Associates tax rate id from tax code to entries.
* @param {any} entries
*/
public assocTaxRateIdFromCodeToEntries = async (entries: any) => {
const entriesWithCode = entries.filter((entry) => entry.taxCode);
const taxCodes = entriesWithCode.map((entry) => entry.taxCode);
const foundTaxCodes = await this.taxRateModel
.query()
.whereIn('code', taxCodes);
const taxCodesMap = keyBy(foundTaxCodes, 'code');
return entries.map((entry) => {
if (entry.taxCode) {
entry.taxRateId = taxCodesMap[entry.taxCode]?.id;
}
return entry;
});
};
/**
* Associates tax rate from tax id to entries.
* @returns {Promise<ItemEntry[]>}
*/
public assocTaxRateFromTaxIdToEntries = async (entries: ItemEntry[]) => {
const entriesWithId = entries.filter((e) => e.taxRateId);
const taxRateIds = entriesWithId.map((e) => e.taxRateId);
const foundTaxes = await this.taxRateModel
.query()
.whereIn('id', taxRateIds);
const taxRatesMap = keyBy(foundTaxes, 'id');
return entries.map((entry) => {
if (entry.taxRateId) {
entry.taxRate = taxRatesMap[entry.taxRateId]?.rate;
}
return entry;
});
};
}

View File

@@ -1,72 +0,0 @@
// import { Inject, Service } from 'typedi';
// import { keyBy, sumBy } from 'lodash';
// import { ItemEntry } from '@/models';
// import HasTenancyService from '../Tenancy/TenancyService';
// import { IItem, IItemEntry, IItemEntryDTO } from '@/interfaces';
// @Service()
// export class ItemEntriesTaxTransactions {
// @Inject()
// private tenancy: HasTenancyService;
// /**
// * Associates tax amount withheld to the model.
// * @param model
// * @returns
// */
// public assocTaxAmountWithheldFromEntries(model: any) {
// const entries = model.entries.map((entry) => ItemEntry.fromJson(entry));
// const taxAmountWithheld = sumBy(entries, 'taxAmount');
// if (taxAmountWithheld) {
// model.taxAmountWithheld = taxAmountWithheld;
// }
// return model;
// }
// /**
// * Associates tax rate id from tax code to entries.
// * @param {number} tenantId
// * @param {} model
// */
// public assocTaxRateIdFromCodeToEntries =
// (tenantId: number) => async (entries: any) => {
// const entriesWithCode = entries.filter((entry) => entry.taxCode);
// const taxCodes = entriesWithCode.map((entry) => entry.taxCode);
// const { TaxRate } = this.tenancy.models(tenantId);
// const foundTaxCodes = await TaxRate.query().whereIn('code', taxCodes);
// const taxCodesMap = keyBy(foundTaxCodes, 'code');
// return entries.map((entry) => {
// if (entry.taxCode) {
// entry.taxRateId = taxCodesMap[entry.taxCode]?.id;
// }
// return entry;
// });
// };
// /**
// * Associates tax rate from tax id to entries.
// * @param {number} tenantId
// * @returns {Promise<IItemEntry[]>}
// */
// public assocTaxRateFromTaxIdToEntries =
// (tenantId: number) => async (entries: IItemEntry[]) => {
// const entriesWithId = entries.filter((e) => e.taxRateId);
// const taxRateIds = entriesWithId.map((e) => e.taxRateId);
// const { TaxRate } = this.tenancy.models(tenantId);
// const foundTaxes = await TaxRate.query().whereIn('id', taxRateIds);
// const taxRatesMap = keyBy(foundTaxes, 'id');
// return entries.map((entry) => {
// if (entry.taxRateId) {
// entry.taxRate = taxRatesMap[entry.taxRateId]?.rate;
// }
// return entry;
// });
// };
// }

View File

@@ -12,6 +12,12 @@ import { events } from '@/common/events/events';
@Injectable()
export class ActivateTaxRateService {
/**
* @param {EventEmitter2} eventEmitter - The event emitter.
* @param {UnitOfWork} uow - The unit of work.
* @param {CommandTaxRatesValidators} validators - The tax rates validators.
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
*/
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly uow: UnitOfWork,

View File

@@ -9,6 +9,9 @@ import { ServiceError } from '@/modules/Items/ServiceError';
@Injectable()
export class CommandTaxRatesValidators {
/**
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
*/
constructor(
@Inject(TaxRateModel.name)
private readonly taxRateModel: typeof TaxRateModel,

View File

@@ -13,6 +13,12 @@ import { events } from '@/common/events/events';
@Injectable()
export class CreateTaxRate {
/**
* @param {EventEmitter2} eventEmitter - The event emitter.
* @param {UnitOfWork} uow - The unit of work.
* @param {CommandTaxRatesValidators} validators - The tax rates validators.
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
*/
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly uow: UnitOfWork,

View File

@@ -12,6 +12,12 @@ import { events } from '@/common/events/events';
@Injectable()
export class DeleteTaxRateService {
/**
* @param {EventEmitter2} eventEmitter - The event emitter.
* @param {UnitOfWork} uow - The unit of work.
* @param {CommandTaxRatesValidators} validators - The tax rates validators.
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
*/
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly uow: UnitOfWork,

View File

@@ -14,6 +14,12 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
@Injectable()
export class EditTaxRateService {
/**
* @param {EventEmitter2} eventEmitter - The event emitter.
* @param {UnitOfWork} uow - The unit of work.
* @param {CommandTaxRatesValidators} validators - The tax rates validators.
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
*/
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly uow: UnitOfWork,
@@ -73,6 +79,12 @@ export class EditTaxRateService {
}
}
/**
* Edits the given tax rate.
* @param {number} taxRateId - The tax rate id.
* @param {IEditTaxRateDTO} editTaxRateDTO - The tax rate data to edit.
* @returns {Promise<ITaxRate>}
*/
public async editTaxRate(
taxRateId: number,
editTaxRateDTO: IEditTaxRateDTO

View File

@@ -12,6 +12,12 @@ import { events } from '@/common/events/events';
@Injectable()
export class InactivateTaxRateService {
/**
* @param {EventEmitter2} eventEmitter - The event emitter.
* @param {UnitOfWork} uow - The unit of work.
* @param {CommandTaxRatesValidators} validators - The tax rates validators.
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
*/
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly uow: UnitOfWork,

View File

@@ -6,6 +6,11 @@ import { CommandTaxRatesValidators } from '../commands/CommandTaxRatesValidator.
@Injectable()
export class GetTaxRateService {
/**
* @param {typeof TaxRateModel} taxRateModel - The tax rate model.
* @param {CommandTaxRatesValidators} validators - The tax rates validators.
* @param {TransformerInjectable} transformer - The transformer.
*/
constructor(
@Inject(TaxRateModel.name)
private readonly taxRateModel: typeof TaxRateModel,