-
-
Notifications
You must be signed in to change notification settings - Fork 0
chore: resolve fallow dead-code findings and decompose dd-rules complexity #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,143 +27,190 @@ | |
| import type { DirectDebitDocument } from './pain008.js' | ||
| import type { ProfileIssue } from '../profile/profile.js' | ||
|
|
||
| /** Internal record of one mandate usage within a document, used for R2/R3 checks. */ | ||
| interface MandateUsage { | ||
| batchIdx: number | ||
| colIdx: number | ||
| collectionDate: string | ||
| signatureDate: string | ||
| sequenceType: string | ||
| localInstrument: string | ||
| } | ||
|
|
||
| /** | ||
| * Check cross-field SEPA rulebook constraints on a DirectDebitDocument. | ||
| * Returns an empty array when the document passes all rules. | ||
| * Returns one ProfileIssue per violation with a precise path and message. | ||
| * | ||
| * @param doc A DirectDebitDocument that has already passed Zod schema validation. | ||
| * @returns An array of ProfileIssue describing any violations (empty when valid). | ||
| * Append a mandate usage record to the index map, creating a new entry if needed. | ||
| * This is a pure accumulator with no side effects beyond the map mutation. | ||
| */ | ||
| export function checkDirectDebitRules(doc: DirectDebitDocument): ProfileIssue[] { | ||
| function accumulateMandateUsage( | ||
| index: Map<string, MandateUsage[]>, | ||
| mandateId: string, | ||
| usage: MandateUsage | ||
| ): void { | ||
| const existing = index.get(mandateId) | ||
| if (existing !== undefined) { | ||
| existing.push(usage) | ||
| } else { | ||
| index.set(mandateId, [usage]) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * R1: mandate signatureDate must not be after the batch collectionDate. | ||
| * Returns a ProfileIssue if violated, null otherwise. | ||
| */ | ||
| function checkR1SignatureBeforeCollection( | ||
| bIdx: number, | ||
| cIdx: number, | ||
| signatureDate: string, | ||
| collectionDate: string | ||
| ): ProfileIssue | null { | ||
| if (signatureDate <= collectionDate) return null | ||
| return { | ||
| path: `batches.${bIdx}.collections.${cIdx}.mandate.signatureDate`, | ||
| message: | ||
| `R1: mandate signatureDate (${signatureDate}) is after` + | ||
| ` the batch collectionDate (${collectionDate}).` + | ||
| ` A debit cannot precede the mandate signature.`, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * R2: a mandate id used in an OOFF batch must appear in exactly one collection | ||
| * across the whole document. Returns issues for all occurrences beyond the first. | ||
| */ | ||
| function checkR2Issues(mandateId: string, usages: MandateUsage[]): ProfileIssue[] { | ||
| const hasOoff = usages.some((u) => u.sequenceType === 'OOFF') | ||
| if (!hasOoff) return [] | ||
|
|
||
| // The mandate appears more than once and at least one occurrence is OOFF. | ||
| // Report all occurrences beyond the first to make the error precise. | ||
| const issues: ProfileIssue[] = [] | ||
| for (let i = 1; i < usages.length; i++) { | ||
| const u = usages[i] | ||
| if (u === undefined) continue | ||
| issues.push({ | ||
| path: `batches.${u.batchIdx}.collections.${u.colIdx}.mandate.id`, | ||
| message: | ||
| `R2: mandate "${mandateId}" is used in an OOFF batch and appears in` + | ||
| ` more than one collection in this document.` + | ||
| ` An OOFF mandate authorizes exactly one collection.`, | ||
| }) | ||
| } | ||
| return issues | ||
| } | ||
|
|
||
| /** | ||
| * R3: a mandate id must not appear under both CORE and B2B local instruments | ||
| * in the same document. Returns issues for all conflicting occurrences. | ||
| */ | ||
| function checkR3Issues(mandateId: string, usages: MandateUsage[]): ProfileIssue[] { | ||
| const instruments = new Set(usages.map((u) => u.localInstrument)) | ||
| if (instruments.size <= 1) return [] | ||
|
|
||
| // Build an index: mandateId -> list of { batchIdx, colIdx, collectionDate, sequenceType, localInstrument } | ||
| interface MandateUsage { | ||
| batchIdx: number | ||
| colIdx: number | ||
| collectionDate: string | ||
| signatureDate: string | ||
| sequenceType: string | ||
| localInstrument: string | ||
| // Mandate appears under multiple instruments (e.g. CORE and B2B). Report from second occurrence. | ||
| const issues: ProfileIssue[] = [] | ||
| const first = usages[0] | ||
| if (first === undefined) return [] | ||
|
|
||
| for (let i = 1; i < usages.length; i++) { | ||
| const u = usages[i] | ||
| if (u === undefined) continue | ||
| if (u.localInstrument !== first.localInstrument) { | ||
| issues.push({ | ||
| path: `batches.${u.batchIdx}.collections.${u.colIdx}.mandate.id`, | ||
| message: | ||
| `R3: mandate "${mandateId}" is used under local instrument "${u.localInstrument}"` + | ||
| ` in batch ${u.batchIdx} but under "${first.localInstrument}" in batch ${first.batchIdx}.` + | ||
| ` A mandate is bound to one scheme (CORE or B2B) and cannot be reused across schemes.`, | ||
| }) | ||
| } | ||
| } | ||
| return issues | ||
| } | ||
|
|
||
| const mandateIndex = new Map<string, MandateUsage[]>() | ||
| /** | ||
| * R4: if any collection in a batch has mandate.amendment.sameMandateNewDebtorAccount === true | ||
| * (SMNDA), the batch's sequenceType must be 'FRST'. | ||
| * Returns issues for each violating collection. | ||
| */ | ||
| function checkR4SmndaRequiresFirst(doc: DirectDebitDocument): ProfileIssue[] { | ||
| const issues: ProfileIssue[] = [] | ||
|
|
||
| for (let bIdx = 0; bIdx < doc.batches.length; bIdx++) { | ||
| const batch = doc.batches[bIdx] | ||
| // doc is validated so batch is always defined here | ||
| if (batch === undefined) continue | ||
|
|
||
| const effectiveLocalInstrument = batch.localInstrument ?? 'CORE' | ||
| if (batch.sequenceType === 'FRST') continue | ||
|
|
||
| for (let cIdx = 0; cIdx < batch.collections.length; cIdx++) { | ||
| const col = batch.collections[cIdx] | ||
| if (col === undefined) continue | ||
|
|
||
| const usage: MandateUsage = { | ||
| batchIdx: bIdx, | ||
| colIdx: cIdx, | ||
| collectionDate: batch.collectionDate, | ||
| signatureDate: col.mandate.signatureDate, | ||
| sequenceType: batch.sequenceType, | ||
| localInstrument: effectiveLocalInstrument, | ||
| } | ||
|
|
||
| // R1: signatureDate must not be after collectionDate (YYYY-MM-DD lexicographic) | ||
| if (col.mandate.signatureDate > batch.collectionDate) { | ||
| if (col.mandate.amendment?.sameMandateNewDebtorAccount === true) { | ||
| issues.push({ | ||
| path: `batches.${bIdx}.collections.${cIdx}.mandate.signatureDate`, | ||
| path: `batches.${bIdx}.collections.${cIdx}.mandate.amendment.sameMandateNewDebtorAccount`, | ||
| message: | ||
| `R1: mandate signatureDate (${col.mandate.signatureDate}) is after` + | ||
| ` the batch collectionDate (${batch.collectionDate}).` + | ||
| ` A debit cannot precede the mandate signature.`, | ||
| `R4: batch ${bIdx} has sequenceType "${batch.sequenceType}" but collection ${cIdx}` + | ||
| ` has sameMandateNewDebtorAccount=true (SMNDA).` + | ||
| ` A batch containing SMNDA collections must have sequenceType FRST,` + | ||
| ` because SMNDA represents the first collection under the amended mandate.`, | ||
| }) | ||
| } | ||
|
|
||
| // Accumulate usage for R2 and R3 | ||
| const existing = mandateIndex.get(col.mandate.id) | ||
| if (existing !== undefined) { | ||
| existing.push(usage) | ||
| } else { | ||
| mandateIndex.set(col.mandate.id, [usage]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // R2 and R3: check each mandate that appears more than once | ||
| for (const [mandateId, usages] of mandateIndex) { | ||
| if (usages.length < 2) continue | ||
|
|
||
| // R2: if any usage is under OOFF, the mandate may appear exactly once in total | ||
| const hasOoff = usages.some((u) => u.sequenceType === 'OOFF') | ||
| if (hasOoff) { | ||
| // The mandate appears more than once and at least one occurrence is OOFF. | ||
| // Report one issue pointing to the second (and later) OOFF or non-OOFF occurrences. | ||
| // We report all occurrences beyond the first to make the error precise. | ||
| for (let i = 1; i < usages.length; i++) { | ||
| const u = usages[i] | ||
| if (u === undefined) continue | ||
| issues.push({ | ||
| path: `batches.${u.batchIdx}.collections.${u.colIdx}.mandate.id`, | ||
| message: | ||
| `R2: mandate "${mandateId}" is used in an OOFF batch and appears in` + | ||
| ` more than one collection in this document.` + | ||
| ` An OOFF mandate authorizes exactly one collection.`, | ||
| }) | ||
| } | ||
| } | ||
| return issues | ||
| } | ||
|
|
||
| // R3: all usages of a mandate must share the same local instrument | ||
| const instruments = new Set(usages.map((u) => u.localInstrument)) | ||
| if (instruments.size > 1) { | ||
| // Mandate appears under multiple instruments (e.g. CORE and B2B). Report from second occurrence. | ||
| for (let i = 1; i < usages.length; i++) { | ||
| const u = usages[i] | ||
| if (u === undefined) continue | ||
| const first = usages[0] | ||
| if (first === undefined) continue | ||
| if (u.localInstrument !== first.localInstrument) { | ||
| issues.push({ | ||
| path: `batches.${u.batchIdx}.collections.${u.colIdx}.mandate.id`, | ||
| message: | ||
| `R3: mandate "${mandateId}" is used under local instrument "${u.localInstrument}"` + | ||
| ` in batch ${u.batchIdx} but under "${first.localInstrument}" in batch ${first.batchIdx}.` + | ||
| ` A mandate is bound to one scheme (CORE or B2B) and cannot be reused across schemes.`, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Check cross-field SEPA rulebook constraints on a DirectDebitDocument. | ||
| * Returns an empty array when the document passes all rules. | ||
| * Returns one ProfileIssue per violation with a precise path and message. | ||
| * | ||
| * @param doc A DirectDebitDocument that has already passed Zod schema validation. | ||
| * @returns An array of ProfileIssue describing any violations (empty when valid). | ||
| */ | ||
| export function checkDirectDebitRules(doc: DirectDebitDocument): ProfileIssue[] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warn |
||
| const issues: ProfileIssue[] = [] | ||
| const mandateIndex = new Map<string, MandateUsage[]>() | ||
|
|
||
| // R4: SMNDA requires FRST sequenceType | ||
| // If any collection in a batch has mandate.amendment.sameMandateNewDebtorAccount === true, | ||
| // the batch's sequenceType must be 'FRST'. | ||
| // Build the mandate index and collect R1 issues in collection-traversal order. | ||
| for (let bIdx = 0; bIdx < doc.batches.length; bIdx++) { | ||
| const batch = doc.batches[bIdx] | ||
| if (batch === undefined) continue | ||
|
|
||
| if (batch.sequenceType === 'FRST') { | ||
| // Already FRST, no violation possible for this batch. | ||
| continue | ||
| } | ||
| const effectiveLocalInstrument = batch.localInstrument ?? 'CORE' | ||
|
|
||
| for (let cIdx = 0; cIdx < batch.collections.length; cIdx++) { | ||
| const col = batch.collections[cIdx] | ||
| if (col === undefined) continue | ||
|
|
||
| if (col.mandate.amendment?.sameMandateNewDebtorAccount === true) { | ||
| issues.push({ | ||
| path: `batches.${bIdx}.collections.${cIdx}.mandate.amendment.sameMandateNewDebtorAccount`, | ||
| message: | ||
| `R4: batch ${bIdx} has sequenceType "${batch.sequenceType}" but collection ${cIdx}` + | ||
| ` has sameMandateNewDebtorAccount=true (SMNDA).` + | ||
| ` A batch containing SMNDA collections must have sequenceType FRST,` + | ||
| ` because SMNDA represents the first collection under the amended mandate.`, | ||
| }) | ||
| } | ||
| const r1Issue = checkR1SignatureBeforeCollection( | ||
| bIdx, | ||
| cIdx, | ||
| col.mandate.signatureDate, | ||
| batch.collectionDate | ||
| ) | ||
| if (r1Issue !== null) issues.push(r1Issue) | ||
|
|
||
| accumulateMandateUsage(mandateIndex, col.mandate.id, { | ||
| batchIdx: bIdx, | ||
| colIdx: cIdx, | ||
| collectionDate: batch.collectionDate, | ||
| signatureDate: col.mandate.signatureDate, | ||
| sequenceType: batch.sequenceType, | ||
| localInstrument: effectiveLocalInstrument, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Collect R2 and R3 issues per mandate entry (R2 before R3 within each mandate, preserving original order). | ||
| for (const [mandateId, usages] of mandateIndex) { | ||
| if (usages.length < 2) continue | ||
| issues.push(...checkR2Issues(mandateId, usages)) | ||
| issues.push(...checkR3Issues(mandateId, usages)) | ||
| } | ||
|
|
||
| // Collect R4 issues (SMNDA must be in a FRST batch). | ||
| issues.push(...checkR4SmndaRequiresFirst(doc)) | ||
|
|
||
| return issues | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warn
fallow/high-cognitive-complexity: 'checkR4SmndaRequiresFirst' has cognitive complexity 13 (threshold: 12)