typeorm#InsertEvent TypeScript Examples

The following examples show how to use typeorm#InsertEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: comments.entity.ts    From NestJs-youtube with MIT License 6 votes vote down vote up
/**
   * Called before post insertion.
   */

  async afterInsert(event: InsertEvent<CommentEntity>) {
    const postRepo: Repository<PostEntity> = event.connection.manager.getRepository<
      PostEntity
    >('posts');
    const commentRepo: Repository<CommentEntity> = event.connection.manager.getRepository<
      CommentEntity
    >('comments');
    commentRepo
      .count({
        where: { post: { id: event.entity.post.id } },
      })
      .then((count: number) => {
        postRepo.update({ id: event.entity.post.id }, { comments_num: count });
      });
  }
Example #2
Source File: bill.subscriber.ts    From bank-server with MIT License 6 votes vote down vote up
public async afterInsert(event: InsertEvent<BillEntity>): Promise<void> {
    const rootEmail = this._configService.get('BANK_ROOT_EMAIL');
    const authorEmail = this._configService.get('BANK_AUTHOR_EMAIL');

    if (![rootEmail, authorEmail].includes(event.entity.user.email)) {
      await this._initRegisterPromotion(event.entity);

      await Promise.all([
        this._initWelcomeMessage(event.entity.user),
        this._initWelcomeTransfer(event.entity),
      ]);
    }
  }
Example #3
Source File: user-auth-forgotten-password.subscriber.ts    From bank-server with MIT License 6 votes vote down vote up
async beforeInsert({
    entity,
  }: InsertEvent<UserAuthForgottenPasswordEntity>): Promise<void> {
    if (entity.hashedToken) {
      /**
       * the token is longer than 72 characters, so it needs to be encoded first with sha256
       */
      const hashedToken = UtilsService.encodeString(entity.hashedToken);

      entity.hashedToken = await UtilsService.generateHash(hashedToken);
    }
  }
Example #4
Source File: question.subscriber.ts    From office-hours with GNU General Public License v3.0 6 votes vote down vote up
async afterInsert(event: InsertEvent<QuestionModel>): Promise<void> {
    const numberOfQuestions = await QuestionModel.waitingInQueue(
      event.entity.queueId,
    ).getCount();

    if (numberOfQuestions === 0) {
      const staff = (
        await QueueModel.findOne(event.entity.queueId, {
          relations: ['staffList'],
        })
      ).staffList;

      staff.forEach((staff) => {
        this.notifService.notifyUser(
          staff.id,
          NotifMsgs.ta.STUDENT_JOINED_EMPTY_QUEUE,
        );
      });
    }

    // Send all listening clients an update
    await this.queueSSEService.updateQuestions(event.entity.queueId);
  }
Example #5
Source File: placement.subscriber.ts    From rest-api.ts with MIT License 5 votes vote down vote up
async afterInsert({entity, manager}: InsertEvent<Placement>) {
    const productId = entity.product.id;
    const product = await manager.findOneOrFail(Product, {id: productId});
    product.quantity -= entity.quantity;
    await manager.save(product);

    await this.updateOrderTotal(manager, entity.order);
  }
Example #6
Source File: user-auth.subscriber.ts    From bank-server with MIT License 5 votes vote down vote up
beforeInsert(event: InsertEvent<UserAuthEntity>): void {
    if (event.entity.password) {
      event.entity.password = UtilsService.generateHash(event.entity.password);
    }
  }
Example #7
Source File: user.subscriber.ts    From bank-server with MIT License 5 votes vote down vote up
beforeInsert(event: InsertEvent<UserEntity>): void {
    event.entity.firstName = UtilsService.capitalizeName(
      event.entity.firstName,
    );
    event.entity.lastName = UtilsService.capitalizeName(event.entity.lastName);
  }
Example #8
Source File: desktop-notif-subscriber.ts    From office-hours with GNU General Public License v3.0 5 votes vote down vote up
async afterInsert(event: InsertEvent<DesktopNotifModel>): Promise<void> {
    await this.notifService.notifyDesktop(
      event.entity,
      "You've successfully signed up for desktop notifications!",
    );
  }