FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

API: monthly quota window shrinks at month boundaries (setMonth overflow) · Issue #283 · textbee/textbee · GitHub

API: monthly quota window shrinks at month boundaries (setMonth overflow) #283

Description

The monthly usage window in billing.service.ts is computed like this, including on the enforcement path in canPerformAction (~line 999):

const processedSmsLastMonth = await this.smsModel.countDocuments({
  user: user._id,
  createdAt: {
    $gte: new Date(new Date().setMonth(new Date().getMonth() - 1)),
  },
})

setMonth overflows on long months. Run on March 31, setMonth(getMonth() - 1) produces "Feb 31", which JS normalizes to March 3. The "last month" window collapses to 28 days, and since this feeds the quota check, usage from the start of the real window stops counting. The mirror problem exists at month starts for display counts. Subtracting 30 days explicitly avoids the overflow, or better, use the subscription's currentPeriodStart, which the schema already carries, so the window matches what the user is actually billed for.

The daily window right above it (~line 994) has its own quirk:

createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) },

Midnight in the server's timezone, so daily limits reset at some arbitrary hour for everyone else. UTC (or the user's timezone) would at least be predictable.

The same date snippet appears three times in the file (~78, ~999, ~1146), so whatever the fix, it wants to be one small helper.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiBackend APIbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL