mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
refactor: tenant proxy providers
This commit is contained in:
@@ -13,6 +13,7 @@ import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class ActivateBranches {
|
||||
@@ -24,7 +25,7 @@ export class ActivateBranches {
|
||||
private readonly i18n: I18nService,
|
||||
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly branchModel: TenantModelProxy<typeof Branch>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,11 +3,12 @@ import { ERRORS } from '../constants';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
|
||||
import { ServiceError } from '../../Items/ServiceError';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
@Injectable()
|
||||
export class BranchCommandValidator {
|
||||
constructor(
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly branchModel: TenantModelProxy<typeof Branch>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -15,7 +16,9 @@ export class BranchCommandValidator {
|
||||
* @param {number} branchId
|
||||
*/
|
||||
public validateBranchNotOnlyWarehouse = async (branchId: number) => {
|
||||
const warehouses = await this.branchModel.query().whereNot('id', branchId);
|
||||
const warehouses = await this.branchModel()
|
||||
.query()
|
||||
.whereNot('id', branchId);
|
||||
|
||||
if (warehouses.length === 0) {
|
||||
throw new ServiceError(ERRORS.COULD_NOT_DELETE_ONLY_BRANCH);
|
||||
@@ -31,7 +34,7 @@ export class BranchCommandValidator {
|
||||
code: string,
|
||||
exceptBranchId?: number,
|
||||
): Promise<void> => {
|
||||
const branch = await this.branchModel
|
||||
const branch = await this.branchModel()
|
||||
.query()
|
||||
.onBuild((query) => {
|
||||
query.select(['id']);
|
||||
|
||||
@@ -9,6 +9,7 @@ import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class CreateBranchService {
|
||||
@@ -22,7 +23,7 @@ export class CreateBranchService {
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly branchModel: TenantModelProxy<typeof Branch>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -41,10 +42,11 @@ export class CreateBranchService {
|
||||
trx,
|
||||
} as IBranchCreatePayload);
|
||||
|
||||
const branch = await this.branchModel.query().insertAndFetch({
|
||||
...createBranchDTO,
|
||||
});
|
||||
|
||||
const branch = await this.branchModel()
|
||||
.query()
|
||||
.insertAndFetch({
|
||||
...createBranchDTO,
|
||||
});
|
||||
// Triggers `onBranchCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.warehouse.onEdited, {
|
||||
createBranchDTO,
|
||||
|
||||
@@ -7,12 +7,13 @@ import { Branch } from '../models/Branch.model';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteBranchService {
|
||||
constructor(
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly branchModel: TenantModelProxy<typeof Branch>,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly validator: BranchCommandValidator,
|
||||
@@ -29,18 +30,18 @@ export class DeleteBranchService {
|
||||
|
||||
/**
|
||||
* Deletes branch.
|
||||
* @param {number} branchId
|
||||
* @param {number} branchId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public deleteBranch = async (branchId: number): Promise<void> => {
|
||||
// Retrieves the old branch or throw not found service error.
|
||||
const oldBranch = await this.branchModel
|
||||
const oldBranch = await this.branchModel()
|
||||
.query()
|
||||
.findById(branchId)
|
||||
.throwIfNotFound();
|
||||
// .queryAndThrowIfHasRelations({
|
||||
// type: ERRORS.BRANCH_HAS_ASSOCIATED_TRANSACTIONS,
|
||||
// });
|
||||
// .queryAndThrowIfHasRelations({
|
||||
// type: ERRORS.BRANCH_HAS_ASSOCIATED_TRANSACTIONS,
|
||||
// });
|
||||
|
||||
// Authorize the branch before deleting.
|
||||
await this.authorize(branchId);
|
||||
@@ -53,7 +54,7 @@ export class DeleteBranchService {
|
||||
trx,
|
||||
} as IBranchDeletePayload);
|
||||
|
||||
await this.branchModel.query().findById(branchId).delete();
|
||||
await this.branchModel().query().findById(branchId).delete();
|
||||
|
||||
// Triggers `onBranchCreate` event.
|
||||
await this.eventPublisher.emitAsync(events.warehouse.onEdited, {
|
||||
|
||||
@@ -9,12 +9,13 @@ import { Branch } from '../models/Branch.model';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class EditBranchService {
|
||||
constructor(
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly branchModel: TenantModelProxy<typeof Branch>,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
) {}
|
||||
@@ -29,7 +30,7 @@ export class EditBranchService {
|
||||
editBranchDTO: IEditBranchDTO,
|
||||
) => {
|
||||
// Retrieves the old branch or throw not found service error.
|
||||
const oldBranch = await this.branchModel
|
||||
const oldBranch = await this.branchModel()
|
||||
.query()
|
||||
.findById(branchId)
|
||||
.throwIfNotFound();
|
||||
@@ -43,7 +44,7 @@ export class EditBranchService {
|
||||
} as IBranchEditPayload);
|
||||
|
||||
// Edits the branch on the storage.
|
||||
const branch = await this.branchModel
|
||||
const branch = await this.branchModel()
|
||||
.query()
|
||||
.patchAndFetchById(branchId, {
|
||||
...editBranchDTO,
|
||||
|
||||
@@ -8,6 +8,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class MarkBranchAsPrimaryService {
|
||||
@@ -16,7 +17,7 @@ export class MarkBranchAsPrimaryService {
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(Branch.name)
|
||||
private readonly branchModel: typeof Branch,
|
||||
private readonly branchModel: TenantModelProxy<typeof Branch>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -26,7 +27,7 @@ export class MarkBranchAsPrimaryService {
|
||||
*/
|
||||
public async markAsPrimary(branchId: number): Promise<Branch> {
|
||||
// Retrieves the old branch or throw not found service error.
|
||||
const oldBranch = await this.branchModel
|
||||
const oldBranch = await this.branchModel()
|
||||
.query()
|
||||
.findById(branchId)
|
||||
.throwIfNotFound();
|
||||
@@ -40,10 +41,10 @@ export class MarkBranchAsPrimaryService {
|
||||
} as IBranchMarkAsPrimaryPayload);
|
||||
|
||||
// Updates all branches as not primary.
|
||||
await this.branchModel.query(trx).update({ primary: false });
|
||||
await this.branchModel().query(trx).update({ primary: false });
|
||||
|
||||
// Updates the given branch as primary.
|
||||
const markedBranch = await this.branchModel
|
||||
const markedBranch = await this.branchModel()
|
||||
.query(trx)
|
||||
.patchAndFetchById(branchId, {
|
||||
primary: true,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class GetBranchService {
|
||||
constructor(
|
||||
@Inject(Branch.name)
|
||||
private readonly branch: typeof Branch,
|
||||
private readonly branch: TenantModelProxy<typeof Branch>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -15,7 +16,7 @@ export class GetBranchService {
|
||||
* @returns {Promise<IBranch>}
|
||||
*/
|
||||
public getBranch = async (branchId: number): Promise<Branch> => {
|
||||
const branch = await this.branch
|
||||
const branch = await this.branch()
|
||||
.query()
|
||||
.findById(branchId)
|
||||
.throwIfNotFound();
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Branch } from '../models/Branch.model';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class GetBranchesService {
|
||||
constructor(
|
||||
@Inject(Branch.name)
|
||||
private readonly branch: typeof Branch,
|
||||
private readonly branch: TenantModelProxy<typeof Branch>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -13,7 +14,7 @@ export class GetBranchesService {
|
||||
* @returns
|
||||
*/
|
||||
public getBranches = async () => {
|
||||
const branches = await this.branch.query().orderBy('name', 'DESC');
|
||||
const branches = await this.branch().query().orderBy('name', 'DESC');
|
||||
|
||||
return branches;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user