All files / controllers storageAddresses.js

100% Statements 181/181
100% Branches 48/48
100% Functions 21/21
100% Lines 176/176

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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831                                  3x   3x 3x         2x                                                                 2x       2x     1x 1x     1x         3x 3x     3x                   2x                                                         2x               2x     1x 1x     1x                                                         2x   2x                                                       2x 2x       1x         1x     1x 1x     1x                                                                           2x   2x                                                                         2x   2x 1x         1x     1x 1x     1x         2x 2x   2x 1x         1x     1x 1x     1x         1x   1x       1x                                                                   3x   3x                                                   3x 69x     3x 3x         2x         2x     1x 1x     1x         2x   2x 2x       1x       1x     1x 1x     1x         4x 4x 4x   4x 2x 2x       2x 2x 2x 2x   2x             4x     3x           3x       3x     1x 1x     1x         4x 4x 4x               4x       4x         3x 2x 1x           2x 1x             3x   3x         3x 1x                 3x   2x           3x     1x 1x     1x         2x 2x   2x             2x       1x   1x             1x       1x     1x 1x     1x         2x   2x 2x   2x       1x       1x     1x 1x     1x         3x   3x 3x     3x         2x       2x 200x             200x     200x     2x       2x     1x 1x     1x         2x 2x 2x       1x         1x         1x                 1x             1x                 1x       1x     1x 1x     1x         2x   2x 2x       2x           1x       1x     1x 1x     1x         2x 2x     2x           1x         1x     1x 1x     1x         2x 2x     2x           1x         1x     1x 1x     1x                                              
import { Op, Sequelize } from 'sequelize';
 
import ReportsHelper from '../helpers/reports';
import { mountWhereBasedFilter } from '../helpers/sequelize/filteredQuery';
import DepositsInformation from '../source/deposits/information';
import PickingAreaInformation from '../source/pickingAreas/information';
import ProductBranchInformation from '../source/productBranches/information';
import StorageAddressCreate from '../source/storageAddresses/create';
import StorageAddressDelete from '../source/storageAddresses/delete';
import StorageAddressesGenerate from '../source/storageAddresses/generate';
import StorageAddressImport from '../source/storageAddresses/import';
import StorageAddressInformation from '../source/storageAddresses/information';
import StorageAddressUpdate from '../source/storageAddresses/update';
import StorageAddressBlockHistoryCreate from '../source/storageAdressBlockHistory/create';
import UserInformation from '../source/users/information';
 
async function getStorageAddress(req, res) {
  const { storageAddressId } = req.query;
 
  try {
    const storageAddress = await StorageAddressInformation.getStorageAddress(
      { id: storageAddressId },
      { userBranches: req.userBranches }
    );
 
    const data = {
      id: storageAddress.id,
      code: storageAddress.code,
      name: storageAddress.name,
      branchCode: storageAddress.branchCode,
      branchName: storageAddress.branch
        ? `${storageAddress.branch.code} - ${storageAddress.branch.name}`
        : '',
      type: storageAddress.type,
      range: storageAddress.range,
      rangeCode: storageAddress.rangeCode,
      sector: storageAddress.sector,
      street: storageAddress.street,
      level: storageAddress.level,
      column: storageAddress.column,
      drawer: storageAddress.drawer,
      note: storageAddress.note,
      blocked: storageAddress.blocked,
      size: storageAddress.size,
      height: storageAddress.height,
      width: storageAddress.width,
      length: storageAddress.length,
      curve: storageAddress.curve,
      addressType: storageAddress.addressType,
      capacityVolume: storageAddress.capacityVolume,
      capacityWeight: storageAddress.capacityWeight,
      shareAddress: storageAddress.shareAddress,
      shareAddressOfProductsWithDifferentLots:
        storageAddress.shareAddressOfProductsWithDifferentLots,
      inboundSequence: storageAddress.inboundSequence,
      outboundSequence: storageAddress.outboundSequence,
    };
 
    const response = {
      success: true,
      data,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getAllStorageAddresses(req, res) {
  try {
    const { page, pageSize = 10, sorted, filtered } = req.body;
 
    const storageAddresses =
      await StorageAddressInformation.getAddressesStorageList(
        page,
        pageSize,
        sorted,
        filtered,
        {
          userBranches: req.userBranches,
        }
      );
 
    const rows = storageAddresses.rows.map((storageAddress) => ({
      id: storageAddress.id,
      code: storageAddress.code,
      name: storageAddress.name,
      branchCode: storageAddress.branchCode,
      branchName: storageAddress.branch
        ? `${storageAddress.branch.code} - ${storageAddress.branch.name}`
        : '',
      type: storageAddress.type,
      rangeCode: storageAddress.rangeCode,
      sector: storageAddress.sector,
      street: storageAddress.street,
      level: storageAddress.level,
      column: storageAddress.column,
      drawer: storageAddress.drawer,
      note: storageAddress.note,
      blocked: storageAddress.blocked,
      size: storageAddress.size,
      height: parseFloat(storageAddress.height),
      width: parseFloat(storageAddress.width),
      length: parseFloat(storageAddress.length),
      curve: storageAddress.curve,
      capacityVolume: parseFloat(storageAddress.capacityVolume),
      capacityWeight: parseFloat(storageAddress.capacityWeight),
      shareAddress: storageAddress.shareAddress,
      inboundSequence: storageAddress.inboundSequence,
      outboundSequence: storageAddress.outboundSequence,
    }));
 
    const response = {
      success: true,
      data: {
        rows,
        pages: Math.ceil(storageAddresses.count / pageSize),
        totalDataLength: storageAddresses.count,
      },
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function createStorageAddress(req, res) {
  const {
    code,
    branchCode,
    name,
    sector,
    street,
    level,
    rangeCode,
    column,
    drawer,
    type,
    blocked,
    note,
    size,
    height,
    width,
    length,
    capacityVolume,
    capacityWeight,
    shareAddress,
    curve,
    shareAddressOfProductsWithDifferentLots,
    inboundSequence,
    outboundSequence,
  } = req.body;
 
  const storageAddress = {
    code,
    name,
    branchCode,
    rangeCode,
    type,
    sector,
    street,
    level,
    column,
    drawer,
    blocked,
    note,
    size,
    height,
    width,
    length,
    curve,
    capacityVolume,
    capacityWeight,
    shareAddress,
    createdUser: req.userId,
    updatedUser: req.userId,
    shareAddressOfProductsWithDifferentLots,
    inboundSequence,
    outboundSequence,
  };
 
  try {
    const newStorageAddress = await StorageAddressCreate.createStorageAddress(
      storageAddress
    );
 
    const response = {
      success: true,
      data: newStorageAddress,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function generateStorageAddresses(req, res) {
  const {
    branchCode,
    sectorFrom,
    streetFrom,
    levelFrom,
    sectorTo,
    streetTo,
    levelTo,
    columnTo,
    drawerTo,
    columnFrom,
    drawerFrom,
    columnSide,
    type,
    blocked,
    note,
    size,
    height,
    width,
    length,
    capacityVolume,
    capacityWeight,
    shareAddress,
    curve,
    sectorCode,
    streetCode,
    levelCode,
    columnCode,
    drawerCode,
    rangeCode,
    shareAddressOfProductsWithDifferentLots,
    inboundSequence,
    outboundSequence,
  } = req.body;
 
  const storageAddresses = {
    branchCode,
    type,
    sectorCode,
    sectorFrom,
    sectorTo,
    streetCode,
    streetFrom,
    streetTo,
    columnSide,
    columnCode,
    columnFrom,
    columnTo,
    levelCode,
    levelFrom,
    levelTo,
    drawerCode,
    drawerFrom,
    drawerTo,
    blocked,
    capacityVolume,
    capacityWeight,
    height,
    width,
    size,
    length,
    note,
    shareAddress,
    curve,
    rangeCode,
    shareAddressOfProductsWithDifferentLots,
    inboundSequence,
    outboundSequence,
    createdUser: req.userId,
    updatedUser: req.userId,
  };
 
  try {
    const newStorageAddress =
      await StorageAddressesGenerate.generateStorageAddresses(storageAddresses);
    const response = {
      success: true,
      data: newStorageAddress,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function storageAddressesList(req, res) {
  const addressesList = req.body;
  try {
    const totalStoredAddresses =
      await StorageAddressesGenerate.storageAddressesList(addressesList);
    const response = {
      success: true,
      data: totalStoredAddresses,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function exportAddressList(req, res) {
  const { columns, rows } = req.body;
 
  const exports = await StorageAddressesGenerate.exportAddressList(
    columns,
    rows
  );
  return ReportsHelper.writeExcelToResponse(
    res,
    exports.headers,
    exports.rows,
    '#ffffff',
    '#f79c20'
  );
}
 
async function updateStorageAddress(req, res) {
  const {
    storageAddressId,
    branchCode,
    name,
    type,
    sector,
    rangeCode,
    street,
    level,
    column,
    drawer,
    blocked,
    note,
    size,
    height,
    width,
    length,
    capacityVolume,
    capacityWeight,
    shareAddress,
    curve,
    shareAddressOfProductsWithDifferentLots,
    inboundSequence,
    outboundSequence,
  } = req.body;
 
  const storageAddress = {
    name,
    branchCode,
    rangeCode,
    type,
    sector,
    street,
    level,
    column,
    drawer,
    blocked,
    note,
    size,
    height,
    width,
    length,
    curve,
    capacityVolume,
    capacityWeight,
    shareAddress,
    updatedUser: req.userId,
    shareAddressOfProductsWithDifferentLots,
    inboundSequence,
    outboundSequence,
  };
 
  Object.keys(storageAddress).forEach(
    (key) => storageAddress[key] == null && delete storageAddress[key]
  );
 
  try {
    const updStorageAddress = await StorageAddressUpdate.updateStorageAddress(
      storageAddressId,
      storageAddress
    );
 
    const response = {
      success: true,
      data: updStorageAddress,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function deleteStorageAddress(req, res) {
  const { storageAddressId } = req.body;
 
  try {
    const storageAddress = await StorageAddressDelete.deleteStorageAddress(
      storageAddressId
    );
 
    const response = {
      success: true,
      data: storageAddress,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getOptionsStorageAddresses(req, res) {
  const { branchCode, depositCode } = req.query;
  try {
    let where = {};
 
    if (depositCode) {
      const status = [];
      const deposit = await DepositsInformation.getDeposit({
        code: depositCode,
      });
 
      if (deposit && deposit.spare) status.push(2);
      if (deposit && deposit.rejected) status.push(5);
      if (deposit && deposit.damage) status.push(6);
      if (deposit && deposit.divergence) status.push(7);
 
      where = status.length
        ? { status }
        : {
            status: [1, 3, 4],
          };
    }
 
    const user = await UserInformation.getUser({ id: req.userId });
 
    const storageAddresses =
      await StorageAddressInformation.getOptionsStorageAddresses(
        branchCode && branchCode !== '' ? branchCode : user.mainBranch,
        {},
        where
      );
 
    const response = {
      success: true,
      data: storageAddresses,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getEmptyAddresses(req, res) {
  const { branchCode, productCode, filterStatus = false } = req.query;
  try {
    const whereTypeAddress = filterStatus
      ? [
          Sequelize.where(Sequelize.col('addressType.str_status'), {
            [Op.in]: [1, 2, 3, 4],
          }),
        ]
      : {};
 
    let query = {
      branchCode,
      ...whereTypeAddress,
    };
    const productBranch = await ProductBranchInformation.getProductBranch({
      productCode,
      branchCode,
    });
 
    if (productBranch) {
      if (productBranch.addressType) {
        query = {
          type: productBranch.addressType,
          ...query,
        };
      }
 
      if (productBranch.curve) {
        query = {
          curve: productBranch.curve,
          ...query,
        };
      }
    }
 
    let storageAddresses = [];
 
    storageAddresses = await StorageAddressInformation.getEmptyAddresses(
      query,
      { userBranches: req.userBranches }
    );
 
    if (!storageAddresses || storageAddresses.length === 0) {
      storageAddresses = await StorageAddressInformation.getEmptyAddresses(
        {
          branchCode,
          ...whereTypeAddress,
        },
        { userBranches: req.userBranches }
      );
    }
 
    const response = {
      success: true,
      data: storageAddresses.map((address) => ({
        code: address.code,
        curve: address.curve,
        size: address.addressSize ? address.addressSize.name : '',
      })),
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function blockAddress(req, res) {
  try {
    const { addressId, blocked, note } = req.body;
 
    await StorageAddressBlockHistoryCreate.createStorageAddressBlockHistory({
      addressId,
      blocked,
      note,
      createdUser: req.userId,
    });
 
    const storageAddress = await StorageAddressInformation.getStorageAddress({
      id: addressId,
    });
 
    const storageAddressId = storageAddress.id;
 
    const updStorageAddress = await StorageAddressUpdate.updateStorageAddress(
      storageAddressId,
      {
        blocked,
      }
    );
 
    const response = {
      success: true,
      data: updStorageAddress,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function previewBlockList(req, res) {
  const { filters } = req.body;
 
  try {
    const query = mountWhereBasedFilter(filters);
 
    const data = await StorageAddressInformation.getStorageAddresses(query, {
      userBranches: req.userBranches,
    });
 
    const response = {
      success: true,
      data,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function persistBlockList(req, res) {
  const { blocked, filters, note } = req.body;
 
  try {
    const query = mountWhereBasedFilter(filters);
 
    const storageAddresses =
      await StorageAddressInformation.getStorageAddresses(query, {
        userBranches: req.userBranches,
      });
 
    const reducedList =
      storageAddresses.length > 200
        ? storageAddresses.slice(0, 199)
        : storageAddresses;
 
    reducedList.forEach(async (addr) => {
      const history = {
        addressId: addr.id,
        blocked,
        note,
        createdUser: req.userId,
      };
 
      await StorageAddressBlockHistoryCreate.createStorageAddressBlockHistory(
        history
      );
      await StorageAddressUpdate.updateStorageAddress(addr.id, { blocked });
    });
 
    const response = {
      success: true,
      data: true,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function getAddressesPickingArea(req, res) {
  const { pickingAreaId } = req.query;
  try {
    const pickingArea = await PickingAreaInformation.getPickingArea({
      id: pickingAreaId,
    });
 
    let query = {
      branchCode: pickingArea.branchCode,
      pickingAreaId,
    };
 
    const queryStock = {
      branchCode: pickingArea.branchCode,
    };
 
    const storageAddressesInPickingArea =
      await StorageAddressInformation.getStorageAddressPickingAreas(
        query,
        queryStock,
        {
          userBranches: req.userBranches,
        },
        pickingArea.productCode
      );
 
    query = {
      branchCode: pickingArea.branchCode,
      pickingAreaId: { [Op.or]: { [Op.ne]: pickingAreaId, [Op.eq]: null } },
      blocked: false,
    };
 
    const storageAddresses =
      await StorageAddressInformation.getStorageAddressPickingAreas(
        query,
        queryStock,
        {
          userBranches: req.userBranches,
        },
        pickingArea.productCode
      );
 
    const response = {
      success: true,
      data: { storageAddressesInPickingArea, storageAddresses },
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function updateAddressesPickingArea(req, res) {
  const { storageAddressId, pickingAreaId } = req.body;
 
  try {
    const query = {
      id: { [Op.in]: storageAddressId },
    };
 
    await StorageAddressUpdate.updateStorageAddressPickingArea(
      query,
      pickingAreaId,
      req.userId
    );
 
    const response = {
      success: true,
      data: true,
    };
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function updateBulkImportAddressesPickingArea(req, res) {
  try {
    const { jsonCsv } = req.body;
 
    const bulkImportAddressesPickingArea =
      await StorageAddressImport.importUpdateAddressesPickingArea(
        jsonCsv,
        req.userId,
        req.appLanguage
      );
 
    const response = {
      success: true,
      data: bulkImportAddressesPickingArea,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function updateBulkImportStorageAddressSequence(req, res) {
  try {
    const { jsonCsv } = req.body;
 
    const bulkImportAddressesPickingArea =
      await StorageAddressImport.importUpdateStorageAddressSequence(
        jsonCsv,
        req.userId,
        req.appLanguage
      );
 
    const response = {
      success: true,
      data: bulkImportAddressesPickingArea,
    };
 
    res.json(response);
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
export default {
  getAllStorageAddresses,
  getStorageAddress,
  createStorageAddress,
  generateStorageAddresses,
  storageAddressesList,
  exportAddressList,
  updateStorageAddress,
  deleteStorageAddress,
  getOptionsStorageAddresses,
  getEmptyAddresses,
  blockAddress,
  previewBlockList,
  persistBlockList,
  getAddressesPickingArea,
  updateAddressesPickingArea,
  updateBulkImportAddressesPickingArea,
  updateBulkImportStorageAddressSequence,
};