feat(server): wip activate/inactivate tax rate

This commit is contained in:
Ahmed Bouhuolia
2023-09-18 01:38:38 +02:00
parent 2356921f27
commit 4e53d08497
10 changed files with 285 additions and 4 deletions

View File

@@ -0,0 +1,64 @@
import {
ITaxRateActivatedPayload,
ITaxRateActivatingPayload,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import UnitOfWork from '../UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '../Tenancy/TenancyService';
import { Knex } from 'knex';
import { CommandTaxRatesValidators } from './CommandTaxRatesValidators';
import events from '@/subscribers/events';
@Service()
export class ActivateTaxRateService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
@Inject()
private validators: CommandTaxRatesValidators;
/**
* Edits the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
* @param {IEditTaxRateDTO} taxRateEditDTO
* @returns {Promise<ITaxRate>}
*/
public activateTaxRate(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 `onTaxRateActivating` event.
await this.eventPublisher.emitAsync(events.taxRates.onActivating, {
taxRateId,
tenantId,
trx,
} as ITaxRateActivatingPayload);
const taxRate = await TaxRate.query(trx)
.findById(taxRateId)
.patch({ active: 1 });
// Triggers `onTaxRateCreated` event.
await this.eventPublisher.emitAsync(events.taxRates.onActivated, {
taxRateId,
tenantId,
trx,
} as ITaxRateActivatedPayload);
return taxRate;
});
}
}

View File

@@ -20,6 +20,26 @@ export class CommandTaxRatesValidators {
}
}
/**
* Validates the given tax rate active.
* @param {ITaxRate} taxRate
*/
public validateTaxRateNotActive(taxRate: ITaxRate) {
if (taxRate.active) {
throw new ServiceError(ERRORS.TAX_RATE_ALREADY_ACTIVE);
}
}
/**
* Validates the given tax rate inactive.
* @param {ITaxRate} taxRate
*/
public validateTaxRateNotInactive(taxRate: ITaxRate) {
if (!taxRate.active) {
throw new ServiceError(ERRORS.TAX_RATE_ALREADY_INACTIVE);
}
}
/**
* Validates the tax code uniquiness.
* @param {number} tenantId

View File

@@ -0,0 +1,67 @@
import {
ITaxRateActivatedPayload,
ITaxRateActivatingPayload,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import UnitOfWork from '../UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '../Tenancy/TenancyService';
import { Knex } from 'knex';
import { CommandTaxRatesValidators } from './CommandTaxRatesValidators';
import events from '@/subscribers/events';
@Service()
export class InactivateTaxRateService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
@Inject()
private validators: CommandTaxRatesValidators;
/**
* Edits the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
* @param {IEditTaxRateDTO} taxRateEditDTO
* @returns {Promise<ITaxRate>}
*/
public inactivateTaxRate(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);
//
this.validators.validateTaxRateNotInactive(oldTaxRate);
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTaxRateActivating` event.
await this.eventPublisher.emitAsync(events.taxRates.onInactivating, {
taxRateId,
tenantId,
trx,
} as ITaxRateActivatingPayload);
const taxRate = await TaxRate.query(trx)
.findById(taxRateId)
.patch({ active: 0 });
// Triggers `onTaxRateCreated` event.
await this.eventPublisher.emitAsync(events.taxRates.onInactivated, {
taxRateId,
tenantId,
trx,
} as ITaxRateActivatedPayload);
return taxRate;
});
}
}

View File

@@ -5,6 +5,8 @@ import { DeleteTaxRateService } from './DeleteTaxRate';
import { EditTaxRateService } from './EditTaxRate';
import { GetTaxRateService } from './GetTaxRate';
import { GetTaxRatesService } from './GetTaxRates';
import { ActivateTaxRateService } from './ActivateTaxRate';
import { InactivateTaxRateService } from './InactivateTaxRate';
@Service()
export class TaxRatesApplication {
@@ -23,6 +25,12 @@ export class TaxRatesApplication {
@Inject()
private getTaxRatesService: GetTaxRatesService;
@Inject()
private activateTaxRateService: ActivateTaxRateService;
@Inject()
private inactivateTaxRateService: InactivateTaxRateService;
/**
* Creates a new tax rate.
* @param {number} tenantId
@@ -80,4 +88,22 @@ export class TaxRatesApplication {
public getTaxRates(tenantId: number) {
return this.getTaxRatesService.getTaxRates(tenantId);
}
/**
* Activates the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
*/
public activateTaxRate(tenantId: number, taxRateId: number) {
return this.activateTaxRateService.activateTaxRate(tenantId, taxRateId);
}
/**
* Inactivates the given tax rate.
* @param {number} tenantId
* @param {number} taxRateId
*/
public inactivateTaxRate(tenantId: number, taxRateId: number) {
return this.inactivateTaxRateService.inactivateTaxRate(tenantId, taxRateId);
}
}

View File

@@ -3,4 +3,6 @@ export const ERRORS = {
TAX_CODE_NOT_UNIQUE: 'TAX_CODE_NOT_UNIQUE',
ITEM_ENTRY_TAX_RATE_CODE_NOT_FOUND: 'ITEM_ENTRY_TAX_RATE_CODE_NOT_FOUND',
ITEM_ENTRY_TAX_RATE_ID_NOT_FOUND: 'ITEM_ENTRY_TAX_RATE_ID_NOT_FOUND',
TAX_RATE_ALREADY_ACTIVE: 'TAX_RATE_ALREADY_ACTIVE',
TAX_RATE_ALREADY_INACTIVE: 'TAX_RATE_ALREADY_INACTIVE'
};