progress track isue for interview level 1 fixed
This commit is contained in:
parent
a9018ab0ff
commit
b6938abc7c
@ -154,7 +154,7 @@ export const syncApplicationProgress = async (applicationId: string, overallStat
|
|||||||
// Statuses that imply the CURRENT stage (single or both parallel) is finished
|
// Statuses that imply the CURRENT stage (single or both parallel) is finished
|
||||||
const completionStatuses = [
|
const completionStatuses = [
|
||||||
'Submitted', 'Questionnaire Completed', 'Shortlisted', 'Level 1 Approved',
|
'Submitted', 'Questionnaire Completed', 'Shortlisted', 'Level 1 Approved',
|
||||||
'Level 2 Approved', 'Level 3 Approved',
|
'Level 2 Approved', 'Level 2 Recommended', 'Level 3 Approved',
|
||||||
'EOR Complete', 'Inauguration', 'Approved', 'Onboarded'
|
'EOR Complete', 'Inauguration', 'Approved', 'Onboarded'
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -163,46 +163,72 @@ export const syncApplicationProgress = async (applicationId: string, overallStat
|
|||||||
// Fetch application to check model-driven parallel status
|
// Fetch application to check model-driven parallel status
|
||||||
const application = await db.Application.findByPk(applicationId);
|
const application = await db.Application.findByPk(applicationId);
|
||||||
|
|
||||||
// Robust Sync: Prepare ALL stages for batch processing
|
// Robust Sync: Prepare ALL stages for batch processing
|
||||||
const upsertData = [];
|
const upsertData: any[] = [];
|
||||||
for (const stage of ONBOARDING_STAGES) {
|
for (const stage of ONBOARDING_STAGES) {
|
||||||
let status: 'pending' | 'active' | 'completed' = 'pending';
|
let status: 'pending' | 'active' | 'completed' = 'pending';
|
||||||
let percentage = 0;
|
let percentage = 0;
|
||||||
|
|
||||||
if (stage.order < currentStage.order) {
|
if (stage.order < currentStage.order) {
|
||||||
status = 'completed';
|
status = 'completed';
|
||||||
percentage = 100;
|
percentage = 100;
|
||||||
} else if (stage.order === currentStage.order) {
|
} else if (stage.order === currentStage.order) {
|
||||||
status = isCurrentStageFinished ? 'completed' : 'active';
|
status = isCurrentStageFinished ? 'completed' : 'active';
|
||||||
percentage = isCurrentStageFinished ? 100 : 50;
|
percentage = isCurrentStageFinished ? 100 : 50;
|
||||||
|
|
||||||
if (stage.name === 'Architecture Work' && application) {
|
if (stage.name === 'Architecture Work' && application) {
|
||||||
status = application.architectureStatus === 'COMPLETED' ? 'completed' :
|
status = application.architectureStatus === 'COMPLETED' ? 'completed' :
|
||||||
(application.architectureStatus === 'IN_PROGRESS' || currentStage.name === 'Architecture Work' || isCurrentStageFinished) ? 'active' : 'pending';
|
(application.architectureStatus === 'IN_PROGRESS' || currentStage.name === 'Architecture Work' || isCurrentStageFinished) ? 'active' : 'pending';
|
||||||
percentage = status === 'completed' ? 100 : status === 'active' ? 50 : 0;
|
percentage = status === 'completed' ? 100 : status === 'active' ? 50 : 0;
|
||||||
|
}
|
||||||
|
if (stage.name === 'Statutory Work' && application) {
|
||||||
|
status = application.statutoryStatus === 'COMPLETED' ? 'completed' :
|
||||||
|
(application.statutoryStatus === 'IN_PROGRESS' || currentStage.name === 'Statutory Work' || isCurrentStageFinished) ? 'active' : 'pending';
|
||||||
|
percentage = status === 'completed' ? 100 : status === 'active' ? 50 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
upsertData.push({
|
||||||
|
applicationId,
|
||||||
|
stageName: stage.name,
|
||||||
|
stageOrder: stage.order,
|
||||||
|
status,
|
||||||
|
completionPercentage: percentage,
|
||||||
|
stageStartedAt: (status === 'active' || status === 'completed') ? new Date() : null,
|
||||||
|
stageCompletedAt: status === 'completed' ? new Date() : null
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (stage.name === 'Statutory Work' && application) {
|
|
||||||
status = application.statutoryStatus === 'COMPLETED' ? 'completed' :
|
// DB Duplication Prevention without Schema Changes (Healing corrupted data loops)
|
||||||
(application.statutoryStatus === 'IN_PROGRESS' || currentStage.name === 'Statutory Work' || isCurrentStageFinished) ? 'active' : 'pending';
|
const existingRecords = await ApplicationProgress.findAll({ where: { applicationId } });
|
||||||
percentage = status === 'completed' ? 100 : status === 'active' ? 50 : 0;
|
const seenStages = new Set<string>();
|
||||||
|
|
||||||
|
// Purge any ghost duplicates created by old logic
|
||||||
|
for (const record of existingRecords) {
|
||||||
|
if (seenStages.has(record.stageName)) {
|
||||||
|
await record.destroy();
|
||||||
|
} else {
|
||||||
|
seenStages.add(record.stageName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
upsertData.push({
|
// Perform single row updates/inserts to enforce exact 1:1 mapping safely
|
||||||
applicationId,
|
const cleanedRecords = await ApplicationProgress.findAll({ where: { applicationId } });
|
||||||
stageName: stage.name,
|
|
||||||
stageOrder: stage.order,
|
|
||||||
status,
|
|
||||||
completionPercentage: percentage,
|
|
||||||
stageStartedAt: (status === 'active' || status === 'completed') ? new Date() : null,
|
|
||||||
stageCompletedAt: status === 'completed' ? new Date() : null
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use bulkCreate with updateOnDuplicate to perform an efficient batch upsert
|
for (const data of upsertData) {
|
||||||
await ApplicationProgress.bulkCreate(upsertData, {
|
const existing = cleanedRecords.find((r: any) => r.stageName === data.stageName);
|
||||||
updateOnDuplicate: ['status', 'completionPercentage', 'stageStartedAt', 'stageCompletedAt']
|
if (existing) {
|
||||||
});
|
await existing.update({
|
||||||
|
stageOrder: data.stageOrder,
|
||||||
|
status: data.status,
|
||||||
|
completionPercentage: data.completionPercentage,
|
||||||
|
stageStartedAt: data.stageStartedAt || existing.stageStartedAt,
|
||||||
|
stageCompletedAt: data.stageCompletedAt || existing.stageCompletedAt
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await ApplicationProgress.create(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user