|
| 1 | +import { |
| 2 | + Injectable, |
| 3 | + NotFoundException, |
| 4 | + ConflictException, |
| 5 | +} from '@nestjs/common'; |
| 6 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 7 | +import { Repository, IsNull } from 'typeorm'; |
| 8 | +import { AssetAssignment } from './entities/asset-assignment.entity'; |
| 9 | +import { CreateAssignmentDto } from './dto/create-assignment.dto'; |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class AssetAssignmentsService { |
| 13 | + constructor( |
| 14 | + @InjectRepository(AssetAssignment) |
| 15 | + private readonly assignmentRepository: Repository<AssetAssignment>, |
| 16 | + ) {} |
| 17 | + |
| 18 | + async assign(createDto: CreateAssignmentDto): Promise<AssetAssignment> { |
| 19 | + // 1. Check if the asset is already actively assigned |
| 20 | + const existingAssignment = await this.assignmentRepository.findOne({ |
| 21 | + where: { |
| 22 | + assetId: createDto.assetId, |
| 23 | + unassignmentDate: IsNull(), |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + if (existingAssignment) { |
| 28 | + throw new ConflictException( |
| 29 | + `Asset with ID "${createDto.assetId}" is already assigned.`, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + if (!createDto.assignedToUserId && !createDto.assignedToDepartmentId) { |
| 34 | + throw new ConflictException( |
| 35 | + 'An assignment must have either a user ID or a department ID.', |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + // 2. Create the new assignment record |
| 40 | + const newAssignment = this.assignmentRepository.create({ |
| 41 | + ...createDto, |
| 42 | + assignmentDate: new Date(), |
| 43 | + }); |
| 44 | + |
| 45 | + return this.assignmentRepository.save(newAssignment); |
| 46 | + } |
| 47 | + |
| 48 | + async unassign(assetId: string): Promise<AssetAssignment> { |
| 49 | + // Find the current active assignment for the asset |
| 50 | + const currentAssignment = await this.assignmentRepository.findOne({ |
| 51 | + where: { |
| 52 | + assetId, |
| 53 | + unassignmentDate: IsNull(), |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + if (!currentAssignment) { |
| 58 | + throw new NotFoundException( |
| 59 | + `No active assignment found for asset with ID "${assetId}".`, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // Mark the assignment as historical by setting the unassignment date |
| 64 | + currentAssignment.unassignmentDate = new Date(); |
| 65 | + return this.assignmentRepository.save(currentAssignment); |
| 66 | + } |
| 67 | + |
| 68 | + async getHistoryForAsset(assetId: string): Promise<AssetAssignment[]> { |
| 69 | + return this.assignmentRepository.find({ |
| 70 | + where: { assetId }, |
| 71 | + order: { assignmentDate: 'DESC' }, // Show the most recent first |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + async getCurrentAssignmentForAsset( |
| 76 | + assetId: string, |
| 77 | + ): Promise<AssetAssignment> { |
| 78 | + const assignment = await this.assignmentRepository.findOne({ |
| 79 | + where: { assetId, unassignmentDate: IsNull() }, |
| 80 | + }); |
| 81 | + if (!assignment) { |
| 82 | + throw new NotFoundException( |
| 83 | + `No active assignment found for asset ID "${assetId}".`, |
| 84 | + ); |
| 85 | + } |
| 86 | + return assignment; |
| 87 | + } |
| 88 | +} |
0 commit comments