Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Submit feedback
    • Contribute to GitLab
  • Sign in
U
upgrade-data-crawler-be
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
  • Issues 0
    • Issues 0
    • List
    • Board
    • Labels
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Charts
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • ThinhNC
  • upgrade-data-crawler-be
  • Merge Requests
  • !17

Merged
Opened Sep 09, 2026 by ThinhNC@ThinhNC
  • Report abuse
Report abuse

feat(quota): implement monthly quota, role-based limits, reset mechanism and cron scheduler

Features & Enhancements: Monthly Quota, Role-Based Resource Limits, Zero-Loss Reset & Cron Scheduler

Summary

This PR introduces enterprise-grade resource quota management, role-based quota templating with automatic user synchronization, zero-data-loss quota reset capabilities, and an automated background job scheduler (Cron) with defense-in-depth permission resolution.


Key Changes

1. Database & Schema Updates (Prisma)

  • Added maxPagesPerMonthLimit (default 10,000 / 1,000), maxJobsPerMonthLimit (default 500 / 100), and quotaResetAt (DateTime?) to both User and Role models.
  • Applied PostgreSQL migration 20260909030359_add_role_and_monthly_quota.

2. Resource Quota Calculation & Zero-Loss Reset (auth & crawl-jobs)

  • UTC+7 Timezone Precision: Calculated startOfDay and startOfMonth using Asia/Ho_Chi_Minh.
  • Effective Horizon: Quota consumption is evaluated against effectiveSince = Math.max(startOfDay/startOfMonth, quotaResetAt).
  • Zero Data Loss: Resetting a quota updates quotaResetAt rather than mutating or deleting historical crawl records.
  • Implemented POST /api/v1/users/:id/reset-quota and POST /api/v1/roles/:id/reset-quota with audit logging (AUDIT_ACTIONS.USER_QUOTA_RESET, AUDIT_ACTIONS.ROLE_QUOTA_RESET).

3. Role-Based Quota Templates & Auto-Sync (roles)

  • Extended CreateRoleDto and UpdateRoleDto to define default quota limits for roles.
  • Supported syncUsersQuota flag in UpdateRoleDto to bulk update limits for all users assigned to the role.

4. Background Job Scheduler Module (cron)

  • Built complete 5-layer scheduler system with BullMQ queue (cron-scheduler-queue) and worker.
  • Supports periodic maintenance tasks: log cleanup, expired token purge, orphan asset cleanup, and summary email digests.
  • Integrated automatic system permission bootstrapping (permissionService.ensureSystemPermissions()) and ADMIN default permission inheritance in permission.middleware.ts.

5. Environment Configuration

  • Updated .env and .env.example with USER_MAX_PAGES_PER_MONTH=1000 and USER_MAX_JOBS_PER_MONTH=100.

Check out, review, and merge locally

Step 1. Fetch and check out the branch for this merge request

git fetch origin
git checkout -b feat/monthly-quota-role-limits-and-cron origin/feat/monthly-quota-role-limits-and-cron

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git fetch origin
git checkout origin/develop
git merge --no-ff feat/monthly-quota-role-limits-and-cron

Step 4. Push the result of the merge to GitLab

git push origin develop

Note that pushing to GitLab requires write access to this repository.

Tip: You can also checkout merge requests locally by following these guidelines.

  • Discussion 0
  • Commits 1
  • Changes 49
Assignee
No assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
0
Labels
None
Assign labels
  • View project labels
Reference: ThinhNC/upgrade-data-crawler-be!17

Revert this merge request

This will create a new commit in order to revert the existing changes.

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.

Cherry-pick this merge request

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.