{"size": 1863, "ext": "js", "max_stars_repo_path": "server/middlewares/findCart.js", "max_stars_repo_name": "obasajujoshua31/turing_backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/middlewares/findCart.js", "max_issues_repo_name": "obasajujoshua31/turing_backend", "max_issues_repo_issues_event_min_datetime": "2020-09-06T11:44:43.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T16:50:59.000Z", "max_forks_repo_path": "server/middlewares/findCart.js", "max_forks_repo_name": "obasajujoshua31/turing_backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 25.5205479452, "max_line_length": 93, "alphanum_fraction": 0.5389157273}
import isEmpty from 'lodash.isempty';
import models from '../models';
import { isValid } from '../utils/getPageParams';

const { shopping_cart } = models;

/**
 * @description This method searches 
 * the shopping cart table given 
 * the cart_id and returns response if shopping cart is found or not
 * @param  {object} req
 * @param  {object} res
 * @param  {Function} next
 * @returns {object} Server Response
 */
export default async (req, res, next) => {
    const { cart_id } = req.body;

    const cart = await shopping_cart.findOne({
        where: {
            cart_id
        }
    });
    if (!isEmpty(cart)) {
        return next();
    }
    return res.status(400).json({
        error: {
            code: 'SHC_02',
            message: 'the field shopping cart is empty',
            field: 'shopping_cart'
        }
    });
};
/**
 * @description This method searches 
 * the shopping cart table where 
 * item_id matches the item_id, it returns response based on whether the item is found or not
 * @param  {object} req
 * @param  {object} res
 * @param  {Function} next
 * @returns {object} Server Response
 */
export const findItem = async (req, res, next) => {
    const { item_id } = req.params;

    if (isValid(item_id).valid) {
        const item = await shopping_cart.findOne({
            where: {
                item_id
            }
        });
        if (!isEmpty(item)) {
            req.item = item;
            return next();
        }
        return res.status(400).json({
            error: {
                code: 'ITM_O2',
                message: 'the field item is empty',
                field: 'item'
            }
        });
    }
    return res.status(400).json({
        error: {
            code: 'ITM_01',
            message: `The ID ${item_id} is not a number`,
            field: 'item_id'
        }
    });
};
