#Quota limits and TypeORM decrements

3 messages · Page 1 of 1 (latest)

old venture
#

I have a Quota service with the following method:

  async decrementWithQuota(user: IUser) {
    if (!user) {
      throw new UnauthorizedException();
    }

    const today = new Date();
    const updateResult = await this.quotaRepository.decrement(
      {
        user,
        year: today.getFullYear(),
        month: today.getMonth(),
        day: today.getDate(),
        limit: MoreThan(0)
      },
      'limit',
      1
    );

    if (updateResult.affected === 1) {
      return updateResult;
    }

    if (updateResult.affected === 0) {
      throw new ForbiddenException('Exceeded todays quota');
    }

    throw new ConflictException(
      'too many quotas being affected by single increment.'
    );
  }

should i use a guard, a pipe or an interceptor to call this method on requests with quota? While I am pretty comfortable playing around with Nest, these kind of decisions seem to confuse me. I also want an user to be passed to the method (which I absolutely can do with a Pipe in my @User decorator), but i want your input

#

there's also a very specific reason i use years, months and days separately - i run an unique index with thhat and the user

#

pipes are generally used for validating and transforming, interceptors are more general purpose (think pagination and caching), and i suppose guards might be the best use here. But I have to pass my user into the guard somehow after the OAuth/JWT guards