import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards } from '@nestjs/common'; import { HqAuthGuard } from '../../common/guards/hq-auth.guard'; import { HqOperation } from '../../common/hq-operation/hq-operation.decorator'; import { HqOperationAction } from '../../common/hq-operation/hq-operation.constants'; import { CityWarehouseService } from '../city-scope/city-warehouse.service'; import { CreateCityWarehouseDto, UpdateCityWarehouseDto } from './dto/admin-mutate.dto'; import { AdminCityWarehousesQueryDto } from './dto/admin-query.dto'; import type { WarehouseFulfillmentMode, WarehouseManagerType, WarehouseStatus, } from '@prisma/client'; function mapWarehouseFulfillment(dto: CreateCityWarehouseDto | UpdateCityWarehouseDto) { return { fulfillmentMode: dto.fulfillmentMode as WarehouseFulfillmentMode | undefined, fulfillmentProviderId: dto.fulfillmentProviderId ? BigInt(dto.fulfillmentProviderId) : undefined, manualCarrierLabel: dto.manualCarrierLabel ?? undefined, manualQueryUrlTemplate: dto.manualQueryUrlTemplate ?? undefined, lng: dto.lng ?? undefined, lat: dto.lat ?? undefined, }; } @Controller('admin/cities/:cityId/warehouses') @UseGuards(HqAuthGuard) export class AdminCityWarehousesController { constructor(private readonly service: CityWarehouseService) {} @Get() list(@Param('cityId') cityId: string) { return this.service.listByCity(BigInt(cityId)); } @Post() @HqOperation({ action: HqOperationAction.WAREHOUSE_CREATE, refType: 'WAREHOUSE', refIdField: 'id', includeBody: true, }) create(@Param('cityId') cityId: string, @Body() dto: CreateCityWarehouseDto) { return this.service.create(BigInt(cityId), { name: dto.name, address: dto.address, contactName: dto.contactName, contactPhone: dto.contactPhone, managerType: dto.managerType as WarehouseManagerType, partnerAccountId: dto.partnerAccountId ? BigInt(dto.partnerAccountId) : undefined, status: dto.status as WarehouseStatus | undefined, ...mapWarehouseFulfillment(dto), }); } } @Controller('admin/city-warehouses') @UseGuards(HqAuthGuard) export class AdminCityWarehouseMutationsController { constructor(private readonly service: CityWarehouseService) {} @Get() listAll(@Query() query: AdminCityWarehousesQueryDto) { return this.service.listAll(query); } @Put(':id') @HqOperation({ action: HqOperationAction.WAREHOUSE_UPDATE, refType: 'WAREHOUSE', refIdParam: 'id', includeBody: true, }) update(@Param('id') id: string, @Body() dto: UpdateCityWarehouseDto) { return this.service.update(BigInt(id), { name: dto.name, address: dto.address, contactName: dto.contactName, contactPhone: dto.contactPhone, managerType: dto.managerType as WarehouseManagerType | undefined, partnerAccountId: dto.partnerAccountId === null ? null : dto.partnerAccountId ? BigInt(dto.partnerAccountId) : undefined, status: dto.status as WarehouseStatus | undefined, fulfillmentMode: dto.fulfillmentMode as WarehouseFulfillmentMode | undefined, fulfillmentProviderId: dto.fulfillmentProviderId === null ? null : dto.fulfillmentProviderId ? BigInt(dto.fulfillmentProviderId) : undefined, manualCarrierLabel: dto.manualCarrierLabel, manualQueryUrlTemplate: dto.manualQueryUrlTemplate, lng: dto.lng, lat: dto.lat, }); } @Delete(':id') @HqOperation({ action: HqOperationAction.WAREHOUSE_DELETE, refType: 'WAREHOUSE', refIdParam: 'id', }) remove(@Param('id') id: string) { return this.service.remove(BigInt(id)); } }