Skip to content

Commit

Permalink
fix: auto-increment setting parsing. (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia authored May 17, 2024
1 parent e380c59 commit 2ada57a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference } from 'lodash';
import { difference, isEmpty } from 'lodash';
import { Service, Inject } from 'typedi';
import { ServiceError } from '@/exceptions';
import {
Expand Down Expand Up @@ -244,16 +244,12 @@ export class CommandManualJournalValidators {
/**
* Validates the manual journal number require.
* @param {string} journalNumber
* @throws {ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED)}
*/
public validateJournalNoRequireWhenAutoNotEnabled = (
tenantId: number,
journalNumber: string
) => {
// Retrieve the next manual journal number.
const autoIncrmenetEnabled =
this.autoIncrement.autoIncrementEnabled(tenantId);

if (!journalNumber || !autoIncrmenetEnabled) {
if (isEmpty(journalNumber)) {
throw new ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export class CreateManualJournalService {

// Validate manual journal number require when auto-increment not enabled.
this.validator.validateJournalNoRequireWhenAutoNotEnabled(
tenantId,
manualJournalDTO.journalNumber
);
// Validate manual journal uniquiness on the storage.
Expand Down
17 changes: 8 additions & 9 deletions packages/server/src/services/Sales/AutoIncrementOrdersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export default class AutoIncrementOrdersService {
const group = settingsGroup;

// Settings service transaction number and prefix.
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false);

return parseBoolean(autoIncrement, false);
}
return settings.get({ group, key: 'auto_increment' }, false);
};

/**
* Retrieve the next service transaction number.
Expand All @@ -27,17 +25,16 @@ export default class AutoIncrementOrdersService {
* @param {Function} getMaxTransactionNo
* @return {Promise<string>}
*/
getNextTransactionNumber(tenantId: number, settingsGroup: string): string {
getNextTransactionNumber(tenantId: number, group: string): string {
const settings = this.tenancy.settings(tenantId);
const group = settingsGroup;

// 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 settingPrefix = settings.get({ group, key: 'number_prefix' }, '');

return parseBoolean(autoIncrement, false) ? `${settingPrefix}${settingNo}` : '';
return autoIncrement ? `${settingPrefix}${settingNo}` : '';
}

/**
Expand All @@ -53,7 +50,9 @@ export default class AutoIncrementOrdersService {
const autoIncrement = settings.get({ group, key: 'auto_increment' });

// Can't continue if the auto-increment of the service was disabled.
if (!autoIncrement) { return; }
if (!autoIncrement) {
return;
}

settings.set(
{ group, key: 'next_number' },
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ const booleanValues: string[] = [
].map((value) => normalizeValue(value));

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);
if (isEmpty(value) || booleanValues.indexOf(normalizedValue) === -1) {
return defaultValue;
Expand Down

0 comments on commit 2ada57a

Please sign in to comment.