# all files / lib/ order.js

100% Statements 23/23
100% Branches 8/8
80% Functions 4/5
100% Lines 22/22

    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
    341×
    1×

    1×
    12×
    4×
    1×
    1×

    3×

    1×
    4×
    4×

    1×
    4×
    4×
    3×
    3×
    3×
    3×

    3×

    1×
    1×

    1×var inventoryData = require('../data/inventory');
    var warehouse = require('./warehouse');

    function findItem(sku) {
        var i = inventoryData.map(item => item.sku).indexOf(sku);
        if (i === -1) {
            console.log(`Item - ${sku} not found`);
            return null;
        } else {
            return inventoryData[i];
        }
    }

    function isInStock(sku, qty) {
        var item = findItem(sku);
        return item && item.qty >= qty;
    }

    function order(sku, quantity, complete) {
        complete = complete || function () {};
        if (isInStock(sku, quantity)) {
            console.log(`ordering ${quantity} of item # ${sku}`);
            warehouse.packageAndShip(sku, quantity, function (tracking) {
                console.log(`order shipped, tracking - ${tracking}`);
                complete(tracking);
            });
            return true;
        } else {
            console.log(`there are not ${quantity} of item '${sku}' in stock`);
            return false;
        }
    }

    module.exports.orderItem = order;