All files / platform/modules/service/src/Services ServiceService.js

96.97% Statements 32/33
83.33% Branches 5/6
100% Functions 12/12
96.97% Lines 32/33

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    1x   1x       13x       13x 13x       1x       1x 1x                     1x 1x 1x 1x       3x       2x 2x 1x     1x       1x 1x 1x 1x 1x       1x       2x       2x 2x 1x     1x 1x 1x       2x       1x  
'use strict';
 
const _ = require('lodash');
 
const CE = use('C2C/Exceptions');
 
class ServiceService {
  static get inject() {
    return ['C2C/Repositories/ServiceCategoryRepo', 'C2C/Repositories/ServiceRepo'];
  }
 
  constructor(serviceCategoryRepo, serviceRepo) {
    this.serviceCategoryRepo = serviceCategoryRepo;
    this.serviceRepo = serviceRepo;
  }
 
  getAllCategories({ serviceConditions, ...rest }) {
    return this.serviceCategoryRepo.allBy(
      rest,
      {
        services: (builder) => {
          builder.where(_.omit(serviceConditions, ['withTrashed'])).orderBy('order', 'desc');
          Iif (serviceConditions.withTrashed) {
            builder.withTrashed();
          }
        },
      },
      ['*'],
      'order desc',
    );
  }
 
  async createCategory(payload) {
    const maxOrder = await this.serviceCategoryRepo.make().getMax('order');
    payload.order = maxOrder + 1;
    const serviceCategory = await this.serviceCategoryRepo.create(payload);
    return serviceCategory;
  }
 
  updateCategory(conditions, payload) {
    return this.serviceCategoryRepo.updateOrFail(conditions, payload);
  }
 
  async deleteCategory(conditions) {
    const serviceCategory = await this.serviceCategoryRepo.findOne(conditions);
    if (!serviceCategory) {
      throw CE.NotFoundException.raise('errors.notFound');
    }
 
    return this.serviceCategoryRepo.delete(serviceCategory);
  }
 
  async createService(payload) {
    const maxOrder = await this.serviceRepo.make().getMax('order');
    payload.order = maxOrder + 1;
    const service = await this.serviceRepo.create(payload);
    await service.reload();
    return service;
  }
 
  getAllServices(conditions) {
    return this.serviceRepo.allBy({ ...conditions }, undefined, undefined, 'order desc');
  }
 
  updateService(conditions, payload) {
    return this.serviceRepo.updateOrFail(conditions, payload);
  }
 
  async deleteService(conditions) {
    const service = await this.serviceRepo.findOne(conditions);
    if (!service) {
      throw CE.NotFoundException.raise('errors.notFound');
    }
 
    await this.serviceRepo.delete(service);
    await this.serviceRepo.findOneWithTrashed({ id: service.id });
    return service;
  }
 
  async loadRelated(serviceInstance) {
    await serviceInstance.loadMany(['category']);
  }
}
 
module.exports = ServiceService;