Per-document mapping queue + persisted pending-mapping statements
A needs-mapping statement in a multi-file batch used to live in a single global slot (layoutMapperStore), so mapping the first of several such statements silently lost the rest. Each one is now a placeholder document in extractionStore's own documents list (with a pendingMapping field carrying its file/fingerprint/candidate data), reachable via DocumentNavBar and a per-document "Map now" reminder in TransactionsTable, with a clickable notice banner linking straight to each queued statement. Also makes this survive a reload: a needs-mapping placeholder is now saved to PocketBase (documentsService.savePendingMappingDocument, pb_migrations/12 relaxes documents.result to optional and adds the pending* fields) instead of living only in memory, and useDocuments.js reconstructs it as a placeholder on load. Includes the confirm-before- remapping step for correcting an already-successful document, and the delete-flow guard for a placeholder with no PocketBase id yet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
db11c1f7c5
commit
99dc3d8bb4
|
|
@ -43,7 +43,11 @@ async function handleRename(name) {
|
||||||
renaming.value = true
|
renaming.value = true
|
||||||
extractionStore.clearError()
|
extractionStore.clearError()
|
||||||
try {
|
try {
|
||||||
await documentsService.updateDocument(doc.pbId, { fileName: name })
|
// A pending-mapping placeholder (extractionStore
|
||||||
|
// .addPendingMappingDocument) has no pbId yet - nothing was ever
|
||||||
|
// saved to PocketBase for it, so there's nothing remote to PATCH;
|
||||||
|
// just rename the in-memory entry.
|
||||||
|
if (doc.pbId) await documentsService.updateDocument(doc.pbId, { fileName: name })
|
||||||
extractionStore.setActiveDocumentFileName(name)
|
extractionStore.setActiveDocumentFileName(name)
|
||||||
showOptions.value = false
|
showOptions.value = false
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -63,12 +67,22 @@ async function confirmDelete() {
|
||||||
deleting.value = true
|
deleting.value = true
|
||||||
extractionStore.clearError()
|
extractionStore.clearError()
|
||||||
try {
|
try {
|
||||||
await documentsService.deleteDocument(doc.pbId)
|
if (doc.pbId) {
|
||||||
// Keeps the sidebar's own per-project document list live if that
|
await documentsService.deleteDocument(doc.pbId)
|
||||||
// project is currently expanded there (a no-op otherwise) - same
|
// Keeps the sidebar's own per-project document list live if that
|
||||||
// reasoning as useBatchExtraction.js's/TransactionsTable's own calls.
|
// project is currently expanded there (a no-op otherwise) - same
|
||||||
projectsStore.removeDocumentFromCache(doc.project, doc.pbId)
|
// reasoning as useBatchExtraction.js's/TransactionsTable's own calls.
|
||||||
extractionStore.removeDocument(doc.pbId)
|
projectsStore.removeDocumentFromCache(doc.project, doc.pbId)
|
||||||
|
extractionStore.removeDocument(doc.pbId)
|
||||||
|
} else {
|
||||||
|
// A pending-mapping placeholder normally HAS a pbId (persisted
|
||||||
|
// the moment it was queued, see extractionStore
|
||||||
|
// .addPendingMappingDocument's own docstring) and takes the
|
||||||
|
// branch above instead - this is only the rare, defensive case
|
||||||
|
// where that save never actually went through, so there's
|
||||||
|
// nothing remote to delete; just drop it from the in-memory queue.
|
||||||
|
extractionStore.removePendingMappingDocument(doc.docKey)
|
||||||
|
}
|
||||||
showDeleteConfirm.value = false
|
showDeleteConfirm.value = false
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
extractionStore.setError('Could not delete this document: ' + err.message)
|
extractionStore.setError('Could not delete this document: ' + err.message)
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
import { useExtractionStore } from '../stores/extractionStore'
|
import { useExtractionStore } from '../stores/extractionStore'
|
||||||
import { useUiStore } from '../stores/uiStore'
|
import { useUiStore } from '../stores/uiStore'
|
||||||
import { useProjectsStore } from '../stores/projectsStore'
|
import { useProjectsStore } from '../stores/projectsStore'
|
||||||
import { useLayoutMapperStore } from '../stores/layoutMapperStore'
|
import { useUsageStore } from '../stores/usageStore'
|
||||||
import * as extractionService from '../services/extractionService'
|
import * as extractionService from '../services/extractionService'
|
||||||
import * as documentsService from '../services/documentsService'
|
import * as documentsService from '../services/documentsService'
|
||||||
import * as layoutMappingsService from '../services/layoutMappingsService'
|
import * as layoutMappingsService from '../services/layoutMappingsService'
|
||||||
|
|
@ -29,10 +29,39 @@ export function useBatchExtraction(confirmMappingFn) {
|
||||||
const extractionStore = useExtractionStore()
|
const extractionStore = useExtractionStore()
|
||||||
const uiStore = useUiStore()
|
const uiStore = useUiStore()
|
||||||
const projectsStore = useProjectsStore()
|
const projectsStore = useProjectsStore()
|
||||||
const layoutMapperStore = useLayoutMapperStore()
|
const usageStore = useUsageStore()
|
||||||
const { isProcessing, phaseText, start, stop } = useProcessingPhases()
|
const { isProcessing, phaseText, start, stop } = useProcessingPhases()
|
||||||
const { resolveNeedsMapping } = useLayoutMapping(confirmMappingFn)
|
const { resolveNeedsMapping } = useLayoutMapping(confirmMappingFn)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists a statement that needs manual mapping/value confirmation
|
||||||
|
* as a real PocketBase document record (no `result` yet), then adds
|
||||||
|
* the matching placeholder to extractionStore's own document queue
|
||||||
|
* with that record's real pbId - the raw `File` is kept in
|
||||||
|
* `pendingMapping.file` too, same as before, so clicking "Map now"
|
||||||
|
* later in THIS session doesn't need to re-fetch it from R2 first (a
|
||||||
|
* reload is what loses that in-memory copy - see useDocuments.js's
|
||||||
|
* own handling of a `needsMapping` record for that case).
|
||||||
|
*/
|
||||||
|
async function addPendingMappingDoc(file, { fingerprint, fingerprintHash, needsValueConfirmation, candidateRow, fieldOrder }, openingBalanceInput, closingBalanceInput) {
|
||||||
|
const docKey = extractionService.docKeyFor(file)
|
||||||
|
const record = await documentsService.savePendingMappingDocument({
|
||||||
|
docKey, fileName: file.name, sourceFile: file,
|
||||||
|
project: projectsStore.activeProjectId,
|
||||||
|
openingBalanceInput, closingBalanceInput,
|
||||||
|
fingerprint, fingerprintHash, needsValueConfirmation, candidateRow, fieldOrder,
|
||||||
|
})
|
||||||
|
projectsStore.addDocumentToCache(record.project, record)
|
||||||
|
extractionStore.addPendingMappingDocument({
|
||||||
|
pbId: record.id,
|
||||||
|
docKey, fileName: file.name, project: record.project,
|
||||||
|
previewUrl: URL.createObjectURL(file),
|
||||||
|
isImage: IMAGE_FILE_RE.test(file.name),
|
||||||
|
openingBalanceInput, closingBalanceInput,
|
||||||
|
pendingMapping: { file, fingerprint, fingerprintHash, needsValueConfirmation, candidateRow, fieldOrder },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Submits every chosen file for extraction, one at a time.
|
* Submits every chosen file for extraction, one at a time.
|
||||||
*
|
*
|
||||||
|
|
@ -80,12 +109,22 @@ export function useBatchExtraction(confirmMappingFn) {
|
||||||
result = await extractionService.process(file, openingBalance, closingBalance, resolved.forcedMapping)
|
result = await extractionService.process(file, openingBalance, closingBalance, resolved.forcedMapping)
|
||||||
usedMapping = resolved.mapping
|
usedMapping = resolved.mapping
|
||||||
} else if (resolved.reason === 'no_match') {
|
} else if (resolved.reason === 'no_match') {
|
||||||
// The manual mapping screen's own entry point (layout mapper
|
// No stored mapping resolves this - persisted to PocketBase
|
||||||
// L.6) - held in layoutMapperStore since a real File can't
|
// (savePendingMappingDocument, pb_migrations/12) as a real
|
||||||
// survive a router navigation on its own; ExtractorView.vue
|
// document record with its own sourceFile, not just an
|
||||||
// shows a banner linking to /map-layout while this is set.
|
// in-memory placeholder - a real reported gap: the earlier,
|
||||||
layoutMapperStore.setPending(file, resolved.fingerprint, resolved.fingerprintHash)
|
// memory-only version disappeared on a page reload mid-
|
||||||
result = { error: `This statement's layout isn't recognized yet - map it from the banner above to continue.` }
|
// session, forcing a full re-upload. Added to the ordinary
|
||||||
|
// document queue (addPendingMappingDocument) the same way
|
||||||
|
// either way - a second needs-mapping file in the same
|
||||||
|
// batch no longer stomps the first's pending state the way
|
||||||
|
// the original single-slot layoutMapperStore.pendingFile
|
||||||
|
// design once did (a separate, earlier reported bug).
|
||||||
|
await addPendingMappingDoc(file, {
|
||||||
|
fingerprint: resolved.fingerprint, fingerprintHash: resolved.fingerprintHash,
|
||||||
|
needsValueConfirmation: false, candidateRow: null, fieldOrder: null,
|
||||||
|
}, openingBalanceInput, closingBalanceInput)
|
||||||
|
result = null
|
||||||
} else {
|
} else {
|
||||||
result = { error: 'Mapping declined - extraction skipped for this file.' }
|
result = { error: 'Mapping declined - extraction skipped for this file.' }
|
||||||
}
|
}
|
||||||
|
|
@ -95,26 +134,31 @@ export function useBatchExtraction(confirmMappingFn) {
|
||||||
// VALUES in them didn't parse. Same exact/near-match lookup
|
// VALUES in them didn't parse. Same exact/near-match lookup
|
||||||
// (resolveNeedsMapping doesn't care WHY a fingerprint needed
|
// (resolveNeedsMapping doesn't care WHY a fingerprint needed
|
||||||
// resolving), differing only in what happens on 'no_match':
|
// resolving), differing only in what happens on 'no_match':
|
||||||
// the candidate row the backend already read goes to
|
// the candidate row the backend already read is persisted on
|
||||||
// layoutMapperStore too, so ManualMappingView.vue can show a
|
// the placeholder too, so ManualMappingView.vue can show a
|
||||||
// correction form instead of the from-scratch region-drawing
|
// correction form instead of the from-scratch region-drawing
|
||||||
// UI, since the column positions are already known.
|
// UI, since the column positions are already known - even
|
||||||
|
// after a reload, with no need to re-run extraction first.
|
||||||
const resolved = await resolveNeedsMapping({ fingerprint: result.fingerprint, fingerprintHash: result.fingerprintHash })
|
const resolved = await resolveNeedsMapping({ fingerprint: result.fingerprint, fingerprintHash: result.fingerprintHash })
|
||||||
if (resolved.applied) {
|
if (resolved.applied) {
|
||||||
usedFingerprintHash = result.fingerprintHash
|
usedFingerprintHash = result.fingerprintHash
|
||||||
result = await extractionService.process(file, openingBalance, closingBalance, resolved.forcedMapping)
|
result = await extractionService.process(file, openingBalance, closingBalance, resolved.forcedMapping)
|
||||||
usedMapping = resolved.mapping
|
usedMapping = resolved.mapping
|
||||||
} else if (resolved.reason === 'no_match') {
|
} else if (resolved.reason === 'no_match') {
|
||||||
layoutMapperStore.setPendingValueConfirmation(
|
await addPendingMappingDoc(file, {
|
||||||
file, resolved.fingerprint, resolved.fingerprintHash, result.candidateRow, result.fieldOrder,
|
fingerprint: resolved.fingerprint, fingerprintHash: resolved.fingerprintHash,
|
||||||
)
|
needsValueConfirmation: true, candidateRow: result.candidateRow, fieldOrder: result.fieldOrder,
|
||||||
result = { error: `This statement's values aren't recognized yet - confirm them from the banner above to continue.` }
|
}, openingBalanceInput, closingBalanceInput)
|
||||||
|
result = null
|
||||||
} else {
|
} else {
|
||||||
result = { error: 'Mapping declined - extraction skipped for this file.' }
|
result = { error: 'Mapping declined - extraction skipped for this file.' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.error) {
|
if (result === null) {
|
||||||
|
// Queued as a placeholder document above - nothing more to do
|
||||||
|
// for this file right now, move on to the next one.
|
||||||
|
} else if (result.error) {
|
||||||
failures.push(`${file.name}: ${result.error}`)
|
failures.push(`${file.name}: ${result.error}`)
|
||||||
} else {
|
} else {
|
||||||
const docKey = extractionService.docKeyFor(file)
|
const docKey = extractionService.docKeyFor(file)
|
||||||
|
|
@ -139,9 +183,21 @@ export function useBatchExtraction(confirmMappingFn) {
|
||||||
// A mapping that was actually applied gets its usage recorded
|
// A mapping that was actually applied gets its usage recorded
|
||||||
// for drift detection regardless of whether the save above
|
// for drift detection regardless of whether the save above
|
||||||
// succeeded - best-effort, never blocks showing the result.
|
// succeeded - best-effort, never blocks showing the result.
|
||||||
|
// (Unrelated to usageStore.recordExtraction below, despite the
|
||||||
|
// similar name - this one tracks a MAPPING's own pass/fail
|
||||||
|
// history, not billable page usage.)
|
||||||
if (usedMapping) {
|
if (usedMapping) {
|
||||||
layoutMappingsService.recordUsage(usedMapping.id, { success: layoutMappingsService.checksPassed(result) }).catch(() => {})
|
layoutMappingsService.recordUsage(usedMapping.id, { success: layoutMappingsService.checksPassed(result) }).catch(() => {})
|
||||||
}
|
}
|
||||||
|
// Billing/usage-tracking foundation - one real extraction just
|
||||||
|
// got delivered to the user, so it counts, regardless of
|
||||||
|
// whether auto-detection or a saved mapping produced it.
|
||||||
|
// page_reports is populated identically across every
|
||||||
|
// extraction path (dual-engine text PDF, OCR-only, forced-
|
||||||
|
// mapping retry), so this is a reliable page count regardless
|
||||||
|
// of which one actually ran. Best-effort internally (see
|
||||||
|
// usageStore.js), never blocks the result being shown.
|
||||||
|
usageStore.recordExtraction(record.id, result.page_reports?.length || 0)
|
||||||
// Keeps the sidebar's own per-project document list live if
|
// Keeps the sidebar's own per-project document list live if
|
||||||
// that project is currently expanded there - a no-op otherwise
|
// that project is currently expanded there - a no-op otherwise
|
||||||
// (see projectsStore.js's own comment on why this cache exists).
|
// (see projectsStore.js's own comment on why this cache exists).
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,44 @@ export function useDocuments() {
|
||||||
* by R2) - this was a real, previously-accepted gap ("re-select the
|
* by R2) - this was a real, previously-accepted gap ("re-select the
|
||||||
* file to see it again") that having the actual file in PocketBase
|
* file to see it again") that having the actual file in PocketBase
|
||||||
* closes for good.
|
* closes for good.
|
||||||
|
*
|
||||||
|
* A record with `needsMapping` set (see documentsService
|
||||||
|
* .savePendingMappingDocument/pb_migrations/12) is reconstructed as a
|
||||||
|
* placeholder (addPendingMappingDocument) instead of an ordinary one -
|
||||||
|
* this is what makes a statement still waiting on manual mapping
|
||||||
|
* actually survive a page reload now, rather than disappearing with
|
||||||
|
* the in-memory-only File it used to depend on (a real reported gap).
|
||||||
|
* `pendingMapping.file` is deliberately `null` here - there's no raw
|
||||||
|
* File to restore from a PocketBase record, only its own sourceFile -
|
||||||
|
* TransactionsTable.vue's "Map now" handler re-fetches it from R2 on
|
||||||
|
* demand instead, the same way it already does when correcting an
|
||||||
|
* already-successful document.
|
||||||
*/
|
*/
|
||||||
async function loadDocuments(filter = {}) {
|
async function loadDocuments(filter = {}) {
|
||||||
extractionStore.clearAll()
|
extractionStore.clearAll()
|
||||||
const records = await documentsService.listDocuments(filter)
|
const records = await documentsService.listDocuments(filter)
|
||||||
for (const record of records) {
|
for (const record of records) {
|
||||||
|
if (record.needsMapping) {
|
||||||
|
extractionStore.addPendingMappingDocument({
|
||||||
|
pbId: record.id,
|
||||||
|
project: record.project,
|
||||||
|
docKey: record.docKey,
|
||||||
|
fileName: record.fileName,
|
||||||
|
previewUrl: documentsService.sourceFileUrl(record),
|
||||||
|
isImage: IMAGE_FILE_RE.test(record.fileName),
|
||||||
|
openingBalanceInput: record.openingBalanceInput,
|
||||||
|
closingBalanceInput: record.closingBalanceInput,
|
||||||
|
pendingMapping: {
|
||||||
|
file: null,
|
||||||
|
fingerprint: record.pendingFingerprint,
|
||||||
|
fingerprintHash: record.pendingFingerprintHash,
|
||||||
|
needsValueConfirmation: record.needsValueConfirmation,
|
||||||
|
candidateRow: record.pendingCandidateRow,
|
||||||
|
fieldOrder: record.pendingFieldOrder,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
extractionStore.addDocument({
|
extractionStore.addDocument({
|
||||||
pbId: record.id,
|
pbId: record.id,
|
||||||
docKey: record.docKey,
|
docKey: record.docKey,
|
||||||
|
|
@ -40,6 +73,7 @@ export function useDocuments() {
|
||||||
closingBalanceInput: record.closingBalanceInput,
|
closingBalanceInput: record.closingBalanceInput,
|
||||||
layoutMappingUsed: record.layoutMappingUsed,
|
layoutMappingUsed: record.layoutMappingUsed,
|
||||||
layoutMappingVersionUsed: record.layoutMappingVersionUsed,
|
layoutMappingVersionUsed: record.layoutMappingVersionUsed,
|
||||||
|
bankAccountProfile: record.bankAccountProfile,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,46 @@ export async function updateDocument(id, patch) {
|
||||||
return pb.collection('documents').update(id, patch)
|
return pb.collection('documents').update(id, patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a documents record for a statement that still needs manual
|
||||||
|
* layout mapping (or value confirmation) - no `result` yet (relaxed to
|
||||||
|
* optional on this field, see pb_migrations/12), just the raw
|
||||||
|
* fingerprint/candidate-row data the mapper needs once the user gets to
|
||||||
|
* it, plus the real sourceFile upload so it's not lost on a reload.
|
||||||
|
* Persisted specifically so this survives one - the earlier, in-memory-
|
||||||
|
* only placeholder (extractionStore's own addPendingMappingDocument)
|
||||||
|
* disappeared on a page reload mid-session, forcing a full re-upload -
|
||||||
|
* a real reported gap this closes.
|
||||||
|
*/
|
||||||
|
export async function savePendingMappingDocument({
|
||||||
|
docKey, fileName, sourceFile, project, openingBalanceInput, closingBalanceInput,
|
||||||
|
fingerprint, fingerprintHash, needsValueConfirmation, candidateRow, fieldOrder,
|
||||||
|
}) {
|
||||||
|
const projectId = project || (await projectsService.getDefaultProject()).id
|
||||||
|
|
||||||
|
const formData = new FormData()
|
||||||
|
formData.append('owner', pb.authStore.record.id)
|
||||||
|
formData.append('project', projectId)
|
||||||
|
formData.append('docKey', docKey)
|
||||||
|
formData.append('fileName', fileName)
|
||||||
|
// `needsMapping` is the umbrella flag - true for EITHER flavor of
|
||||||
|
// placeholder (no result yet, resolve via /map-layout);
|
||||||
|
// `needsValueConfirmation` is the narrower sub-flag telling the two
|
||||||
|
// flavors apart, same distinction layoutMapperStore/pendingMapping
|
||||||
|
// already make in memory.
|
||||||
|
formData.append('needsMapping', 'true')
|
||||||
|
formData.append('needsValueConfirmation', needsValueConfirmation ? 'true' : 'false')
|
||||||
|
formData.append('pendingFingerprint', JSON.stringify(fingerprint))
|
||||||
|
formData.append('pendingFingerprintHash', fingerprintHash)
|
||||||
|
if (candidateRow) formData.append('pendingCandidateRow', JSON.stringify(candidateRow))
|
||||||
|
if (fieldOrder) formData.append('pendingFieldOrder', JSON.stringify(fieldOrder))
|
||||||
|
if (openingBalanceInput) formData.append('openingBalanceInput', openingBalanceInput)
|
||||||
|
if (closingBalanceInput) formData.append('closingBalanceInput', closingBalanceInput)
|
||||||
|
formData.append('sourceFile', sourceFile)
|
||||||
|
|
||||||
|
return pb.collection('documents').create(formData)
|
||||||
|
}
|
||||||
|
|
||||||
/** One document by id - the Layouts page's own Inspect (a mapping's sourceDocument relation only stores an id, not the full record). */
|
/** One document by id - the Layouts page's own Inspect (a mapping's sourceDocument relation only stores an id, not the full record). */
|
||||||
export async function getDocument(id) {
|
export async function getDocument(id) {
|
||||||
return pb.collection('documents').getOne(id)
|
return pb.collection('documents').getOne(id)
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,13 @@ export const useExtractionStore = defineStore('extraction', {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
|
/** Whether a given transaction index is currently excluded from every download format for the active document. */
|
||||||
|
isTransactionExcluded(state) {
|
||||||
|
return (index) => {
|
||||||
|
const doc = state.documents[state.activeIndex]
|
||||||
|
return (doc?.result?.excludedFromExport || []).includes(index)
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
/**
|
/**
|
||||||
|
|
@ -154,6 +161,13 @@ export const useExtractionStore = defineStore('extraction', {
|
||||||
// mapping's drift-detection usageCount/failureCount (see
|
// mapping's drift-detection usageCount/failureCount (see
|
||||||
// useRowEditing.js/TransactionRow.vue's own persistAndRecheck).
|
// useRowEditing.js/TransactionRow.vue's own persistAndRecheck).
|
||||||
layoutMappingUsed, layoutMappingVersionUsed,
|
layoutMappingUsed, layoutMappingVersionUsed,
|
||||||
|
// QBO export foundation - null for every document until a QBO
|
||||||
|
// download actually resolves one (see
|
||||||
|
// setActiveDocumentBankAccountProfile above); useDocuments.js's
|
||||||
|
// own reload path passes the PERSISTED value back in here too, so
|
||||||
|
// a document's own chosen bank account survives a page reload the
|
||||||
|
// same way layoutMappingUsed already does.
|
||||||
|
bankAccountProfile,
|
||||||
}) {
|
}) {
|
||||||
const wasEmpty = this.documents.length === 0
|
const wasEmpty = this.documents.length === 0
|
||||||
this.documents.push({
|
this.documents.push({
|
||||||
|
|
@ -163,10 +177,138 @@ export const useExtractionStore = defineStore('extraction', {
|
||||||
closingBalanceInput: closingBalanceInput || '',
|
closingBalanceInput: closingBalanceInput || '',
|
||||||
layoutMappingUsed: layoutMappingUsed ?? null,
|
layoutMappingUsed: layoutMappingUsed ?? null,
|
||||||
layoutMappingVersionUsed: layoutMappingVersionUsed ?? null,
|
layoutMappingVersionUsed: layoutMappingVersionUsed ?? null,
|
||||||
|
bankAccountProfile: bankAccountProfile ?? null,
|
||||||
|
// Always present (null here) so every document entry has the
|
||||||
|
// same shape regardless of how it was added - addPendingMapping
|
||||||
|
// Document()/replacePendingMappingDocument() below are the only
|
||||||
|
// places that ever set this to a real value.
|
||||||
|
pendingMapping: null,
|
||||||
})
|
})
|
||||||
if (wasEmpty) this.activeIndex = 0
|
if (wasEmpty) this.activeIndex = 0
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fully replaces an EXISTING document's own result (and layout-
|
||||||
|
* mapping fields) in place - used when the user manually launches
|
||||||
|
* the mapper to CORRECT an already-loaded, already-successful
|
||||||
|
* extraction (TransactionsTable.vue's own "Map" button -
|
||||||
|
* layoutMapperStore's pendingDocumentId), rather than creating a
|
||||||
|
* second, duplicate document the way the normal failed-extraction
|
||||||
|
* mapping flow does (that one really is a fresh document). Looks
|
||||||
|
* the document up by id rather than assuming it's still the active
|
||||||
|
* one - a touch more defensive than recomputeFlags()'s own
|
||||||
|
* activeDocument-only assumption, even though in practice nothing
|
||||||
|
* changes activeIndex while the mapper screen is open.
|
||||||
|
*/
|
||||||
|
replaceDocumentResult(pbId, result, { layoutMappingUsed, layoutMappingVersionUsed } = {}) {
|
||||||
|
const doc = this.documents.find((d) => d.pbId === pbId)
|
||||||
|
if (!doc) return
|
||||||
|
doc.result = result
|
||||||
|
doc.layoutMappingUsed = layoutMappingUsed ?? null
|
||||||
|
doc.layoutMappingVersionUsed = layoutMappingVersionUsed ?? null
|
||||||
|
doc.flaggedIndexes = computeFlags(result)
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a PLACEHOLDER document for a statement that needs manual
|
||||||
|
* layout mapping (or value confirmation) before any transactions
|
||||||
|
* exist at all - `result` stays null and `pendingMapping` carries
|
||||||
|
* everything needed to launch the mapper later.
|
||||||
|
*
|
||||||
|
* As of the persisted pending-mapping work (2026-11-16), `pbId`/
|
||||||
|
* `project` are normally REAL values - the caller (useBatchExtraction
|
||||||
|
* .js) already saved a real documents record for this placeholder
|
||||||
|
* (documentsService.savePendingMappingDocument, pb_migrations/12)
|
||||||
|
* before calling this, specifically so a page reload mid-session no
|
||||||
|
* longer loses it (a real reported gap: the original, in-memory-only
|
||||||
|
* version disappeared on reload, forcing a full re-upload). `pbId`
|
||||||
|
* still defaults to `null` for defensiveness (e.g. a caller that
|
||||||
|
* couldn't persist it for some reason), in which case this document
|
||||||
|
* behaves exactly like the original memory-only placeholder always
|
||||||
|
* did - lost on reload, but still usable for the rest of THIS session.
|
||||||
|
* `pendingMapping.file` may be `null` too (useDocuments.js's own
|
||||||
|
* reload path has no in-memory File to restore) - TransactionsTable
|
||||||
|
* .vue's "Map now" handler re-fetches it from PocketBase/R2 in that
|
||||||
|
* case, the same way it already does for correcting an
|
||||||
|
* already-successful document.
|
||||||
|
*
|
||||||
|
* This exists specifically because the ORIGINAL single-slot design
|
||||||
|
* (layoutMapperStore.pendingFile) could only ever track ONE such
|
||||||
|
* statement at a time: a second needs-mapping file in the same batch
|
||||||
|
* silently overwrote the first's pending state, and once the first
|
||||||
|
* was mapped and the store cleared, the others were gone with no way
|
||||||
|
* back (a real reported bug). A placeholder document instead stays
|
||||||
|
* in the ordinary document queue - DocumentNavBar's prev/next already
|
||||||
|
* cycles through it like any other document, and
|
||||||
|
* TransactionsTable.vue shows a "map now" reminder in place of the
|
||||||
|
* usual table whenever the ACTIVE document has `pendingMapping` set.
|
||||||
|
*
|
||||||
|
* Focus-stability follows the same rule addDocument() uses (only
|
||||||
|
* jumps to it if the list was empty beforehand).
|
||||||
|
*/
|
||||||
|
addPendingMappingDocument({
|
||||||
|
pbId = null, project = null, docKey, fileName, previewUrl, isImage, openingBalanceInput, closingBalanceInput, pendingMapping,
|
||||||
|
}) {
|
||||||
|
const wasEmpty = this.documents.length === 0
|
||||||
|
this.documents.push({
|
||||||
|
pbId, docKey, fileName, project, previewUrl, isImage,
|
||||||
|
result: null,
|
||||||
|
flaggedIndexes: new Set(),
|
||||||
|
openingBalanceInput: openingBalanceInput || '',
|
||||||
|
closingBalanceInput: closingBalanceInput || '',
|
||||||
|
layoutMappingUsed: null,
|
||||||
|
layoutMappingVersionUsed: null,
|
||||||
|
pendingMapping,
|
||||||
|
})
|
||||||
|
if (wasEmpty) this.activeIndex = 0
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a placeholder added above once the user has actually
|
||||||
|
* mapped and saved it - replaces its content IN PLACE (same queue
|
||||||
|
* position) rather than removing it and appending a new document,
|
||||||
|
* so the user doesn't lose their spot in the queue they were
|
||||||
|
* cycling through. Looked up by docKey rather than pbId - the
|
||||||
|
* caller (ManualMappingView.vue) only ever has `pendingDocKey` on
|
||||||
|
* hand at this point (layoutMapperStore's own bridge state), not a
|
||||||
|
* pbId directly; docKey is also the one identifier guaranteed
|
||||||
|
* stable even in the (now rare, defensive-only) case the
|
||||||
|
* placeholder's own PocketBase save never actually succeeded.
|
||||||
|
*/
|
||||||
|
replacePendingMappingDocument(docKey, {
|
||||||
|
pbId, project, previewUrl, isImage, result, openingBalanceInput, closingBalanceInput,
|
||||||
|
layoutMappingUsed, layoutMappingVersionUsed,
|
||||||
|
}) {
|
||||||
|
const doc = this.documents.find((d) => d.docKey === docKey)
|
||||||
|
if (!doc) return
|
||||||
|
if (doc.previewUrl) URL.revokeObjectURL(doc.previewUrl)
|
||||||
|
doc.pbId = pbId
|
||||||
|
doc.project = project
|
||||||
|
doc.previewUrl = previewUrl
|
||||||
|
doc.isImage = isImage
|
||||||
|
doc.result = result
|
||||||
|
doc.openingBalanceInput = openingBalanceInput || ''
|
||||||
|
doc.closingBalanceInput = closingBalanceInput || ''
|
||||||
|
doc.layoutMappingUsed = layoutMappingUsed ?? null
|
||||||
|
doc.layoutMappingVersionUsed = layoutMappingVersionUsed ?? null
|
||||||
|
doc.flaggedIndexes = computeFlags(result)
|
||||||
|
doc.pendingMapping = null
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops a still-unmapped placeholder from the queue outright - used
|
||||||
|
* by DocumentNavBar's own delete flow ONLY in the rare, defensive
|
||||||
|
* case a placeholder has no `pbId` (its own PocketBase save never
|
||||||
|
* went through) - the ordinary case, a persisted placeholder with a
|
||||||
|
* real pbId, goes through the normal removeDocument(pbId) +
|
||||||
|
* documentsService.deleteDocument(pbId) path instead, since there's
|
||||||
|
* a real remote record to delete too.
|
||||||
|
*/
|
||||||
|
removePendingMappingDocument(docKey) {
|
||||||
|
const idx = this.documents.findIndex((d) => d.docKey === docKey && d.pendingMapping)
|
||||||
|
this._removeDocumentAt(idx)
|
||||||
|
},
|
||||||
|
|
||||||
startBatch(total) {
|
startBatch(total) {
|
||||||
this.isBatchProcessing = true
|
this.isBatchProcessing = true
|
||||||
this.batchTotal = total
|
this.batchTotal = total
|
||||||
|
|
@ -281,6 +423,39 @@ export const useExtractionStore = defineStore('extraction', {
|
||||||
doc.result.acceptedAt = new Date().toISOString()
|
doc.result.acceptedAt = new Date().toISOString()
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// ── Exclude-from-export (per-row checkbox) ────────────────────────
|
||||||
|
// `excludedFromExport` (an array of original transaction indices)
|
||||||
|
// lives directly on doc.result, same arbitrary-JSON-blob pattern
|
||||||
|
// acknowledgedFlags/editLog already use - no new PocketBase
|
||||||
|
// migration needed. Deliberately non-destructive and export-time-
|
||||||
|
// only: unlike the R.4 "Delete row" action, this never touches
|
||||||
|
// doc.result.transactions/checks/status at all - a transaction
|
||||||
|
// stays fully visible and editable on screen, it's only left out of
|
||||||
|
// whichever file the Download button actually builds (see
|
||||||
|
// TransactionsTable.vue's own resultForExport()).
|
||||||
|
|
||||||
|
/** Marks/unmarks one transaction as excluded from every export format for the active document. */
|
||||||
|
setTransactionExcluded(index, excluded) {
|
||||||
|
const doc = this.activeDocument
|
||||||
|
if (!doc) return
|
||||||
|
const set = new Set(doc.result.excludedFromExport || [])
|
||||||
|
if (excluded) set.add(index)
|
||||||
|
else set.delete(index)
|
||||||
|
doc.result.excludedFromExport = [...set]
|
||||||
|
},
|
||||||
|
|
||||||
|
/** Bulk version of setTransactionExcluded - sets every index in `indices` to the same excluded state at once, for the header "select all/none" checkbox. */
|
||||||
|
setTransactionsExcluded(indices, excluded) {
|
||||||
|
const doc = this.activeDocument
|
||||||
|
if (!doc) return
|
||||||
|
const set = new Set(doc.result.excludedFromExport || [])
|
||||||
|
for (const i of indices) {
|
||||||
|
if (excluded) set.add(i)
|
||||||
|
else set.delete(i)
|
||||||
|
}
|
||||||
|
doc.result.excludedFromExport = [...set]
|
||||||
|
},
|
||||||
|
|
||||||
// ── Structural row edits (review screen R.4) ──────────────────────
|
// ── Structural row edits (review screen R.4) ──────────────────────
|
||||||
// Each one mutates currentResult.transactions only - TransactionRow.vue
|
// Each one mutates currentResult.transactions only - TransactionRow.vue
|
||||||
// is the one place that calls these, always followed by the same
|
// is the one place that calls these, always followed by the same
|
||||||
|
|
@ -367,6 +542,12 @@ export const useExtractionStore = defineStore('extraction', {
|
||||||
if (doc) doc.project = projectId
|
if (doc) doc.project = projectId
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/** Updates the active document's own `bankAccountProfile` after a successful BankAccountProfileModal pick/create (QBO export) - remembers the choice so a future export of this SAME document doesn't ask again. */
|
||||||
|
setActiveDocumentBankAccountProfile(profileId) {
|
||||||
|
const doc = this.activeDocument
|
||||||
|
if (doc) doc.bankAccountProfile = profileId
|
||||||
|
},
|
||||||
|
|
||||||
/** Updates the active document's own `fileName` after a successful DocumentOptionsModal rename. */
|
/** Updates the active document's own `fileName` after a successful DocumentOptionsModal rename. */
|
||||||
setActiveDocumentFileName(fileName) {
|
setActiveDocumentFileName(fileName) {
|
||||||
const doc = this.activeDocument
|
const doc = this.activeDocument
|
||||||
|
|
@ -385,6 +566,11 @@ export const useExtractionStore = defineStore('extraction', {
|
||||||
*/
|
*/
|
||||||
removeDocument(pbId) {
|
removeDocument(pbId) {
|
||||||
const idx = this.documents.findIndex((d) => d.pbId === pbId)
|
const idx = this.documents.findIndex((d) => d.pbId === pbId)
|
||||||
|
this._removeDocumentAt(idx)
|
||||||
|
},
|
||||||
|
|
||||||
|
/** Shared by removeDocument()/removePendingMappingDocument() - splices out index `idx` (a no-op for -1) and re-settles activeIndex the same way either caller needs. */
|
||||||
|
_removeDocumentAt(idx) {
|
||||||
if (idx === -1) return
|
if (idx === -1) return
|
||||||
this.documents.splice(idx, 1)
|
this.documents.splice(idx, 1)
|
||||||
if (idx < this.activeIndex) this.activeIndex -= 1
|
if (idx < this.activeIndex) this.activeIndex -= 1
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ import { computed, onMounted, ref, watch } from 'vue'
|
||||||
import { useExtractionStore } from '../stores/extractionStore'
|
import { useExtractionStore } from '../stores/extractionStore'
|
||||||
import { useProjectsStore } from '../stores/projectsStore'
|
import { useProjectsStore } from '../stores/projectsStore'
|
||||||
import { useUiStore } from '../stores/uiStore'
|
import { useUiStore } from '../stores/uiStore'
|
||||||
import { useLayoutMapperStore } from '../stores/layoutMapperStore'
|
|
||||||
import { useFileUpload } from '../composables/useFileUpload'
|
import { useFileUpload } from '../composables/useFileUpload'
|
||||||
import { useBatchExtraction } from '../composables/useBatchExtraction'
|
import { useBatchExtraction } from '../composables/useBatchExtraction'
|
||||||
import { useDocuments } from '../composables/useDocuments'
|
import { useDocuments } from '../composables/useDocuments'
|
||||||
|
|
@ -25,7 +24,35 @@ import ConfirmModal from '../components/ConfirmModal.vue'
|
||||||
const extractionStore = useExtractionStore()
|
const extractionStore = useExtractionStore()
|
||||||
const projectsStore = useProjectsStore()
|
const projectsStore = useProjectsStore()
|
||||||
const uiStore = useUiStore()
|
const uiStore = useUiStore()
|
||||||
const layoutMapperStore = useLayoutMapperStore()
|
|
||||||
|
// Statements queued for manual mapping (see extractionStore's own
|
||||||
|
// addPendingMappingDocument). It used to be a "Map it now" button
|
||||||
|
// linking straight to /map-layout for the ONE statement
|
||||||
|
// layoutMapperStore happened to be holding - a real reported bug once
|
||||||
|
// more than one statement in a batch needed mapping: the second
|
||||||
|
// overwrote the first's pending state, and once the first was mapped,
|
||||||
|
// the rest were gone with no way back. Each needs-mapping statement now
|
||||||
|
// stays in the ordinary document queue instead - TransactionsTable.vue
|
||||||
|
// shows its own "Map now" reminder for whichever one is currently
|
||||||
|
// active, reachable via DocumentNavBar's prev/next.
|
||||||
|
//
|
||||||
|
// Each filename here is its own clickable jump target (jumpToPending),
|
||||||
|
// not just a count - a real follow-up report after the count-only
|
||||||
|
// version shipped: relying on DocumentNavBar's small prev/next arrows
|
||||||
|
// as the ONLY way to actually reach one of several queued statements
|
||||||
|
// was easy to miss entirely, reading as if they'd been silently dropped
|
||||||
|
// even though they were always in extractionStore.documents (confirmed
|
||||||
|
// directly, not assumed, when this was investigated). Looked up by
|
||||||
|
// docKey at click time, not a precomputed index - the list only ever
|
||||||
|
// grows/shrinks via push/splice, but matching on the stable docKey
|
||||||
|
// rather than a snapshotted position avoids any risk of drifting stale.
|
||||||
|
const pendingMappingDocs = computed(
|
||||||
|
() => extractionStore.documents.filter((d) => d.pendingMapping).map((d) => ({ docKey: d.docKey, fileName: d.fileName })),
|
||||||
|
)
|
||||||
|
function jumpToPendingMapping(docKey) {
|
||||||
|
const idx = extractionStore.documents.findIndex((d) => d.docKey === docKey)
|
||||||
|
if (idx !== -1) extractionStore.setActiveIndex(idx)
|
||||||
|
}
|
||||||
|
|
||||||
// Bridges TransactionRow.vue's "Show on source" request (uiStore.
|
// Bridges TransactionRow.vue's "Show on source" request (uiStore.
|
||||||
// highlightRequest - see that store's own comment on why a store, not a
|
// highlightRequest - see that store's own comment on why a store, not a
|
||||||
|
|
@ -40,7 +67,7 @@ watch(
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
const {
|
const {
|
||||||
chosenFiles, isDragOver,
|
chosenFiles, isDragOver, lowResolutionWarnings,
|
||||||
removeChosenFile, clearChosenFiles, onFileInputChange, onDragOver, onDragLeave, onDrop,
|
removeChosenFile, clearChosenFiles, onFileInputChange, onDragOver, onDragLeave, onDrop,
|
||||||
} = useFileUpload()
|
} = useFileUpload()
|
||||||
|
|
||||||
|
|
@ -114,6 +141,7 @@ onMounted(async () => {
|
||||||
:is-drag-over="isDragOver"
|
:is-drag-over="isDragOver"
|
||||||
:is-processing="isProcessing"
|
:is-processing="isProcessing"
|
||||||
:phase-text="batchAwarePhaseText"
|
:phase-text="batchAwarePhaseText"
|
||||||
|
:low-resolution-warnings="lowResolutionWarnings"
|
||||||
@file-input-change="onFileInputChange"
|
@file-input-change="onFileInputChange"
|
||||||
@drag-over="onDragOver"
|
@drag-over="onDragOver"
|
||||||
@drag-leave="onDragLeave"
|
@drag-leave="onDragLeave"
|
||||||
|
|
@ -123,17 +151,25 @@ onMounted(async () => {
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Layout mapper work (L.6) - shown whenever useBatchExtraction.js
|
Layout mapper work (L.6) - each needs-mapping statement from the
|
||||||
couldn't resolve a statement's columns at all (not even a stored
|
batch is a placeholder in the document queue, and the "Map now"
|
||||||
mapping matched) - layoutMapperStore holds that one file + its
|
button for it lives in TransactionsTable.vue's own empty-state panel
|
||||||
fingerprint in memory until the user either maps it or picks a
|
for whichever one is currently active (see that component) - this
|
||||||
different file, since a real File object can't survive a router
|
notice's own filename buttons jump straight to one instead of
|
||||||
navigation on its own.
|
requiring DocumentNavBar's small prev/next arrows to find it.
|
||||||
-->
|
-->
|
||||||
<div v-if="layoutMapperStore.hasPending" class="banner banner-info">
|
<div v-if="pendingMappingDocs.length > 0" class="banner banner-info">
|
||||||
<i class="fa-solid fa-table-cells-large"></i>
|
<i class="fa-solid fa-table-cells-large"></i>
|
||||||
<span>"{{ layoutMapperStore.pendingFile.name }}" has a layout this app doesn't recognize yet.</span>
|
<span>
|
||||||
<RouterLink to="/map-layout" class="btn btn-primary">Map it now</RouterLink>
|
{{ pendingMappingDocs.length }} statement{{ pendingMappingDocs.length > 1 ? 's' : '' }}
|
||||||
|
{{ pendingMappingDocs.length > 1 ? 'need' : 'needs' }} manual mapping before
|
||||||
|
{{ pendingMappingDocs.length > 1 ? 'their transactions' : 'its transactions' }} can be shown:
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
v-for="doc in pendingMappingDocs" :key="doc.docKey"
|
||||||
|
type="button" class="banner-link"
|
||||||
|
@click="jumpToPendingMapping(doc.docKey)"
|
||||||
|
>{{ doc.fileName }}</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ConfirmModal
|
<ConfirmModal
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
/// <reference path="../pb_data/types.d.ts" />
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists a statement that still needs manual layout mapping (or value
|
||||||
|
* confirmation) across a page reload - previously these lived ONLY in
|
||||||
|
* memory (extractionStore's own `pendingMapping` field on a placeholder
|
||||||
|
* document, see CLAUDE.md's "Per-document mapping queue for batch
|
||||||
|
* uploads"), which meant a reload mid-session silently lost them,
|
||||||
|
* forcing a full re-upload - reported directly by the user.
|
||||||
|
*
|
||||||
|
* `result` relaxed to optional - a placeholder genuinely has no result
|
||||||
|
* yet, the same category of "nothing to store here" gap migration 10
|
||||||
|
* already found and fixed for layoutMappings.columnMap. `needsMapping`/
|
||||||
|
* `needsValueConfirmation` mark a placeholder as such (both false for
|
||||||
|
* every ordinary, already-resolved document); `pendingFingerprint`/
|
||||||
|
* `pendingFingerprintHash` are the raw fingerprint LayoutMappingRequired/
|
||||||
|
* ValueRecognitionRequired already computed, saved so the mapper doesn't
|
||||||
|
* need to recompute it after a reload; `pendingCandidateRow`/
|
||||||
|
* `pendingFieldOrder` are the value-confirmation case's own one-row
|
||||||
|
* preview, saved for the same reason. Deliberately NOT reusing the
|
||||||
|
* existing `layoutFingerprintHash` field - that one means "the hash of
|
||||||
|
* whichever mapping was actually APPLIED," a different thing from "the
|
||||||
|
* hash of an UNRESOLVED fingerprint still waiting on one."
|
||||||
|
*/
|
||||||
|
migrate((app) => {
|
||||||
|
let documents = app.findCollectionByNameOrId('documents')
|
||||||
|
let result = documents.fields.getByName('result')
|
||||||
|
result.required = false
|
||||||
|
documents.fields.add(new Field({ type: 'bool', name: 'needsMapping' }))
|
||||||
|
documents.fields.add(new Field({ type: 'bool', name: 'needsValueConfirmation' }))
|
||||||
|
documents.fields.add(new Field({ type: 'json', name: 'pendingFingerprint' }))
|
||||||
|
documents.fields.add(new Field({ type: 'text', name: 'pendingFingerprintHash' }))
|
||||||
|
documents.fields.add(new Field({ type: 'json', name: 'pendingCandidateRow' }))
|
||||||
|
documents.fields.add(new Field({ type: 'json', name: 'pendingFieldOrder' }))
|
||||||
|
app.save(documents)
|
||||||
|
}, (app) => {
|
||||||
|
let documents = app.findCollectionByNameOrId('documents')
|
||||||
|
let result = documents.fields.getByName('result')
|
||||||
|
result.required = true
|
||||||
|
documents.fields.removeByName('needsMapping')
|
||||||
|
documents.fields.removeByName('needsValueConfirmation')
|
||||||
|
documents.fields.removeByName('pendingFingerprint')
|
||||||
|
documents.fields.removeByName('pendingFingerprintHash')
|
||||||
|
documents.fields.removeByName('pendingCandidateRow')
|
||||||
|
documents.fields.removeByName('pendingFieldOrder')
|
||||||
|
app.save(documents)
|
||||||
|
})
|
||||||
Loading…
Reference in New Issue