mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-14 18:05:48 +00:00
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { DeviceMessagesAddDto } from '../dtos/device-messages.dto';
|
|
import { DeviceNotificationRepository } from '@app/common/modules/device/repositories';
|
|
|
|
@Injectable()
|
|
export class DeviceMessagesSubscriptionService {
|
|
constructor(
|
|
private readonly deviceNotificationRepository: DeviceNotificationRepository,
|
|
) {}
|
|
|
|
async addDeviceMessagesSubscription(
|
|
deviceMessagesAddDto: DeviceMessagesAddDto,
|
|
) {
|
|
try {
|
|
return await this.deviceNotificationRepository.save({
|
|
user: {
|
|
uuid: deviceMessagesAddDto.userUuid,
|
|
},
|
|
device: {
|
|
uuid: deviceMessagesAddDto.deviceUuid,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
if (error.code === '23505') {
|
|
throw new HttpException(
|
|
'This User already belongs to this device',
|
|
HttpStatus.BAD_REQUEST,
|
|
);
|
|
}
|
|
throw new HttpException(
|
|
error.message || 'Internal Server Error',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
async getDeviceMessagesSubscription(userUuid: string, deviceUuid: string) {
|
|
try {
|
|
const deviceUserSubscription =
|
|
await this.deviceNotificationRepository.findOne({
|
|
where: {
|
|
user: { uuid: userUuid },
|
|
device: { uuid: deviceUuid },
|
|
},
|
|
});
|
|
|
|
return {
|
|
uuid: deviceUserSubscription.uuid,
|
|
deviceUuid: deviceUserSubscription.deviceUuid,
|
|
userUuid: deviceUserSubscription.userUuid,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
'User device subscription not found',
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
}
|
|
async deleteDeviceMessagesSubscription(
|
|
deviceMessagesAddDto: DeviceMessagesAddDto,
|
|
) {
|
|
try {
|
|
const result = await this.deviceNotificationRepository.delete({
|
|
user: { uuid: deviceMessagesAddDto.userUuid },
|
|
device: { uuid: deviceMessagesAddDto.deviceUuid },
|
|
});
|
|
|
|
return result;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'device not found',
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
}
|
|
}
|