All files / controllers printers.js

100% Statements 81/81
100% Branches 2/2
100% Functions 12/12
100% Lines 80/80

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257                      2x   2x 2x       1x 1x   1x 1x           1x   1x       1x     1x 1x     1x         2x 2x 2x   1x 1x           1x       1x     1x 1x     1x         2x 2x   1x 1x   1x 1x           1x 1x     1x       1x     1x 1x     1x           2x   2x                       2x 2x   1x         1x     1x 1x     1x           3x   3x                   3x 21x     3x 3x   2x         2x     1x 1x     1x         2x   2x 2x   1x       1x     1x 1x     1x         2x 2x 2x       1x 1x   1x                 1x 1x     1x         2x 2x 2x   1x               1x 1x     1x                            
import PrinterInformation from '../source/printers/information';
import PrinterCreate from '../source/printers/create';
import PrinterUpdate from '../source/printers/update';
import PrinterDelete from '../source/printers/delete';
 
import UserPrinterInformation from '../source/userPrinters/information';
import UserInformation from '../source/users/information';
 
import AgentInformation from '../source/agents/information';
 
async function getPrinter(req, res) {
  const { printerId } = req.query;
 
  try {
    const printer = await PrinterInformation.getPrinter({
      id: printerId,
    });
 
    const agentList = []; // value, label
    const agents = await AgentInformation.getAllAgents();
 
    agents.forEach((agent) => {
      agentList.push({
        value: agent.code,
        label: `${agent.code} - ${agent.name} - ${agent.description}`,
      });
    });
 
    printer.agentList = agentList;
 
    const response = {
      success: true,
      data: printer,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getAllAgentsOptions(req, res) {
  try {
    const agentList = [{ value: '', label: '' }]; // value, label
    const agents = await AgentInformation.getAllAgents();
 
    agents.forEach((agent) => {
      agentList.push({
        value: agent.code,
        label: `${agent.code} - ${agent.name} - ${agent.description}`,
      });
    });
 
    const response = {
      success: true,
      data: agentList,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getAllPrinters(req, res) {
  try {
    const printers = await PrinterInformation.getAllPrinters({ raw: true });
 
    const agentList = [{ value: '', label: '' }]; // value, label
    const agents = await AgentInformation.getAllAgents();
 
    agents.forEach((agent) => {
      agentList.push({
        value: agent.code,
        label: `${agent.code} - ${agent.name} - ${agent.description}`,
      });
    });
 
    for (let index = 0; index < printers.length; index++) {
      printers[index].agentList = agentList;
    }
 
    const response = {
      success: true,
      data: printers,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function createPrinter(req, res) {
  const { code, name, location, type, note, agentCode, languageCode } =
    req.body;
 
  const printer = {
    code,
    name,
    location,
    type,
    note,
    agentCode,
    languageCode,
    createdUser: req.userId,
    updatedUser: req.userId,
  };
 
  try {
    const newPrinter = await PrinterCreate.createPrinter(printer);
 
    const response = {
      success: true,
      data: newPrinter,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function updatePrinter(req, res) {
  const { printerId, name, location, type, note, agentCode, languageCode } =
    req.body;
 
  const printer = {
    name,
    location,
    type,
    note,
    agentCode,
    languageCode,
    updatedUser: req.userId,
  };
 
  Object.keys(printer).forEach(
    (key) => printer[key] == null && delete printer[key]
  );
 
  try {
    const updPrinter = await PrinterUpdate.updatePrinter(printerId, printer);
 
    const response = {
      success: true,
      data: updPrinter,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function deletePrinter(req, res) {
  const { printerId } = req.body;
 
  try {
    const printer = await PrinterDelete.deletePrinter(printerId);
 
    const response = {
      success: true,
      data: printer,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getUserPrinterOptions(req, res) {
  try {
    const { userId } = req;
    const printerOptions = await UserPrinterInformation.getUserPrinterOptions(
      userId
    );
 
    const user = await UserInformation.getUser({ id: userId });
    const userPrinter = user.printer;
 
    res.json({
      success: true,
      data: {
        printerOptions,
        userPrinter,
      },
    });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getUsersPrinter(req, res) {
  const printerCode = req.query.printerId;
  try {
    const printer = await UserPrinterInformation.getUsersPrinter(printerCode);
 
    res.json({
      success: true,
      data: {
        printer,
      },
    });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
export default {
  getAllPrinters,
  getPrinter,
  createPrinter,
  updatePrinter,
  deletePrinter,
  getUserPrinterOptions,
  getUsersPrinter,
  getAllAgentsOptions,
};