forked from Chatbot/v3-api
first commit v3 initialiazed a temporary repository for darmanet client
This commit is contained in:
156
src/business-hours/business-hours.controller.ts
Normal file
156
src/business-hours/business-hours.controller.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Patch,
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
HttpStatus,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiTags,
|
||||
ApiParam,
|
||||
ApiResponse,
|
||||
ApiBody,
|
||||
} from '@nestjs/swagger';
|
||||
import { AdminGuard } from 'src/auth/guards/admin.guard';
|
||||
import { AdminIdentity } from 'src/common/decorators/Identity.decorator';
|
||||
import { Permissions } from 'src/common/decorators/permission.decorator';
|
||||
import { Permission } from 'src/common/types/permissions.catalog';
|
||||
import { AdminModel } from 'src/database/model/admin.model';
|
||||
import { BusinessHoursService } from './business-hours.service';
|
||||
import { CreateBusinessHoursDto } from './dto/create-business-hours.dto';
|
||||
import { BaseResponseDTO } from 'src/common/dto/base-response.dto';
|
||||
import { Public } from 'src/auth/auth.decorator';
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminGuard)
|
||||
@Permissions(Permission.BusinessHoursManage)
|
||||
@ApiTags('business-hours')
|
||||
@Controller('admin/business-hours')
|
||||
export class BusinessHoursController {
|
||||
constructor(private readonly businessHoursService: BusinessHoursService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get all business hours configurations' })
|
||||
@ApiResponse({ status: 200, description: 'List of all configurations' })
|
||||
async getAllConfigs(@AdminIdentity() admin: AdminModel) {
|
||||
try {
|
||||
const configs = await this.businessHoursService.getAllConfigs();
|
||||
return new BaseResponseDTO(
|
||||
HttpStatus.OK,
|
||||
'Business hours configurations retrieved successfully',
|
||||
configs,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to retrieve configurations',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Get('status')
|
||||
@Public()
|
||||
@ApiOperation({ summary: 'Get current business hours status (public)' })
|
||||
@ApiResponse({ status: 200, description: 'Current status' })
|
||||
async getStatus() {
|
||||
try {
|
||||
const status = await this.businessHoursService.isOpen();
|
||||
const nextOpen = await this.businessHoursService.getNextOpen();
|
||||
const config = await this.businessHoursService.getActiveConfig();
|
||||
|
||||
return new BaseResponseDTO(HttpStatus.OK, 'Status retrieved successfully', {
|
||||
open: status.open,
|
||||
nextOpen: nextOpen?.toISOString() || null,
|
||||
timezone: config?.timezone || null,
|
||||
currentWindow: status.currentWindow || null,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to get status',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a new business hours configuration' })
|
||||
@ApiBody({ type: CreateBusinessHoursDto })
|
||||
@ApiResponse({ status: 201, description: 'Configuration created' })
|
||||
async createConfig(
|
||||
@Body() createDto: CreateBusinessHoursDto,
|
||||
@AdminIdentity() admin: AdminModel,
|
||||
) {
|
||||
try {
|
||||
const config = await this.businessHoursService.createConfig(
|
||||
createDto as any,
|
||||
admin.username,
|
||||
);
|
||||
return new BaseResponseDTO(
|
||||
HttpStatus.CREATED,
|
||||
'Business hours configuration created successfully',
|
||||
config,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to create configuration',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@ApiOperation({ summary: 'Update an existing business hours configuration' })
|
||||
@ApiParam({ name: 'id', description: 'Configuration ID' })
|
||||
@ApiBody({ type: CreateBusinessHoursDto })
|
||||
@ApiResponse({ status: 200, description: 'Configuration updated' })
|
||||
async updateConfig(
|
||||
@Param('id') id: string,
|
||||
@Body() updateDto: CreateBusinessHoursDto,
|
||||
@AdminIdentity() admin: AdminModel,
|
||||
) {
|
||||
try {
|
||||
const config = await this.businessHoursService.updateConfig(
|
||||
id,
|
||||
updateDto as any,
|
||||
admin.username,
|
||||
);
|
||||
return new BaseResponseDTO(
|
||||
HttpStatus.OK,
|
||||
'Business hours configuration updated successfully',
|
||||
config,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to update configuration',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Patch(':id/deactivate')
|
||||
@ApiOperation({ summary: 'Deactivate a business hours configuration' })
|
||||
@ApiParam({ name: 'id', description: 'Configuration ID' })
|
||||
@ApiResponse({ status: 200, description: 'Configuration deactivated' })
|
||||
async deactivateConfig(@Param('id') id: string) {
|
||||
try {
|
||||
await this.businessHoursService.deactivateConfig(id);
|
||||
return new BaseResponseDTO(
|
||||
HttpStatus.OK,
|
||||
'Business hours configuration deactivated successfully',
|
||||
null,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to deactivate configuration',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user