All files / platform/modules/customer/src/Services CustomerService.js

100% Statements 23/23
100% Branches 6/6
100% Functions 6/6
100% Lines 22/22

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    1x   1x       11x       11x       2x 2x   2x       3x       4x 4x 4x 4x 1x 1x 1x     4x                         2x 2x 1x     1x   1x       1x  
'use strict';
 
const _ = require('lodash');
 
const CE = use('C2C/Exceptions');
 
class CustomerService {
  static get inject() {
    return ['C2C/Repositories/CustomerRepo'];
  }
 
  constructor(customerRepo) {
    this.customerRepo = customerRepo;
  }
 
  async create(customerInfo) {
    const customer = await this.customerRepo.create({ ...customerInfo });
    await customer.reload();
 
    return customer;
  }
 
  updateOrFail(conditions, payload) {
    return this.customerRepo.updateOrFail(conditions, payload);
  }
 
  findByPage({ userId, page, limit, active, sort, keyword }) {
    const conditions = { userId };
    const search = {};
    if (active) conditions.active = active === 'true';
    if (!_.isEmpty(keyword)) {
      search.name = keyword;
      search.email = keyword;
      search.phoneNumber = keyword;
    }
 
    return this.customerRepo.paginate(
      page,
      limit,
      conditions,
      {
        address: null,
      },
      sort,
      search,
    );
  }
 
  async getDetails(conditions) {
    const customer = await this.customerRepo.findOne({ ...conditions });
    if (!customer) {
      throw CE.NotFoundException.raise('errors.notFound');
    }
 
    await customer.loadMany(['address']);
 
    return customer;
  }
}
 
module.exports = CustomerService;