Skip to content

feat(billing): credit signupGrant on org creation post-signup#3663

Merged
PierreBrisorgueil merged 1 commit into
masterfrom
feat-billing-signup-grant-hook
May 11, 2026
Merged

feat(billing): credit signupGrant on org creation post-signup#3663
PierreBrisorgueil merged 1 commit into
masterfrom
feat-billing-signup-grant-hook

Conversation

@PierreBrisorgueil
Copy link
Copy Markdown
Contributor

Summary

  • Adds BillingSignupGrantService.grantOnSignup({ orgId, planId }) — best-effort, always resolves, never throws
  • Hooks into createOrganizationForUser outside the rollback catch block (billing failure cannot trigger org/membership cleanup)
  • Free orgs receive 500 compute at signup via the idempotent creditGrant method (N2 plan)
  • Warns on duplicate grant (idempotency replay) and missing plan config

Test plan

  • Unit tests: 7 cases — credits grant, no-op when no signupGrant, swallows errors, default planId, plan not found, duplicate grant warn, plan null warn
  • Integration test: signup via /api/auth/signup → org created → balance=500, ledger has signup_grant entry
  • All 1430 unit tests pass
  • Integration suite passes (pre-existing listLedger.perf failure unrelated — MongoDB $sortArray version compat issue)

Notes

Adds BillingSignupGrantService.grantOnSignup (best-effort, never throws)
and hooks it into createOrganizationForUser outside the rollback catch block.
Free orgs receive 500 compute at signup via the idempotent creditGrant method.
Warns on duplicate grant (idempotency replay) and missing plan config.
Copilot AI review requested due to automatic review settings May 11, 2026 19:49
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

Warning

Rate limit exceeded

@PierreBrisorgueil has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 20 minutes and 43 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: b928b46f-7a9f-4806-a024-9f974b78b97f

📥 Commits

Reviewing files that changed from the base of the PR and between 41e7bd9 and 53b8237.

📒 Files selected for processing (4)
  • modules/billing/services/billing.signupGrant.service.js
  • modules/billing/tests/billing.signupGrant.service.unit.tests.js
  • modules/organizations/services/organizations.service.js
  • modules/organizations/tests/organizations.integration.tests.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat-billing-signup-grant-hook

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 and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.54%. Comparing base (41e7bd9) to head (53b8237).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3663      +/-   ##
==========================================
+ Coverage   89.43%   89.54%   +0.10%     
==========================================
  Files         137      138       +1     
  Lines        4714     4733      +19     
  Branches     1466     1472       +6     
==========================================
+ Hits         4216     4238      +22     
+ Misses        389      388       -1     
+ Partials      109      107       -2     
Flag Coverage Δ
integration 59.83% <75.00%> (+0.39%) ⬆️
unit 64.96% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 41e7bd9...53b8237. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@PierreBrisorgueil PierreBrisorgueil merged commit 3a3ca09 into master May 11, 2026
9 checks passed
@PierreBrisorgueil PierreBrisorgueil deleted the feat-billing-signup-grant-hook branch May 11, 2026 19:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements the N2 “one-shot signup grant” hook by crediting an organization’s extra balance immediately after a successful org+membership creation during signup, without allowing billing failures to trigger org rollback.

Changes:

  • Added BillingSignupGrantService.grantOnSignup({ orgId, planId }) as a best-effort wrapper around the idempotent creditGrant repository call.
  • Hooked the signup-grant credit into createOrganizationForUser after org/membership/user updates succeed (outside rollback handling).
  • Added unit + integration coverage for the signup-grant behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
modules/organizations/services/organizations.service.js Calls the billing signup-grant service after successfully creating an org/membership during signup.
modules/billing/services/billing.signupGrant.service.js Introduces a best-effort service to credit a configured signup grant via creditGrant.
modules/billing/tests/billing.signupGrant.service.unit.tests.js Adds unit tests for success, no-op, missing plan, duplicate grant, and error-swallow behavior.
modules/organizations/tests/organizations.integration.tests.js Adds an integration test asserting the signup grant is credited and a ledger entry exists post-signup.

Comment on lines +170 to +183
test('credits 500 compute to the org ExtraBalance ledger at signup', async () => {
// Resolve the org created for this user
const memberships = await MembershipRepository.list({ userId: grantUser._id || grantUser.id });
expect(memberships.length).toBeGreaterThan(0);
const orgId = (memberships[0].organizationId._id || memberships[0].organizationId).toString();

const balance = await BillingExtraBalanceRepository.getBalance(orgId);
expect(balance).toBe(500);

const ledger = await BillingExtraBalanceRepository.findLedgerByOrg(orgId);
expect(ledger).not.toBeNull();
const grantEntry = ledger.find((e) => e.source === 'signup_grant');
expect(grantEntry).toBeDefined();
expect(grantEntry.amount).toBe(500);
Comment on lines +186 to +188
afterAll(async () => {
await cleanupUser(grantUser);
});
Comment on lines +40 to +42
if (result?.applied === false) {
// Idempotent no-op — org already has a signup_grant entry (e.g. admin re-ran or replay).
logger.warn('[billing.signupGrant] duplicate grant skipped (idempotent)', { orgId, planId, reason: result.reason });
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.

2 participants