Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto-increment setting parsing #453

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading