All files / lib/models Product.ts

83.33% Statements 15/18
100% Branches 0/0
50% Functions 1/2
83.33% Lines 15/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  1x 1x 1x 1x 1x 1x 1x 1x     1x 1x                                     1x 1x 1x 1x               1x                    
import * as dynogels from 'drandx-dynogels';
import * as uuid from 'uuid';
import * as joi from 'joi';
import { BaseModel } from './BaseModel';
import { STATUS_ENUM, CATEGORY_ENUM } from './Enums';
import { awsConfig, globalConst } from '../config/db/appVariables';
 
dynogels.AWS.config.update(awsConfig);
 
export class Product extends BaseModel {
  public id: string;
  public product: string;
  public presentation: string;
  public status: STATUS_ENUM;
  public requiredPrescription: boolean;
  private tagsName: string;
  public category: string;
  
  constructor() {
    super();
    this.id = uuid.v4();
    this.status = STATUS_ENUM.ACTIVE;
    this.requiredPrescription = false;
    this.category = CATEGORY_ENUM.MEDICINE;
  }
  
  public afterFillFromJSON() {
    const name: string = this.product.toLowerCase().split(' ').join('');
    const id: string = this.id.substring(1, 5);
    this.tagsName = `${name}-${id}`;
  }
 
  public model: dynogels.Model = dynogels.define(`${globalConst.stage}_products`, {
    hashKey: 'id',
    timestamps: false,
    schema: {
      id: joi.string(),
      product: joi.string(),
      presentation: joi.string().allow(null),
      category: joi.string(),
      tagsName: joi.string(),
      status: joi.string(),
      createdAt: joi.number(),
      updatedAt: joi.number(),
      requiredPrescription: joi.boolean(),
    },
    tableName: `${globalConst.stage}_products`,
    indexes: [
      { hashKey: 'status', rangeKey: 'tagsName', type: 'global', name: 'statusIndex',},
      ]
});
}