fix duplication and ticket disappear in adminklinik loket
This commit is contained in:
@@ -1367,34 +1367,64 @@ const fetchPatientsForRoom = async (ruang) => {
|
||||
|
||||
/**
|
||||
* Merge API patients into queueStore.allPatients
|
||||
* SMART MERGE: Preserves local updates (status, call indicators) while refreshing API data
|
||||
*/
|
||||
const mergeApiPatientsToStore = () => {
|
||||
if (apiPatients.value.length === 0) return;
|
||||
|
||||
// Build a Set of unique identifiers from API patients for fast lookup
|
||||
const apiPatientIds = new Set(
|
||||
apiPatients.value.map(p => p.visitId || p.visitCode || p.barcode || p.noAntrian).filter(Boolean)
|
||||
);
|
||||
|
||||
// Remove duplicates: remove any patient (API or not) that matches visitId, visitCode, barcode, or noAntrian
|
||||
queueStore.allPatients = queueStore.allPatients.filter(p => {
|
||||
const patientId = p.visitId || p.visitCode || p.barcode || p.noAntrian;
|
||||
const isDuplicate = apiPatientIds.has(patientId);
|
||||
|
||||
if (isDuplicate) {
|
||||
console.log('🗑️ Removing duplicate patient:', {
|
||||
ticket: p.noAntrian,
|
||||
barcode: p.barcode,
|
||||
visitId: p.visitId,
|
||||
registrationType: p.registrationType
|
||||
});
|
||||
}
|
||||
|
||||
return !isDuplicate;
|
||||
// Build a Map of API patients by identifier for fast lookup
|
||||
const apiPatientMap = new Map();
|
||||
apiPatients.value.forEach(apiP => {
|
||||
const id = apiP.visitId || apiP.visitCode || apiP.barcode || apiP.noAntrian;
|
||||
if (id) apiPatientMap.set(id, apiP);
|
||||
});
|
||||
|
||||
// Add new API patients
|
||||
queueStore.allPatients.push(...apiPatients.value);
|
||||
// Update existing patients OR preserve local changes
|
||||
const updatedPatients = queueStore.allPatients.map(localP => {
|
||||
const id = localP.visitId || localP.visitCode || localP.barcode || localP.noAntrian;
|
||||
const apiP = apiPatientMap.get(id);
|
||||
|
||||
if (!apiP) return localP; // No API match, keep local patient
|
||||
|
||||
// Found API match - MERGE data intelligently
|
||||
// Check if patient has local UI updates that should be preserved
|
||||
const hasLocalUpdates = (
|
||||
localP.status !== 'pemeriksaan' || // Status changed from default
|
||||
localP.calledPemeriksaanAwal || // Called for pemeriksaan awal
|
||||
localP.calledTindakan || // Called for tindakan
|
||||
localP.lastCalledAt // Has call timestamp
|
||||
);
|
||||
|
||||
if (hasLocalUpdates) {
|
||||
console.log('✅ Preserving local updates for:', localP.noAntrian);
|
||||
// Keep local patient data but refresh certain API fields
|
||||
return {
|
||||
...apiP, // Base API data (fresh barcode, noAntrian, etc)
|
||||
status: localP.status, // Preserve local status
|
||||
calledPemeriksaanAwal: localP.calledPemeriksaanAwal,
|
||||
calledTindakan: localP.calledTindakan,
|
||||
tipeLayanan: localP.tipeLayanan,
|
||||
lastCalledAt: localP.lastCalledAt,
|
||||
lastCalledTipeLayanan: localP.lastCalledTipeLayanan
|
||||
};
|
||||
} else {
|
||||
// No local updates, use fresh API data
|
||||
return apiP;
|
||||
}
|
||||
});
|
||||
|
||||
// Find NEW API patients (not in allPatients)
|
||||
const existingIds = new Set(
|
||||
queueStore.allPatients.map(p => p.visitId || p.visitCode || p.barcode || p.noAntrian).filter(Boolean)
|
||||
);
|
||||
|
||||
const newApiPatients = apiPatients.value.filter(apiP => {
|
||||
const id = apiP.visitId || apiP.visitCode || apiP.barcode || apiP.noAntrian;
|
||||
return !existingIds.has(id);
|
||||
});
|
||||
|
||||
// Replace with updated patients + add new ones
|
||||
queueStore.allPatients = [...updatedPatients, ...newApiPatients];
|
||||
|
||||
console.log(`📊 Total patients in store: ${queueStore.allPatients.length}`);
|
||||
};
|
||||
@@ -1415,7 +1445,8 @@ const getAllPatientsForRoom = (ruang) => {
|
||||
p.kodeKlinik === klinikData.value?.kodeKlinik &&
|
||||
p.nomorRuang === ruang.nomorRuang &&
|
||||
p.processStage === 'klinik-ruang' &&
|
||||
// CRITICAL: Only show patients with status "pemeriksaan" (from loket to klinik ruang)
|
||||
// CRITICAL: Only show patients with status "pemeriksaan"
|
||||
// This prevents duplicate patients with different statuses
|
||||
p.status === 'pemeriksaan';
|
||||
|
||||
if (!matches && p.kodeKlinik === klinikData.value?.kodeKlinik) {
|
||||
|
||||
Reference in New Issue
Block a user