Skip to content

Commit

Permalink
created the notification feature for users
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTemoye committed Jun 26, 2024
1 parent fd682b3 commit fa65459
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 12 deletions.
35 changes: 24 additions & 11 deletions src/shared/schema/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ export type UserDocument = HydratedDocument<User>;

@Schema({ timestamps: true })
export class User {

@Prop({ type: Types.ObjectId, ref: 'User', required: true })
userId: string

@Prop({ required: true })
message: string;

@Prop({ default: false })
read: boolean;

@Prop({ type: [{ message: String, read: Boolean, createdAt: Date }], default: [] })
notifications: { message: string; read: boolean; createdAt: Date }[];

@Prop({ index: true })
firstName: string;

Expand Down Expand Up @@ -65,8 +78,8 @@ export class User {

@Prop({ index: true })
gender: string;



@Prop({
type: {
Expand Down Expand Up @@ -116,15 +129,15 @@ export class User {

@Prop(raw(SocialsRawSchema))
socials: Socials;
@Prop({
index: true,
default: VerificationStatus.NOT_VERIFIED
})
verificationStatus: VerificationStatus;

@Prop({ type: Date })
nextVerificationRequestDate: Date;

@Prop({
index: true,
default: VerificationStatus.NOT_VERIFIED
})
verificationStatus: VerificationStatus;

@Prop({ type: Date })
nextVerificationRequestDate: Date;
}

export const UserSchema = SchemaFactory.createForClass(User);
Expand Down
10 changes: 10 additions & 0 deletions src/users/dto/create-notification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNotEmpty, IsString } from "class-validator";

export class CreateNotificationDto {
@ApiProperty()
@IsString()
@IsNotEmpty()
message: any;

}
8 changes: 8 additions & 0 deletions src/users/dto/update-notification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IsMongoId, IsNotEmpty } from 'class-validator';

export class UpdateNotificationDto {
@IsMongoId()
@IsNotEmpty()
message: string;
read: boolean;
}
32 changes: 31 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import { ApiReq, userRoles, userStatuses } from 'src/shared/interfaces';
import { CreateUserDto } from 'src/shared/dtos/create-user.dto';
import { UserChangePasswordDto } from './dto/user-change-password.dto';
import { UserAddPhotoDto } from './dto/user-add-photo.dto';
import { CreateNotificationDto } from './dto/create-notification.dto';

@ApiTags('users')
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
constructor(private readonly usersService: UsersService) { }

@Post()
create(@Request() req: ApiReq, @Body() createUserDto: CreateUserDto) {
Expand Down Expand Up @@ -127,4 +128,33 @@ export class UsersController {
) {
return this.usersService.requestVerification(req, userId);
}

@ApiBearerAuth()
@UseGuards(JwtUsersGuard)
@Post(':userId/create-notification')
async createNotification(
@Param('userId') userId: string,
@Body() createNotificationDto: CreateNotificationDto,

) {
const { message } = createNotificationDto;
return this.usersService.createNotification(userId, message)
}

@ApiBearerAuth()
@UseGuards(JwtUsersGuard)
@Get(':userId/get-notifications')
async getAllNotificationsForUser(
@Param('userId') userId: string
) {
return this.usersService.getAllNotificationsForUser(userId);
}

@Patch(':userId/is-read-notifications/:notificationId')
async markNotificationAsRead(
@Param('userId') userId: string,
@Param('notificationId') notificationId: string,
) {
return this.usersService.markNotificationAsRead(userId, notificationId);
}
}
35 changes: 35 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
} from 'src/shared/interfaces';
import { UserInviteDto } from './dto/user-invite.dto';
import { VerificationStatus } from 'src/shared/interfaces/user.type';
import { UpdateNotificationDto } from './dto/update-notification.dto';
import { CreateNotificationDto } from './dto/create-notification.dto';
@Injectable()
export class UsersService {
constructor(
Expand Down Expand Up @@ -299,5 +301,38 @@ export class UsersService {
//5. Save the updated user
await user.save();
}

async createNotification(userId: string, message: string) {
const notification = { message: message, read: false, createdAt: new Date() };
return this.userModel.findOneAndUpdate(
{ _id: userId },
{ $push: { notifications: notification } },
{ new: true }
).exec();

}

async getAllNotificationsForUser(userId) {
const user = await this.userModel.findById(userId, 'notifications').exec();
if (!user) {
throw new NotFoundException(`User with ID ${userId} not found`);
}
return user.notifications;
}
async updateNotification(userId: string, notificationId: string, updateNotificationDto: UpdateNotificationDto): Promise<User> {
return this.userModel.findOneAndUpdate(
{ _id: userId, 'notifications._id': notificationId },
{ $set: { 'notifications.$.message': updateNotificationDto.message } },
{ new: true }
).exec();
}
async markNotificationAsRead(userId: string, notificationId: string): Promise<User> {
return this.userModel.findOneAndUpdate(
{ _id: userId, 'notifications._id': notificationId },
{ $set: { 'notifications.$.read': true } },
{ new: true }
).exec();
}

}

0 comments on commit fa65459

Please sign in to comment.