Skip to content

Commit e3983ef

Browse files
committed
Recalculate org value on warehouse delete
Fixes: #11
1 parent 01c49ea commit e3983ef

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

apps/api/src/models/warehouses/warehouses.controller.spec.ts

+31-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { InventoryService } from '../inventory/inventory.service';
66
import { OrganizationsService } from '../organizations/organizations.service';
77
import { WarehousesController } from './warehouses.controller';
88
import { WarehousesService } from './warehouses.service';
9+
import { OrganizationsStatsService } from '../organizations/organizations-stats.service';
910

1011
const MOCK_TAKEN_ORG_ID = new Types.ObjectId('62a23958e5a9e9b88f853a67');
1112
const MOCK_FREE_ORG_ID = new Types.ObjectId('657047c4e0cecd73abbad627');
@@ -50,14 +51,28 @@ describe('WarehousesController', () => {
5051
};
5152

5253
const mockOrganizationsService = {
53-
addWarehouseReference: jest.fn(),
54-
deleteWarehouseReference: jest.fn(),
55-
updateWarehouseReference: jest.fn(),
54+
addWarehouseReference: jest.fn(() => ({
55+
_id: MOCK_TAKEN_ORG_ID,
56+
})),
57+
deleteWarehouseReference: jest.fn(() => ({
58+
_id: MOCK_TAKEN_ORG_ID,
59+
})),
60+
updateWarehouseReference: jest.fn(() => ({
61+
_id: MOCK_TAKEN_ORG_ID,
62+
})),
5663
exist: jest.fn((id: Types.ObjectId) => {
5764
return id.toString() == MOCK_TAKEN_ORG_ID.toString();
5865
}),
5966
};
6067

68+
const mockOrganizationsStatsService = {
69+
recalculateTotalValue: jest.fn(),
70+
};
71+
72+
const mockInventoryService = {
73+
deleteManyByWarehouse: jest.fn(),
74+
};
75+
6176
const addWarehouseReferenceSpy = jest.spyOn(mockOrganizationsService, 'addWarehouseReference');
6277
const deleteWarehouseReferenceSpy = jest.spyOn(
6378
mockOrganizationsService,
@@ -67,22 +82,29 @@ describe('WarehousesController', () => {
6782
mockOrganizationsService,
6883
'updateWarehouseReference',
6984
);
70-
71-
const mockInventoryService = {
72-
deleteManyByWarehouse: jest.fn(),
73-
};
85+
const recalculateTotalValueSpy = jest.spyOn(
86+
mockOrganizationsStatsService,
87+
'recalculateTotalValue',
88+
);
7489

7590
beforeEach(async () => {
7691
const module: TestingModule = await Test.createTestingModule({
7792
controllers: [WarehousesController],
78-
providers: [WarehousesService, OrganizationsService, InventoryService],
93+
providers: [
94+
WarehousesService,
95+
OrganizationsService,
96+
OrganizationsStatsService,
97+
InventoryService,
98+
],
7999
})
80100
.overrideProvider(WarehousesService)
81101
.useValue(mockWarehouseService)
82102
.overrideProvider(OrganizationsService)
83103
.useValue(mockOrganizationsService)
84104
.overrideProvider(InventoryService)
85105
.useValue(mockInventoryService)
106+
.overrideProvider(OrganizationsStatsService)
107+
.useValue(mockOrganizationsStatsService)
86108
.compile();
87109

88110
controller = module.get<WarehousesController>(WarehousesController);
@@ -191,6 +213,7 @@ describe('WarehousesController', () => {
191213
}),
192214
);
193215
expect(deleteWarehouseReferenceSpy).toHaveBeenCalledWith(MOCK_TAKEN_WAREHOUSE_ID);
216+
expect(recalculateTotalValueSpy).toHaveBeenCalledWith(MOCK_TAKEN_ORG_ID);
194217
});
195218

196219
it('should not delete warehouse that does not exist', () => {

apps/api/src/models/warehouses/warehouses.controller.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import { InventoryService } from '../inventory/inventory.service';
1616
import { OrganizationsService } from '../organizations/organizations.service';
1717
import { Warehouse } from './schemas/warehouse.schema';
1818
import { WarehousesService } from './warehouses.service';
19+
import { OrganizationsStatsService } from '../organizations/organizations-stats.service';
1920

2021
@Controller('warehouses')
2122
export class WarehousesController {
2223
constructor(
2324
private readonly warehousesService: WarehousesService,
2425
private readonly organizationsService: OrganizationsService,
26+
private readonly organizationsStatsService: OrganizationsStatsService,
2527
private readonly inventoryService: InventoryService,
2628
) {}
2729

@@ -72,8 +74,9 @@ export class WarehousesController {
7274
throw new NotFoundException();
7375
}
7476

75-
await this.organizationsService.deleteWarehouseReference(warehouse._id);
77+
const org = await this.organizationsService.deleteWarehouseReference(warehouse._id);
7678
await this.inventoryService.deleteManyByWarehouse(warehouse._id);
79+
await this.organizationsStatsService.recalculateTotalValue(org._id);
7780

7881
return Warehouse.toDto(warehouse);
7982
}

0 commit comments

Comments
 (0)