All files / src/interfaces/mobile-api/employee mobile-assets.controller.ts

0% Statements 0/41
0% Branches 0/48
0% Functions 0/6
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
import { Controller, Get, Post, Body, Param, Req, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { AssetAssignmentService } from '../../../domains/assets/services/asset-assignment.service';
import { AssetMaintenanceService } from '../../../domains/assets/services/asset-maintenance.service';
import { AssetVerificationService } from '../../../domains/assets/services/asset-verification.service';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Asset, AssetAssignment, AssetWorkOrder } from '../../../domains/assets/schemas';
 
@ApiTags('Mobile Assets')
@Controller('api/v1/mobile/assets')
export class MobileAssetsController {
  constructor(
    private readonly assignSvc: AssetAssignmentService,
    private readonly maintSvc: AssetMaintenanceService,
    private readonly verifySvc: AssetVerificationService,
    @InjectModel(Asset.name) private readonly assetModel: Model<Asset>,
    @InjectModel(AssetAssignment.name) private readonly assignmentModel: Model<AssetAssignment>,
    @InjectModel(AssetWorkOrder.name) private readonly workOrderModel: Model<AssetWorkOrder>
  ) {}
 
  @Get('employee/custody')
  @ApiOperation({ summary: 'List my assigned assets custody items' })
  async getEmployeeCustody(@Req() req: any) {
    const tenantId = req.headers['x-tenant-id'] || '60c72b2f9b1d8b23c4d5e6f1';
    const employeeId = req.headers['x-employee-id'] || 'EMP-001';
    
    // Find active and pending assignments
    const assignments = await this.assignmentModel.find({
      tenantId,
      assignedToType: 'Employee',
      assignedToId: employeeId,
      status: { $in: ['pending_acknowledgement', 'active'] }
    }).exec();
 
    // Hydrate asset details
    const result: any[] = [];
    for (const assign of assignments) {
      const asset = await this.assetModel.findById(assign.assetId).exec();
      if (asset) {
        result.push({
          assignmentId: assign._id,
          assetId: asset._id,
          assetNumber: asset.assetNumber,
          assetName: asset.assetName,
          status: assign.status,
          assignedAt: assign.assignedAt,
          condition: asset.condition
        });
      }
    }
    return result;
  }
 
  @Post('employee/custody/:id/acknowledge')
  @ApiOperation({ summary: 'Acknowledge custody acceptance or rejection' })
  async acknowledgeCustody(@Req() req: any, @Param('id') id: string, @Body() body: any) {
    const tenantId = req.headers['x-tenant-id'] || '60c72b2f9b1d8b23c4d5e6f1';
    return this.assignSvc.acknowledgeCustody(tenantId, id, body.accept, body.notes);
  }
 
  @Get('technician/work-orders')
  @ApiOperation({ summary: 'List my assigned maintenance work orders' })
  async getTechnicianWorkOrders(@Req() req: any) {
    const tenantId = req.headers['x-tenant-id'] || '60c72b2f9b1d8b23c4d5e6f1';
    const employeeId = req.headers['x-employee-id'] || 'EMP-TECH-01';
 
    return this.workOrderModel.find({
      tenantId,
      assignedTechnicianId: employeeId,
      status: { $in: ['assigned', 'in_progress'] }
    }).exec();
  }
 
  @Post('technician/work-orders/:id/complete')
  @ApiOperation({ summary: 'Complete assigned technician work order' })
  async completeWorkOrder(@Req() req: any, @Param('id') id: string, @Body() body: any) {
    const tenantId = req.headers['x-tenant-id'] || '60c72b2f9b1d8b23c4d5e6f1';
    return this.maintSvc.completeWorkOrder(tenantId, id, body.actualCostMinor || 0, body.partsCostMinor || 0, body.downtimeHours || 0, body.notes);
  }
 
  @Post('auditor/verify')
  @ApiOperation({ summary: 'Submit blind physical verification scan' })
  async submitVerificationScan(@Req() req: any, @Body() body: any) {
    const tenantId = req.headers['x-tenant-id'] || '60c72b2f9b1d8b23c4d5e6f1';
    const auditorId = req.headers['x-user-id'] || '60c72b2f9b1d8b23c4d5e6f2';
 
    return this.verifySvc.verifyAssetScan(
      tenantId,
      body.sessionId,
      body.assetId,
      body.scannedLocationId,
      body.condition,
      auditorId
    );
  }
}