Skip to content

refactor(db): use regex instead of fixed substr in repairSequences #29

Description

@khaira777

Context

In PR #28, repairSequences() was added to main/db.ts to rebuild sequence counters from existing order/bill data on startup. It works correctly but parses order/bill numbers using fixed substr positions:

substr(order_number, 5, 8)   // date
substr(order_number, 14)      // sequence number

Problem

This is fragile — if the numbering format ever changes (e.g. longer prefixes, different separators), the extraction silently returns wrong values.

Proposed Fix

Replace substr with a regex:

const match = row.order_number.match(/^ORD-(\d{8})-(\d+)$/);
if (match) { /* match[1] = date, match[2] = sequence */ }

Priority

Low — the current format (ORD-YYYYMMDD-NNNN / INV-YYYYMMDD-NNNN) is stable and the function runs with try/catch, so no risk today. Just a robustness improvement for the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions