|
| 1 | +import { Injectable, NotFoundException } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { Repository, In } from 'typeorm'; |
| 4 | +import { Asset } from './entities/asset.entity'; |
| 5 | +import { AssetNote } from './entities/asset-note.entity'; |
| 6 | +import { CreateNoteDto } from './dtos/create-note.dto'; |
| 7 | +import { BulkStatusDto } from './dtos/bulk-status.dto'; |
| 8 | +import { BulkDeleteDto } from './dtos/bulk-delete.dto'; |
| 9 | +import { BulkTransferDto } from './dtos/bulk-transfer.dto'; |
| 10 | +import * as QRCode from 'qrcode'; |
| 11 | +import * as bwipjs from 'bwip-js'; |
| 12 | + |
| 13 | +@Injectable() |
| 14 | +export class AssetsOpsService { |
| 15 | + constructor( |
| 16 | + @InjectRepository(Asset) |
| 17 | + private readonly assetRepository: Repository<Asset>, |
| 18 | + @InjectRepository(AssetNote) |
| 19 | + private readonly noteRepository: Repository<AssetNote>, |
| 20 | + ) {} |
| 21 | + |
| 22 | + async createNote(assetId: string, dto: CreateNoteDto, userId?: string): Promise<AssetNote> { |
| 23 | + const asset = await this.assetRepository.findOne({ where: { id: assetId } }); |
| 24 | + if (!asset) throw new NotFoundException('Asset not found'); |
| 25 | + |
| 26 | + const note = this.noteRepository.create({ |
| 27 | + assetId, |
| 28 | + content: dto.content, |
| 29 | + createdById: userId, |
| 30 | + }); |
| 31 | + return this.noteRepository.save(note); |
| 32 | + } |
| 33 | + |
| 34 | + async getNotes(assetId: string): Promise<AssetNote[]> { |
| 35 | + return this.noteRepository.find({ |
| 36 | + where: { assetId }, |
| 37 | + relations: ['createdBy'], |
| 38 | + order: { createdAt: 'DESC' }, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + async deleteNote(assetId: string, noteId: string): Promise<void> { |
| 43 | + const note = await this.noteRepository.findOne({ where: { id: noteId, assetId } }); |
| 44 | + if (!note) throw new NotFoundException('Note not found'); |
| 45 | + await this.noteRepository.remove(note); |
| 46 | + } |
| 47 | + |
| 48 | + async generateQRCode(assetId: string): Promise<string> { |
| 49 | + const asset = await this.assetRepository.findOne({ where: { id: assetId } }); |
| 50 | + if (!asset) throw new NotFoundException('Asset not found'); |
| 51 | + |
| 52 | + const qrData = JSON.stringify({ id: asset.id, assetId: asset.assetId, name: asset.name }); |
| 53 | + const qrCode = await QRCode.toDataURL(qrData); |
| 54 | + |
| 55 | + await this.assetRepository.update(assetId, { qrCode }); |
| 56 | + return qrCode; |
| 57 | + } |
| 58 | + |
| 59 | + async generateBarcode(assetId: string): Promise<string> { |
| 60 | + const asset = await this.assetRepository.findOne({ where: { id: assetId } }); |
| 61 | + if (!asset) throw new NotFoundException('Asset not found'); |
| 62 | + |
| 63 | + const barcode = await new Promise<string>((resolve, reject) => { |
| 64 | + bwipjs.toBuffer({ |
| 65 | + bcid: 'code128', |
| 66 | + text: asset.assetId, |
| 67 | + scale: 3, |
| 68 | + height: 10, |
| 69 | + includetext: true, |
| 70 | + textxalign: 'center', |
| 71 | + }, (err: Error | null, buffer?: Buffer) => { |
| 72 | + if (err) reject(err); |
| 73 | + else resolve(`data:image/png;base64,${buffer.toString('base64')}`); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + await this.assetRepository.update(assetId, { barcode }); |
| 78 | + return barcode; |
| 79 | + } |
| 80 | + |
| 81 | + async bulkStatusUpdate(dto: BulkStatusDto, userId?: string): Promise<number> { |
| 82 | + const result = await this.assetRepository.update( |
| 83 | + { id: In(dto.ids) }, |
| 84 | + { status: dto.status, updatedById: userId }, |
| 85 | + ); |
| 86 | + return result.affected || 0; |
| 87 | + } |
| 88 | + |
| 89 | + async bulkDelete(dto: BulkDeleteDto): Promise<number> { |
| 90 | + const result = await this.assetRepository.softDelete({ id: In(dto.ids) }); |
| 91 | + return result.affected || 0; |
| 92 | + } |
| 93 | + |
| 94 | + async bulkTransfer(dto: BulkTransferDto, userId?: string): Promise<number> { |
| 95 | + const updateData: Partial<Asset> = { updatedById: userId }; |
| 96 | + if (dto.assignedToId) updateData.assignedToId = dto.assignedToId; |
| 97 | + if (dto.departmentId) updateData.departmentId = dto.departmentId; |
| 98 | + if (dto.location) updateData.location = dto.location; |
| 99 | + |
| 100 | + const result = await this.assetRepository.update( |
| 101 | + { id: In(dto.ids) }, |
| 102 | + updateData, |
| 103 | + ); |
| 104 | + return result.affected || 0; |
| 105 | + } |
| 106 | + |
| 107 | + async bulkExport(ids?: string[]) { |
| 108 | + const where = ids ? { id: In(ids) } : {}; |
| 109 | + return this.assetRepository.find({ where, relations: ['assignedTo', 'createdBy'] }); |
| 110 | + } |
| 111 | +} |
0 commit comments