src/user/user-notifcation.service.ts
Methods |
|
constructor(userNotificationRepository: Repository<DbUserNotification>, userService: UserService)
|
|||||||||
Defined in src/user/user-notifcation.service.ts:13
|
|||||||||
Parameters :
|
Async addUserFollowerNotification |
addUserFollowerNotification(userId: number, followedUserId: number)
|
Defined in src/user/user-notifcation.service.ts:63
|
Returns :
unknown
|
Async addUserHighlightNotification |
addUserHighlightNotification(highlightId: number, userId: number, highlightUserId: number)
|
Defined in src/user/user-notifcation.service.ts:87
|
Returns :
unknown
|
Async addUserHighlightReactionNotification |
addUserHighlightReactionNotification(userId: number, highlightUserId: number, highlightId: number)
|
Defined in src/user/user-notifcation.service.ts:75
|
Returns :
unknown
|
Async addUserNotification | ||||||
addUserNotification(userNotification: Partial<DbUserNotification>)
|
||||||
Defined in src/user/user-notifcation.service.ts:52
|
||||||
Parameters :
Returns :
unknown
|
baseQueryBuilder |
baseQueryBuilder()
|
Defined in src/user/user-notifcation.service.ts:20
|
Returns :
SelectQueryBuilder<DbUserNotification>
|
Async findAllByUserId | |||||||||
findAllByUserId(userId: number, pageOptionsDto: PageOptionsDto)
|
|||||||||
Defined in src/user/user-notifcation.service.ts:26
|
|||||||||
Parameters :
Returns :
unknown
|
Async markNotificationsAsRead | ||||||
markNotificationsAsRead(notificationIds: number[])
|
||||||
Defined in src/user/user-notifcation.service.ts:99
|
||||||
Parameters :
Returns :
any
|
import { Injectable } from "@nestjs/common";
import { Repository, SelectQueryBuilder } from "typeorm";
import { InjectRepository } from "@nestjs/typeorm";
import { PageMetaDto } from "../common/dtos/page-meta.dto";
import { PageDto } from "../common/dtos/page.dto";
import { PageOptionsDto } from "../common/dtos/page-options.dto";
import { DbUserNotification } from "./entities/user-notification.entity";
import { UserNotificationTypes, userNotificationTypes } from "./entities/user-notification.constants";
import { UserService } from "./services/user.service";
@Injectable()
export class UserNotificationService {
constructor(
@InjectRepository(DbUserNotification, "ApiConnection")
private userNotificationRepository: Repository<DbUserNotification>,
private userService: UserService
) {}
baseQueryBuilder(): SelectQueryBuilder<DbUserNotification> {
const builder = this.userNotificationRepository.createQueryBuilder("user_notifications");
return builder;
}
async findAllByUserId(userId: number, pageOptionsDto: PageOptionsDto) {
const queryBuilder = this.baseQueryBuilder();
queryBuilder
.innerJoin("users", "users", "user_notifications.user_id=users.id")
.innerJoinAndSelect("user_notifications.from_user", "from_user")
.where("user_id = :userId", { userId })
.andWhere("user_notifications.type IN (:...userNotificationTypes)", { userNotificationTypes })
.orderBy("user_notifications.notified_at", "DESC");
queryBuilder.offset(pageOptionsDto.skip).limit(pageOptionsDto.limit);
const entities = await queryBuilder.getMany();
const itemCount = await queryBuilder.getCount();
const notificationIds = entities
.filter((notification) => !notification.read_at)
.map((notification) => notification.id);
await this.markNotificationsAsRead(notificationIds);
const pageMetaDto = new PageMetaDto({ itemCount, pageOptionsDto });
return new PageDto(entities, pageMetaDto);
}
async addUserNotification(userNotification: Partial<DbUserNotification>) {
return this.userNotificationRepository.save({
type: userNotification.type,
notified_at: new Date(),
user_id: userNotification.user_id,
message: userNotification.message,
from_user_id: userNotification.from_user_id,
meta_id: userNotification.meta_id,
});
}
async addUserFollowerNotification(userId: number, followedUserId: number) {
const followUser = await this.userService.tryFindUserOrMakeStub({ userId });
return this.addUserNotification({
type: UserNotificationTypes.Follow,
user_id: followedUserId,
from_user_id: userId,
message: `${followUser.login} followed you`,
meta_id: followUser.login,
});
}
async addUserHighlightReactionNotification(userId: number, highlightUserId: number, highlightId: number) {
const followUser = await this.userService.tryFindUserOrMakeStub({ userId });
return this.addUserNotification({
type: UserNotificationTypes.HighlightReaction,
user_id: highlightUserId,
from_user_id: userId,
message: `${followUser.login} reacted to your highlight`,
meta_id: `${highlightId}`,
});
}
async addUserHighlightNotification(highlightId: number, userId: number, highlightUserId: number) {
const followUser = await this.userService.tryFindUserOrMakeStub({ userId });
return this.addUserNotification({
type: UserNotificationTypes.HighlightCreated,
user_id: highlightUserId,
from_user_id: userId,
message: `${followUser.login} created a new highlight`,
meta_id: `${highlightId}`,
});
}
async markNotificationsAsRead(notificationIds: number[]) {
const updates = notificationIds.map(async (id) =>
this.userNotificationRepository.update(id, { read_at: new Date() })
);
await Promise.all(updates);
}
}