All files / platform/modules/staff/src/Validators SaveStaff.js

100% Statements 14/14
85.71% Branches 6/7
100% Functions 2/2
100% Lines 14/14

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    1x 1x       21x 21x 21x 21x                               21x 12x     21x 21x 6x     21x       28x                   1x  
'use strict';
 
const ValidatorAbstract = use('C2C/Abstracts/ValidatorAbstract');
const Config = use('Config');
 
class SaveStaff extends ValidatorAbstract {
  get rules() {
    const { request } = this.ctx;
    const staffId = request.params.id;
    const ageLimit = Config.get('modules.staff.general.ageLimit');
    const rules = {
      name: 'required|string|max:255',
      phoneNumber: `required|string|max:30|unique:staff,phoneNumber,id,${staffId}`,
      email: `email|max:255|unique:staff,email,id,${staffId}`,
      birthday: `dateFormat:YYYY-MM-DD|beforeOffsetOf:${ageLimit},years`,
      username: 'string|max:255',
      avatar: 'string|imageUrl',
      gender: 'integer',
      active: 'boolean',
      location: 'object',
      'location.postCode': 'integer',
      'location.prefecture': 'string',
      'location.cityOrTown': 'string',
      'location.address': 'string',
    };
 
    if (!staffId) {
      rules.email = `required|${rules.email}`;
    }
 
    const { location } = request.post();
    if (location && (location.prefecture || location.cityOrTown)) {
      rules['location.postCode'] = 'required|integer';
    }
 
    return rules;
  }
 
  get sanitizationRules() {
    return {
      name: 'trim',
      email: 'normalize_email',
      phoneNumber: 'trim',
      gender: 'to_int',
      active: 'to_boolean',
    };
  }
}
 
module.exports = SaveStaff;