feat: Implement EOI document validation and orphaned data cleanup

- Add new /api/eoi/validate-document endpoint to check document existence
- Automatically clean up orphaned database records when documents don't exist in Documenso
- Update EOISection component to validate documents on mount
- Enhanced delete-generated-document endpoint to handle already-deleted documents
- Updated check-signature-status endpoint with validation logic
- Prevents EOI section from showing when document no longer exists
- Self-healing system that fixes data inconsistencies automatically

Key improvements:
- Validates document existence before showing EOI management UI
- Cleans up documensoID, signature links, and status fields when document is missing
- Graceful handling of 404 errors from Documenso API
- Background validation with user-friendly notifications
- Prevents phantom EOI states that appear generated but don't exist
This commit is contained in:
2025-06-12 17:04:45 +02:00
parent b25e93d2a0
commit 41a6f7f1c8
4 changed files with 247 additions and 13 deletions

View File

@@ -520,6 +520,7 @@ const isDeleting = ref(false);
const isDeletingGenerated = ref(false);
const signatureStatus = ref<any>(null);
const isCheckingSignatures = ref(false);
const isValidatingDocument = ref(false);
const hasGeneratedEOI = computed(() => {
// Primary check: documensoID must exist for a generated EOI
@@ -857,10 +858,56 @@ const deleteGeneratedEOI = async () => {
}
};
const validateDocument = async () => {
if (!props.interest['documensoID']) return;
isValidatingDocument.value = true;
try {
console.log('[EOI Section] Validating document for interest:', props.interest.Id);
const response = await $fetch<{
success: boolean;
valid: boolean;
cleaned: boolean;
message: string;
documentStatus?: string;
}>('/api/eoi/validate-document', {
headers: {
'x-tag': '094ut234'
},
params: {
interestId: props.interest.Id.toString()
}
});
if (response.success) {
if (response.cleaned) {
console.log('[EOI Section] Document validation cleaned up orphaned records');
toast.info('Detected and cleaned up orphaned EOI data');
emit('update'); // Refresh parent to reflect cleaned state
} else {
console.log('[EOI Section] Document validation passed');
}
}
} catch (error: any) {
console.error('[EOI Section] Failed to validate document:', error);
// Don't show error to user - validation is a background process
} finally {
isValidatingDocument.value = false;
}
};
// Check signature status on mount if EOI is generated
onMounted(() => {
if (hasGeneratedEOI.value && !isEOISigned.value) {
checkSignatureStatus();
if (hasGeneratedEOI.value) {
// First validate the document exists
validateDocument().then(() => {
// Only check signature status if document is still valid after validation
if (hasGeneratedEOI.value && !isEOISigned.value) {
checkSignatureStatus();
}
});
}
});
</script>