Refactor handleFinish to streamline READ_AND_CONFIRM flow and improve validation checks

This commit is contained in:
2026-01-20 11:57:04 +01:00
parent ca248c3aa6
commit 2795b91386
2 changed files with 40 additions and 101 deletions

View File

@@ -185,6 +185,9 @@ class App {
}
async handleFinish(event) {
let annotResult = undefined;
// READ_AND_CONFIRM flow: require all pages viewed, skip annotation validations
if (READ_AND_CONFIRM) {
const allViewed = JSON.parse(sessionStorage.getItem('pspdf_all_pages_rendered') || 'false') === true
@@ -201,117 +204,56 @@ class App {
})
return false
}
return Swal.fire({
title: localized.confirmation,
html: `<div class="text-start fs-6 p-0 m-0">${localized.sigAgree}</div>`,
icon: "question",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: localized.finalize,
cancelButtonText: localized.back
}).then(async (result) => {
if (!result.isConfirmed)
return false
try {
await this.pdfKit.save()
} catch (e) {
Swal.fire({
title: 'Fehler',
text: 'Umschlag konnte nicht signiert werden!',
icon: 'error',
})
return false
}
try {
const iJSON = await this.pdfKit.exportInstantJSON()
const res = await signEnvelope({
instant: iJSON,
structured: mapSignature(iJSON)
})
if (!res.ok) {
if (res.status === 409) {
Swal.fire({
title: 'Warnung',
text: 'Umschlag ist nicht mehr verfügbar.',
icon: 'warning',
})
return false
} else if (res.status === 423) {
Swal.fire({
title: 'Info',
text: 'Dokument wurde von einem Empfänger abgelehnt. Sie werden weitergeleitet...',
icon: 'info',
timer: 2000,
showConfirmButton: false
}).then(() => {
location.reload()
})
} else {
throw new Error()
}
} else
return true
} catch (e) {
Swal.fire({
title: 'Fehler',
text: 'Umschlag konnte nicht signiert werden!',
icon: 'error',
})
return false
}
})
}
else {
const iJSON = await this.pdfKit.exportInstantJSON()
const iJSON = await this.pdfKit.exportInstantJSON()
const iFormFieldValues = iJSON.formFieldValues;
const iFormFieldValues = iJSON.formFieldValues;
//check required
const iReqFields = iFormFieldValues.filter(f => isFieldRequired(f))
const hasEmptyReq = iReqFields.some(f => (f.value === undefined || f.value === null || f.value === ""))
//check required
const iReqFields = iFormFieldValues.filter(f => isFieldRequired(f))
const hasEmptyReq = iReqFields.some(f => (f.value === undefined || f.value === null || f.value === ""))
if (hasEmptyReq) {
Swal.fire({
title: 'Warnung',
text: 'Bitte füllen Sie alle Standortinformationen vollständig aus!',
icon: 'warning',
})
return false;
}
//check city
const city_regex = new RegExp("^[a-zA-Z\\u0080-\\u024F]+(?:([\\ \\-\\']|(\\.\\ ))[a-zA-Z\\u0080-\\u024F]+)*$")
const iCityFields = iFormFieldValues.filter(f => isCityField(f))
for (var f of iCityFields)
if (!IS_MOBILE_DEVICE && !city_regex.test(f.value)) {
if (hasEmptyReq) {
Swal.fire({
title: 'Warnung',
text: `Bitte überprüfen Sie die eingegebene Ortsangabe "${f.value}" auf korrekte Formatierung. Beispiele für richtige Formate sind: München, Île-de-France, Sauðárkrókur, San Francisco, St. Catharines usw.`,
text: 'Bitte füllen Sie alle Standortinformationen vollständig aus!',
icon: 'warning',
})
return false;
}
//check # of signature
const validationResult = await this.validateAnnotations(this.signatureCount)
if (validationResult === false) {
Swal.fire({
title: 'Warnung',
text: 'Es wurden nicht alle Signaturfelder ausgefüllt!',
icon: 'warning',
})
return false
//check city
const city_regex = new RegExp("^[a-zA-Z\\u0080-\\u024F]+(?:([\\ \\-\\']|(\\.\\ ))[a-zA-Z\\u0080-\\u024F]+)*$")
const iCityFields = iFormFieldValues.filter(f => isCityField(f))
for (var f of iCityFields)
if (!IS_MOBILE_DEVICE && !city_regex.test(f.value)) {
Swal.fire({
title: 'Warnung',
text: `Bitte überprüfen Sie die eingegebene Ortsangabe "${f.value}" auf korrekte Formatierung. Beispiele für richtige Formate sind: München, Île-de-France, Sauðárkrókur, San Francisco, St. Catharines usw.`,
icon: 'warning',
})
return false;
}
//check # of signature
const validationResult = await this.validateAnnotations(this.signatureCount)
if (validationResult === false) {
Swal.fire({
title: 'Warnung',
text: 'Es wurden nicht alle Signaturfelder ausgefüllt!',
icon: 'warning',
})
return false
}
// set annot-result if all validations passed
annotResult = { instant: iJSON, structured: mapSignature(iJSON) };
}
return Swal.fire({
title: localized.confirmation,
html: `<div class="text-start fs-6 p-0 m-0">${localized.sigAgree}</div>`
,
html: `<div class="text-start fs-6 p-0 m-0">${localized.sigAgree}</div>`,
icon: "question",
showCancelButton: true,
confirmButtonColor: "#3085d6",
@@ -335,10 +277,7 @@ class App {
// Export annotation data and save to database
try {
const res = await signEnvelope({
instant: iJSON,
structured: mapSignature(iJSON)
});
const res = READ_AND_CONFIRM ? await signEnvelope() : await signEnvelope(annotResult);
if (!res.ok) {
if (res.status === 409) {