All files / src/entities variation_field.ts

57.14% Statements 52/91
0% Branches 0/16
25% Functions 1/4
58.82% Lines 50/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x   1x 1x 1x     1x   1x 1x 1x 1x   1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x                                                                                                                     1x  
import { cloneDeepWith } from 'lodash';
import { DiscountGroup } from './discount_group.js';
import { Entity } from '../entity.js';
import { InventoryGroup } from './inventory_group.js';
import { Product } from './product.js';
import { Variation } from './variation.js';
import { VariationFieldsOption } from './variation_fields_option.js';
import { FieldType } from '../constants/field_types.js';
 
export class VariationField extends Entity {
  protected static resourceName = 'variation_fields';
  protected static singularName = 'variationField';
  protected static pluralName = 'variationFields';
 
  @VariationField.property({type: Date})
  public archived?: Date | null;
 
  @VariationField.property()
  public id?: number;
 
  @VariationField.property()
  public position?: number;
 
  @VariationField.property()
  public required?: boolean;
 
  @VariationField.property()
  public independent?: boolean;
 
  @VariationField.property()
  public name?: string;
 
  @VariationField.property()
  public instructions?: string;
 
  @VariationField.property({type: String})
  public placeholder?: string | null;
 
  @VariationField.property()
  public defaultValue?: string;
 
  @VariationField.property()
  public currency?: string;
 
  @VariationField.property()
  public fieldType?: FieldType;
 
  @VariationField.property()
  public margin?: number;
 
  @VariationField.property()
  public variationCost?: number;
 
  @VariationField.property({type: 'DiscountGroup'})
  public variationCostDiscountGroup?: DiscountGroup | null;
 
  @VariationField.property()
  public variationUnitCost?: number;
 
  @VariationField.property({embeddedByDefault: false})
  public buyUnitCost?: number;
 
  @VariationField.property({embeddedByDefault: false})
  public buyCost?: number;
 
  @VariationField.property({type: 'DiscountGroup'})
  public variationUnitCostDiscountGroup?: DiscountGroup | null;
 
  @VariationField.property()
  public rows?: number;
 
  @VariationField.property({type: Number})
  public fieldMin?: number | null;
 
  @VariationField.property({type: Number})
  public fieldMax?: number | null;
 
  @VariationField.property()
  public allowDecimal?: boolean;
 
  @VariationField.property()
  public isHtml?: boolean;
 
  @VariationField.property()
  public considerBusinessHours?: boolean;
 
  @VariationField.property()
  public shippingTimeIncluded?: boolean;
 
  @VariationField.property()
  public sellerProductEditable?: boolean;
 
  @VariationField.property()
  public multipleSelect?: boolean;
 
  @VariationField.property()
  public showFilePreview?: boolean;
 
  @VariationField.property()
  public allowFileMultiple?: boolean;
 
  @VariationField.property()
  public allowFileJpeg?: boolean;
 
  @VariationField.property()
  public allowFileGif?: boolean;
 
  @VariationField.property()
  public allowFilePdf?: boolean;
 
  @VariationField.property()
  public allowFilePng?: boolean;
 
  @VariationField.property()
  public allowFileAi?: boolean;
 
  @VariationField.property()
  public product?: Product;
 
  @VariationField.property({arrayType: 'VariationFieldsOption'})
  public selectedBy?: VariationFieldsOption[];
 
  @VariationField.property({type: 'InventoryGroup'})
  public inventoryGroup?: InventoryGroup;
 
  @VariationField.property({type: 'InventoryGroup'})
  public linkedInventoryGroup?: InventoryGroup;
 
  @VariationField.property({arrayType: 'Variation'})
  public variations?: Variation[];
 
  @VariationField.property({arrayType: 'VariationFieldsOption'})
  public options?: VariationFieldsOption[];
 
  public isSelectable = () => {
    Iif (this.fieldType === undefined) {
      throw new Error('fieldType is undefined, did you forget to embed it?');
    }
    const selectable = new Set([FieldType.SELECT,
      FieldType.CHECKBOX,
      FieldType.RADIO,
      FieldType.IMAGE_SELECT,
      FieldType.COLOUR_SELECT]);
    return selectable.has(this.fieldType);
  };
 
  public buildEmptyVariation = () => {
    Iif (this.defaultValue === undefined) {
      throw new Error('defaultValue is undefined, did you forget to embed it?');
    }
    Iif (this.variationCost === undefined) {
      const err = 'variationCost is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    Iif (this.options === undefined) {
      throw new Error('options is undefined, did you forget to embed it?');
    }
    const result = new this.merchi.Variation(this.merchi);
    result.selectableOptions = [];
    if (this.isSelectable()) {
      let onceOffCost = 0;
      const value = [];
      for (const option of this.options) {
        Iif ((this.sellerProductEditable && option.include) ||
          (!this.sellerProductEditable && option.default)) {
          Iif (option.variationCost === undefined) {
            throw new Error('option.variationCost is undefined, did you ' +
                            'forget to embed it?');
          }
          value.push(option.id);
          onceOffCost += option.variationCost;
        }
        result.selectableOptions.push(option.buildVariationOption());
      }
      result.value = value.join();
      result.onceOffCost = onceOffCost;
    } else {
      result.value = this.defaultValue;
      result.onceOffCost = this.variationCost;
    }
    result.unitCostTotal = 0;
    result.cost = result.onceOffCost;
    function customiser(value: any, index: any) {
      Iif (index === 'merchi') {
        return value;
      }
    }
    result.variationField = cloneDeepWith(this, customiser);
    return result;
  };
}