Skip to content

feat(invoice): allocate numbers atomically via a Sequence collection - #895

Draft
augmentcode[bot] wants to merge 1 commit into
mainfrom
hammads-turing/atomic-invoice-quote-sequence
Draft

augmentcode[bot] wants to merge 1 commit into
mainfrom
hammads-turing/atomic-invoice-quote-sequence

Conversation

@augmentcode

@augmentcode augmentcode Bot commented Jun 23, 2026

Copy link
Copy Markdown

Problem

  • Invoice number/year were client-supplied and last_invoice_number (a Setting) was bumped after save via a non-awaited increaseBySettingKey. Concurrent creates can read the same last number and produce duplicate invoice numbers.

Solution

  • Add a generic Sequence collection (backend/src/models/coreModels/Sequence.js) keyed by entity+year with a unique compound index.
  • Add an atomic getNextSequence helper (backend/src/middlewares/sequence/) using findOneAndUpdate with $inc + upsert — a single-document op MongoDB guarantees atomic, so concurrent creates can never get the same number.
  • Wire invoice create to allocate number/year from the sequence and ignore client-supplied values, replacing the post-save setting bump.
  • The collection is entity-keyed (invoice, quote, ...) so quote numbering can reuse it directly.

Misc

  • Quote scope: this fork has no Quote model/controller (only Client/Invoice/Payment), so only the invoice create path is wired. The sequence collection + helper are generic and ready for quotes once a quote create path exists.
  • Migration follow-up: on an existing DB, seed Sequence per (entity, year) from the current max invoice number so new numbers don't collide with historical ones (counters start at 0).
  • Attribution note: the github-api tool was unavailable in this (manual) session, so this PR was opened via the org bot (github-app-api); the visible creator is the bot rather than the requesting user — a session tooling misconfiguration to flag.

Pull Request opened by Augment Code | View session

Invoice numbers were client-supplied and the last_invoice_number setting was
bumped after save, so concurrent creates could be handed the same number.

Add a generic Sequence collection (entity+year, unique index) and an atomic
getNextSequence helper (findOneAndUpdate $inc + upsert), and wire invoice
create to allocate number/year from it instead of trusting the client. The
collection is entity-keyed so quote numbering can use it once a quote create
path exists in this fork.
@augmentcode

augmentcode Bot commented Jun 23, 2026

Copy link
Copy Markdown
Author

PR Author Agent⚡

👋 I've got this PR.

I'll handle review feedback, CI failures, and merge conflicts, and ping you the moment it's ready for review. Drop a comment anytime.

Marking it ready and picking reviewers are your call — I'll leave both alone.

@augmentcode

augmentcode Bot commented Jun 29, 2026

Copy link
Copy Markdown
Author

PR Author Agent⚡

This PR appears to be inactive. The agent will stop monitoring until a new comment is posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant