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

0% Statements 0/23
0% Branches 0/38
0% Functions 0/4
0% Lines 0/20

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                                                                                                                                                                           
import {
  Controller,
  Get,
  Post,
  Body,
  Req,
  UseGuards,
  Query,
} from '@nestjs/common';
import { LeaveRequestService } from '../../../domains/hr/leave/request/leave-request.service';
import { LeaveBalanceEngine } from '../../../domains/hr/leave/balance/leave-balance.engine';
import { JwtAuthGuard } from '../../../platform/auth/jwt-auth.guard';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/mobile/leave')
@ApiTags('MobileLeave')
@UseGuards(JwtAuthGuard)
export class MobileLeaveController {
  constructor(
    private readonly requestService: LeaveRequestService,
    private readonly balanceEngine: LeaveBalanceEngine,
  ) {}
 
  @Get('balances')
  @ApiOperation({ summary: 'Get current employee leave balances' })
  @ApiResponse({ status: 200, description: 'Leave balances array' })
  async getBalances(@Req() req: any, @Query('periodId') periodId?: string) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const employeeId = req.user?.employeeId || req.user?.id || 'SYSTEM';
 
    const balances = await this.balanceEngine.getAllBalancesForEmployee(
      tenantId,
      employeeId,
      periodId || '',
    );
 
    // Return compact DTO
    return balances.map((b: any) => ({
      leaveType: b.leaveTypeId || b.leaveType,
      credited: b.creditedCount || 0,
      allocated: b.allocatedCount || 0,
      taken: b.takenCount || 0,
      balance: (b.creditedCount || 0) - (b.takenCount || 0),
    }));
  }
 
  @Post('requests')
  @ApiOperation({ summary: 'Submit leave request via mobile' })
  @ApiResponse({ status: 201, description: 'Submitted leave request' })
  async createRequest(
    @Req() req: any,
    @Body()
    body: {
      leaveTypeId: string;
      startDate: string;
      endDate: string;
      reason: string;
      idempotencyKey?: string;
    },
  ) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || 'SYSTEM';
    const employeeId = req.user?.employeeId || req.user?.id || 'SYSTEM';
 
    const dto = {
      employeeId,
      leaveTypeId: body.leaveTypeId,
      startDate: new Date(body.startDate),
      endDate: new Date(body.endDate),
      reason: body.reason,
      status: 'PENDING_APPROVAL',
      idempotencyKey: body.idempotencyKey,
    };
 
    const request = await this.requestService.create(tenantId, dto, userId);
 
    return {
      id: request._id,
      requestNumber: request.requestNumber || 'LR-TEMP',
      status: request.status,
    };
  }
}