mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:14:55 +00:00
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { ProductType } from '@app/common/constants/product-type.enum';
|
|
import {
|
|
Injectable,
|
|
CanActivate,
|
|
ExecutionContext,
|
|
BadRequestException,
|
|
HttpException,
|
|
} from '@nestjs/common';
|
|
import { DeviceService } from 'src/device/services';
|
|
|
|
@Injectable()
|
|
export class CheckFourAndSixSceneDeviceTypeGuard implements CanActivate {
|
|
constructor(private readonly deviceService: DeviceService) {}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const request = context.switchToHttp().getRequest();
|
|
const deviceUuid = request.params.deviceUuid;
|
|
|
|
if (!deviceUuid) {
|
|
throw new BadRequestException('Device UUID is required');
|
|
}
|
|
|
|
try {
|
|
const deviceDetails =
|
|
await this.deviceService.getDeviceByDeviceUuid(deviceUuid);
|
|
|
|
if (
|
|
deviceDetails.productDevice.prodType !== ProductType.FOUR_S &&
|
|
deviceDetails.productDevice.prodType !== ProductType.SIX_S
|
|
) {
|
|
throw new BadRequestException('The device type is not supported');
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'An error occurred',
|
|
error.status || 500,
|
|
);
|
|
}
|
|
}
|
|
}
|