All files / source/customerBranch information.js

80.76% Statements 21/26
70.83% Branches 17/24
100% Functions 3/3
80% Lines 20/25

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                2x 2x             6x   7x     3x   1x     1x     1x           7x   7x 3x 3x 3x   1x     1x   1x     1x   1x 1x         7x                                                
import { Sequelize, Op } from 'sequelize';
 
import CustomerBranchDao from '../../database/dao/customerBranch';
import APIError from '../../helpers/error';
import { models } from '../../../config/database';
 
 
async function getCustomerBranch(query, opts = {}) {
  try {
    return CustomerBranchDao.findCustomerBranch(query, opts);
  } catch (error) {
    console.log(error);
    throw new APIError('', error);
  }
}
 
const getCustomerBranches = async (page = 0, pageSize = 0, sorted = [], filtered = [], opts = {}) => {
 
  const sortParams = sorted.length
    ? {
      order: sorted.map(it => {
        switch (it.id) {
          case 'branchName':
            return [Sequelize.col('branch.str_name'), it.desc ? 'DESC' : 'ASC']
            break;
          case 'customerName':
            return [Sequelize.col('customer.str_name'), it.desc ? 'DESC' : 'ASC']
            break;
          default:
            return [it.id, it.desc ? 'DESC' : 'ASC']
            break;
        }
      })
    } : {}
 
  const completeFilter = (filtered.length) ? { [Op.and]: [] } : {};
 
  if (filtered.length) {
    for (let index = 0; index < filtered.length; index++) {
      const element = filtered[index];
      switch (element.id) {
        case 'branchName':
          completeFilter[Op.and].push(
            Sequelize.where(Sequelize.col('branch.str_name'), { [Op.like]: `${element.value}%` })
          );
          break;
        case 'customerName':
          completeFilter[Op.and].push(
            Sequelize.where(Sequelize.col('customer.str_name'), { [Op.like]: `${element.value}%` }),
          );
          break;
        default:
          completeFilter[Op.and].push({ [`${element.id}`]: { [Op.like]: `${element.value}%` } })
          break;
      }
    }
  }
 
  return CustomerBranchDao.findAndCountCustomerBranches(completeFilter, {
    include: [
      {
        required: true,
        model: models.Branch,
        as: 'branch',
        attributes: ['code', 'name']
      }, {
        required: true,
        model: models.Customer,
        as: 'customer',
        attributes: ['code', 'name']
      },
    ],
    ...sortParams,
    offset: page * pageSize,
    limit: pageSize,
    ...opts
  });
}
 
export default {
  getCustomerBranch,
  getCustomerBranches,
};