690402:2240 fix dashboard
This commit is contained in:
@@ -776,127 +776,6 @@ export class CorrespondenceService {
|
||||
await recipientRepo.save(newRecipients);
|
||||
}
|
||||
|
||||
// 6. Regenerate Document Number if structural fields changed (Recipient, Discipline, Type, Project)
|
||||
// AND it is a DRAFT.
|
||||
|
||||
// Fetch fresh data for context and comparison
|
||||
const currentCorr = await this.correspondenceRepo.findOne({
|
||||
where: { id },
|
||||
relations: ['type', 'recipients', 'recipients.recipientOrganization'],
|
||||
});
|
||||
|
||||
if (currentCorr) {
|
||||
const currentToRecipient = currentCorr.recipients?.find(
|
||||
(r) => r.recipientType === 'TO'
|
||||
);
|
||||
const currentRecipientId = currentToRecipient?.recipientOrganizationId;
|
||||
|
||||
// Check for ACTUAL value changes
|
||||
const isProjectChanged =
|
||||
updResolvedProjectId !== undefined &&
|
||||
updResolvedProjectId !== currentCorr.projectId;
|
||||
const isOriginatorChanged =
|
||||
updResolvedOriginatorId !== undefined &&
|
||||
updResolvedOriginatorId !== currentCorr.originatorId;
|
||||
const isDisciplineChanged =
|
||||
updateDto.disciplineId !== undefined &&
|
||||
updateDto.disciplineId !== currentCorr.disciplineId;
|
||||
const isTypeChanged =
|
||||
updateDto.typeId !== undefined &&
|
||||
updateDto.typeId !== currentCorr.correspondenceTypeId;
|
||||
|
||||
let isRecipientChanged = false;
|
||||
let newRecipientId: number | undefined;
|
||||
|
||||
if (updResolvedRecipients) {
|
||||
const newToRecipient = updResolvedRecipients.find(
|
||||
(r) => r.type === 'TO'
|
||||
);
|
||||
newRecipientId = newToRecipient?.organizationId;
|
||||
|
||||
if (newRecipientId !== currentRecipientId) {
|
||||
isRecipientChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
isProjectChanged ||
|
||||
isDisciplineChanged ||
|
||||
isTypeChanged ||
|
||||
isRecipientChanged ||
|
||||
isOriginatorChanged
|
||||
) {
|
||||
const targetRecipientId = isRecipientChanged
|
||||
? newRecipientId
|
||||
: currentRecipientId;
|
||||
|
||||
// Resolve Recipient Code for the NEW context
|
||||
let recipientCode = '';
|
||||
if (targetRecipientId) {
|
||||
const recOrg = await this.dataSource.manager.findOne(Organization, {
|
||||
where: { id: targetRecipientId },
|
||||
});
|
||||
if (recOrg) recipientCode = recOrg.organizationCode;
|
||||
}
|
||||
|
||||
// [Fix #6] Fetch real ORG Code from originator organization
|
||||
const originatorOrgForUpdate = await this.dataSource.manager.findOne(
|
||||
Organization,
|
||||
{
|
||||
where: {
|
||||
id: updResolvedOriginatorId ?? currentCorr.originatorId ?? 0,
|
||||
},
|
||||
}
|
||||
);
|
||||
const orgCode = originatorOrgForUpdate?.organizationCode ?? 'UNK';
|
||||
|
||||
// Prepare Contexts
|
||||
const oldCtx = {
|
||||
projectId: currentCorr.projectId,
|
||||
originatorOrganizationId: currentCorr.originatorId ?? 0,
|
||||
typeId: currentCorr.correspondenceTypeId,
|
||||
disciplineId: currentCorr.disciplineId,
|
||||
recipientOrganizationId: currentRecipientId,
|
||||
year: new Date().getFullYear(),
|
||||
};
|
||||
|
||||
const newCtx = {
|
||||
projectId: updResolvedProjectId ?? currentCorr.projectId,
|
||||
originatorOrganizationId:
|
||||
updResolvedOriginatorId ?? currentCorr.originatorId ?? 0,
|
||||
typeId: updateDto.typeId ?? currentCorr.correspondenceTypeId,
|
||||
disciplineId: updateDto.disciplineId ?? currentCorr.disciplineId,
|
||||
recipientOrganizationId: targetRecipientId,
|
||||
year: new Date().getFullYear(),
|
||||
userId: user.user_id, // Pass User ID for Audit
|
||||
customTokens: {
|
||||
TYPE_CODE: currentCorr.type?.typeCode || '',
|
||||
ORG_CODE: orgCode,
|
||||
RECIPIENT_CODE: recipientCode,
|
||||
REC_CODE: recipientCode,
|
||||
},
|
||||
};
|
||||
|
||||
// If Type Changed, need NEW Type Code
|
||||
if (isTypeChanged) {
|
||||
const newType = await this.typeRepo.findOne({
|
||||
where: { id: newCtx.typeId },
|
||||
});
|
||||
if (newType) newCtx.customTokens.TYPE_CODE = newType.typeCode;
|
||||
}
|
||||
|
||||
const newDocNumber = await this.numberingService.updateNumberForDraft(
|
||||
currentCorr.correspondenceNumber,
|
||||
oldCtx,
|
||||
newCtx
|
||||
);
|
||||
|
||||
await this.correspondenceRepo.update(id, {
|
||||
correspondenceNumber: newDocNumber,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await this.findOne(id);
|
||||
|
||||
// Re-index updated document in Elasticsearch (fire-and-forget)
|
||||
|
||||
Reference in New Issue
Block a user