Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Warehouse, StorageLocation } from '../schemas/warehouse.schema'; @Injectable() export class WarehouseService { constructor( @InjectModel(Warehouse.name) private readonly warehouseModel: Model<Warehouse>, @InjectModel(StorageLocation.name) private readonly locationModel: Model<StorageLocation> ) {} async createWarehouse(tenantId: string, data: any): Promise<Warehouse> { const existing = await this.warehouseModel.findOne({ tenantId, warehouseCode: data.warehouseCode }).exec(); if (existing) { throw new BadRequestException(`Warehouse with code ${data.warehouseCode} already exists`); } // Circular Hierarchy check if (data.parentWarehouseId) { let currentParentId = data.parentWarehouseId; while (currentParentId) { const parent = await this.warehouseModel.findOne({ tenantId, _id: currentParentId }).exec(); if (!parent) break; if (parent.warehouseCode === data.warehouseCode) { throw new BadRequestException('Circular hierarchy detected in warehouse relationship'); } currentParentId = parent.parentWarehouseId; } } return this.warehouseModel.create({ ...data, tenantId, active: true, }); } async getWarehouses(tenantId: string): Promise<Warehouse[]> { return this.warehouseModel.find({ tenantId }).exec(); } async getWarehouseById(tenantId: string, id: string): Promise<Warehouse> { const wh = await this.warehouseModel.findOne({ tenantId, _id: id }).exec(); if (!wh) throw new NotFoundException('Warehouse not found'); return wh; } async createLocation(tenantId: string, data: any): Promise<StorageLocation> { const wh = await this.getWarehouseById(tenantId, data.warehouseId); if (!wh.active) { throw new BadRequestException(`Cannot create locations on inactive warehouse ${wh.warehouseCode}`); } return this.locationModel.create({ ...data, tenantId, active: true, }); } async getLocations(tenantId: string, warehouseId: string): Promise<StorageLocation[]> { return this.locationModel.find({ tenantId, warehouseId }).exec(); } } |