diff --git a/strr-base-web/app/components/BaseUploadAdditionalDocuments.vue b/strr-base-web/app/components/BaseUploadAdditionalDocuments.vue index 61e29e9d9..6dd5c0803 100644 --- a/strr-base-web/app/components/BaseUploadAdditionalDocuments.vue +++ b/strr-base-web/app/components/BaseUploadAdditionalDocuments.vue @@ -42,7 +42,9 @@ const addDocumentToList = (doc: File) => { const removeDocumentFromList = (uiDoc: UiDocument) => { const index = documentList.value.findIndex(item => uiDoc.id === item.id) - documentList.value.splice(index, 1) + if (index !== -1) { + documentList.value.splice(index, 1) + } } const cancelDocumentsUpload = () => { diff --git a/strr-base-web/package.json b/strr-base-web/package.json index e6c8927e3..91028e8b0 100644 --- a/strr-base-web/package.json +++ b/strr-base-web/package.json @@ -2,7 +2,7 @@ "name": "strr-base-web", "private": true, "type": "module", - "version": "0.0.63", + "version": "0.0.64", "engines": { "node": ">=24" }, diff --git a/strr-examiner-web/app/stores/document.ts b/strr-examiner-web/app/stores/document.ts index 8ee064e66..b7026244d 100644 --- a/strr-examiner-web/app/stores/document.ts +++ b/strr-examiner-web/app/stores/document.ts @@ -96,7 +96,7 @@ export const useExaminerDocumentStore = defineStore('examiner/document', () => { * * @param {UiDocument} uiDoc - The document to upload, containing file data and metadata. * @param {number} registrationId - The registration ID to which the document is being added. - * @returns {Promise} A promise that resolves when the document has been added or rejects if an error occurs. + * @returns {Promise} Always resolves; upload errors are shown in an error modal instead of being thrown. */ async function addDocumentToRegistration (uiDoc: UiDocument, registrationId: number): Promise { try { @@ -140,7 +140,7 @@ export const useExaminerDocumentStore = defineStore('examiner/document', () => { * * @param {UiDocument} uiDoc - The document to upload, containing file data and metadata. * @param {string} applicationNumber - The application number to which the document is being added. - * @returns {Promise} A promise that resolves when the document has been added or rejects if an error occurs. + * @returns {Promise} Always resolves; upload errors are shown in an error modal instead of being thrown. */ async function addDocumentToApplication (uiDoc: UiDocument, applicationNumber: string): Promise { try { diff --git a/strr-examiner-web/package.json b/strr-examiner-web/package.json index 4891b675c..f28cf75de 100644 --- a/strr-examiner-web/package.json +++ b/strr-examiner-web/package.json @@ -2,7 +2,7 @@ "name": "strr-examiner-web", "private": true, "type": "module", - "version": "0.2.72", + "version": "0.2.73", "engines": { "node": ">=24" }, diff --git a/strr-examiner-web/tests/unit/store-document.spec.ts b/strr-examiner-web/tests/unit/store-document.spec.ts index 5553e3fdd..1b72a995a 100644 --- a/strr-examiner-web/tests/unit/store-document.spec.ts +++ b/strr-examiner-web/tests/unit/store-document.spec.ts @@ -80,6 +80,32 @@ describe('Document Store', () => { expect(exStore.activeReg!.documents).toHaveLength(mockDocuments.length) // no doc appended }) + it('should show error modal and resolve when adding document to application fails', async () => { + const exStore = useExaminerStore() + exStore.activeRecord = { ...mockHostRegistration, documents: [...mockDocuments] } + const recordBefore = exStore.activeRecord + + const docStore = useExaminerDocumentStore() + mockStrrApi.mockRejectedValueOnce(new Error('Upload failed')) + + const uiDoc = { + file: new File(['content'], 'utility-bill.pdf', { type: 'application/pdf' }), + type: DocumentUploadType.UTILITY_BILL, + loading: false + } as UiDocument + + await expect(docStore.addDocumentToApplication(uiDoc, 'APP12345')).resolves.toBeUndefined() + + expect(mockOpenErrorModal).toHaveBeenCalledWith( + 'error.docUpload.generic.title', + 'error.docUpload.generic.description', + false + ) + expect(uiDoc.loading).toBe(false) + expect(exStore.activeRecord).toBe(recordBefore) // record not replaced + expect(exStore.activeReg!.documents).toHaveLength(mockDocuments.length) + }) + it('should append document to activeReg when adding document to registration', async () => { const exStore = useExaminerStore() exStore.activeRecord = { ...mockHostRegistration, documents: [...mockDocuments] } diff --git a/strr-host-pm-web/app/stores/document.ts b/strr-host-pm-web/app/stores/document.ts index e31705176..b76c46cc9 100644 --- a/strr-host-pm-web/app/stores/document.ts +++ b/strr-host-pm-web/app/stores/document.ts @@ -356,7 +356,8 @@ export const useDocumentStore = defineStore('host/document', () => { // Remove document from local list to ensure createApplicationBody uses updated list const index = storedDocuments.value.findIndex(item => uiDoc.id === item.id) if (index === -1) { - return // not in the list (e.g. upload failed before it was added), nothing to remove + // already removed (e.g. double click) or never added - splice(-1, 1) would drop the last document + return } storedDocuments.value.splice(index, 1) @@ -436,7 +437,6 @@ export const useDocumentStore = defineStore('host/document', () => { } catch (e) { logFetchError(e, 'Error uploading document') strrModal.openErrorModal(t('error.docUpload.generic.title'), t('error.docUpload.generic.description'), false) - await removeStoredDocument(uiDoc) } finally { // cleanup loading on ui object uiDoc.loading = false diff --git a/strr-host-pm-web/package.json b/strr-host-pm-web/package.json index 295e42849..dbf9932ad 100644 --- a/strr-host-pm-web/package.json +++ b/strr-host-pm-web/package.json @@ -2,7 +2,7 @@ "name": "strr-host-pm-web", "private": true, "type": "module", - "version": "1.3.38", + "version": "1.3.39", "engines": { "node": ">=24" }, diff --git a/strr-host-pm-web/tests/unit/store-document.spec.ts b/strr-host-pm-web/tests/unit/store-document.spec.ts index e534e17d0..97b096832 100644 --- a/strr-host-pm-web/tests/unit/store-document.spec.ts +++ b/strr-host-pm-web/tests/unit/store-document.spec.ts @@ -511,6 +511,21 @@ describe('Document Removal', () => { expect(mockSubmitApplication).not.toHaveBeenCalled() }) + it('should leave the list alone and not save or delete when the document is not in it', async () => { + const store = useDocumentStore() + const docA = makeUiDoc('1', DocumentUploadType.BC_DRIVERS_LICENSE) + const docB = makeUiDoc('2', DocumentUploadType.UTILITY_BILL) + store.storedDocuments = [docA, docB] + mockPermitStore.application = { header: { status: 'draft', applicationNumber: 'APP-123' } } + + // e.g. second click on remove, or a doc removed while its upload was still running + await store.removeStoredDocument(makeUiDoc('3', DocumentUploadType.UTILITY_BILL)) + + expect(store.storedDocuments.map(d => d.id)).toEqual(['1', '2']) + expect(mockSubmitApplication).not.toHaveBeenCalled() + expect(mockStrrApi).not.toHaveBeenCalled() + }) + it('should not remove any document if names do not match the given types', async () => { const store = useDocumentStore() store.storedDocuments = [makeUiDoc('1', DocumentUploadType.BC_DRIVERS_LICENSE)] diff --git a/strr-strata-web/app/stores/document.ts b/strr-strata-web/app/stores/document.ts index 0b7156a64..f9f0be25b 100644 --- a/strr-strata-web/app/stores/document.ts +++ b/strr-strata-web/app/stores/document.ts @@ -41,8 +41,8 @@ export const useDocumentStore = defineStore('strata/document', () => { async function removeStoredDocument (uiDoc: UiDocument) { const index = storedDocuments.value.findIndex(item => uiDoc.id === item.id) - if (index === -1) { - return // not in the list (e.g. upload failed before it was added), nothing to remove + if (index < 0) { + return // not in the list, nothing to remove or delete } storedDocuments.value.splice(index, 1) if (uiDoc.apiDoc.fileKey) { @@ -83,7 +83,6 @@ export const useDocumentStore = defineStore('strata/document', () => { } catch (e) { logFetchError(e, 'Error uploading document') strrModal.openErrorModal(t('error.docUpload.generic.title'), t('error.docUpload.generic.description'), false) - await removeStoredDocument(uiDoc) } finally { // cleanup loading on ui object uiDoc.loading = false diff --git a/strr-strata-web/package.json b/strr-strata-web/package.json index 37395c27b..43c7b7d2e 100644 --- a/strr-strata-web/package.json +++ b/strr-strata-web/package.json @@ -2,7 +2,7 @@ "name": "strr-strata-web", "private": true, "type": "module", - "version": "1.2.14", + "version": "1.2.15", "engines": { "node": ">=24" }, diff --git a/strr-strata-web/tests/unit/store-document.spec.ts b/strr-strata-web/tests/unit/store-document.spec.ts index e7989b974..128aa06cc 100644 --- a/strr-strata-web/tests/unit/store-document.spec.ts +++ b/strr-strata-web/tests/unit/store-document.spec.ts @@ -1,9 +1,23 @@ -import { describe, beforeEach, it, expect } from 'vitest' +import { describe, beforeEach, it, expect, vi } from 'vitest' +import { mockNuxtImport } from '@nuxt/test-utils/runtime' import { mockStoredDocument } from '../mocks/mockedData' +const mockStrrApi = vi.fn().mockResolvedValue(undefined) +const mockOpenErrorModal = vi.fn() + +mockNuxtImport('useNuxtApp', original => () => Object.assign(Object.create(original()), { + $strrApi: mockStrrApi, + $i18n: { t: (key: string) => key } +})) + +mockNuxtImport('useStrrModals', () => () => ({ openErrorModal: mockOpenErrorModal })) + describe('Document Store', () => { beforeEach(() => { useDocumentStore().$reset() + mockStrrApi.mockReset() + mockStrrApi.mockResolvedValue(undefined) + mockOpenErrorModal.mockClear() }) it('reset function clears storedDocuments', () => { @@ -70,4 +84,37 @@ describe('Document Store', () => { await store.resetApiDocs() expect(store.storedDocuments).toHaveLength(0) }) + + it('does nothing when removing a document that is not in storedDocuments', async () => { + const store = useDocumentStore() + store.storedDocuments.push(mockStoredDocument) + const missingDoc: UiDocument = { + ...mockStoredDocument, + id: 'doc-missing', + apiDoc: { ...mockStoredDocument.apiDoc, fileKey: 'missing-key' } + } + + await store.removeStoredDocument(missingDoc) + + expect(store.storedDocuments).toHaveLength(1) + expect(store.storedDocuments[0]!.id).toBe(mockStoredDocument.id) + expect(mockStrrApi).not.toHaveBeenCalled() + }) + + it('keeps existing documents and deletes nothing when a dashboard upload is rejected', async () => { + const store = useDocumentStore() + store.storedDocuments.push(mockStoredDocument) + mockStrrApi.mockRejectedValueOnce(new Error('File contents do not match the file type')) + // upload never succeeded, so the new doc has no apiDoc and was never added to storedDocuments + const rejectedDoc: UiDocument = { ...mockStoredDocument, id: 'doc-rejected', apiDoc: {} as ApiDocument } + + await store.addDocumentToApplication(rejectedDoc, 'APP-123') + + expect(mockOpenErrorModal).toHaveBeenCalled() + expect(store.storedDocuments).toHaveLength(1) + expect(store.storedDocuments[0]!.id).toBe(mockStoredDocument.id) + // only the failed PUT, no DELETE /documents/{fileKey} + expect(mockStrrApi).toHaveBeenCalledTimes(1) + expect(mockStrrApi).toHaveBeenCalledWith('/applications/APP-123/documents', expect.objectContaining({ method: 'PUT' })) + }) })