Skip to content

feat(entitlements): add metering threshold tasks manager#20785

Merged
julianpoy merged 1 commit into
mainfrom
ENT-13
Jun 23, 2026
Merged

feat(entitlements): add metering threshold tasks manager#20785
julianpoy merged 1 commit into
mainfrom
ENT-13

Conversation

@julianpoy

Copy link
Copy Markdown
Member

Because

  • We want to have a centralized place to work with cloud tasks for thresholds when metering

This pull request

  • Adds MeteringThresholdTasksManager

Issue that this pull request solves

Closes ENT-13

@julianpoy julianpoy requested a review from a team as a code owner June 23, 2026 17:17
Copilot AI review requested due to automatic review settings June 23, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 introduces a centralized Cloud Tasks manager for metering threshold-check enqueues, including supporting utilities for task-id deduplication and enqueue error classification, and wires it into the payments API module.

Changes:

  • Added MeteringThresholdTasksManager to enqueue threshold-check tasks with name-based deduplication.
  • Added utilities for Cloud Tasks config validation, deterministic task-id building, and enqueue error classification (with unit tests).
  • Registered a CloudTasksClient provider and the new manager in payments-api DI.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
libs/entitlements/metering/src/lib/utils/requireMeteringCloudTasksConfig.ts New helper to fail fast when required Cloud Tasks config is missing.
libs/entitlements/metering/src/lib/utils/requireMeteringCloudTasksConfig.spec.ts Unit tests for Cloud Tasks config requirement behavior.
libs/entitlements/metering/src/lib/utils/classifyEnqueueError.ts New helper to bucket Cloud Tasks enqueue errors into bounded “reason” tags.
libs/entitlements/metering/src/lib/utils/classifyEnqueueError.spec.ts Unit tests for enqueue error classification.
libs/entitlements/metering/src/lib/utils/buildThresholdTaskId.ts New helper to build deterministic, bucketed task ids for deduplication.
libs/entitlements/metering/src/lib/utils/buildThresholdTaskId.spec.ts Unit tests for task id stability, bucketing, and sanitization.
libs/entitlements/metering/src/lib/metering-threshold-tasks.manager.ts New NestJS manager that enqueues threshold-check tasks and reports metrics.
libs/entitlements/metering/src/lib/metering-threshold-tasks.manager.spec.ts Unit tests verifying enqueue payload, scheduling, dedup handling, and metrics.
libs/entitlements/metering/src/index.ts Exports the new manager and utilities from the package entrypoint.
apps/payments/api/src/app/app.module.ts Registers a CloudTasksClient factory and adds MeteringThresholdTasksManager to providers.
Comments suppressed due to low confidence (1)

libs/entitlements/metering/src/lib/utils/buildThresholdTaskId.ts:30

  • buildThresholdTaskId embeds a sanitized userIdentifier directly into the Cloud Tasks task name. The accompanying unit test explicitly uses an email address (user@example.com), which means task names (and any logs/metrics containing them) will include PII. For dedup you only need a stable identifier, so consider hashing the userIdentifier (and optionally slug) into a fixed-length, non-reversible string (e.g., sha256 hex) before building the task id to avoid PII exposure and also cap the task-id length.
  const bucket = Math.floor(now.getTime() / bucketSizeMs);
  const slug = sanitizeForTaskId(payload.slug);
  const user = sanitizeForTaskId(payload.userIdentifier);
  return `threshold-${slug}-${user}-${bucket}`;
}

function sanitizeForTaskId(input: string): string {
  return input.replace(/[^A-Za-z0-9-_]/g, '_');
}


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +7 to +22
export function requireMeteringCloudTasksConfig(
meteringConfig: MeteringConfig
): MeteringCloudTasksConfig {
if (!meteringConfig.cloudTasks) {
throw new Error(
'MeteringConfig.cloudTasks is required for threshold task enqueue'
);
}
if (!meteringConfig.cloudTasks.threshold.queueName) {
throw new Error(
'MeteringConfig.cloudTasks.threshold.queueName is required'
);
}
if (!meteringConfig.cloudTasks.threshold.taskUrl) {
throw new Error('MeteringConfig.cloudTasks.threshold.taskUrl is required');
}
Comment thread apps/payments/api/src/app/app.module.ts Outdated
Comment on lines 137 to 148
MeteringConfig,
MeteringAuthGuard,
{
provide: CloudTasksClient,
useFactory: (meteringConfig: MeteringConfig) =>
CloudTaskClientFactory({
cloudTasks: requireMeteringCloudTasksConfig(meteringConfig),
}),
inject: [MeteringConfig],
},
MeteringThresholdTasksManager,
MeteringWebhookManager,
@julianpoy julianpoy force-pushed the ENT-13 branch 2 times, most recently from fb04be7 to d7d81a6 Compare June 23, 2026 18:14

@xlisachan xlisachan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks Julian!

Because:

- We want to have a centralized place to work with cloud tasks for
  thresholds when metering

This commit:

- Adds MeteringThresholdTasksManager

Closes ENT-13
@julianpoy julianpoy merged commit 63d0717 into main Jun 23, 2026
17 of 19 checks passed
@julianpoy julianpoy deleted the ENT-13 branch June 23, 2026 20:01
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.

3 participants