@@ -10,6 +10,7 @@ import { Repository, DataSource } from 'typeorm';
1010import { Asset , AssetStatus } from './entities/asset.entity' ;
1111import { AssetCategory } from '../asset-categories/asset-category.entity' ;
1212import { Department } from '../departments/department.entity' ;
13+ import { Location } from '../locations/location.entity' ;
1314import { User } from '../users/entities/user.entity' ;
1415import { CreateAssetDto , BulkCreateAssetDto } from './dto/create-asset.dto' ;
1516import {
@@ -38,6 +39,8 @@ export class AssetsService {
3839 private readonly categoryRepository : Repository < AssetCategory > ,
3940 @InjectRepository ( Department )
4041 private readonly departmentRepository : Repository < Department > ,
42+ @InjectRepository ( Location )
43+ private readonly locationRepository : Repository < Location > ,
4144 @InjectRepository ( User )
4245 private readonly userRepository : Repository < User > ,
4346 private readonly dataSource : DataSource ,
@@ -67,6 +70,18 @@ export class AssetsService {
6770 ) ;
6871 }
6972
73+ let location = null ;
74+ if ( createAssetDto . locationId ) {
75+ location = await this . locationRepository . findOne ( {
76+ where : { id : createAssetDto . locationId } ,
77+ } ) ;
78+ if ( ! location ) {
79+ throw new NotFoundException (
80+ `Location with ID ${ createAssetDto . locationId } not found` ,
81+ ) ;
82+ }
83+ }
84+
7085 let assignedUser = null ;
7186 if ( createAssetDto . assignedToId ) {
7287 assignedUser = await this . userRepository . findOne ( {
@@ -123,7 +138,7 @@ export class AssetsService {
123138 assetId,
124139 category,
125140 department,
126- location : createAssetDto . location ,
141+ location,
127142 assignedTo : assignedUser ,
128143 currentValue,
129144 createdBy : creatingUser ,
@@ -154,7 +169,7 @@ export class AssetsService {
154169 sortOrder = 'DESC' ,
155170 categoryId,
156171 departmentId,
157- location ,
172+ locationId ,
158173 assignedToId,
159174 status,
160175 condition,
@@ -169,6 +184,7 @@ export class AssetsService {
169184 . createQueryBuilder ( 'asset' )
170185 . leftJoinAndSelect ( 'asset.category' , 'category' )
171186 . leftJoinAndSelect ( 'asset.department' , 'department' )
187+ . leftJoinAndSelect ( 'asset.location' , 'location' )
172188 . leftJoinAndSelect ( 'asset.assignedTo' , 'assignedTo' )
173189 . leftJoinAndSelect ( 'asset.createdBy' , 'createdBy' )
174190 . leftJoinAndSelect ( 'asset.updatedBy' , 'updatedBy' ) ;
@@ -188,10 +204,8 @@ export class AssetsService {
188204 queryBuilder . andWhere ( 'asset.departmentId = :departmentId' , { departmentId } ) ;
189205 }
190206
191- if ( location ) {
192- queryBuilder . andWhere ( 'asset.location ILIKE :location' , {
193- location : `%${ location } %` ,
194- } ) ;
207+ if ( locationId ) {
208+ queryBuilder . andWhere ( 'asset.locationId = :locationId' , { locationId } ) ;
195209 }
196210
197211 if ( assignedToId ) {
@@ -302,6 +316,17 @@ export class AssetsService {
302316 }
303317 }
304318
319+ if ( updateAssetDto . locationId ) {
320+ const location = await this . locationRepository . findOne ( {
321+ where : { id : updateAssetDto . locationId } ,
322+ } ) ;
323+ if ( ! location ) {
324+ throw new NotFoundException (
325+ `Location with ID ${ updateAssetDto . locationId } not found` ,
326+ ) ;
327+ }
328+ }
329+
305330 if ( updateAssetDto . assignedToId ) {
306331 const assignedUser = await this . userRepository . findOne ( {
307332 where : { id : updateAssetDto . assignedToId } ,
0 commit comments