Skip to content

feat(compliance): implement license and compliance module#335

Merged
yusuftomilola merged 1 commit intoDistinctCodes:mainfrom
Tinna23:feat/license-compliance-module
Oct 2, 2025
Merged

feat(compliance): implement license and compliance module#335
yusuftomilola merged 1 commit intoDistinctCodes:mainfrom
Tinna23:feat/license-compliance-module

Conversation

@Tinna23
Copy link
Contributor

@Tinna23 Tinna23 commented Oct 2, 2025

PR: feat(compliance): implement license and compliance module

Closes #294

Description

This pull request introduces a new LicensesModule to track and manage compliance documents and licenses associated with assets (e.g., vehicle registrations, software licenses).

The core of this feature is a proactive alerting system. A scheduled job runs daily to automatically detect licenses that are nearing their expiration date and triggers an event to notify the relevant parties. This transforms compliance management from a reactive, manual process into an automated, proactive one.

Implementation Details

Core Components

  • License Entity: A new TypeORM entity that defines the licenses table. It stores the assetId, licenseType, expiryDate, a documentUrl, and an isExpiryNotified flag to prevent duplicate alerts.
  • LicensesService: Provides the core CRUD logic for managing license records and includes a specific method, findLicensesNearingExpiry, to query for licenses expiring within the next 30 days.
  • LicenseExpiryTask: A scheduled job (cron job) using @nestjs/schedule that runs every day at 9 AM. It calls the service to find expiring licenses and emits a license.nearing_expiry event for each one, which can be handled by a separate notifications module.
  • LicensesController: Exposes RESTful API endpoints for creating, retrieving, and deleting license records.

Workflow

  1. A user adds a new license for an asset via the POST /licenses endpoint.
  2. The LicenseExpiryTask runs daily.
  3. It queries the database for any licenses that will expire in the next 30 days and have not yet been flagged as notified.
  4. For each expiring license found, it emits a license.nearing_expiry event and then updates the license's isExpiryNotified flag to true.
  5. A separate NotificationsModule (listening for this event) can then send an email or in-app alert to the asset owner or administrator.

How to Test

1. Setup

  1. Run npm install @nestjs/schedule.
  2. Import LicensesModule, ScheduleModule.forRoot(), and EventEmitterModule.forRoot() into your main app.module.ts.
  3. Run database migrations to create the licenses table.
  4. Start the NestJS application.

2. Test CRUD Functionality

  1. Create: Make a POST request to /licenses with a valid request body, including an expiryDate set for two weeks from now.
    {
      "assetId": "asset-123",
      "licenseType": "Vehicle Registration",
      "expiryDate": "2025-10-16",
      "documentUrl": "[http://example.com/doc.pdf](http://example.com/doc.pdf)"
    }
    Verify the license is created successfully.
  2. Read: Make a GET request to /licenses/asset/asset-123. Verify the new license is in the returned array.

3. Test the Scheduled Job

  1. To test the cron job without waiting, you can temporarily change the decorator in LicenseExpiryTask from @Cron(CronExpression.EVERY_DAY_AT_9AM) to @Cron(CronExpression.EVERY_10_SECONDS).
  2. Set up a simple event listener in another module to log when the event is caught:
    @OnEvent('license.nearing_expiry')
    handleLicenseExpiry(payload: any) {
      console.log('ALERT! License is expiring:', payload);
    }
  3. Restart the application.
  4. Observe: Within 10 seconds, you should see the log message "Running scheduled job..." followed by "ALERT! License is expiring..." with the payload of the license you created in the previous step. The isExpiryNotified flag in the database for that license should now be true.

@vercel
Copy link

vercel bot commented Oct 2, 2025

@Tinna23 is attempting to deploy a commit to the naijabuz's projects Team on Vercel.

A member of the Team first needs to authorize it.

@yusuftomilola yusuftomilola merged commit 5c77291 into DistinctCodes:main Oct 2, 2025
4 of 5 checks passed
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.

[BACKEND] Implement License & Compliance Module

2 participants