cart.prototype.createCartItem = function()

in source/api/services/cart/lib/cart.js [227:312]


    cart.prototype.createCartItem = function(item, ticket, cb) {

        // check to see if package is already a pending item in cart
        let params = {
            TableName: ddbTable,
            KeyConditionExpression: 'user_id = :uid',
            FilterExpression: 'package_id = :pid and cart_item_status in (:pending, :error)',
            ExpressionAttributeValues: {
                ':uid': ticket.userid,
                ':pid': item.package_id,
                ':pending': 'pending',
                ':error': 'unable_to_process'
            }
        };

        docClient.query(params, function(err, cart) {
            if (err) {
                console.log(err);
                return cb(err, null);
            }

            if (cart.Items.length === 0) {
                let _newCartItem = {
                    user_id: ticket.userid,
                    item_id: shortid.generate(),
                    package_id: item.package_id,
                    created_at: moment.utc().format(),
                    cart_item_status: 'pending'
                };

                let _schemaCheck = v.validate(_newCartItem, cartSchema);
                if (_schemaCheck.valid) {
                    let params = {
                        TableName: 'data-lake-packages',
                        KeyConditionExpression: 'package_id = :pid',
                        ExpressionAttributeValues: {
                            ':pid': item.package_id
                        }
                    };

                    // check to see if the package id sent exists
                    docClient.query(params, function(err, resp) {
                        if (err) {
                            console.log(err);
                            return cb(err, null);
                        }

                        if (resp.Items.length > 0) {
                            let params = {
                                TableName: ddbTable,
                                Item: _newCartItem
                            };

                            docClient.put(params, function(err, data) {
                                if (err) {
                                    console.log(err);
                                    return cb(err, null);
                                }

                                return cb(null, _newCartItem);
                            });
                        } else {
                            return cb({
                                error: {
                                    message: 'The package requested to add to the cart does not exist.'
                                }
                            }, null);
                        }

                    });
                } else {
                    return cb({
                        error: {
                            message: 'Invalid schema provided when attempting to add item to cart.'
                        }
                    }, null);
                }
            } else {
                return cb(null, {
                    message: 'The package requested to add is already in the user\'s cart'
                });
            }

        });

    };