File

src/codes/codes.service.ts

Index

Properties
Methods

Constructor

constructor(utilsService: UtilsService, databaseService: DatabaseService)
Parameters :
Name Type Optional
utilsService UtilsService No
databaseService DatabaseService No

Methods

Async createCode
createCode(type, expiration: number, user?: string, excess?: string, encryptExcess)
Parameters :
Name Type Optional Default value
type No
expiration number No
user string Yes
excess string Yes
encryptExcess No true
Returns : Promise<CodeDescriptor>
Public Async delete
delete(code: string)
Parameters :
Name Type Optional
code string No
Returns : any
Async fetchCode
fetchCode(code: string)
Parameters :
Name Type Optional
code string No
Returns : Promise<CodeDescriptor | null>
Public Async getCodePool
getCodePool()
Returns : unknown
Public Async getUsedCodes
getUsedCodes()
Returns : unknown
Public Async isCodeExpired
isCodeExpired(code: string, expiry?: string)
Parameters :
Name Type Optional
code string No
expiry string Yes
Returns : Promise<boolean>
Public Async isCodeUsed
isCodeUsed(code: string)
Parameters :
Name Type Optional
code string No
Returns : Promise<boolean>
Async isCodeValid
isCodeValid(code: string)
Parameters :
Name Type Optional
code string No
Returns : Promise<boolean>
Async setCodeUsed
setCodeUsed(code: string)
Parameters :
Name Type Optional
code string No
Returns : any

Properties

Private Readonly logger
Default value : new Logger(CodesService.name)
import { Injectable, Logger } from '@nestjs/common';
import { DatabaseService } from '../database/database.service';
import { UtilsService } from '../utils/utils.service';
import { CodesEnum } from '../utils/enums/codes.enum';
import { CodeDescriptor } from '../../types';
@Injectable()
export class CodesService {
  private readonly logger = new Logger(CodesService.name);
  constructor(
    private readonly utilsService: UtilsService,
    private readonly databaseService: DatabaseService,
  ) {}

  async createCode(
    type: keyof typeof CodesEnum,
    expiration: number,
    user?: string,
    excess?: string,
    encryptExcess = true,
  ): Promise<CodeDescriptor> {
    expiration = 1000 * (60 * expiration);
    this.logger.debug(`Generating code ${type}`);
    return (await this.databaseService.write.codes.create({
      data: {
        user,
        code: await this.utilsService.generateSnowflake(),
        issued_at: Date.now().toString(),
        expiration: (Date.now() + expiration).toString(),
        type,
        excess:
          typeof excess === 'string'
            ? encryptExcess
              ? this.utilsService.encrypt(excess)
              : excess
            : undefined,
      },
    })) as CodeDescriptor;
  }

  async fetchCode(code: string): Promise<CodeDescriptor | null> {
    if (!code) {
      return null;
    }
    return (await this.databaseService.read.codes.findFirst({
      where: {
        code: code || undefined,
      },
    })) as CodeDescriptor;
  }

  async isCodeValid(code: string): Promise<boolean> {
    return (
      (await this.databaseService.read.codes.count({
        where: { code },
      })) > 0
    );
  }

  async setCodeUsed(code: string) {
    await this.databaseService.write.codes.update({
      where: {
        code,
      },
      data: { used: true },
    });
  }

  public async isCodeExpired(code: string, expiry?: string): Promise<boolean> {
    expiry = expiry || (await this.fetchCode(code))?.expiration;
    if (!expiry) {
      return false;
    }
    this.logger.debug('Fetching code yo');
    return this.utilsService.didTimeStampExpire(parseInt(expiry));
  }

  public async isCodeUsed(code: string): Promise<boolean> {
    const r = await this.databaseService.read.codes.count({
      where: { code, used: false },
    });
    return r !== 0;
  }

  public async getUsedCodes() {
    return await this.databaseService.read.codes.findMany({
      where: {
        used: true,
      },
    });
  }

  public async delete(code: string) {
    await this.databaseService.write.codes.delete({
      where: {
        code,
      },
    });
  }
  public async getCodePool() {
    return await this.databaseService.read.codes.count();
  }
}

results matching ""

    No results matching ""