All files / src/domains/hr/organization branch.controller.ts

0% Statements 0/30
0% Branches 0/4
0% Functions 0/8
0% Lines 0/28

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                                                                       
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Request,
  UseGuards,
} from '@nestjs/common';
import { BranchService } from './branch.service';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiResponse,
} from '@nestjs/swagger';
 
import { PermissionGuard } from '../../../platform/permissions/permission.guard';
import { RequirePermission } from '../../../platform/permissions/permission.decorator';
 
@ApiTags('HR - Branches')
@ApiBearerAuth()
@UseGuards(PermissionGuard)
@Controller('api/v1/hr/branches')
export class BranchController {
  constructor(private readonly branchService: BranchService) {}
 
  @Get()
  @ApiOperation({ summary: 'List all branches' })
  @RequirePermission('hr.branch', 'read')
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async findAll(@Request() req: any) {
    const branches = await this.branchService.findAll(req.user.tenantId);
    return { success: true, data: branches };
  }
 
  @Post()
  @ApiOperation({ summary: 'Create a new branch' })
  @RequirePermission('hr.branch', 'create')
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async create(@Request() req: any, @Body() body: any) {
    const branch = await this.branchService.create(
      req.user.tenantId,
      body,
      req.user.userId,
    );
    return { success: true, data: branch };
  }
 
  @Get(':id')
  @ApiOperation({ summary: 'Get branch details' })
  @RequirePermission('hr.branch', 'read')
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async findOne(@Request() req: any, @Param('id') id: string) {
    const branch = await this.branchService.findOne(req.user.tenantId, id);
    return { success: true, data: branch };
  }
 
  @Put(':id')
  @ApiOperation({ summary: 'Update a branch' })
  @RequirePermission('hr.branch', 'update')
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async update(
    @Request() req: any,
    @Param('id') id: string,
    @Body() body: any,
  ) {
    const branch = await this.branchService.update(
      req.user.tenantId,
      id,
      body,
      req.user.userId,
    );
    return { success: true, data: branch };
  }
 
  @Delete(':id')
  @ApiOperation({ summary: 'Soft-delete a branch' })
  @RequirePermission('hr.branch', 'delete')
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async delete(@Request() req: any, @Param('id') id: string) {
    await this.branchService.delete(req.user.tenantId, id, req.user.userId);
    return { success: true };
  }
 
  @Post(':id/activate')
  @ApiOperation({ summary: 'Activate a branch' })
  @RequirePermission('hr.branch', 'update')
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async activate(@Request() req: any, @Param('id') id: string) {
    const branch = await this.branchService.update(
      req.user.tenantId,
      id,
      { status: 'active' },
      req.user.userId,
    );
    return { success: true, data: branch };
  }
 
  @Post(':id/deactivate')
  @ApiOperation({ summary: 'Deactivate a branch' })
  @RequirePermission('hr.branch', 'update')
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async deactivate(@Request() req: any, @Param('id') id: string) {
    const branch = await this.branchService.update(
      req.user.tenantId,
      id,
      { status: 'inactive' },
      req.user.userId,
    );
    return { success: true, data: branch };
  }
}