|
|
@@ -11,9 +11,12 @@ import { TaxRate } from '../../entity/tax-rate/tax-rate.entity';
|
|
|
import { Zone } from '../../entity/zone/zone.entity';
|
|
|
import { I18nError } from '../../i18n/i18n-error';
|
|
|
import { buildListQuery } from '../helpers/build-list-query';
|
|
|
+import { getEntityOrThrow } from '../helpers/get-entity-or-throw';
|
|
|
import { patchEntity } from '../helpers/patch-entity';
|
|
|
|
|
|
export class TaxRateService {
|
|
|
+ private activeTaxRates: TaxRate[] = [];
|
|
|
+
|
|
|
constructor(@InjectConnection() private connection: Connection) {}
|
|
|
|
|
|
findAll(options?: ListQueryOptions<TaxRate>): Promise<PaginatedList<TaxRate>> {
|
|
|
@@ -30,12 +33,17 @@ export class TaxRateService {
|
|
|
relations: ['category', 'zone', 'customerGroup'],
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
async create(input: CreateTaxRateInput): Promise<TaxRate> {
|
|
|
const taxRate = new TaxRate(input);
|
|
|
- taxRate.category = await this.getTaxCategoryOrThrow(input.categoryId);
|
|
|
- taxRate.zone = await this.getZoneOrThrow(input.zoneId);
|
|
|
+ taxRate.category = await getEntityOrThrow(this.connection, TaxCategory, input.categoryId);
|
|
|
+ taxRate.zone = await getEntityOrThrow(this.connection, Zone, input.zoneId);
|
|
|
if (input.customerGroupId) {
|
|
|
- taxRate.customerGroup = await this.getCustomerGroupOrThrow(input.customerGroupId);
|
|
|
+ taxRate.customerGroup = await getEntityOrThrow(
|
|
|
+ this.connection,
|
|
|
+ CustomerGroup,
|
|
|
+ input.customerGroupId,
|
|
|
+ );
|
|
|
}
|
|
|
const newTaxRate = await this.connection.getRepository(TaxRate).save(taxRate);
|
|
|
return assertFound(this.findOne(newTaxRate.id));
|
|
|
@@ -51,48 +59,19 @@ export class TaxRateService {
|
|
|
}
|
|
|
const updatedTaxRate = patchEntity(taxRate, input);
|
|
|
if (input.categoryId) {
|
|
|
- updatedTaxRate.category = await this.getTaxCategoryOrThrow(input.categoryId);
|
|
|
+ updatedTaxRate.category = await getEntityOrThrow(this.connection, TaxCategory, input.categoryId);
|
|
|
}
|
|
|
if (input.zoneId) {
|
|
|
- updatedTaxRate.category = await this.getZoneOrThrow(input.zoneId);
|
|
|
+ updatedTaxRate.category = await getEntityOrThrow(this.connection, Zone, input.zoneId);
|
|
|
}
|
|
|
if (input.customerGroupId) {
|
|
|
- updatedTaxRate.customerGroup = await this.getCustomerGroupOrThrow(input.customerGroupId);
|
|
|
+ updatedTaxRate.customerGroup = await getEntityOrThrow(
|
|
|
+ this.connection,
|
|
|
+ CustomerGroup,
|
|
|
+ input.customerGroupId,
|
|
|
+ );
|
|
|
}
|
|
|
await this.connection.getRepository(TaxRate).save(updatedTaxRate);
|
|
|
return assertFound(this.findOne(taxRate.id));
|
|
|
}
|
|
|
-
|
|
|
- private async getTaxCategoryOrThrow(id: ID): Promise<TaxCategory> {
|
|
|
- const taxCategory = await this.connection.getRepository(TaxCategory).findOne(id);
|
|
|
- if (!taxCategory) {
|
|
|
- throw new I18nError(`error.entity-with-id-not-found`, {
|
|
|
- entityName: 'TaxCategory',
|
|
|
- id,
|
|
|
- });
|
|
|
- }
|
|
|
- return taxCategory;
|
|
|
- }
|
|
|
-
|
|
|
- private async getZoneOrThrow(id: ID): Promise<Zone> {
|
|
|
- const zone = await this.connection.getRepository(Zone).findOne(id);
|
|
|
- if (!zone) {
|
|
|
- throw new I18nError(`error.entity-with-id-not-found`, {
|
|
|
- entityName: 'Zone',
|
|
|
- id,
|
|
|
- });
|
|
|
- }
|
|
|
- return zone;
|
|
|
- }
|
|
|
-
|
|
|
- private async getCustomerGroupOrThrow(id: ID): Promise<CustomerGroup> {
|
|
|
- const group = await this.connection.getRepository(CustomerGroup).findOne(id);
|
|
|
- if (!group) {
|
|
|
- throw new I18nError(`error.entity-with-id-not-found`, {
|
|
|
- entityName: 'CustomerGroup',
|
|
|
- id,
|
|
|
- });
|
|
|
- }
|
|
|
- return group;
|
|
|
- }
|
|
|
}
|