fix: auto-increment setting parsing. (#453)

This commit is contained in:
Ahmed Bouhuolia
2024-05-17 12:49:04 +02:00
committed by GitHub
parent e380c598d3
commit 2ada57a2b4
4 changed files with 14 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { difference } from 'lodash'; import { difference, isEmpty } from 'lodash';
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import { ServiceError } from '@/exceptions'; import { ServiceError } from '@/exceptions';
import { import {
@@ -244,16 +244,12 @@ export class CommandManualJournalValidators {
/** /**
* Validates the manual journal number require. * Validates the manual journal number require.
* @param {string} journalNumber * @param {string} journalNumber
* @throws {ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED)}
*/ */
public validateJournalNoRequireWhenAutoNotEnabled = ( public validateJournalNoRequireWhenAutoNotEnabled = (
tenantId: number,
journalNumber: string journalNumber: string
) => { ) => {
// Retrieve the next manual journal number. if (isEmpty(journalNumber)) {
const autoIncrmenetEnabled =
this.autoIncrement.autoIncrementEnabled(tenantId);
if (!journalNumber || !autoIncrmenetEnabled) {
throw new ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED); throw new ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED);
} }
}; };

View File

@@ -99,7 +99,6 @@ export class CreateManualJournalService {
// Validate manual journal number require when auto-increment not enabled. // Validate manual journal number require when auto-increment not enabled.
this.validator.validateJournalNoRequireWhenAutoNotEnabled( this.validator.validateJournalNoRequireWhenAutoNotEnabled(
tenantId,
manualJournalDTO.journalNumber manualJournalDTO.journalNumber
); );
// Validate manual journal uniquiness on the storage. // Validate manual journal uniquiness on the storage.

View File

@@ -15,10 +15,8 @@ export default class AutoIncrementOrdersService {
const group = settingsGroup; const group = settingsGroup;
// Settings service transaction number and prefix. // Settings service transaction number and prefix.
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false); return settings.get({ group, key: 'auto_increment' }, false);
};
return parseBoolean(autoIncrement, false);
}
/** /**
* Retrieve the next service transaction number. * Retrieve the next service transaction number.
@@ -27,17 +25,16 @@ export default class AutoIncrementOrdersService {
* @param {Function} getMaxTransactionNo * @param {Function} getMaxTransactionNo
* @return {Promise<string>} * @return {Promise<string>}
*/ */
getNextTransactionNumber(tenantId: number, settingsGroup: string): string { getNextTransactionNumber(tenantId: number, group: string): string {
const settings = this.tenancy.settings(tenantId); const settings = this.tenancy.settings(tenantId);
const group = settingsGroup;
// Settings service transaction number and prefix. // Settings service transaction number and prefix.
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false); const autoIncrement = this.autoIncrementEnabled(tenantId, group);
const settingNo = settings.get({ group, key: 'next_number' }, ''); const settingNo = settings.get({ group, key: 'next_number' }, '');
const settingPrefix = settings.get({ group, key: 'number_prefix' }, ''); const settingPrefix = settings.get({ group, key: 'number_prefix' }, '');
return parseBoolean(autoIncrement, false) ? `${settingPrefix}${settingNo}` : ''; return autoIncrement ? `${settingPrefix}${settingNo}` : '';
} }
/** /**
@@ -53,7 +50,9 @@ export default class AutoIncrementOrdersService {
const autoIncrement = settings.get({ group, key: 'auto_increment' }); const autoIncrement = settings.get({ group, key: 'auto_increment' });
// Can't continue if the auto-increment of the service was disabled. // Can't continue if the auto-increment of the service was disabled.
if (!autoIncrement) { return; } if (!autoIncrement) {
return;
}
settings.set( settings.set(
{ group, key: 'next_number' }, { group, key: 'next_number' },

View File

@@ -337,6 +337,9 @@ const booleanValues: string[] = [
].map((value) => normalizeValue(value)); ].map((value) => normalizeValue(value));
export const parseBoolean = <T>(value: any, defaultValue: T): T | boolean => { export const parseBoolean = <T>(value: any, defaultValue: T): T | boolean => {
if (typeof value === 'boolean') {
return value; // Retrun early we have nothing to parse.
}
const normalizedValue = normalizeValue(value); const normalizedValue = normalizeValue(value);
if (isEmpty(value) || booleanValues.indexOf(normalizedValue) === -1) { if (isEmpty(value) || booleanValues.indexOf(normalizedValue) === -1) {
return defaultValue; return defaultValue;