feat: tax rates crud service

This commit is contained in:
Ahmed Bouhuolia
2023-08-11 16:00:39 +02:00
parent 04d134806b
commit d6f56568a3
12 changed files with 189 additions and 50 deletions

View File

@@ -1,16 +1,36 @@
import { ServiceError } from '@/exceptions';
import TaxRate from '@/models/TaxRate';
import { Service } from 'typedi';
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { ITaxRate } from '@/interfaces';
import { ERRORS } from './constants';
@Service()
export class CommandTaxRatesValidators {
@Inject()
private tenancy: HasTenancyService;
/**
*
* @param {} taxRate
* Validates the tax rate existance.
* @param {TaxRate | undefined | null} taxRate
*/
public validateTaxRateExistance(taxRate: TaxRate | undefined | null) {
public validateTaxRateExistance(taxRate: ITaxRate | undefined | null) {
if (!taxRate) {
throw new ServiceError(ERRORS.TAX_RATE_NOT_FOUND);
}
}
/**
* Validates the tax code uniquiness.
* @param {number} tenantId
* @param {string} taxCode
*/
public async validateTaxCodeUnique(tenantId: number, taxCode: string) {
const { TaxRate } = this.tenancy.models(tenantId);
const foundTaxCode = await TaxRate.query().findOne({ code: taxCode });
if (foundTaxCode) {
throw new ServiceError(ERRORS.TAX_CODE_NOT_UNIQUE);
}
}
}

View File

@@ -9,6 +9,7 @@ import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '../Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { CommandTaxRatesValidators } from './CommandTaxRatesValidators';
@Service()
export class CreateTaxRate {
@@ -21,14 +22,25 @@ export class CreateTaxRate {
@Inject()
private uow: UnitOfWork;
@Inject()
private validators: CommandTaxRatesValidators;
/**
* Creates a new tax rate.
* @param {number} tenantId
* @param {ICreateTaxRateDTO} createTaxRateDTO
*/
public createTaxRate(tenantId: number, createTaxRateDTO: ICreateTaxRateDTO) {
public async createTaxRate(
tenantId: number,
createTaxRateDTO: ICreateTaxRateDTO
) {
const { TaxRate } = this.tenancy.models(tenantId);
// Validates the tax code uniquiness.
await this.validators.validateTaxCodeUnique(
tenantId,
createTaxRateDTO.code
);
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTaxRateCreating` event.
await this.eventPublisher.emitAsync(events.taxRates.onCreating, {

View File

@@ -22,19 +22,21 @@ export class DeleteTaxRateService {
private validators: CommandTaxRatesValidators;
/**
*
* @param tenantId
* @param taxRateId
* Deletes the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
* @returns {Promise<void>}
*/
public deleteTaxRate(tenantId: number, taxRateId: number) {
const { TaxRate } = this.tenancy.models(tenantId);
const oldTaxRate = TaxRate.query().findById(taxRateId);
// Validates the tax rate existance.
this.validators.validateTaxRateExistance(oldTaxRate);
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onSaleInvoiceCreating` event.
// Triggers `onTaxRateDeleting` event.
await this.eventPublisher.emitAsync(events.taxRates.onDeleting, {
oldTaxRate,
tenantId,
@@ -43,7 +45,7 @@ export class DeleteTaxRateService {
await TaxRate.query(trx).findById(taxRateId).delete();
//
// Triggers `onTaxRateDeleted` event.
await this.eventPublisher.emitAsync(events.taxRates.onDeleted, {
oldTaxRate,
tenantId,

View File

@@ -26,10 +26,11 @@ export class EditTaxRateService {
private validators: CommandTaxRatesValidators;
/**
*
* Edits the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
* @param {IEditTaxRateDTO} taxRateEditDTO
* @returns {Promise<ITaxRate>}
*/
public editTaxRate(
tenantId: number,
@@ -40,6 +41,7 @@ export class EditTaxRateService {
const oldTaxRate = TaxRate.query().findById(taxRateId);
// Validates the tax rate existance.
this.validators.validateTaxRateExistance(oldTaxRate);
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {

View File

@@ -11,16 +11,17 @@ export class GetTaxRateService {
private validators: CommandTaxRatesValidators;
/**
*
* Retrieves the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
* @returns
* @returns {Promise<ITaxRate>}
*/
public async getTaxRate(tenantId: number, taxRateId: number) {
const { TaxRate } = this.tenancy.models(tenantId);
const taxRate = await TaxRate.query().findById(taxRateId);
// Validates the tax rate existance.
this.validators.validateTaxRateExistance(taxRate);
return taxRate;

View File

@@ -7,9 +7,9 @@ export class GetTaxRatesService {
private tenancy: HasTenancyService;
/**
*
* Retrieves the tax rates list.
* @param {number} tenantId
* @returns
* @returns {Promise<ITaxRate[]>}
*/
public async getTaxRates(tenantId: number) {
const { TaxRate } = this.tenancy.models(tenantId);

View File

@@ -27,7 +27,7 @@ export class TaxRatesApplication {
* Creates a new tax rate.
* @param {number} tenantId
* @param {ICreateTaxRateDTO} createTaxRateDTO
* @returns
* @returns {Promise<ITaxRate>}
*/
public createTaxRate(tenantId: number, createTaxRateDTO: ICreateTaxRateDTO) {
return this.createTaxRateService.createTaxRate(tenantId, createTaxRateDTO);
@@ -38,6 +38,7 @@ export class TaxRatesApplication {
* @param {number} tenantId
* @param {number} taxRateId
* @param {IEditTaxRateDTO} taxRateEditDTO
* @returns {Promise<ITaxRate>}
*/
public editTaxRate(
tenantId: number,
@@ -65,7 +66,7 @@ export class TaxRatesApplication {
* Retrieves the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
* @returns
* @returns {Promise<ITaxRate>}
*/
public getTaxRate(tenantId: number, taxRateId: number) {
return this.getTaxRateService.getTaxRate(tenantId, taxRateId);
@@ -74,7 +75,7 @@ export class TaxRatesApplication {
/**
* Retrieves the tax rates list.
* @param {number} tenantId
* @returns
* @returns {Promise<ITaxRate[]>}
*/
public getTaxRates(tenantId: number) {
return this.getTaxRatesService.getTaxRates(tenantId);

View File

@@ -1,3 +1,4 @@
const ERRORS = {
export const ERRORS = {
TAX_RATE_NOT_FOUND: 'TAX_RATE_NOT_FOUND',
TAX_CODE_NOT_UNIQUE: 'TAX_CODE_NOT_UNIQUE',
};