mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
refactor: warehouses to nestjs
This commit is contained in:
@@ -9,8 +9,10 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { BranchesApplication } from './BranchesApplication.service';
|
||||
import { ICreateBranchDTO, IEditBranchDTO } from './Branches.types';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
|
||||
@Controller('branches')
|
||||
@PublicRoute()
|
||||
export class BranchesController {
|
||||
constructor(private readonly branchesApplication: BranchesApplication) {}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import { GetBranchService } from './queries/GetBranch.service';
|
||||
import { GetBranchesService } from './queries/GetBranches.service';
|
||||
import { ActivateBranches } from './commands/ActivateBranchesFeature.service';
|
||||
import { BranchesApplication } from './BranchesApplication.service';
|
||||
import { BranchesSettingsService } from './BranchesSettings';
|
||||
import { BranchCommandValidator } from './commands/BranchCommandValidator.service';
|
||||
|
||||
@Module({
|
||||
imports: [TenancyDatabaseModule],
|
||||
@@ -24,8 +26,10 @@ import { BranchesApplication } from './BranchesApplication.service';
|
||||
MarkBranchAsPrimaryService,
|
||||
ActivateBranches,
|
||||
BranchesApplication,
|
||||
BranchesSettingsService,
|
||||
TenancyContext,
|
||||
TransformerInjectable,
|
||||
BranchCommandValidator
|
||||
],
|
||||
})
|
||||
export class BranchesModule {}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
import { Knex } from 'knex';
|
||||
|
||||
export interface IBranch {
|
||||
id?: number;
|
||||
}
|
||||
import { Branch } from './models/Branch.model';
|
||||
|
||||
export interface ICreateBranchDTO {
|
||||
name: string;
|
||||
code: string;
|
||||
|
||||
primary?: boolean;
|
||||
}
|
||||
export interface IEditBranchDTO {
|
||||
@@ -15,7 +11,7 @@ export interface IEditBranchDTO {
|
||||
}
|
||||
|
||||
export interface IBranchCreatePayload {
|
||||
tenantId: number;
|
||||
// tenantId: number;
|
||||
createBranchDTO: ICreateBranchDTO;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
@@ -33,18 +29,18 @@ export interface IBranchesActivatePayload {
|
||||
}
|
||||
export interface IBranchesActivatedPayload {
|
||||
// tenantId: number;
|
||||
primaryBranch: IBranch;
|
||||
primaryBranch: Branch;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBranchMarkAsPrimaryPayload {
|
||||
// tenantId: number;
|
||||
oldBranch: IBranch;
|
||||
oldBranch: Branch;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
export interface IBranchMarkedAsPrimaryPayload {
|
||||
// tenantId: number;
|
||||
oldBranch: IBranch;
|
||||
markedBranch: IBranch;
|
||||
oldBranch: Branch;
|
||||
markedBranch: Branch;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IBranch, ICreateBranchDTO, IEditBranchDTO } from './Branches.types';
|
||||
import { ICreateBranchDTO, IEditBranchDTO } from './Branches.types';
|
||||
import { ActivateBranches } from './commands/ActivateBranchesFeature.service';
|
||||
import {
|
||||
CreateBranchService,
|
||||
@@ -39,7 +39,7 @@ export class BranchesApplication {
|
||||
* @param {number} branchId - Branch id.
|
||||
* @returns {Promise<IBranch>}
|
||||
*/
|
||||
public getBranch = (branchId: number): Promise<IBranch> => {
|
||||
public getBranch = (branchId: number): Promise<Branch> => {
|
||||
return this.getBranchService.getBranch(branchId);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,29 +1,23 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Features } from '@/interfaces';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
|
||||
@Service()
|
||||
export class BranchesSettings {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class BranchesSettingsService {
|
||||
/**
|
||||
* Marks multi-branches as activated.
|
||||
* @param {number} tenantId -
|
||||
*/
|
||||
public markMultiBranchesAsActivated = (tenantId: number) => {
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
public markMultiBranchesAsActivated = () => {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
settings.set({ group: 'features', key: Features.BRANCHES, value: 1 });
|
||||
// settings.set({ group: 'features', key: Features.BRANCHES, value: 1 });
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves whether multi-branches is active.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
public isMultiBranchesActive = (tenantId: number) => {
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
public isMultiBranchesActive = () => {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
return settings.get({ group: 'features', key: Features.BRANCHES });
|
||||
// return settings.get({ group: 'features', key: Features.BRANCHES });
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
IBranchesActivatePayload,
|
||||
} from '../Branches.types';
|
||||
import { CreateBranchService } from './CreateBranch.service';
|
||||
import { BranchesSettings } from '../BranchesSettings';
|
||||
import { BranchesSettingsService } from '../BranchesSettings';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
@@ -20,7 +20,7 @@ export class ActivateBranches {
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly createBranch: CreateBranchService,
|
||||
private readonly branchesSettings: BranchesSettings,
|
||||
private readonly branchesSettings: BranchesSettingsService,
|
||||
private readonly i18n: I18nService,
|
||||
|
||||
@Inject(Branch.name)
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
IBranchCreatePayload,
|
||||
ICreateBranchDTO,
|
||||
} from '../Branches.types';
|
||||
import { BranchCommandValidator } from './BranchCommandValidator.service';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
@@ -13,10 +12,14 @@ import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class CreateBranchService {
|
||||
/**
|
||||
* @param {UnitOfWork} uow - Unit of Work for tenant database transactions.
|
||||
* @param {EventEmitter2} eventPublisher - Event emitter for publishing branch creation events.
|
||||
* @param {typeof Branch} branchModel - The Branch model class for database operations.
|
||||
*/
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly validator: BranchCommandValidator,
|
||||
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { IBranchDeletedPayload, IBranchDeletePayload } from './Branch.types';
|
||||
import { IBranchDeletedPayload, IBranchDeletePayload } from '../Branches.types';
|
||||
import { BranchCommandValidator } from './BranchCommandValidator.service';
|
||||
import { ERRORS } from '../constants';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
@@ -37,10 +37,10 @@ export class DeleteBranchService {
|
||||
const oldBranch = await this.branchModel
|
||||
.query()
|
||||
.findById(branchId)
|
||||
.throwIfNotFound()
|
||||
.queryAndThrowIfHasRelations({
|
||||
type: ERRORS.BRANCH_HAS_ASSOCIATED_TRANSACTIONS,
|
||||
});
|
||||
.throwIfNotFound();
|
||||
// .queryAndThrowIfHasRelations({
|
||||
// type: ERRORS.BRANCH_HAS_ASSOCIATED_TRANSACTIONS,
|
||||
// });
|
||||
|
||||
// Authorize the branch before deleting.
|
||||
await this.authorize(branchId);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
IBranchEditedPayload,
|
||||
@@ -13,6 +13,7 @@ import { events } from '@/common/events/events';
|
||||
@Injectable()
|
||||
export class EditBranchService {
|
||||
constructor(
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import TenantModel from 'models/TenantModel';
|
||||
import BranchMetadata from './Branch.settings';
|
||||
import ModelSetting from './ModelSetting';
|
||||
// import { Model, mixin } from 'objection';
|
||||
// import TenantModel from 'models/TenantModel';
|
||||
// import BranchMetadata from './Branch.settings';
|
||||
// import ModelSetting from './ModelSetting';
|
||||
import { BaseModel } from '@/models/Model';
|
||||
|
||||
export class Branch extends BaseModel{
|
||||
@@ -186,7 +186,7 @@ export class Branch extends BaseModel{
|
||||
/**
|
||||
* Model settings.
|
||||
*/
|
||||
static get meta() {
|
||||
return BranchMetadata;
|
||||
}
|
||||
// static get meta() {
|
||||
// return BranchMetadata;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
export * from './BillBranchSubscriber';
|
||||
export * from './CashflowBranchDTOValidatorSubscriber';
|
||||
export * from './CreditNoteBranchesSubscriber';
|
||||
export * from './CreditNoteRefundBranchSubscriber';
|
||||
export * from './ExpenseBranchSubscriber';
|
||||
export * from './ManualJournalBranchSubscriber';
|
||||
export * from './PaymentMadeBranchSubscriber';
|
||||
export * from './PaymentReceiveBranchSubscriber';
|
||||
export * from './SaleEstimateMultiBranchesSubscriber';
|
||||
export * from './SaleReceiptBranchesSubscriber';
|
||||
export * from './VendorCreditBranchSubscriber';
|
||||
export * from './VendorCreditRefundBranchSubscriber';
|
||||
export * from './InvoiceBranchValidatorSubscriber';
|
||||
export * from './ContactOpeningBalanceBranchSubscriber';
|
||||
export * from './InventoryAdjustmentBranchValidatorSubscriber';
|
||||
// export * from './BillBranchSubscriber';
|
||||
// export * from './CashflowBranchDTOValidatorSubscriber';
|
||||
// export * from './CreditNoteBranchesSubscriber';
|
||||
// export * from './CreditNoteRefundBranchSubscriber';
|
||||
// export * from './ExpenseBranchSubscriber';
|
||||
// export * from './ManualJournalBranchSubscriber';
|
||||
// export * from './PaymentMadeBranchSubscriber';
|
||||
// export * from './PaymentReceiveBranchSubscriber';
|
||||
// export * from './SaleEstimateMultiBranchesSubscriber';
|
||||
// export * from './SaleReceiptBranchesSubscriber';
|
||||
// export * from './VendorCreditBranchSubscriber';
|
||||
// export * from './VendorCreditRefundBranchSubscriber';
|
||||
// export * from './InvoiceBranchValidatorSubscriber';
|
||||
// export * from './ContactOpeningBalanceBranchSubscriber';
|
||||
// export * from './InventoryAdjustmentBranchValidatorSubscriber';
|
||||
Reference in New Issue
Block a user