mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
refactor: settings module
This commit is contained in:
@@ -8,11 +8,13 @@ import {
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { SettingsStore } from '../Settings/SettingsStore';
|
||||
import { SETTINGS_PROVIDER } from '../Settings/Settings.types';
|
||||
import { SettingsApplicationService } from '../Settings/SettingsApplication.service';
|
||||
|
||||
@Injectable()
|
||||
export class TransactionsLockingRepository {
|
||||
constructor(
|
||||
@Inject(SETTINGS_PROVIDER) private readonly settingsStore: SettingsStore,
|
||||
@Inject(SETTINGS_PROVIDER)
|
||||
private readonly settingsStore: () => SettingsStore,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -25,57 +27,58 @@ export class TransactionsLockingRepository {
|
||||
transactionlocking,
|
||||
) {
|
||||
const group = `transactions-locking`;
|
||||
const settingsStore = await this.settingsStore();
|
||||
|
||||
if (!isUndefined(transactionlocking.active)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.active`,
|
||||
value: transactionlocking.active,
|
||||
});
|
||||
}
|
||||
if (!isUndefined(transactionlocking.lockToDate)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.lock_to_date`,
|
||||
value: parseDate(transactionlocking.lockToDate),
|
||||
});
|
||||
}
|
||||
if (!isUndefined(transactionlocking.unlockFromDate)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.unlock_from_date`,
|
||||
value: parseDate(transactionlocking.unlockFromDate),
|
||||
});
|
||||
}
|
||||
if (!isUndefined(transactionlocking.unlockToDate)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.unlock_to_date`,
|
||||
value: parseDate(transactionlocking.unlockToDate),
|
||||
});
|
||||
}
|
||||
if (!isUndefined(transactionlocking.lockReason)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.lock_reason`,
|
||||
value: transactionlocking.lockReason,
|
||||
});
|
||||
}
|
||||
if (!isUndefined(transactionlocking.unlockReason)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.unlock_reason`,
|
||||
value: transactionlocking.unlockReason,
|
||||
});
|
||||
}
|
||||
if (!isUndefined(transactionlocking.partialUnlockReason)) {
|
||||
this.settingsStore.set({
|
||||
settingsStore.set({
|
||||
group,
|
||||
key: `${lockingGroup}.partial_unlock_reason`,
|
||||
value: transactionlocking.partialUnlockReason,
|
||||
});
|
||||
}
|
||||
await this.settingsStore.save();
|
||||
await settingsStore.save();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,40 +86,41 @@ export class TransactionsLockingRepository {
|
||||
* @param {string} lockingGroup - The group of the transactions locking
|
||||
* @returns {ITransactionMeta} - The transactions locking settings
|
||||
*/
|
||||
public getTransactionsLocking(
|
||||
public async getTransactionsLocking(
|
||||
lockingGroup: string = TransactionsLockingGroup.All,
|
||||
): ITransactionMeta {
|
||||
): Promise<ITransactionMeta> {
|
||||
const group = `transactions-locking`;
|
||||
const settingsStore = await this.settingsStore();
|
||||
|
||||
const isEnabled = this.settingsStore.get({
|
||||
const isEnabled = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.active`,
|
||||
});
|
||||
const lockFromDate = this.settingsStore.get({
|
||||
const lockFromDate = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.lock_from_date`,
|
||||
});
|
||||
const lockToDate = this.settingsStore.get({
|
||||
const lockToDate = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.lock_to_date`,
|
||||
});
|
||||
const unlockFromDate = this.settingsStore.get({
|
||||
const unlockFromDate = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.unlock_from_date`,
|
||||
});
|
||||
const unlockToDate = this.settingsStore.get({
|
||||
const unlockToDate = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.unlock_to_date`,
|
||||
});
|
||||
const lockReason = this.settingsStore.get({
|
||||
const lockReason = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.lock_reason`,
|
||||
});
|
||||
const unlockReason = this.settingsStore.get({
|
||||
const unlockReason = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.unlock_reason`,
|
||||
});
|
||||
const partialUnlockReason = this.settingsStore.get({
|
||||
const partialUnlockReason = settingsStore.get({
|
||||
group,
|
||||
key: `${lockingGroup}.partial_unlock_reason`,
|
||||
});
|
||||
@@ -137,8 +141,10 @@ export class TransactionsLockingRepository {
|
||||
* Get transactions locking type
|
||||
* @returns {string} - The transactions locking type
|
||||
*/
|
||||
public getTransactionsLockingType() {
|
||||
const lockingType = this.settingsStore.get({
|
||||
public async getTransactionsLockingType() {
|
||||
const settingsStore = await this.settingsStore();
|
||||
|
||||
const lockingType = settingsStore.get({
|
||||
group: 'transactions-locking',
|
||||
key: 'locking-type',
|
||||
});
|
||||
@@ -149,14 +155,17 @@ export class TransactionsLockingRepository {
|
||||
* Flag transactions locking type
|
||||
* @param {TransactionsLockingType} transactionsType - The transactions locking type
|
||||
*/
|
||||
public flagTransactionsLockingType(
|
||||
public async flagTransactionsLockingType(
|
||||
transactionsType: TransactionsLockingType,
|
||||
) {
|
||||
this.settingsStore.set({
|
||||
const settingsStore = await this.settingsStore();
|
||||
|
||||
settingsStore.set({
|
||||
group: 'transactions-locking',
|
||||
key: 'locking-type',
|
||||
value: transactionsType,
|
||||
});
|
||||
await settingsStore.save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ export class TransactionsLockingService {
|
||||
|
||||
/**
|
||||
* Enable/disable all transacations locking.
|
||||
* @param {TransactionsLockingGroup} module - The transaction locking module.
|
||||
* @param {Partial<ITransactionsLockingAllDTO>} allLockingDTO
|
||||
* @param {TransactionsLockingGroup} module - The transaction locking module.
|
||||
* @param {Partial<ITransactionsLockingAllDTO>} allLockingDTO
|
||||
* @returns {Promise<ITransactionMeta>}
|
||||
*/
|
||||
public commandTransactionsLocking = async (
|
||||
@@ -64,8 +64,8 @@ export class TransactionsLockingService {
|
||||
|
||||
/**
|
||||
* Cancels the full transactions locking.
|
||||
* @param {TransactionsLockingGroup} module - The transaction locking module.
|
||||
* @param {ICancelTransactionsLockingDTO} cancelLockingDTO
|
||||
* @param {TransactionsLockingGroup} module - The transaction locking module.
|
||||
* @param {ICancelTransactionsLockingDTO} cancelLockingDTO
|
||||
* @returns {Promise<ITransactionMeta>}
|
||||
*/
|
||||
public cancelTransactionLocking = async (
|
||||
@@ -112,7 +112,7 @@ export class TransactionsLockingService {
|
||||
|
||||
// Retrieve the current transactions locking type.
|
||||
const lockingType =
|
||||
this.transactionsLockingRepo.getTransactionsLockingType();
|
||||
await this.transactionsLockingRepo.getTransactionsLockingType();
|
||||
|
||||
if (moduleGroup !== TransactionsLockingGroup.All) {
|
||||
this.validateLockingTypeNotAll(lockingType);
|
||||
|
||||
@@ -13,8 +13,8 @@ export class FinancialTransactionLocking {
|
||||
* @param {Date} transactionDate - The transaction date.
|
||||
* @throws {ServiceError(TRANSACTIONS_DATE_LOCKED)}
|
||||
*/
|
||||
public transactionLockingGuard = (transactionDate: Date) => {
|
||||
this.transactionLockingGuardService.transactionsLockingGuard(
|
||||
public transactionLockingGuard = async (transactionDate: Date) => {
|
||||
await this.transactionLockingGuardService.transactionsLockingGuard(
|
||||
transactionDate,
|
||||
TransactionsLockingGroup.Financial,
|
||||
);
|
||||
|
||||
@@ -16,7 +16,7 @@ export class PurchasesTransactionLockingGuard {
|
||||
public transactionLockingGuard = async (
|
||||
transactionDate: MomentInput
|
||||
) => {
|
||||
this.transactionLockingGuardService.transactionsLockingGuard(
|
||||
await this.transactionLockingGuardService.transactionsLockingGuard(
|
||||
transactionDate,
|
||||
TransactionsLockingGroup.Purchases
|
||||
);
|
||||
|
||||
@@ -18,14 +18,12 @@ export class TransactionsLockingGuard {
|
||||
* @param {TransactionsLockingGroup} lockingGroup - The transaction group.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isTransactionsLocking = (
|
||||
public isTransactionsLocking = async (
|
||||
transactionDate: MomentInput,
|
||||
lockingGroup: string = TransactionsLockingGroup.All
|
||||
): boolean => {
|
||||
lockingGroup: string = TransactionsLockingGroup.All,
|
||||
): Promise<boolean> => {
|
||||
const { isEnabled, unlockFromDate, unlockToDate, lockToDate } =
|
||||
this.transactionsLockingRepo.getTransactionsLocking(
|
||||
lockingGroup
|
||||
);
|
||||
await this.transactionsLockingRepo.getTransactionsLocking(lockingGroup);
|
||||
// Returns false anyway in case if the transaction locking is disabled.
|
||||
if (!isEnabled) return false;
|
||||
|
||||
@@ -49,14 +47,15 @@ export class TransactionsLockingGuard {
|
||||
*
|
||||
* @throws {ServiceError}
|
||||
*/
|
||||
public validateTransactionsLocking = (
|
||||
public validateTransactionsLocking = async (
|
||||
transactionDate: MomentInput,
|
||||
lockingGroup: TransactionsLockingGroup
|
||||
lockingGroup: TransactionsLockingGroup,
|
||||
) => {
|
||||
const isLocked = this.isTransactionsLocking(
|
||||
const isLocked = await this.isTransactionsLocking(
|
||||
transactionDate,
|
||||
lockingGroup
|
||||
lockingGroup,
|
||||
);
|
||||
|
||||
if (isLocked) {
|
||||
this.throwTransactionsLockError(lockingGroup);
|
||||
}
|
||||
@@ -66,12 +65,11 @@ export class TransactionsLockingGuard {
|
||||
* Throws transactions locking error.
|
||||
* @param {TransactionsLockingGroup} lockingGroup - The transaction group.
|
||||
*/
|
||||
public throwTransactionsLockError = (
|
||||
lockingGroup: TransactionsLockingGroup
|
||||
public throwTransactionsLockError = async (
|
||||
lockingGroup: TransactionsLockingGroup,
|
||||
) => {
|
||||
const { lockToDate } = this.transactionsLockingRepo.getTransactionsLocking(
|
||||
lockingGroup
|
||||
);
|
||||
const { lockToDate } =
|
||||
await this.transactionsLockingRepo.getTransactionsLocking(lockingGroup);
|
||||
throw new ServiceError(ERRORS.TRANSACTIONS_DATE_LOCKED, null, {
|
||||
lockedToDate: lockToDate,
|
||||
formattedLockedToDate: moment(lockToDate).format('YYYY/MM/DD'),
|
||||
@@ -83,24 +81,21 @@ export class TransactionsLockingGuard {
|
||||
* @param {TransactionsLockingGroup} lockingGroup - The transaction group.
|
||||
* @param {Date} transactionDate - The transaction date.
|
||||
*/
|
||||
public transactionsLockingGuard = (
|
||||
public transactionsLockingGuard = async (
|
||||
transactionDate: MomentInput,
|
||||
moduleType: TransactionsLockingGroup
|
||||
moduleType: TransactionsLockingGroup,
|
||||
) => {
|
||||
const lockingType =
|
||||
this.transactionsLockingRepo.getTransactionsLockingType();
|
||||
await this.transactionsLockingRepo.getTransactionsLockingType();
|
||||
|
||||
//
|
||||
if (lockingType === TransactionsLockingGroup.All) {
|
||||
return this.validateTransactionsLocking(
|
||||
transactionDate,
|
||||
TransactionsLockingGroup.All
|
||||
TransactionsLockingGroup.All,
|
||||
);
|
||||
}
|
||||
//
|
||||
return this.validateTransactionsLocking(
|
||||
transactionDate,
|
||||
moduleType
|
||||
);
|
||||
return this.validateTransactionsLocking(transactionDate, moduleType);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export class QueryTransactionsLocking {
|
||||
* Retrieve transactions locking modules.
|
||||
* @returns {ITransactionLockingMetaPOJO[]}
|
||||
*/
|
||||
public getTransactionsLockingModules = (): Promise<
|
||||
public getTransactionsLockingModules = async (): Promise<
|
||||
ITransactionLockingMetaPOJO[]
|
||||
> => {
|
||||
const modules = TRANSACTIONS_LOCKING_SCHEMA.map(
|
||||
@@ -47,10 +47,12 @@ export class QueryTransactionsLocking {
|
||||
* @param {TransactionsLockingGroup} module -
|
||||
* @returns {ITransactionLockingMetaPOJO}
|
||||
*/
|
||||
public getTransactionsLockingModuleMeta = (
|
||||
public getTransactionsLockingModuleMeta = async (
|
||||
module: TransactionsLockingGroup,
|
||||
): Promise<ITransactionLockingMetaPOJO> => {
|
||||
const meta = this.transactionsLockingRepo.getTransactionsLocking(module);
|
||||
const meta =
|
||||
await this.transactionsLockingRepo.getTransactionsLocking(module);
|
||||
|
||||
return this.transformer.transform(
|
||||
meta,
|
||||
new TransactionsLockingMetaTransformer(),
|
||||
@@ -66,7 +68,7 @@ export class QueryTransactionsLocking {
|
||||
async (): Promise<ITransactionsLockingListPOJO> => {
|
||||
// Retrieve the current transactions locking type.
|
||||
const lockingType =
|
||||
this.transactionsLockingRepo.getTransactionsLockingType();
|
||||
await this.transactionsLockingRepo.getTransactionsLockingType();
|
||||
|
||||
const all = await this.getTransactionsLockingAll();
|
||||
const modules = await this.getTransactionsLockingModules();
|
||||
|
||||
Reference in New Issue
Block a user