All files / controllers customerBranch.js

100% Statements 37/37
100% Branches 5/5
100% Functions 5/5
100% Lines 36/36

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            2x   2x 2x               1x     1x 1x     1x           3x 3x               3x 15x     3x 3x         2x     1x 1x     1x         2x   2x 2x 1x     1x 1x     1x         3x 3x     3x               2x   2x 2x 2x                   2x                 2x     1x 1x     1x                    
import customerBranchCreate from '../source/customerBranch/create';
import customerBranchDelete from '../source/customerBranch/delete';
import customerBranchInformation from '../source/customerBranch/information';
import customerBranchUpdate from '../source/customerBranch/update';
 
async function createCustomerBranch(req, res) {
  const { customerCode, branchCode, restriction, note } = req.body;
 
  try {
    await customerBranchCreate.createCustomerBranch({
      customerCode,
      branchCode,
      restriction,
      note,
      createdUser: req.userId,
      updatedUser: req.userId,
    });
    res.json({ success: true });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function updateCustomerBranch(req, res) {
  const { customerBranchId, customerCode, branchCode, restriction, note } =
    req.body;
  const customerBranch = {
    customerCode,
    branchCode,
    restriction,
    note,
    updatedUser: req.userId,
  };
 
  Object.keys(customerBranch).forEach(
    (key) => customerBranch[key] == null && delete customerBranch[key]
  );
 
  try {
    await customerBranchUpdate.updateCustomerBranch(
      customerBranchId,
      customerBranch
    );
 
    res.json({ success: true });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function deleteCustomerBranch(req, res) {
  const { customerBranchId } = req.body;
 
  try {
    await customerBranchDelete.deleteCustomerBranch(customerBranchId);
    res.json({ success: true });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getCustomerBranches(req, res) {
  try {
    const { page, pageSize = 10, sorted, filtered } = req.body;
 
    const customerBranches =
      await customerBranchInformation.getCustomerBranches(
        parseInt(page, 10),
        parseInt(pageSize, 10),
        sorted,
        filtered,
        { userBranches: req.userBranches }
      );
 
    const rows = [];
 
    for (let index = 0; index < customerBranches.rows.length; index++) {
      const element = customerBranches.rows[index];
      rows.push({
        id: element.id,
        branchCode: element.branch.code,
        branchName: element.branch.name,
        customerCode: element.customer.code,
        customerName: element.customer.name,
        restriction: element.restriction,
        note: element.note ? element.note : '',
      });
    }
    const response = {
      success: true,
      data: {
        rows,
        pages: Math.ceil(customerBranches.count / pageSize),
        totalDataLength: customerBranches.count,
      },
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
export default {
  createCustomerBranch,
  updateCustomerBranch,
  deleteCustomerBranch,
  getCustomerBranches,
};