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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | import { Injectable, Logger, NotFoundException, BadRequestException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { MasterDataVersion } from '../../infrastructure/schemas/master-data-record.schema'; import { MasterDataRecord } from '../../infrastructure/schemas/master-data-record.schema'; @Injectable() export class MasterDataVersionService { private readonly logger = new Logger(MasterDataVersionService.name); constructor( @InjectModel(MasterDataVersion.name) private readonly versionModel: Model<MasterDataVersion>, @InjectModel(MasterDataRecord.name) private readonly recordModel: Model<MasterDataRecord>, ) {} async createSnapshot( recordId: string, changeReason?: string, publishedBy?: string, ): Promise<any> { const record = await this.recordModel.findById(recordId).lean().exec(); if (!record) throw new NotFoundException(`Record not found: ${recordId}`); const lastVersion = await this.versionModel .findOne({ recordId: record._id }) .sort({ versionNumber: -1 }) .lean() .exec(); const nextVersion = (lastVersion?.versionNumber || 0) + 1; // Supersede previous version if (lastVersion) { await this.versionModel.updateOne( { _id: lastVersion._id }, { $set: { status: 'superseded', effectiveTo: new Date() } }, ); } const version = await this.versionModel.create({ recordId: record._id, definitionKey: record.definitionKey, recordCode: record.recordCode, versionNumber: nextVersion, snapshotData: { ...record, _id: undefined }, changeReason, status: 'published', publishedBy, publishedAt: new Date(), effectiveFrom: new Date(), }); // Update record version counter await this.recordModel.updateOne( { _id: record._id }, { $set: { version: nextVersion, currentVersionId: version._id } }, ); this.logger.log( `Version ${nextVersion} created for ${record.definitionKey}/${record.recordCode}`, ); return this.toDTO(version); } async getVersionHistory(recordId: string): Promise<any[]> { const versions = await this.versionModel .find({ recordId }) .sort({ versionNumber: -1 }) .lean() .exec(); return versions.map((v) => this.toDTO(v)); } async getVersion(recordId: string, versionNumber: number): Promise<any> { const version = await this.versionModel .findOne({ recordId, versionNumber }) .lean() .exec(); if (!version) throw new NotFoundException( `Version ${versionNumber} not found for record ${recordId}`, ); return this.toDTO(version); } async rollback( recordId: string, targetVersionNumber: number, userId: string, ): Promise<any> { const version = await this.versionModel .findOne({ recordId, versionNumber: targetVersionNumber }) .lean() .exec(); if (!version) throw new NotFoundException(`Version ${targetVersionNumber} not found`); const record = await this.recordModel.findById(recordId).exec(); if (!record) throw new NotFoundException(`Record not found: ${recordId}`); if (record.systemManaged) throw new BadRequestException('Cannot rollback a system-managed record'); // Apply snapshot data back to the record const snapshot = version.snapshotData || {}; record.recordName = snapshot.recordName || record.recordName; record.shortName = snapshot.shortName; record.description = snapshot.description; record.attributes = snapshot.attributes || {}; record.metadata = snapshot.metadata || {}; record.updatedBy = userId as any; await record.save(); // Create a new version reflecting the rollback await this.createSnapshot( recordId, `Rollback to version ${targetVersionNumber}`, userId, ); this.logger.log( `Rolled back record ${recordId} to version ${targetVersionNumber}`, ); return this.toDTO(record); } private toDTO(doc: any): any { const obj = doc.toObject ? doc.toObject() : doc; return { id: obj._id?.toString(), recordId: obj.recordId?.toString(), definitionKey: obj.definitionKey, recordCode: obj.recordCode, versionNumber: obj.versionNumber, snapshotData: obj.snapshotData, changeReason: obj.changeReason, status: obj.status, publishedBy: obj.publishedBy?.toString(), publishedAt: obj.publishedAt, effectiveFrom: obj.effectiveFrom, effectiveTo: obj.effectiveTo, createdAt: obj.createdAt, }; } } |