mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 21:24:53 +00:00
feat: Add User Notification and Device Messages Subscription Modules
This commit is contained in:
1
src/user-notification/services/index.ts
Normal file
1
src/user-notification/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './user-notification.service';
|
||||
83
src/user-notification/services/user-notification.service.ts
Normal file
83
src/user-notification/services/user-notification.service.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
UserNotificationAddDto,
|
||||
UserNotificationUpdateDto,
|
||||
} from '../dtos/user-notification.dto';
|
||||
import { UserNotificationRepository } from '@app/common/modules/user-notification/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class UserNotificationService {
|
||||
constructor(
|
||||
private readonly userNotificationRepository: UserNotificationRepository,
|
||||
) {}
|
||||
|
||||
async addUserSubscription(userNotificationAddDto: UserNotificationAddDto) {
|
||||
try {
|
||||
return await this.userNotificationRepository.save({
|
||||
user: {
|
||||
uuid: userNotificationAddDto.userUuid,
|
||||
},
|
||||
subscriptionUuid: userNotificationAddDto.subscriptionUuid,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.code === '23505') {
|
||||
throw new HttpException(
|
||||
'This User already has this subscription uuid',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async fetchUserSubscriptions(userUuid: string) {
|
||||
try {
|
||||
const userNotifications = await this.userNotificationRepository.find({
|
||||
where: {
|
||||
user: { uuid: userUuid },
|
||||
active: true,
|
||||
},
|
||||
});
|
||||
return {
|
||||
userUuid,
|
||||
subscriptionUuids: [
|
||||
...userNotifications.map((sub) => sub.subscriptionUuid),
|
||||
],
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'User subscription not found',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
async updateUserSubscription(
|
||||
userNotificationUpdateDto: UserNotificationUpdateDto,
|
||||
) {
|
||||
try {
|
||||
const result = await this.userNotificationRepository.update(
|
||||
{
|
||||
user: { uuid: userNotificationUpdateDto.userUuid },
|
||||
subscriptionUuid: userNotificationUpdateDto.subscriptionUuid,
|
||||
},
|
||||
{ active: userNotificationUpdateDto.active },
|
||||
);
|
||||
|
||||
if (result.affected === 0) {
|
||||
throw new HttpException(
|
||||
'Subscription uuid not found',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user