Skip to content

fix(cash-sessions): enforce open-session guard - #1129

Merged
khaira777 merged 3 commits into
mainfrom
fm/flocafe-issue-1085-r1
Oct 14, 2026
Merged

khaira777 merged 3 commits into
mainfrom
fm/flocafe-issue-1085-r1

Conversation

@khaira777

@khaira777 khaira777 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1085

Intent

Verify and implement the surgical fix for FreeOpenSourcePOS/FloCafe issue #1085 ("Cash shift can be opened on an already-closed business day"): in main/routes/cash-sessions.ts, check closedDayExists inside the open-shift transaction using the store's configured business timezone and day-start boundary, and return 409 (day_already_closed) when the business day is already closed. Deliver via the no-mistakes pipeline as its own PR with Fixes #1085 in the PR summary. Strengthen regression tests in tests/cash-sessions.test.ts with a deterministic fixed clock, non-UTC timezone, and custom business_day_start_time covering calendar-vs-effective business date boundaries and both zero and positive opening floats.

What Changed

  • Reject POST /api/cash-sessions/open with 409 (day_already_closed) inside the open-shift transaction when closedDayExists is true for the current effective business date (localDateInTimezone(new Date(), tenantTimezone(), tenantBusinessDayStartTime(db))), and allow day_already_closed in CASH_SESSION_OPEN_DISPLAY_CODES.
  • Document the 409 (session_already_open and day_already_closed) responses for POST /api/cash-sessions/open in docs/reference/api.md.
  • Add regression coverage in tests/cash-sessions.test.ts verifying closed-day shift-open rejection across a non-UTC timezone (America/New_York) and custom business_day_start_time (04:00) for both positive and zero opening floats.

Fixes #1085

Risk Assessment

✅ Low: The change is a small, well-bounded guard inside the shift-open transaction using existing business-date and closed-day helpers, accompanied by matching diagnostic-code allowlist updates, API documentation, and deterministic boundary tests.

Testing

Built the backend (npm run build), executed the targeted npm run test:cash-sessions (181/181 assertions passed, including Section 9) and npm run test:cash-closures (359/359 assertions passed) suites, and stood up the live FloCafe HTTP server (startServer() from main/server.ts) against an isolated SQLite database to drive all three boundary and closed-day shift-open scenarios over HTTP, capturing the responses and database state in ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json (no screenshot or visual artifact applies because this is a backend API route change with no UI surface change).

  • Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario Result Live Evidence
A cashier opens a shift before the configured 04:00 America/New_York business-day cutoff (at 2026-06-15T06:30:00.000Z, 02:30 local) while calendar dates 2026-06-13 and 2026-06-15 are already closed, a… ✅ pass live ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json
Adversarial: After the owner closes the effective business date 2026-06-14, a cashier attempts to open a shift with a positive opening float (opening_float_cents: 1000) at 2026-06-15T06:30:00.000Z, an… ✅ pass live ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json
Adversarial: After the owner closes the effective business date 2026-06-14, a cashier attempts to open a shift with a zero opening float (opening_float_cents: 0) at 2026-06-15T06:30:00.000Z, and POST… ✅ pass live ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json
Evidence: cash-sessions-section-9-test-output
Section 9: closed business day blocks new shift open
✓ owner closes prior business day 2026-06-13 (201)
✓ owner closes calendar date 2026-06-15 before boundary test (201)
✓ closing calendar date 2026-06-15 does not block opening on effective business day 2026-06-14 (got 200)
✓ shift open before 04:00 local cutoff mirrors opening float onto effective business date 2026-06-14
✓ shift on effective business day 2026-06-14 closes cleanly (200)
✓ owner closes effective business day 2026-06-14 (201)
✓ opening a shift with positive float on closed effective business day is 409 (got 409)
✓ positive-float shift open rejection uses day_already_closed
✓ rejected positive-float open on closed day creates no cash session
✓ rejected positive-float open on closed day creates no drawer movement
✓ opening a shift with zero float on closed effective business day is 409 (got 409)
✓ zero-float shift open rejection uses day_already_closed
✓ rejected zero-float open on closed day creates no cash session
✓ rejected zero-float open on closed day creates no drawer movement
=
Passed 181/181, failed 0
Evidence: issue-1085-live-verification-json
{
  "serverUrl": "http://127.0.0.1:13851",
  "scenarios": {
    "openBeforeCutoffOnUnclosedEffectiveDay": {
      "fixedClockUtc": "2026-06-15T06:30:00.000Z",
      "timezone": "America/New_York",
      "businessDayStartTime": "04:00",
      "openShiftResponse": {
        "status": 200,
        "body": {
          "id": 1,
          "opened_by": "cashier-live",
          "opened_by_name": "Cashier",
          "opened_at": "2026-06-15 06:30:00",
          "opening_float_cents": 2500,
          "status": "open",
          "closed_at": null,
          "closed_by": null,
          "closure_id": null
        }
      },
      "mirroredFloatMovement": {
        "business_date": "2026-06-14",
        "movement_type": "opening_float",
        "amount_cents": 2500
      },
      "closeShiftResponse": {
        "status": 200,
        "body": {
          "closure_id": 3,
          "variance_cents": 0,
          "expected_cash_cents": 2500,
          "counted_cash_cents": 2500
        }
      }
    },
    "rejectPositiveFloatOnClosedEffectiveDay": {
      "closeEffectiveDayStatus": 201,
      "openShiftResponse": {
        "status": 409,
        "body": {
          "error": "This day is already closed",
          "reason_code": "cash.session_rejected",
          "operation": "cash.session.open",
          "stage": "validation",
          "classification": "expected_rejection",
          "outcome": "not_applied",
          "correlation_id": "55555555-5555-4555-8555-555555555555",
          "display_code": "day_already_closed"
        }
      },
      "sessionsBefore": 1,
      "sessionsAfter": 1,
      "openSessionsAfter": 0,
      "movementsBefore": 3,
      "movementsAfter": 3
    },
    "rejectZeroFloatOnClosedEffectiveDay": {
      "openShiftResponse": {
        "status": 409,
        "body": {
          "error": "This day is already closed",
          "reason_code": "cash.session_rejected",
          "operation": "cash.session.open",
          "stage": "validation",
          "classification": "expected_rejection",
          "outcome": "not_applied",
          "correlation_id": "66666666-6666-4666-8666-666666666666",
          "display_code": "day_already_closed"
        }
      },
      "sessionsBefore": 1,
      "sessionsAfter": 1,
      "openSessionsAfter": 0,
      "movementsBefore": 3,
      "movementsAfter": 3
    }
  },
  "closePriorDay": {
    "status": 201,
    "body": {
      "zReport": {
        "id": 1,
        "scope": "day",
        "business_date": "2026-06-13",
        "period_start": "2026-06-13 08:00:00",
        "period_end": "2026-06-14 08:00:00",
        "opening_float_cents": 0,
        "expected_cash_cents": 0,
        "counted_cash_cents": 0,
        "variance_cents": 0,
        "gross_collected_cents": 0,
        "refunded_cents": 0,
        "net_collected_cents": 0,
        "bill_count": 0,
        "refund_count": 0,
        "payment_methods": [],
        "staff_sales": [],
        "tax_components": [],
        "pay_in_cents": 0,
        "pay_out_cents": 0,
        "safe_drop_cents": 0,
        "cash_movements": [
          {
            "id": 1,
            "business_date": "2026-06-13",
            "movement_type": "opening_float",
            "amount_cents": 0,
            "reason": "Opening float",
            "created_by": "owner-live",
            "created_at": "2026-06-15 12:00:00",
            "voided_at": null,
            "voided_by": null,
            "void_reason": null,
            "cash_session_id": null,
            "created_by_name": "Owner",
            "voided_by_name": null
          }
        ],
        "z_number": 1,
        "closed_by": "owner-live",
        "notes": null,
        "created_at": "2026-06-15 12:00:00"
      }
    }
  },
  "closeCalendarDay": {
    "status": 201,
    "body": {
      "zReport": {
        "id": 2,
        "scope": "day",
        "business_date": "2026-06-15",
        "period_start": "2026-06-15 08:00:00",
        "period_end": "2026-06-16 08:00:00",
        "opening_float_cents": 0,
        "expected_cash_cents": 0,
        "counted_cash_cents": 0,
        "variance_cents": 0,
        "gross_collected_cents": 0,
        "refunded_cents": 0,
        "net_collected_cents": 0,
        "bill_count": 0,
        "refund_count": 0,
        "payment_methods": [],
        "staff_sales": [],
        "tax_components": [],
        "pay_in_cents": 0,
        "pay_out_cents": 0,
        "safe_drop_cents": 0,
        "cash_movements": [
          {
            "id": 2,
            "business_date": "2026-06-15",
            "movement_type": "opening_float",
            "amount_cents": 0,
            "reason": "Opening float",
            "created_by": "owner-live",
            "created_at": "2026-06-15 12:00:00",
            "voided_at": null,
            "voided_by": null,
            "void_reason": null,
            "cash_session_id": null,
            "created_by_name": "Owner",
            "voided_by_name": null
          }
        ],
        "z_number": 2,
        "closed_by": "owner-live",
        "notes": null,
        "created_at": "2026-06-15 12:00:00"
      }
    }
  }
}

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario Result Live Evidence
A cashier opens a shift before the configured 04:00 America/New_York business-day cutoff (at 2026-06-15T06:30:00.000Z, 02:30 local) while calendar dates 2026-06-13 and 2026-06-15 are already closed, a… ✅ pass live ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json
Adversarial: After the owner closes the effective business date 2026-06-14, a cashier attempts to open a shift with a positive opening float (opening_float_cents: 1000) at 2026-06-15T06:30:00.000Z, an… ✅ pass live ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json
Adversarial: After the owner closes the effective business date 2026-06-14, a cashier attempts to open a shift with a zero opening float (opening_float_cents: 0) at 2026-06-15T06:30:00.000Z, and POST… ✅ pass live ~/.no-mistakes/evidence/01M4VTVCKC1GZ9V1TVDJGC1N0M/issue-1085-live-verification.json
  • npm run build
  • npm run test:cash-sessions
  • npm run test:cash-closures
  • node -e '<live Electron + main/server.ts HTTP verification script for issue #1085>'
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository: FreeOpenSourcePOS/FloCafe/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6ca66307-bffc-49b6-94c1-bb8a327495bb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@khaira777

Copy link
Copy Markdown
Contributor Author

@greptile review

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium impact] Adds validation to prevent opening cash shifts on closed business days.

The PR appears safe to merge, with a non-blocking request to strengthen the business-day regression test.

Findings

  1. P2 Tests miss business-day boundaries ▶

Summary

This PR rejects new cash shifts when the store's current business day is already closed.

  • Cashiers can’t open a shift after the store closes its business day.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Open shift request] --> B[Begin transaction]
  B --> C{Shift already open?}
  C -->|Yes| D[Return 409 session_already_open]
  C -->|No| E[Compute store business date]
  E --> F{Day already closed?}
  F -->|Yes| G[Return 409 day_already_closed]
  F -->|No| H[Create shift and optional float movement]
  H --> I[Commit and return shift]
Loading

Reviews (1) · Last reviewed commit: "no-mistakes(document): docs(api): docume..." · Reviewed by Greptile

Comment thread tests/cash-sessions.test.ts Resolved
@khaira777 khaira777 changed the title fix: reject opening cash shift on a closed business day fix(cash): block opening shift on closed day Oct 13, 2026
@khaira777

Copy link
Copy Markdown
Contributor Author

The production fix looks correct. I verified that the shift-opening transaction reuses the existing closedDayExists() helper and calculates the business date using the configured timezone and day-start boundary.

Before merging, please strengthen the regression tests:

  1. Add a deterministic test using a fixed clock, a non-UTC timezone, and a custom business_day_start_time.
  2. Cover a time when the calendar date differs from the effective business date.
  3. Confirm that closing a different calendar day does not incorrectly block shift opening.
  4. Confirm that closing the effective business day returns HTTP 409 with day_already_closed.
  5. Test both zero and positive opening floats, ensuring rejected attempts create no cash session or drawer movement.
  6. Run the cash-session and cash-closure tests and report the results.

Keep the existing production approach unless testing reveals an actual defect. Do not modify the day-close accounting formulas or introduce unrelated changes.

@khaira777
khaira777 force-pushed the fm/flocafe-issue-1085-r1 branch from 2e7632f to 297f036 Compare October 14, 2026 00:01
@khaira777 khaira777 changed the title fix(cash): block opening shift on closed day fix: reject opening cash shift on closed business day Oct 14, 2026
@khaira777 khaira777 changed the title fix: reject opening cash shift on closed business day fix(cash): reject opening shift on closed day Oct 14, 2026
@khaira777 khaira777 changed the title fix(cash): reject opening shift on closed day fix(cash-sessions): enforce open-session guard Oct 14, 2026
Check closedDayExists inside POST /api/cash-sessions/open using the store's configured business timezone and day-start boundary, and return 409 (day_already_closed) when the business day is already closed.

Fixes #1085
Add deterministic fixed-clock regression coverage using America/New_York and a 04:00 business_day_start_time where calendar date differs from effective business date, verifying closed adjacent calendar days do not block opening and closed effective business day rejects both positive and zero opening floats with 409 (day_already_closed) without creating sessions or movements.
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.

fix(cash): reject opening cash shift on an already-closed trading day

1 participant