File

src/user/user-notifcation.service.ts

Index

Methods

Constructor

constructor(userNotificationRepository: Repository<DbUserNotification>, userService: UserService)
Parameters :
Name Type Optional
userNotificationRepository Repository<DbUserNotification> No
userService UserService No

Methods

Async addUserFollowerNotification
addUserFollowerNotification(userId: number, followedUserId: number)
Parameters :
Name Type Optional
userId number No
followedUserId number No
Returns : unknown
Async addUserHighlightNotification
addUserHighlightNotification(highlightId: number, userId: number, highlightUserId: number)
Parameters :
Name Type Optional
highlightId number No
userId number No
highlightUserId number No
Returns : unknown
Async addUserHighlightReactionNotification
addUserHighlightReactionNotification(userId: number, highlightUserId: number, highlightId: number)
Parameters :
Name Type Optional
userId number No
highlightUserId number No
highlightId number No
Returns : unknown
Async addUserNotification
addUserNotification(userNotification: Partial<DbUserNotification>)
Parameters :
Name Type Optional
userNotification Partial<DbUserNotification> No
Returns : unknown
baseQueryBuilder
baseQueryBuilder()
Async findAllByUserId
findAllByUserId(userId: number, pageOptionsDto: PageOptionsDto)
Parameters :
Name Type Optional
userId number No
pageOptionsDto PageOptionsDto No
Returns : unknown
Async markNotificationsAsRead
markNotificationsAsRead(notificationIds: number[])
Parameters :
Name Type Optional
notificationIds number[] No
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);
  }
}

results matching ""

    No results matching ""