All files / source/typeOrders information.js

100% Statements 41/41
100% Branches 22/22
100% Functions 16/16
100% Lines 41/41

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            2x         1x       1x                                                                       1x       1x                     1x 1x             1x       1x         1x       1x   1x       3x         3x         3x 2x 2x 2x                     2x 4x   1x 1x 1x 2x   2x 4x   1x 1x 1x 2x     3x       3x         3x 2x 2x                 2x 1x 1x 1x 2x       1x                        
/* eslint-disable prettier/prettier */
import { Op } from 'sequelize';
import TypeOrderDao from '../../database/dao/typeOrder';
import DepositsDao from '../../database/dao/deposit';
 
async function getAllTypeOrders(query = {}, opts = {}) {
  const typeOrders = await TypeOrderDao.findOrders(query, {
    raw: true,
    ...opts,
  });
 
  return typeOrders;
}
 
async function getTypeOrder(query, opts = {}) {
  const typeOrders = await TypeOrderDao.findOrder(query, {
    attributes: [
      'inboundDeposits',
      'type',
      'customerReturn',
      'productionSeparation',
      'automaticCheckAfterPicking',
      'restrictionPicking',
      'usesCableCuttingPlan',
      'inboundCheckPlate',
      'partialInboundCheck',
      'requestWeightDimensions',
      'mandatoryWeightDimensions',
      'preConference',
      'receiveLeftover',
      'awaitBilling',
      'usesRequestManual',
      'allocateBalanceDeposits',
      'automaticAllocation',
      'automaticPickingConfirmation',
      'urgentAutoAssignSeparator',
      'documentSeparationUzSuggestion',
      'prefix',
      'fullAllocation',
      'preStorageStage',
      'separationStage',
      'columnOrderSeparation',
      'inboundManualRequest',
      'inboundColumnOrderCheck',
      'inboundColumnOrderStorage',
      'outboundColumnOrderCheck',
      'automaticStorage',
    ],
    ...opts,
  });
 
  return typeOrders;
}
 
async function getOptionsTypeOrders(opts = {}) {
  const typeOrders = await TypeOrderDao.findOrders({}, {
    attributes: [
      'id',
      'type',
      'name',
      'branchCode'
    ],
    raw: true,
    ...opts
  });
 
  const result = typeOrders.map(order => {
    return {
      branch: order.branchCode,
      value: order.type,
      label: `${order.type} - ${order.name}`,
    }
  });
 
  return result;
}
 
async function getTypeOrderValid(query, opts = {}) {
  const typeOrders = await TypeOrderDao.findOrder(query, {
    raw: true,
    ...opts,
  });
 
  return typeOrders;
}
 
async function findTypeOrders(query, opt = {}) {
  const typeOrders = await TypeOrderDao.findOrder(query, opt);
 
  return typeOrders;
}
 
async function getDepositsOptions(query, opt = {}) {
  const typeOrders = await TypeOrderDao.findOrder(query, {
    attributes: ['inboundDeposits', 'allocateBalanceDeposits'],
    ...opt,
  });
 
  const ret = {
    inboundDeposits: [], 
    allocateBalanceDeposits: [],
  };
 
  if (typeOrders) {
    const splittedInboundDeposits = (typeOrders.inboundDeposits || '').split('|');
    const splittedAllocateBalanceDeposits = (typeOrders.allocateBalanceDeposits || '').split('|');
    const depositsDb = await DepositsDao.findDeposits(
      {
        [Op.or]: [
          { code: { [Op.in]: splittedInboundDeposits }, resale: true },
          { code: { [Op.in]: splittedAllocateBalanceDeposits }, allocateBalance: true },
        ],
        inventory: false,
      },
      { attributes: ['code', 'resale', 'allocateBalance'] }
    );
 
    ret.inboundDeposits = depositsDb.filter(
      item => item.resale && splittedInboundDeposits.includes(item.code)
    ).sort((a, b) => {
      const indexA = splittedInboundDeposits.indexOf(a.code);
      const indexB = splittedInboundDeposits.indexOf(b.code);
      return indexA - indexB;
    }).map(item => item.code);
 
    ret.allocateBalanceDeposits = depositsDb.filter(
      item => item.allocateBalance && splittedAllocateBalanceDeposits.includes(item.code)
    ).sort((a, b) => {
      const indexA = splittedAllocateBalanceDeposits.indexOf(a.code);
      const indexB = splittedAllocateBalanceDeposits.indexOf(b.code);
      return indexA - indexB;
    }).map(item => item.code);
  }
 
  return ret;
}
 
async function getInboundDepositsOptions(query, opt = {}) {
  const typeOrders = await TypeOrderDao.findOrder(query, {
    attributes: ['inboundDeposits'],
    ...opt,
  });
 
  if (typeOrders) {
    const splittedInboundDeposits = (typeOrders.inboundDeposits || '').split('|');
    const depositsDb = await DepositsDao.findDeposits(
      {
        code: { [Op.in]: splittedInboundDeposits }, 
        resale: true,
        inventory: false,
      },
      { attributes: ['code', 'name'] }
    );
 
    return depositsDb.sort((a, b) => {
      const indexA = splittedInboundDeposits.indexOf(a.code);
      const indexB = splittedInboundDeposits.indexOf(b.code);
      return indexA - indexB;
    }).map(item => ({ value: item.code, label: item.name }));
 
  }
 
  return [];
}
 
export default {
  getAllTypeOrders,
  getTypeOrder,
  getTypeOrderValid,
  findTypeOrders,
  getDepositsOptions,
  getOptionsTypeOrders,
  getInboundDepositsOptions
};