File

src/zones/zones.controller.ts

Prefix

zones

Index

Methods

Methods

Async assignDistributor
assignDistributor(orgId: string, id: string, body: literal type)
Decorators :
@Post(':id/distributors')
@Roles(undefined)
@ApiOperation({summary: 'Assign a client/distributor to a zone'})
Parameters :
Name Type Optional
orgId string No
id string No
body literal type No
Returns : unknown
Async bulkUpload
bulkUpload(orgId: string, file: Express.Multer.File)
Decorators :
@Post('upload')
@Roles(undefined)
@ApiOperation({summary: 'Bulk upload zones from Excel/CSV'})
@ApiConsumes('multipart/form-data')
@UseInterceptors(undefined)
Parameters :
Name Type Optional
orgId string No
file Express.Multer.File No
Returns : unknown
Async create
create(orgId: string, dto: CreateZoneDto)
Decorators :
@Post()
@Roles(undefined)
@ApiOperation({summary: 'Create a new zone'})
Parameters :
Name Type Optional
orgId string No
dto CreateZoneDto No
Returns : unknown
Async findAll
findAll(orgId: string, query: PaginationParams)
Decorators :
@Get()
@ApiPagination()
@ApiOperation({summary: 'List all zones (org-scoped)'})
Parameters :
Name Type Optional
orgId string No
query PaginationParams No
Returns : unknown
Async findOne
findOne(orgId: string, id: string)
Decorators :
@Get(':id')
@ApiOperation({summary: 'Get zone by ID (org-scoped)'})
Parameters :
Name Type Optional
orgId string No
id string No
Returns : unknown
Async getDistributors
getDistributors(orgId: string, id: string)
Decorators :
@Get(':id/distributors')
@ApiOperation({summary: 'List clients/distributors in a zone'})
Parameters :
Name Type Optional
orgId string No
id string No
Returns : unknown
Async getMappings
getMappings(orgId: string)
Decorators :
@Get('mappings')
@ApiOperation({summary: 'Zones with their mapped distributors/clients'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async remove
remove(orgId: string, id: string)
Decorators :
@Delete(':id')
@Roles(undefined)
@ApiOperation({summary: 'Delete a zone (blocked when in use)'})
Parameters :
Name Type Optional
orgId string No
id string No
Returns : unknown
Async unassignDistributor
unassignDistributor(orgId: string, id: string, clientId: string)
Decorators :
@Delete(':id/distributors/:clientId')
@Roles(undefined)
@ApiOperation({summary: 'Remove a distributor mapping from a zone'})
Parameters :
Name Type Optional
orgId string No
id string No
clientId string No
Returns : unknown
Async update
update(orgId: string, id: string, dto: UpdateZoneDto)
Decorators :
@Patch(':id')
@Roles(undefined)
@ApiOperation({summary: 'Update a zone (org-scoped)'})
Parameters :
Name Type Optional
orgId string No
id string No
dto UpdateZoneDto No
Returns : unknown
import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  BadRequestException,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiConsumes } from '@nestjs/swagger';
import { ZonesService } from './zones.service';
import { CreateZoneDto } from './dto/create-zone.dto';
import { UpdateZoneDto } from './dto/update-zone.dto';
import { CombinedAuthGuard } from '../auth/guards/combined-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { ApiPagination } from '../common/decorators/api-pagination.decorator';
import { PaginationParams } from '../common/utils/pagination.util';

/**
 * Roles allowed to MUTATE zones (create / update / delete / assign).
 * Reads are open to any authenticated user — operational staff need to
 * see the zone list to make routing decisions, but only managers can
 * change the zone fabric itself.
 */
const ZONE_WRITERS = ['SUPER_ADMIN', 'ADMIN', 'OPERATIONS_MANAGER'] as const;

@ApiTags('Zones')
@ApiBearerAuth()
@UseGuards(CombinedAuthGuard, RolesGuard)
@Controller('zones')
export class ZonesController {
  constructor(private readonly zonesService: ZonesService) {}

  @Post()
  @Roles(...ZONE_WRITERS)
  @ApiOperation({ summary: 'Create a new zone' })
  async create(
    @CurrentUser('organizationId') orgId: string,
    @Body() dto: CreateZoneDto,
  ) {
    return this.zonesService.create(orgId, dto);
  }

  @Get()
  @ApiPagination()
  @ApiOperation({ summary: 'List all zones (org-scoped)' })
  async findAll(
    @CurrentUser('organizationId') orgId: string,
    @Query() query: PaginationParams,
  ) {
    return this.zonesService.findAll(orgId, query);
  }

  @Get('mappings')
  @ApiOperation({ summary: 'Zones with their mapped distributors/clients' })
  async getMappings(@CurrentUser('organizationId') orgId: string) {
    return this.zonesService.getZoneMappings(orgId);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get zone by ID (org-scoped)' })
  async findOne(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
  ) {
    return this.zonesService.findOne(orgId, id);
  }

  @Patch(':id')
  @Roles(...ZONE_WRITERS)
  @ApiOperation({ summary: 'Update a zone (org-scoped)' })
  async update(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
    @Body() dto: UpdateZoneDto,
  ) {
    return this.zonesService.update(orgId, id, dto);
  }

  @Delete(':id')
  @Roles(...ZONE_WRITERS)
  @ApiOperation({ summary: 'Delete a zone (blocked when in use)' })
  async remove(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
  ) {
    return this.zonesService.remove(orgId, id);
  }

  @Get(':id/distributors')
  @ApiOperation({ summary: 'List clients/distributors in a zone' })
  async getDistributors(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
  ) {
    return this.zonesService.getDistributors(orgId, id);
  }

  @Post(':id/distributors')
  @Roles(...ZONE_WRITERS)
  @ApiOperation({ summary: 'Assign a client/distributor to a zone' })
  async assignDistributor(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
    @Body() body: { clientId: string },
  ) {
    if (!body?.clientId) throw new BadRequestException('clientId is required');
    return this.zonesService.assignDistributor(orgId, id, body.clientId);
  }

  @Delete(':id/distributors/:clientId')
  @Roles(...ZONE_WRITERS)
  @ApiOperation({ summary: 'Remove a distributor mapping from a zone' })
  async unassignDistributor(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
    @Param('clientId') clientId: string,
  ) {
    return this.zonesService.unassignDistributor(orgId, id, clientId);
  }

  @Post('upload')
  @Roles(...ZONE_WRITERS)
  @ApiOperation({ summary: 'Bulk upload zones from Excel/CSV' })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FileInterceptor('file', { limits: { fileSize: 10 * 1024 * 1024 } }))
  async bulkUpload(
    @CurrentUser('organizationId') orgId: string,
    @UploadedFile() file: Express.Multer.File,
  ) {
    if (!file) throw new BadRequestException('No file uploaded');
    return this.zonesService.bulkUploadFromFile(orgId, file);
  }
}

results matching ""

    No results matching ""