Skip to content

Feat/asset assignment module#336

Merged
yusuftomilola merged 2 commits intoDistinctCodes:mainfrom
Tinna23:feat/asset-assignment-module
Oct 2, 2025
Merged

Feat/asset assignment module#336
yusuftomilola merged 2 commits intoDistinctCodes:mainfrom
Tinna23:feat/asset-assignment-module

Conversation

@Tinna23
Copy link
Contributor

@Tinna23 Tinna23 commented Oct 2, 2025

PR: feat(assets): implement asset assignment module with history tracking

Closes #293

Description

This pull request introduces a new AssetAssignmentsModule to manage the assignment of assets to employees, departments, or branches. This provides a clear and auditable chain of custody for all company assets.

A key architectural feature of this module is its non-destructive data model. Instead of deleting or overwriting records, the system maintains a full, immutable history of every assignment an asset has ever had. This is achieved by marking old assignments as inactive rather than removing them, which is critical for auditing and asset lifecycle management.

Implementation Details

Core Components

  • AssetAssignment Entity: A new TypeORM entity that defines the asset_assignments table. It includes an unassignmentDate column, which, when NULL, indicates the assignment is currently active.
  • AssetAssignmentsService:
    • assign(): Creates a new assignment record. It includes a crucial check to prevent an asset from being actively assigned to more than one entity at the same time.
    • unassign(): Finds the current active assignment for an asset and sets its unassignmentDate to the current time, effectively closing the assignment while preserving the record.
    • getHistoryForAsset(): Retrieves all historical and current assignment records for a specific asset, sorted with the most recent first.
  • AssetAssignmentsController: Exposes clear, intention-driven endpoints for managing assignments.

Endpoints Implemented

  • POST /asset-assignments/assign: Assigns an asset to a user or department.
  • PATCH /asset-assignments/unassign/:assetId: Unassigns an asset, closing its current assignment period.
  • GET /asset-assignments/history/:assetId: Retrieves the full assignment history of an asset.
  • GET /asset-assignments/current/:assetId: Retrieves only the current, active assignment for an asset.

How to Test

1. Setup

  1. Import the AssetAssignmentsModule into your main app.module.ts.
  2. Run database migrations to create the asset_assignments table.
  3. Start the NestJS application.

2. Test the Full Lifecycle

  1. Assign: Make a POST request to /asset-assignments/assign:

    {
      "assetId": "LAPTOP-001",
      "assignedToUserId": "user-123"
    }

    Verify that a new assignment record is created and returned.

  2. Attempt Re-assignment (Failure): Immediately make another POST request to assign the same asset (LAPTOP-001) to someone else.
    Observe: The API should return a 409 Conflict error with a message that the asset is already assigned.

  3. Unassign: Make a PATCH request to /asset-assignments/unassign/LAPTOP-001.
    Observe: The original assignment record should be returned, but now with the unassignmentDate field populated.

  4. Re-assign (Success): Now that the asset is free, repeat the POST request from step 1, but assign it to a different user (user-456).
    Observe: The request should now succeed, creating a new active assignment.

  5. Check History: Make a GET request to /asset-assignments/history/LAPTOP-001.
    Observe: The API should return an array containing two records: the new, active assignment for user-456 and the previous, historical assignment for user-123.

@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 f3a7490 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 Asset Assignment Module

2 participants