let User = global.User,
    Record = global.Record,
    router = global.variables.router,
    uuid = global.variables.uuid;

/**
 * @function create_card
 * @instance
 * @param {string} type [credit|normal|orther] (Required).
 * @param {string} balance Balance of user's card (Required).
 * @param {string} name Card holder (Required).
 * @param {string} exp Expiration of user's card [timestamp] (Required).
 * @param {string} number Card number (Required).
 * @param {string} cvv CVV of user's card (Required).
 * @param {string} note Note of card (Option).
 * @param {string} email email of user (Required).
 * @example <caption>Requesting /v1/card/create with the following POST data.</caption>
 * {
 *  type: credit,
 *  balance: 123456789,
 *  name: 'Huynh Tran Dang Khoa',
 *  exp: 1500879600,
 *  number: '4214-9458-0103-2509',
 *  cvv: 961,
 *  email: 'abc@gmail.com'
 * }
 */
router.post("/card/create", (req, res) => {
    var type = req.body.type,
        amount = req.body.amount,
        name = req.body.name,
        exp = req.body.exp,
        number = req.body.number,
        cvv = req.body.cvv,
        email = req.body.email;

    if (
        !req.user
    ) return res.redirect("/");
    
    if (
        global.variables.types_card.indexOf(type) === -1 ||
        isNaN(parseInt(amount)) ||
        global.isEmpty(name) ||
        global.isEmpty(exp) ||
        global.isEmpty(number) ||
        global.isEmpty(cvv) ||
        global.isEmpty(email)
    ) return global.errorHandler(res, 400, "Bad request.");

    User
    .findOne({
        email
    })
    .then(user => {
        if (!user) return global.errorHandler(res, 404, "Email does not exist.");

        user.cards.push({
            id: uuid.v4(),
            type,
            amount,
            name,
            exp,
            number,
            cvv
        });
        user.save();

        return global.successHandler(res, 201, "The card was created successfully.");
    })
    .catch(error => {
        return global.errorHandler(res, 200, error);
    })
});

/**
 * @function get_cards
 * @instance
 * @param {string} email Email of user (Required).
 * @example <caption>Requesting /v1/cards?id=59761a77393efd07a1f77a1e with the following GET data.</caption>
 */
router.get("/cards", (req, res) => {
    var email = req.param("email");
    
    if (
        !req.user
    ) return res.redirect("/");

    if (
        global.isEmpty(email)
    ) return global.errorHandler(res, 400, "Bad request.");

    User
    .find({
        email
    }, ["cards"])
    .then(result => {
        // console.log(cards[0].cards)

        result[0].cards.forEach(function(element, i) {
            console.log(element.id)
            
            Record
            .aggregate([
                {
                    $match: {
                        card: element.id
                    }
                },
                {
                    $group: {
                        _id: {
                            mode: "$mode"
                        },
                        total: {
                            $sum: "$value"
                        }
                    }
                }
            ])
            .then(r => {
                result[0].cards[i].current_balance = r;
                if (
                    i == result[0].cards.length -1
                ) return global.successHandler(res, 200, result);
            })
            .catch(e => {
                console.log(e)
            })
        }, this);

        if (result.length === 0) {
            return global.errorHandler(res, 404, "Email does not exist.");
        }

        
    })
    .catch(error => {
        return global.errorHandler(res, 200, error);
    })
})

module.exports = router;
@@ -1671,24 +1671,222 @@
  exist.%22);%0A%0A
+        var cards = user.cards;%0A%0A        for (var i = 0; i %3C cards.length; i++) %7B%0A            if (cards%5Bi%5D.number === number) return global.errorHandler(res, 302, %22Card already exist.%22);%0A        %7D%0A%0A
         user

Fix duplicate card