refactor: warehouses to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-21 15:07:01 +02:00
parent cb8fd68d46
commit 8a12caf48d
29 changed files with 348 additions and 100 deletions

View File

@@ -129,7 +129,7 @@ export interface IItemEditDTO extends IItemDTO {}
// } // }
export interface IItemEventCreatedPayload { export interface IItemEventCreatedPayload {
tenantId: number; // tenantId: number;
item: Item; item: Item;
itemId: number; itemId: number;
trx: Knex.Transaction; trx: Knex.Transaction;
@@ -143,15 +143,15 @@ export interface IItemEventEditedPayload {
} }
export interface IItemEventDeletingPayload { export interface IItemEventDeletingPayload {
tenantId: number; // tenantId: number;
trx: Knex.Transaction; trx: Knex.Transaction;
oldItem: IItem; oldItem: Item;
} }
export interface IItemEventDeletedPayload { export interface IItemEventDeletedPayload {
tenantId: number; // tenantId: number;
oldItem: IItem;
itemId: number; itemId: number;
oldItem: Item;
trx: Knex.Transaction; trx: Knex.Transaction;
} }

View File

@@ -37,6 +37,7 @@ import { ItemCategoryModule } from '../ItemCategories/ItemCategory.module';
import { TaxRatesModule } from '../TaxRates/TaxRate.module'; import { TaxRatesModule } from '../TaxRates/TaxRate.module';
import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module'; import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module';
import { BranchesModule } from '../Branches/Branches.module'; import { BranchesModule } from '../Branches/Branches.module';
import { WarehousesModule } from '../Warehouses/Warehouses.module';
@Module({ @Module({
imports: [ imports: [
@@ -97,6 +98,7 @@ import { BranchesModule } from '../Branches/Branches.module';
TaxRatesModule, TaxRatesModule,
PdfTemplatesModule, PdfTemplatesModule,
BranchesModule, BranchesModule,
WarehousesModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [

View File

@@ -9,8 +9,10 @@ import {
} from '@nestjs/common'; } from '@nestjs/common';
import { BranchesApplication } from './BranchesApplication.service'; import { BranchesApplication } from './BranchesApplication.service';
import { ICreateBranchDTO, IEditBranchDTO } from './Branches.types'; import { ICreateBranchDTO, IEditBranchDTO } from './Branches.types';
import { PublicRoute } from '../Auth/Jwt.guard';
@Controller('branches') @Controller('branches')
@PublicRoute()
export class BranchesController { export class BranchesController {
constructor(private readonly branchesApplication: BranchesApplication) {} constructor(private readonly branchesApplication: BranchesApplication) {}

View File

@@ -11,6 +11,8 @@ import { GetBranchService } from './queries/GetBranch.service';
import { GetBranchesService } from './queries/GetBranches.service'; import { GetBranchesService } from './queries/GetBranches.service';
import { ActivateBranches } from './commands/ActivateBranchesFeature.service'; import { ActivateBranches } from './commands/ActivateBranchesFeature.service';
import { BranchesApplication } from './BranchesApplication.service'; import { BranchesApplication } from './BranchesApplication.service';
import { BranchesSettingsService } from './BranchesSettings';
import { BranchCommandValidator } from './commands/BranchCommandValidator.service';
@Module({ @Module({
imports: [TenancyDatabaseModule], imports: [TenancyDatabaseModule],
@@ -24,8 +26,10 @@ import { BranchesApplication } from './BranchesApplication.service';
MarkBranchAsPrimaryService, MarkBranchAsPrimaryService,
ActivateBranches, ActivateBranches,
BranchesApplication, BranchesApplication,
BranchesSettingsService,
TenancyContext, TenancyContext,
TransformerInjectable, TransformerInjectable,
BranchCommandValidator
], ],
}) })
export class BranchesModule {} export class BranchesModule {}

View File

@@ -1,13 +1,9 @@
import { Knex } from 'knex'; import { Knex } from 'knex';
import { Branch } from './models/Branch.model';
export interface IBranch {
id?: number;
}
export interface ICreateBranchDTO { export interface ICreateBranchDTO {
name: string; name: string;
code: string; code: string;
primary?: boolean; primary?: boolean;
} }
export interface IEditBranchDTO { export interface IEditBranchDTO {
@@ -15,7 +11,7 @@ export interface IEditBranchDTO {
} }
export interface IBranchCreatePayload { export interface IBranchCreatePayload {
tenantId: number; // tenantId: number;
createBranchDTO: ICreateBranchDTO; createBranchDTO: ICreateBranchDTO;
trx: Knex.Transaction; trx: Knex.Transaction;
} }
@@ -33,18 +29,18 @@ export interface IBranchesActivatePayload {
} }
export interface IBranchesActivatedPayload { export interface IBranchesActivatedPayload {
// tenantId: number; // tenantId: number;
primaryBranch: IBranch; primaryBranch: Branch;
trx: Knex.Transaction; trx: Knex.Transaction;
} }
export interface IBranchMarkAsPrimaryPayload { export interface IBranchMarkAsPrimaryPayload {
// tenantId: number; // tenantId: number;
oldBranch: IBranch; oldBranch: Branch;
trx: Knex.Transaction; trx: Knex.Transaction;
} }
export interface IBranchMarkedAsPrimaryPayload { export interface IBranchMarkedAsPrimaryPayload {
// tenantId: number; // tenantId: number;
oldBranch: IBranch; oldBranch: Branch;
markedBranch: IBranch; markedBranch: Branch;
trx: Knex.Transaction; trx: Knex.Transaction;
} }

View File

@@ -1,4 +1,4 @@
import { IBranch, ICreateBranchDTO, IEditBranchDTO } from './Branches.types'; import { ICreateBranchDTO, IEditBranchDTO } from './Branches.types';
import { ActivateBranches } from './commands/ActivateBranchesFeature.service'; import { ActivateBranches } from './commands/ActivateBranchesFeature.service';
import { import {
CreateBranchService, CreateBranchService,
@@ -39,7 +39,7 @@ export class BranchesApplication {
* @param {number} branchId - Branch id. * @param {number} branchId - Branch id.
* @returns {Promise<IBranch>} * @returns {Promise<IBranch>}
*/ */
public getBranch = (branchId: number): Promise<IBranch> => { public getBranch = (branchId: number): Promise<Branch> => {
return this.getBranchService.getBranch(branchId); return this.getBranchService.getBranch(branchId);
}; };

View File

@@ -1,29 +1,23 @@
import { Service, Inject } from 'typedi'; import { Injectable } from '@nestjs/common';
import { Features } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class BranchesSettings {
@Inject()
private tenancy: HasTenancyService;
@Injectable()
export class BranchesSettingsService {
/** /**
* Marks multi-branches as activated. * Marks multi-branches as activated.
* @param {number} tenantId -
*/ */
public markMultiBranchesAsActivated = (tenantId: number) => { public markMultiBranchesAsActivated = () => {
const settings = this.tenancy.settings(tenantId); // 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. * Retrieves whether multi-branches is active.
* @param {number} tenantId
*/ */
public isMultiBranchesActive = (tenantId: number) => { public isMultiBranchesActive = () => {
const settings = this.tenancy.settings(tenantId); // const settings = this.tenancy.settings(tenantId);
return settings.get({ group: 'features', key: Features.BRANCHES }); // return settings.get({ group: 'features', key: Features.BRANCHES });
return false;
}; };
} }

View File

@@ -8,7 +8,7 @@ import {
IBranchesActivatePayload, IBranchesActivatePayload,
} from '../Branches.types'; } from '../Branches.types';
import { CreateBranchService } from './CreateBranch.service'; import { CreateBranchService } from './CreateBranch.service';
import { BranchesSettings } from '../BranchesSettings'; import { BranchesSettingsService } from '../BranchesSettings';
import { ServiceError } from '@/modules/Items/ServiceError'; import { ServiceError } from '@/modules/Items/ServiceError';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service'; import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { Branch } from '../models/Branch.model'; import { Branch } from '../models/Branch.model';
@@ -20,7 +20,7 @@ export class ActivateBranches {
private readonly uow: UnitOfWork, private readonly uow: UnitOfWork,
private readonly eventPublisher: EventEmitter2, private readonly eventPublisher: EventEmitter2,
private readonly createBranch: CreateBranchService, private readonly createBranch: CreateBranchService,
private readonly branchesSettings: BranchesSettings, private readonly branchesSettings: BranchesSettingsService,
private readonly i18n: I18nService, private readonly i18n: I18nService,
@Inject(Branch.name) @Inject(Branch.name)

View File

@@ -5,7 +5,6 @@ import {
IBranchCreatePayload, IBranchCreatePayload,
ICreateBranchDTO, ICreateBranchDTO,
} from '../Branches.types'; } from '../Branches.types';
import { BranchCommandValidator } from './BranchCommandValidator.service';
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service'; import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
import { EventEmitter2 } from '@nestjs/event-emitter'; import { EventEmitter2 } from '@nestjs/event-emitter';
import { Branch } from '../models/Branch.model'; import { Branch } from '../models/Branch.model';
@@ -13,10 +12,14 @@ import { events } from '@/common/events/events';
@Injectable() @Injectable()
export class CreateBranchService { 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( constructor(
private readonly uow: UnitOfWork, private readonly uow: UnitOfWork,
private readonly eventPublisher: EventEmitter2, private readonly eventPublisher: EventEmitter2,
private readonly validator: BranchCommandValidator,
@Inject(Branch.name) @Inject(Branch.name)
private readonly branchModel: typeof Branch, private readonly branchModel: typeof Branch,

View File

@@ -1,6 +1,6 @@
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Knex } from 'knex'; import { Knex } from 'knex';
import { IBranchDeletedPayload, IBranchDeletePayload } from './Branch.types'; import { IBranchDeletedPayload, IBranchDeletePayload } from '../Branches.types';
import { BranchCommandValidator } from './BranchCommandValidator.service'; import { BranchCommandValidator } from './BranchCommandValidator.service';
import { ERRORS } from '../constants'; import { ERRORS } from '../constants';
import { Branch } from '../models/Branch.model'; import { Branch } from '../models/Branch.model';
@@ -37,10 +37,10 @@ export class DeleteBranchService {
const oldBranch = await this.branchModel const oldBranch = await this.branchModel
.query() .query()
.findById(branchId) .findById(branchId)
.throwIfNotFound() .throwIfNotFound();
.queryAndThrowIfHasRelations({ // .queryAndThrowIfHasRelations({
type: ERRORS.BRANCH_HAS_ASSOCIATED_TRANSACTIONS, // type: ERRORS.BRANCH_HAS_ASSOCIATED_TRANSACTIONS,
}); // });
// Authorize the branch before deleting. // Authorize the branch before deleting.
await this.authorize(branchId); await this.authorize(branchId);

View File

@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Knex } from 'knex'; import { Knex } from 'knex';
import { import {
IBranchEditedPayload, IBranchEditedPayload,
@@ -13,6 +13,7 @@ import { events } from '@/common/events/events';
@Injectable() @Injectable()
export class EditBranchService { export class EditBranchService {
constructor( constructor(
@Inject(Branch.name)
private readonly branchModel: typeof Branch, private readonly branchModel: typeof Branch,
private readonly uow: UnitOfWork, private readonly uow: UnitOfWork,
private readonly eventPublisher: EventEmitter2, private readonly eventPublisher: EventEmitter2,

View File

@@ -1,7 +1,7 @@
import { Model, mixin } from 'objection'; // import { Model, mixin } from 'objection';
import TenantModel from 'models/TenantModel'; // import TenantModel from 'models/TenantModel';
import BranchMetadata from './Branch.settings'; // import BranchMetadata from './Branch.settings';
import ModelSetting from './ModelSetting'; // import ModelSetting from './ModelSetting';
import { BaseModel } from '@/models/Model'; import { BaseModel } from '@/models/Model';
export class Branch extends BaseModel{ export class Branch extends BaseModel{
@@ -186,7 +186,7 @@ export class Branch extends BaseModel{
/** /**
* Model settings. * Model settings.
*/ */
static get meta() { // static get meta() {
return BranchMetadata; // return BranchMetadata;
} // }
} }

View File

@@ -1,15 +1,15 @@
export * from './BillBranchSubscriber'; // export * from './BillBranchSubscriber';
export * from './CashflowBranchDTOValidatorSubscriber'; // export * from './CashflowBranchDTOValidatorSubscriber';
export * from './CreditNoteBranchesSubscriber'; // export * from './CreditNoteBranchesSubscriber';
export * from './CreditNoteRefundBranchSubscriber'; // export * from './CreditNoteRefundBranchSubscriber';
export * from './ExpenseBranchSubscriber'; // export * from './ExpenseBranchSubscriber';
export * from './ManualJournalBranchSubscriber'; // export * from './ManualJournalBranchSubscriber';
export * from './PaymentMadeBranchSubscriber'; // export * from './PaymentMadeBranchSubscriber';
export * from './PaymentReceiveBranchSubscriber'; // export * from './PaymentReceiveBranchSubscriber';
export * from './SaleEstimateMultiBranchesSubscriber'; // export * from './SaleEstimateMultiBranchesSubscriber';
export * from './SaleReceiptBranchesSubscriber'; // export * from './SaleReceiptBranchesSubscriber';
export * from './VendorCreditBranchSubscriber'; // export * from './VendorCreditBranchSubscriber';
export * from './VendorCreditRefundBranchSubscriber'; // export * from './VendorCreditRefundBranchSubscriber';
export * from './InvoiceBranchValidatorSubscriber'; // export * from './InvoiceBranchValidatorSubscriber';
export * from './ContactOpeningBalanceBranchSubscriber'; // export * from './ContactOpeningBalanceBranchSubscriber';
export * from './InventoryAdjustmentBranchValidatorSubscriber'; // export * from './InventoryAdjustmentBranchValidatorSubscriber';

View File

@@ -1,9 +1,9 @@
import { Knex } from 'knex';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Item } from './models/Item'; import { Item } from './models/Item';
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service'; import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { events } from '@/common/events/events'; import { events } from '@/common/events/events';
import { Knex } from 'knex';
@Injectable() @Injectable()
export class ActivateItemService { export class ActivateItemService {

View File

@@ -39,11 +39,10 @@ export class DeleteItemService {
const oldItem = await this.itemModel const oldItem = await this.itemModel
.query() .query()
.findById(itemId) .findById(itemId)
.throwIfNotFound() .throwIfNotFound();
// @ts-expect-error // .queryAndThrowIfHasRelations({
.queryAndThrowIfHasRelations({ // type: ERRORS.ITEM_HAS_ASSOCIATED_TRANSACTIONS,
type: ERRORS.ITEM_HAS_ASSOCIATED_TRANSACTIONS, // });
});
// Delete item in unit of work. // Delete item in unit of work.
return this.uow.withTransaction(async (trx: Knex.Transaction) => { return this.uow.withTransaction(async (trx: Knex.Transaction) => {

View File

@@ -2,6 +2,7 @@ import * as F from 'fp-ts/function';
import * as R from 'ramda'; import * as R from 'ramda';
import { SearchableModel } from '@/modules/Search/SearchableMdel'; import { SearchableModel } from '@/modules/Search/SearchableMdel';
import { BaseModel } from '@/models/Model'; import { BaseModel } from '@/models/Model';
import { Warehouse } from '@/modules/Warehouses/models/Warehouse.model';
// import { TenantModel } from '@/modules/System/models/TenantModel'; // import { TenantModel } from '@/modules/System/models/TenantModel';
// const Extend = R.compose(SearchableModel)(TenantModel); // const Extend = R.compose(SearchableModel)(TenantModel);
@@ -21,6 +22,8 @@ export class Item extends BaseModel {
public readonly inventoryAccountId: number; public readonly inventoryAccountId: number;
public readonly categoryId: number; public readonly categoryId: number;
public readonly warehouse!: Warehouse;
static get tableName() { static get tableName() {
return 'items'; return 'items';
} }

View File

@@ -11,8 +11,8 @@ import { GetOrganizationBrandingAttributesService } from './queries/GetOrganizat
export class PdfTemplateApplication { export class PdfTemplateApplication {
constructor( constructor(
private readonly createPdfTemplateService: CreatePdfTemplateService, private readonly createPdfTemplateService: CreatePdfTemplateService,
private readonly deletePdfTemplateService: DeletePdfTemplateService,
private readonly getPdfTemplateService: GetPdfTemplateService, private readonly getPdfTemplateService: GetPdfTemplateService,
private readonly deletePdfTemplateService: DeletePdfTemplateService,
// private readonly getPdfTemplatesService: GetPdfTemplatesService, // private readonly getPdfTemplatesService: GetPdfTemplatesService,
private readonly editPdfTemplateService: EditPdfTemplateService, private readonly editPdfTemplateService: EditPdfTemplateService,
private readonly assignPdfTemplateDefaultService: AssignPdfTemplateDefaultService, private readonly assignPdfTemplateDefaultService: AssignPdfTemplateDefaultService,

View File

@@ -11,6 +11,9 @@ import ExpenseCategory from '@/modules/Expenses/models/ExpenseCategory.model';
import { ItemCategory } from '@/modules/ItemCategories/models/ItemCategory.model'; import { ItemCategory } from '@/modules/ItemCategories/models/ItemCategory.model';
import { TaxRateModel } from '@/modules/TaxRates/models/TaxRate.model'; import { TaxRateModel } from '@/modules/TaxRates/models/TaxRate.model';
import { PdfTemplateModel } from '@/modules/PdfTemplate/models/PdfTemplate'; import { PdfTemplateModel } from '@/modules/PdfTemplate/models/PdfTemplate';
import { Warehouse } from '@/modules/Warehouses/models/Warehouse.model';
import { ItemWarehouseQuantity } from '@/modules/Warehouses/models/ItemWarehouseQuantity';
import { Branch } from '@/modules/Branches/models/Branch.model';
const models = [ const models = [
Item, Item,
@@ -21,7 +24,10 @@ const models = [
ExpenseCategory, ExpenseCategory,
ItemCategory, ItemCategory,
TaxRateModel, TaxRateModel,
PdfTemplateModel PdfTemplateModel,
Warehouse,
ItemWarehouseQuantity,
Branch,
]; ];
const modelProviders = models.map((model) => { const modelProviders = models.map((model) => {

View File

@@ -38,7 +38,6 @@ export interface ICreateWarehouseDTO {
export interface IEditWarehouseDTO { export interface IEditWarehouseDTO {
name: string; name: string;
code: string; code: string;
city: string; city: string;
country: string; country: string;
address: string; address: string;

View File

@@ -0,0 +1,64 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
} from '@nestjs/common';
import { WarehousesApplication } from './WarehousesApplication.service';
import { ICreateWarehouseDTO, IEditWarehouseDTO } from './Warehouse.types';
import { PublicRoute } from '../Auth/Jwt.guard';
@Controller('warehouses')
@PublicRoute()
export class WarehousesController {
constructor(private warehousesApplication: WarehousesApplication) {}
@Post()
createWarehouse(@Body() createWarehouseDTO: ICreateWarehouseDTO) {
return this.warehousesApplication.createWarehouse(createWarehouseDTO);
}
@Put(':id')
editWarehouse(
@Param('id') warehouseId: string,
@Body() editWarehouseDTO: IEditWarehouseDTO,
) {
return this.warehousesApplication.editWarehouse(
Number(warehouseId),
editWarehouseDTO,
);
}
@Delete(':id')
deleteWarehouse(@Param('id') warehouseId: string) {
return this.warehousesApplication.deleteWarehouse(Number(warehouseId));
}
@Get(':id')
getWarehouse(@Param('id') warehouseId: string) {
return this.warehousesApplication.getWarehouse(Number(warehouseId));
}
@Get()
getWarehouses() {
return this.warehousesApplication.getWarehouses();
}
@Post('activate')
activateWarehouses() {
return this.warehousesApplication.activateWarehouses();
}
@Post(':id/mark-primary')
markWarehousePrimary(@Param('id') warehouseId: string) {
return this.warehousesApplication.markWarehousePrimary(Number(warehouseId));
}
@Get('items/:itemId')
getItemWarehouses(@Param('itemId') itemId: string) {
return this.warehousesApplication.getItemWarehouses(Number(itemId));
}
}

View File

@@ -0,0 +1,41 @@
import { Module } from '@nestjs/common';
import { TenancyDatabaseModule } from '../Tenancy/TenancyDB/TenancyDB.module';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
import { CreateWarehouse } from './commands/CreateWarehouse.service';
import { EditWarehouse } from './commands/EditWarehouse.service';
import { DeleteWarehouseService } from './commands/DeleteWarehouse.service';
import { WarehousesController } from './Warehouses.controller';
import { GetWarehouse } from './queries/GetWarehouse';
import { WarehouseMarkPrimary } from './commands/WarehouseMarkPrimary.service';
import { GetWarehouses } from './queries/GetWarehouses';
import { GetItemWarehouses } from './Items/GetItemWarehouses';
import { WarehouseValidator } from './commands/WarehouseValidator.service';
import { WarehousesApplication } from './WarehousesApplication.service';
import { ActivateWarehousesService } from './commands/ActivateWarehouses.service';
import { CreateInitialWarehouse } from './commands/CreateInitialWarehouse.service';
import { WarehousesSettings } from './WarehousesSettings';
import { I18nContext } from 'nestjs-i18n';
@Module({
imports: [TenancyDatabaseModule],
controllers: [WarehousesController],
providers: [
CreateWarehouse,
EditWarehouse,
DeleteWarehouseService,
GetWarehouse,
GetWarehouses,
GetItemWarehouses,
WarehouseMarkPrimary,
WarehouseValidator,
WarehousesApplication,
ActivateWarehousesService,
CreateInitialWarehouse,
WarehousesSettings,
I18nContext,
TenancyContext,
TransformerInjectable,
],
})
export class WarehousesModule {}

View File

@@ -3,7 +3,7 @@ import {
IEditWarehouseDTO, IEditWarehouseDTO,
IWarehouse, IWarehouse,
} from './Warehouse.types'; } from './Warehouse.types';
import { ActivateWarehousesService } from './commands/ActivateWarehouses'; import { ActivateWarehousesService } from './commands/ActivateWarehouses.service';
import { CreateWarehouse } from './commands/CreateWarehouse.service'; import { CreateWarehouse } from './commands/CreateWarehouse.service';
import { DeleteWarehouseService } from './commands/DeleteWarehouse.service'; import { DeleteWarehouseService } from './commands/DeleteWarehouse.service';
import { EditWarehouse } from './commands/EditWarehouse.service'; import { EditWarehouse } from './commands/EditWarehouse.service';
@@ -92,10 +92,7 @@ export class WarehousesApplication {
* @param {number} tenantId - * @param {number} tenantId -
* @returns {Promise<IWarehouse>} * @returns {Promise<IWarehouse>}
*/ */
public markWarehousePrimary = ( public markWarehousePrimary = (warehouseId: number): Promise<IWarehouse> => {
tenantId: number,
warehouseId: number,
): Promise<IWarehouse> => {
return this.markWarehousePrimaryService.markAsPrimary(warehouseId); return this.markWarehousePrimaryService.markAsPrimary(warehouseId);
}; };

View File

@@ -47,10 +47,10 @@ export class DeleteWarehouseService {
const oldWarehouse = await this.warehouseModel const oldWarehouse = await this.warehouseModel
.query() .query()
.findById(warehouseId) .findById(warehouseId)
.throwIfNotFound() .throwIfNotFound();
.queryAndThrowIfHasRelations({ // .queryAndThrowIfHasRelations({
type: ERRORS.WAREHOUSE_HAS_ASSOCIATED_TRANSACTIONS, // type: ERRORS.WAREHOUSE_HAS_ASSOCIATED_TRANSACTIONS,
}); // });
// Validates the given warehouse before deleting. // Validates the given warehouse before deleting.
await this.authorize(warehouseId); await this.authorize(warehouseId);

View File

@@ -47,7 +47,7 @@ export class EditWarehouse {
public editWarehouse = async ( public editWarehouse = async (
warehouseId: number, warehouseId: number,
warehouseDTO: IEditWarehouseDTO, warehouseDTO: IEditWarehouseDTO,
): Promise<IWarehouse> => { ): Promise<Warehouse> => {
// Authorize the warehouse DTO before editing. // Authorize the warehouse DTO before editing.
await this.authorize(warehouseDTO, warehouseId); await this.authorize(warehouseDTO, warehouseId);

View File

@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Knex } from 'knex'; import { Knex } from 'knex';
import { import {
IWarehouseMarkAsPrimaryPayload, IWarehouseMarkAsPrimaryPayload,
@@ -12,6 +12,7 @@ import { events } from '@/common/events/events';
@Injectable() @Injectable()
export class WarehouseMarkPrimary { export class WarehouseMarkPrimary {
constructor( constructor(
@Inject(Warehouse.name)
private readonly warehouseModel: typeof Warehouse, private readonly warehouseModel: typeof Warehouse,
private readonly uow: UnitOfWork, private readonly uow: UnitOfWork,
private readonly eventPublisher: EventEmitter2, private readonly eventPublisher: EventEmitter2,

View File

@@ -1,23 +1,17 @@
// import { Model } from 'objection'; // import { Model } from 'objection';
import { BaseModel } from '@/models/Model'; import { BaseModel } from '@/models/Model';
import { Item } from '@/modules/Items/models/Item';
export class Warehouse extends BaseModel { export class Warehouse extends BaseModel {
date!: Date; name!: string;
code!: string;
fromWarehouseId!: number; city!: string;
toWarehouseId!: number; country!: string;
address!: string;
reason!: string;
transactionNumber!: string;
transferInitiatedAt!: Date;
transferDeliveredAt!: Date;
isInitiated!: boolean;
isTransferred!: boolean;
primary!: boolean; primary!: boolean;
items!: Item[];
/** /**
* Table name. * Table name.
*/ */

View File

@@ -0,0 +1,71 @@
import * as request from 'supertest';
import { faker } from '@faker-js/faker';
import { app } from './init-app-test';
describe('Branches (e2e)', () => {
it('/branches (POST)', () => {
return request(app.getHttpServer())
.post('/branches')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
})
.expect(201);
});
it('/branches/:id (DELETE)', async () => {
const response = await request(app.getHttpServer())
.post('/branches')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
});
const branchId = response.body.id;
return request(app.getHttpServer())
.delete(`/branches/${branchId}`)
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
it('/branches/:id (PUT)', async () => {
const response = await request(app.getHttpServer())
.post('/branches')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
});
const branchId = response.body.id;
return request(app.getHttpServer())
.put(`/branches/${branchId}`)
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
it('/branches/:id (GET)', async () => {
const response = await request(app.getHttpServer())
.post('/branches')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
});
const branchId = response.body.id;
return request(app.getHttpServer())
.get(`/branches/${branchId}`)
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
it('/branches (GET)', async () => {
return request(app.getHttpServer())
.get('/branches')
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
});

View File

@@ -0,0 +1,71 @@
import * as request from 'supertest';
import { faker } from '@faker-js/faker';
import { app } from './init-app-test';
describe('Warehouses (e2e)', () => {
it('/warehouses (POST)', () => {
return request(app.getHttpServer())
.post('/warehouses')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
})
.expect(201);
});
it('/warehouses/:id (DELETE)', async () => {
const response = await request(app.getHttpServer())
.post('/warehouses')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
});
const warehouseId = response.body.id;
return request(app.getHttpServer())
.delete(`/warehouses/${warehouseId}`)
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
it('/warehouses/:id (PUT)', async () => {
const response = await request(app.getHttpServer())
.post('/warehouses')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
});
const warehouseId = response.body.id;
return request(app.getHttpServer())
.put(`/warehouses/${warehouseId}`)
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
it('/warehouses/:id (GET)', async () => {
const response = await request(app.getHttpServer())
.post('/warehouses')
.set('organization-id', '4064541lv40nhca')
.send({
name: faker.commerce.productName(),
code: faker.string.alpha(4),
});
const warehouseId = response.body.id;
return request(app.getHttpServer())
.get(`/warehouses/${warehouseId}`)
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
it('/warehouses (GET)', async () => {
return request(app.getHttpServer())
.get('/warehouses')
.set('organization-id', '4064541lv40nhca')
.expect(200);
});
});