feat: Add User Notification and Device Messages Subscription Modules

This commit is contained in:
faris Aljohari
2024-05-26 00:34:51 +03:00
parent fdab3fa687
commit 167cb055c1
15 changed files with 452 additions and 0 deletions

View File

@ -0,0 +1 @@
export * from './user-notification.controller';

View File

@ -0,0 +1,94 @@
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
Put,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { UserNotificationService } from '../services/user-notification.service';
import {
UserNotificationAddDto,
UserNotificationUpdateDto,
} from '../dtos/user-notification.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
@ApiTags('User Notification Module')
@Controller({
version: '1',
path: 'user-notification/subscription',
})
export class UserNotificationController {
constructor(
private readonly userNotificationService: UserNotificationService,
) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post()
async addUserSubscription(
@Body() userNotificationAddDto: UserNotificationAddDto,
) {
try {
const addDetails = await this.userNotificationService.addUserSubscription(
userNotificationAddDto,
);
return {
statusCode: HttpStatus.CREATED,
message: 'User Notification Added Successfully',
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':userUuid')
async fetchUserSubscriptions(@Param('userUuid') userUuid: string) {
try {
const userDetails =
await this.userNotificationService.fetchUserSubscriptions(userUuid);
return {
statusCode: HttpStatus.OK,
message: 'User Notification fetched Successfully',
data: { ...userDetails },
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put()
async updateUserSubscription(
@Body() userNotificationUpdateDto: UserNotificationUpdateDto,
) {
try {
await this.userNotificationService.updateUserSubscription(
userNotificationUpdateDto,
);
return {
statusCode: HttpStatus.OK,
message: 'User subscription updated Successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -0,0 +1 @@
export * from './user-notification.dto';

View File

@ -0,0 +1,44 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
export class UserNotificationAddDto {
@ApiProperty({
description: 'user uuid',
required: true,
})
@IsString()
@IsNotEmpty()
userUuid: string;
@ApiProperty({
description: 'subscription uuid',
required: true,
})
@IsString()
@IsNotEmpty()
subscriptionUuid: string;
}
export class UserNotificationUpdateDto {
@ApiProperty({
description: 'user uuid',
required: true,
})
@IsString()
@IsNotEmpty()
userUuid: string;
@ApiProperty({
description: 'subscription uuid',
required: true,
})
@IsString()
@IsNotEmpty()
subscriptionUuid: string;
@ApiProperty({
description: 'active',
required: true,
})
@IsBoolean()
@IsNotEmpty()
active: boolean;
}

View File

@ -0,0 +1 @@
export * from './user-notification.service';

View 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,
);
}
}
}

View File

@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { UserNotificationRepositoryModule } from '@app/common/modules/user-notification/user.notification.repository.module';
import { UserNotificationRepository } from '@app/common/modules/user-notification/repositories';
import { UserNotificationService } from 'src/user-notification/services';
import { UserNotificationController } from 'src/user-notification/controllers';
@Module({
imports: [ConfigModule, UserNotificationRepositoryModule],
controllers: [UserNotificationController],
providers: [UserNotificationRepository, UserNotificationService],
exports: [UserNotificationService],
})
export class UserNotificationModule {}