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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 2x 2x 2x 1x 2x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 3x 2x 1x 1x 1x 1x 1x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 3x 3x 3x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 4x 4x 4x 3x 2x 2x 1x 1x 1x 1x 1x 2x 1x 1x | import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import {
QualityConfiguration,
QualitySpecification,
QualityInspectionPlan,
QmsInspectionLot,
QmsNonConformance,
QmsCapa,
QmsDeviation,
QmsChangeRequest,
QmsAudit,
QmsRiskAssessment,
QmsRecall
} from './schemas';
import { EventBusService } from '../../platform/events/event-bus.service';
@Injectable()
export class QualityService {
constructor(
@InjectModel(QualityConfiguration.name)
private readonly configModel: Model<QualityConfiguration>,
@InjectModel(QualitySpecification.name)
private readonly specModel: Model<QualitySpecification>,
@InjectModel(QualityInspectionPlan.name)
private readonly inspectionPlanModel: Model<QualityInspectionPlan>,
@InjectModel(QmsInspectionLot.name)
private readonly lotModel: Model<QmsInspectionLot>,
@InjectModel(QmsNonConformance.name)
private readonly ncrModel: Model<QmsNonConformance>,
@InjectModel(QmsCapa.name)
private readonly capaModel: Model<QmsCapa>,
@InjectModel(QmsDeviation.name)
private readonly deviationModel: Model<QmsDeviation>,
@InjectModel(QmsChangeRequest.name)
private readonly changeModel: Model<QmsChangeRequest>,
@InjectModel(QmsAudit.name)
private readonly auditModel: Model<QmsAudit>,
@InjectModel(QmsRiskAssessment.name)
private readonly riskModel: Model<QmsRiskAssessment>,
@InjectModel(QmsRecall.name)
private readonly recallModel: Model<QmsRecall>,
private readonly eventBus: EventBusService
) {}
// 1. Quality configuration setup
async getConfiguration(tenantId: string): Promise<QualityConfiguration> {
const tenantObjId = new Types.ObjectId(tenantId);
let config = await this.configModel.findOne({ tenantId: tenantObjId }).exec();
if (!config) {
config = await this.configModel.create({ tenantId: tenantObjId });
}
return config;
}
// 2. Immutable Published Specifications
async createSpecification(tenantId: string, data: any): Promise<QualitySpecification> {
return this.specModel.create({
tenantId: new Types.ObjectId(tenantId),
specificationCode: data.specificationCode || `SPEC-${Date.now()}`,
specificationName: data.specificationName,
characteristics: data.characteristics || [],
status: 'draft'
});
}
async approveSpecification(tenantId: string, specId: string, approverId: string): Promise<QualitySpecification> {
const tenantObjId = new Types.ObjectId(tenantId);
const spec = await this.specModel.findOne({ _id: new Types.ObjectId(specId), tenantId: tenantObjId }).exec();
if (!spec) throw new NotFoundException('Specification not found');
spec.status = 'approved';
spec.approvedBy = new Types.ObjectId(approverId);
spec.approvedAt = new Date();
await spec.save();
await this.eventBus.publish('quality.specification.approved.v1', { specificationId: specId }, tenantId);
return spec;
}
async updateSpecification(tenantId: string, specId: string, updates: any): Promise<QualitySpecification> {
const tenantObjId = new Types.ObjectId(tenantId);
const spec = await this.specModel.findOne({ _id: new Types.ObjectId(specId), tenantId: tenantObjId }).exec();
if (!spec) throw new NotFoundException('Specification not found');
// Immutability Check
if (spec.status === 'approved' || spec.status === 'superseded') {
throw new BadRequestException(`Approved specifications are immutable. Status: ${spec.status}`);
}
Object.assign(spec, updates);
await spec.save();
return spec;
}
// 3. Inspections & Fail lots lock batch
async createInspectionLot(tenantId: string, data: any): Promise<QmsInspectionLot> {
return this.lotModel.create({
tenantId: new Types.ObjectId(tenantId),
lotNumber: `LOT-${Date.now()}`,
sourceType: data.sourceType,
sourceRecordId: new Types.ObjectId(data.sourceRecordId),
status: 'created'
});
}
async recordInspectionResults(tenantId: string, lotId: string, results: any[]): Promise<QmsInspectionLot> {
const tenantObjId = new Types.ObjectId(tenantId);
const lot = await this.lotModel.findOne({ _id: new Types.ObjectId(lotId), tenantId: tenantObjId }).exec();
if (!lot) throw new NotFoundException('Inspection Lot not found');
lot.results = results;
lot.status = 'in_progress';
// Verify if any result fails
const hasFailures = results.some(r => !r.passed);
if (hasFailures) {
lot.status = 'rejected';
await lot.save();
// Emit failed event and batch lock
await this.eventBus.publish('quality.inspection.failed.v1', { lotId, lotNumber: lot.lotNumber }, tenantId);
} else {
lot.status = 'approved';
await lot.save();
await this.eventBus.publish('quality.inspection.accepted.v1', { lotId, lotNumber: lot.lotNumber }, tenantId);
}
return lot;
}
// 4. NCR Disposition Costs
async createNonConformance(tenantId: string, data: any): Promise<QmsNonConformance> {
const ncr = await this.ncrModel.create({
tenantId: new Types.ObjectId(tenantId),
ncrNumber: `NCR-${Date.now()}`,
inspectionLotId: data.inspectionLotId ? new Types.ObjectId(data.inspectionLotId) : undefined,
defectDescription: data.defectDescription,
dispositionType: data.dispositionType || 'hold',
status: 'reported'
});
await this.eventBus.publish('quality.nonconformance.created.v1', { ncrId: ncr._id.toString() }, tenantId);
return ncr;
}
// 5. CAPA Close verification checks
async createCapa(tenantId: string, data: any): Promise<QmsCapa> {
return this.capaModel.create({
tenantId: new Types.ObjectId(tenantId),
capaNumber: `CAPA-${Date.now()}`,
nonConformanceId: new Types.ObjectId(data.nonConformanceId),
correctiveAction: data.correctiveAction,
status: 'draft'
});
}
async verifyCapaEffectiveness(tenantId: string, capaId: string): Promise<QmsCapa> {
const tenantObjId = new Types.ObjectId(tenantId);
const capa = await this.capaModel.findOne({ _id: new Types.ObjectId(capaId), tenantId: tenantObjId }).exec();
if (!capa) throw new NotFoundException('CAPA not found');
capa.effectivenessVerified = true;
await capa.save();
await this.eventBus.publish('quality.capa.effectiveness-verified.v1', { capaId }, tenantId);
return capa;
}
async closeCapa(tenantId: string, capaId: string): Promise<QmsCapa> {
const tenantObjId = new Types.ObjectId(tenantId);
const capa = await this.capaModel.findOne({ _id: new Types.ObjectId(capaId), tenantId: tenantObjId }).exec();
if (!capa) throw new NotFoundException('CAPA not found');
// Effectiveness Verification check
if (!capa.effectivenessVerified) {
throw new BadRequestException('CAPA cannot close without required effectiveness verification.');
}
capa.status = 'closed';
await capa.save();
await this.eventBus.publish('quality.capa.closed.v1', { capaId }, tenantId);
return capa;
}
// 6. Audit checklists questions protection locks
async createAudit(tenantId: string, data: any): Promise<QmsAudit> {
return this.auditModel.create({
tenantId: new Types.ObjectId(tenantId),
auditCode: `AUD-${Date.now()}`,
auditName: data.auditName,
checklist: data.checklist || [],
status: 'planned'
});
}
async issueAuditFindings(tenantId: string, auditId: string): Promise<QmsAudit> {
const tenantObjId = new Types.ObjectId(tenantId);
const audit = await this.auditModel.findOneAndUpdate(
{ _id: new Types.ObjectId(auditId), tenantId: tenantObjId },
{ status: 'findings_issued' },
{ new: true }
).exec();
if (!audit) throw new NotFoundException('Audit not found');
await this.eventBus.publish('quality.audit.finding-created.v1', { auditId }, tenantId);
return audit;
}
async updateAuditChecklistQuestion(tenantId: string, auditId: string, index: number, updates: any): Promise<QmsAudit> {
const tenantObjId = new Types.ObjectId(tenantId);
const audit = await this.auditModel.findOne({ _id: new Types.ObjectId(auditId), tenantId: tenantObjId }).exec();
if (!audit) throw new NotFoundException('Audit not found');
if (!audit.checklist[index]) throw new BadRequestException('Question index out of bounds');
const original = audit.checklist[index];
// Audit Findings Lock Check
if (audit.status === 'findings_issued' && original.isFinding) {
throw new BadRequestException('Audit findings cannot be updated or deleted after issue.');
}
Object.assign(original, updates);
audit.markModified('checklist');
await audit.save();
return audit;
}
// 7. RPN Risk Matrix calculation
async calculateFmeaRPN(tenantId: string, data: { severity: number; occurrence: number; detection: number }): Promise<number> {
if (data.severity < 1 || data.severity > 10 || data.occurrence < 1 || data.occurrence > 10 || data.detection < 1 || data.detection > 10) {
throw new BadRequestException('FMEA rating parameters must be between 1 and 10');
}
return data.severity * data.occurrence * data.detection;
}
}
|