cart.prototype.checkout = function()

in source/api/services/cart/lib/cart.js [365:459]


    cart.prototype.checkout = function(body, ticket, token, cb) {

        if (body.operation != 'checkout') {
            return cb({
                error: {
                    message: 'Invalid operation requested on the user\'s cart.'
                }
            }, null);
        }

        // get the items in the users cart
        let params = {
            TableName: ddbTable,
            KeyConditionExpression: 'user_id = :uid',
            FilterExpression: 'cart_item_status = :pending or cart_item_status = :error',
            ExpressionAttributeValues: {
                ':uid': ticket.userid,
                ':pending': 'pending',
                ':error': 'unable_to_process'
            }
        };

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

            let _pending = resp.Items;

            if (_pending.length > 0) {

                let _batchRequest = [];
                for (let i = 0; i < _pending.length; i++) {
                    let _item = _pending[i];
                    _item.cart_item_status = 'processing';
                    _batchRequest.push({
                        PutRequest: {
                            Item: _item
                        }
                    });
                }

                let params = {
                    RequestItems: {
                        'data-lake-cart': _batchRequest
                    }
                };

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

                    let _payload = {
                        cart: _pending,
                        operation: 'generate',
                        format: body.format,
                        authorizationToken: token
                    };

                    // add async invocation to lambda function that processes manifest file
                    let params = {
                        FunctionName: 'data-lake-manifest-service',
                        InvocationType: 'Event',
                        LogType: 'None',
                        Payload: JSON.stringify(_payload)
                    };
                    let lambda = new AWS.Lambda();
                    lambda.invoke(params, function(err, data) {
                        if (err) {
                            console.log(err);
                            return cb({
                                    error: {
                                        message: 'Error occured when triggering manifest import'
                                    }
                                },
                                null);
                        }

                        return cb(null, {
                            message: 'manifest file generation initiated'
                        });
                    });
                });

            } else {
                return cb(null, {
                    message: 'No cart items in a pending state to process'
                });
            }
        });

    };