75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Get,
|
|
Param,
|
|
Body,
|
|
UseGuards,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Req,
|
|
} from '@nestjs/common';
|
|
import { SkipThrottle, Throttle } from '@nestjs/throttler';
|
|
import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse } from '@nestjs/swagger';
|
|
import { DevicesService } from './devices.service';
|
|
import { AuthService } from '../auth/auth.service';
|
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
|
import { DeviceAuthGuard } from '../auth/guards/device-auth.guard';
|
|
import { RolesGuard } from '../common/guards/roles.guard';
|
|
import { Roles } from '../common/decorators/roles.decorator';
|
|
import { RegisterDeviceDto } from './dto/register-device.dto';
|
|
import { SyncDto } from './dto/sync.dto';
|
|
import { AckCommandDto } from './dto/ack-command.dto';
|
|
|
|
@ApiTags('Devices')
|
|
@Controller('devices')
|
|
export class DevicesController {
|
|
constructor(
|
|
private devicesService: DevicesService,
|
|
private authService: AuthService,
|
|
) {}
|
|
|
|
@Post('register')
|
|
@Throttle({ default: { limit: 10, ttl: 60000 } })
|
|
@ApiOperation({ summary: 'Register a new device' })
|
|
@ApiResponse({ status: 201, description: 'Device registered successfully' })
|
|
async register(@Body() dto: RegisterDeviceDto) {
|
|
return this.authService.registerDevice(dto.venueId, dto.name, dto.registrationCode);
|
|
}
|
|
|
|
// Authenticated by the device token; the till must never be rate-limited out by other
|
|
// traffic from the venue's shared IP
|
|
@Post(':deviceId/sync')
|
|
@SkipThrottle()
|
|
@UseGuards(DeviceAuthGuard)
|
|
@ApiOperation({ summary: 'Sync device state with server' })
|
|
@ApiResponse({ status: 200, description: 'Sync successful, returns pending commands' })
|
|
async sync(@Param('deviceId') deviceId: string, @Body() dto: SyncDto, @Req() req: any) {
|
|
return this.devicesService.sync(req.user, dto);
|
|
}
|
|
|
|
@Post(':deviceId/commands/:commandId/ack')
|
|
@SkipThrottle()
|
|
@UseGuards(DeviceAuthGuard)
|
|
@ApiOperation({ summary: 'Acknowledge a command result' })
|
|
@ApiResponse({ status: 200, description: 'Command acknowledged' })
|
|
async ackCommand(
|
|
@Param('deviceId') deviceId: string,
|
|
@Param('commandId') commandId: string,
|
|
@Body() dto: AckCommandDto,
|
|
@Req() req: any,
|
|
) {
|
|
return this.devicesService.ackCommand(req.user, commandId, dto);
|
|
}
|
|
|
|
@Get('generation-code')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles('admin')
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'Generate a registration code for pairing' })
|
|
@ApiResponse({ status: 200, description: 'Registration code generated' })
|
|
async generateCode(@Req() req: any) {
|
|
const venueId = req.user.venueId;
|
|
return this.authService.generateRegistrationCode(venueId);
|
|
}
|
|
} |