Untitled

mail@pastecode.io avatar
unknown
javascript
15 days ago
5.4 kB
2
Indexable
Never
transfer: async (req, res, next) => {
        const { amount, userNameTo, api, note } = req.body
        const { idUser } = req.user
        const flag = validationBody({ amount, userNameTo, api })
        const keyName = `${idUser}${process.env.DOMAIN}${api}`
        const io = req.io
        try {



            if (flag.length > 0) {
                await delRedis(keyName)
                return error_400(res, 'Not be empty', flag)
            }
            if (api != "transfer") {
                await delRedis(keyName)
                return error_400(res, "Api is not define")
            }
            if (amount < 5) {
                await delRedis(keyName)
                return error_400(res, 'Minimum amount must be 5$')
            }
            const profileTo = await getRowToTable(`tb_user`, `userName='${userNameTo}'`)
            const profile = await getRowToTable(`tb_user`, `id=${idUser}`)
            if (profile[0].marketing == 1 && profileTo[0].marketing != 1) {
                await delRedis(keyName)
                return error_400(res, 'This function is locked')
            }
            if (userNameTo == profile[0].userName) {
                await delRedis(keyName)
                return error_400(res, 'username does not exist')
            }
            if (profileTo.length <= 0) {
                await delRedis(keyName)
                return error_400(res, 'username does not exist')
            }
            const flagBalance = await checkBalanceStr(amount, idUser, `balanceUSDT`)
            if (!flagBalance.status) {
                await delRedis(keyName)
                return error_400(res, "Insufficient balance")
            }
            const queryGetFeeMode = await getRowToTable(`tb_config`, `name='feePercent'`)
            const feePercent = queryGetFeeMode[0].value == 1 ? true : false
            const queryGetTransfer = await getRowToTable(`tb_config`, `name='transfer'`)
            const percent = queryGetTransfer[0].value
            const receive = feePercent ? amount - (percent / 100) * amount : amount - percent
            const minusBalanceQuery = minusBalance(amount, idUser)
            const addBalanceStrQuery = addBalanceStr(receive, profileTo[0].id, `balanceUSDT`)

            const obj = {
                userid: idUser,
                useridTo: profileTo[0].id,
                userName: profile[0].userName,
                userNameTo: profileTo[0].userName,
                emailTo: profileTo[0].email,
                email: profile[0].email,
                amount,
                receive,
                note: note == undefined ? "" : note,
                marketing: profile[0].marketing == 1 && profileTo[0].marketing == 1 ? 1 : 0
            }
            const objNotification = {
                title: `Internal withdrawal successful`,
                detail: `
                - Quantity : ${amount} USDT
                - Memo : ${note == undefined ? "" : note}
                - Receiver : ${profileTo[0].userName}
                `,
                userid: idUser,
                email: profile[0].email,
                userName: profile[0].userName,
                amountTransfer: amount,
                userNameTransfer: profileTo[0].userName,
                type: 4,
                memo: note == undefined ? "" : note
            }
            await addRowToTable(`tb_notification`, objNotification)
            const objNotificationTo = {
                title: `Successful internal recharge`,
                detail: `
                - Quantity : ${amount} USDT
                - Memo :${note == undefined ? "" : note}
                - Receiver : ${profile[0].userName}
                `,
                userid: profileTo[0].id,
                email: profileTo[0].email,
                userName: profileTo[0].userName,
                amountTransfer: amount,
                userNameTransfer: profile[0].userName,
                type: 5,
                memo: note == undefined ? "" : note
            }

            let message = `You have received ${amount} USDT from ${profile[0].userName}`
            try {
                await sendMailMessage(profileTo[0].email, `${process.env.SERVICENAME} | Transfer`, `${profileTo[0].userName}`, message)
            } catch (error) {
                console.log(error);
            }

            await addRowToTable(`tb_notification`, objNotificationTo)
            await updateStatusBalance(idUser, `transfer`, amount, `-`)
            await updateStatusBalance(profileTo[0].id, `transfer`, receive, `+`)

            await Promise.all([addRowToTable(`tb_transfer`, obj), minusBalanceQuery, addBalanceStrQuery])
            io.to(`${profileTo[0].id}`).emit('transfer', "transfer");

            messageBotRutNoiBoTelegram(`Người dùng \`${profile[0].userName}\` ${profile[0].marketing == 1 ? "_[marketing]_ ":" "}vừa thực hiện chuyển nội bộ đến \`${profileTo[0].userName}\` ${profileTo[0].marketing == 1 ? "_[marketing]_ ":" "}*$${amount}*`)

            await delRedis(keyName)
            success(res, "Money transfer successful")
        } catch (error) {
            log.info(error);
            await delRedis(keyName)
            error_500(res, error)
        }
    },
Leave a Comment