All files / controllers lots.js

100% Statements 41/41
100% Branches 18/18
100% Functions 4/4
100% Lines 40/40

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            4x 4x   3x 3x   3x   3x       3x       3x                       1x 1x     1x         2x 2x   2x       1x       1x     1x 1x     1x         2x 2x   2x   1x       1x     1x 1x     1x         4x 4x   4x   3x 3x 3x   3x 3x                 3x   3x           1x 1x     1x                    
import LotInformation from '../source/lots/information';
import LotCreate from '../source/lots/create';
import UserInformation from '../source/users/information';
import UserPrinterInformation from '../source/userPrinters/information';
 
async function getLotInfo(req, res) {
  try {
    const user = await UserInformation.getUser({ id: req.userId });
 
    const branchCode = user.branchUser ? user.branchUser.code : '';
    const branchName = user.branchUser ? user.branchUser.name : '';
 
    const userPrinter = user.printer;
 
    const printerOptions = await UserPrinterInformation.getUserPrinterOptions(
      req.userId
    );
 
    const lastLot = user.branchUser
      ? await LotInformation.getLastLot(branchCode)
      : await LotInformation.getLastLotWithoutBranchCode();
 
    res.json({
      success: true,
      data: {
        branchCode,
        branchName,
        lastLot: lastLot || 0,
        userPrinter,
        printerOptions,
      },
    });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getLots(req, res) {
  try {
    const branchCode = req.body.branchCode || req.query.branchCode;
 
    const lots = await LotInformation.getLots({
      branchCode,
    });
 
    const result = {
      success: true,
      data: lots,
    };
    res.json(result);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getOptionsLots(req, res) {
  try {
    const branchCode = req.body.branchCode || req.query.branchCode;
 
    const lots = await LotInformation.getOptionsLots({ branchCode });
 
    const result = {
      success: true,
      data: lots,
    };
    res.json(result);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function createLot(req, res) {
  try {
    const { branchCode, quantity, initial } = req.body || req.query;
 
    const lastLot = await LotInformation.getLastLot(branchCode);
 
    const firstNew = lastLot ? lastLot + 1 : 1;
    const lastNew = lastLot ? lastLot + quantity : quantity;
    const lots = [];
 
    for (let index = firstNew; index <= lastNew; index++) {
      lots.push({
        initial,
        branchCode,
        code: index,
        createdUser: req.userId,
        updatedUser: req.userId,
      });
    }
 
    const data = await LotCreate.createLots(lots);
 
    res.json({
      success: true,
      data,
    });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
export default {
  getLotInfo,
  getLots,
  getOptionsLots,
  createLot,
};