Commit 470c325c authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent d8b9b5f9
......@@ -18,6 +18,7 @@ bot.on('message', (msg) => {
});
async function processMessage(msg) {
await isAdmin(msg)
if (msg.document) {
commitDoc(msg)
......@@ -39,12 +40,13 @@ bot.on('callback_query', function onCallbackQuery(msg) {
});
async function processCallback(msg) {
console.log(msg)
await isAdmin(msg)
if (msg.data.startsWith("user_")) showUser(msg)
else if (msg.data.startsWith("request_")) request(msg)
else if (msg.data.startsWith("buy_")) showCurrency(msg, 'buy')
else if (msg.data.startsWith("sell_")) showCurrency(msg, 'sell')
else if (msg.data.startsWith("variz_")) showCurrency(msg, 'variz')
else if (msg.data.startsWith("toman_")) showCount(msg, 'toman')
else if (msg.data.startsWith("dollar_")) showCount(msg, 'dollar')
else if (msg.data.startsWith("euro_")) showCount(msg, 'euro')
......@@ -99,6 +101,9 @@ async function showCurrency(msg, type) {
case 'sell':
msg.data = msg.data.replace("sell_", "")
break
case 'variz':
msg.data = msg.data.replace("variz_", "")
break
}
......@@ -132,10 +137,12 @@ async function showCount(msg, unit) {
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
let type = admin.data.action
console.log(admin.data)
admin.data.vahed = unit
await db.updateData(msg, JSON.stringify(admin.data))
message.showCountMessage(bot, msg)
message.showCountMessage(bot, msg, type === 'variz')
}
async function elseMessage(msg) {
......@@ -158,12 +165,16 @@ async function input1(msg) {
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
let type = admin.data.action
admin.data.count = msg.text
admin.data = JSON.stringify(admin.data)
await db.updateData(msg, admin.data)
message.showFeeMessage(bot, msg)
if (type === 'variz')
message.sendDocMessage(bot, msg)
else
message.showFeeMessage(bot, msg)
}
async function input2(msg) {
......@@ -184,8 +195,9 @@ async function input2(msg) {
}
async function yesNoDoc(msg, type) {
console.log("Yes?????")
if (type.includes('yes')) {
message.deleteMessage(bot, msg)
message.simpleMessage(bot, msg, 'لطفا فایل ضمیمه را ارسال کنید دقت شود که به عنوان فایل ارسال شود.')
return
}
......@@ -196,13 +208,13 @@ async function yesNoDoc(msg, type) {
await db.createRequest(admin.data)
await db.updateData(msg, null)
message.deleteMessage(bot, msg)
message.simpleMessage(bot, msg, 'فیش با موفقیت ثبت شد.')
}
async function commitRequest(msg, type) {
console.log("Here")
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
......@@ -211,7 +223,7 @@ async function commitRequest(msg, type) {
// await db.createRequest(admin.data)
await db.updateData(msg, JSON.stringify(admin.data))
message.deleteMessage(bot, msg)
message.sendDocMessage(bot, msg, userId)
}
......@@ -293,6 +305,7 @@ async function canOrder(data, msg) {
async function showRequest(msg) {
msg.data = msg.data.replace("reqs_", "")
let req = await db.findRequest(msg.data)
message.deleteMessage(bot, msg)
message.showRequest(bot, msg, req)
}
......@@ -311,6 +324,7 @@ async function reportRequest(msg, date) {
break
}
let reqs = await db.getRequestByDate(msg.data, date)
message.deleteMessage(bot, msg)
message.listRequest(bot, msg, reqs)
}
......
......@@ -7,13 +7,13 @@ const dbAddress = process.env.DEBUG === 'true' ? process.env.HOST_Local : proces
const dbPassword = process.env.DEBUG === 'true' ? 'root' : process.env.DB_PASS_SERVER;
const sequelize = new Sequelize('accounting', 'root', dbPassword, {
const sequelize = new Sequelize('accounting', 'root', dbPassword, {
define: {
charset: 'utf8',
charset: 'utf8',
collate: 'utf8_general_ci'
},
host: dbAddress,
host: dbAddress,
dialect: 'mysql',
logging: false,
......@@ -60,13 +60,20 @@ const Request = sequelize.define('request', {
async function createRequest(data) {
let user = await findUser(data.user)
user = runCurrency(data, user)
await updateBalance(user)
let mRequest = await Request.create({
user_request: data.user,
fee: data.fee,
count: data.count,
unit: data.vahed,
file_id: data.file_id,
type:data.action,
type: data.action,
toUnit: data.to,
is_today: true
......@@ -74,6 +81,128 @@ async function createRequest(data) {
return mRequest;
}
function runCurrency(data, user) {
if (data.action === 'variz') {
switch (data.vahed) {
case 'toman':
user.toman_balance = parseFloat(user.toman_balance) + (-1* parseFloat(data.count))
break
case 'dollar':
user.dollar_balance = user.dollar_balance + (-1* parseFloat(data.count))
break
case 'euro':
user.euro_balance = user.euro_balance +(-1 * parseFloat(data.count))
break
case 'lir':
user.lir_balance = user.lir_balance + (-1 *parseFloat(data.count))
break
case 'aed':
user.aed_balance = user.aed_balance + (-1 * parseFloat(data.count))
break
}
return user
}
if (data.vahed === 'toman') {
if (data.action === 'buy')
user.toman_balance = user.toman_balance + parseFloat(data.count)
else
user.toman_balance = user.toman_balance + (-1 * parseFloat(data.count))
}
else if (data.vahed === 'dollar') {
if (data.action === 'buy')
user.dollar_balance = user.dollar_balance + parseFloat(data.count)
else
user.dollar_balance = user.dollar_balance + (-1 * parseFloat(data.count))
}
else if (data.vahed === 'euro') {
if (data.action === 'buy')
user.euro_balance = user.euro_balance + parseFloat(data.count)
else
user.euro_balance = user.euro_balance + (-1 * parseFloat(data.count))
}
else if (data.vahed === 'lir') {
if (data.action === 'buy')
user.lir_balance = user.lir_balance + parseFloat(data.count)
else
user.lir_balance = user.lir_balance + (-1 * parseFloat(data.count))
}
else if (data.vahed === 'aed') {
if (data.action === 'buy')
user.aed_balance = user.aed_balance + parseFloat(data.count)
else
user.aed_balance = user.aed_balance + (-1 * parseFloat(data.count))
}
if (data.to === 'toman') {
if (data.action === 'buy')
user.toman_balance = user.toman_balance + (-1 * parseFloat(data.count) * parseFloat(data.fee))
else
user.toman_balance = user.toman_balance + (parseFloat(data.count) * parseFloat(data.fee))
}
else if (data.vahed === 'dollar') {
if (data.action === 'buy')
user.dollar_balance = user.dollar_balance + (-1 * parseFloat(data.count) * parseFloat(data.fee))
else
user.toman_balance = user.dollar_balance + (parseFloat(data.count) * parseFloat(data.fee))
}
else if (data.vahed === 'euro') {
if (data.action === 'buy')
user.euro_balance = user.euro_balance + (-1 * parseFloat(data.count) * parseFloat(data.fee))
else
user.euro_balance = user.euro_balance + (parseFloat(data.count) * parseFloat(data.fee))
}
else if (data.vahed === 'lir') {
if (data.action === 'buy')
user.lir_balance = user.lir_balance + (-1 * parseFloat(data.count) * parseFloat(data.fee))
else
user.lir_balance = user.lir_balance + (parseFloat(data.count) * parseFloat(data.fee))
}
else if (data.vahed === 'aed') {
if (data.action === 'buy')
user.aed_balance = user.aed_balance + (-1 * parseFloat(data.count) * parseFloat(data.fee))
else
user.aed_balance = user.aed_balance + (parseFloat(data.count) * parseFloat(data.fee))
}
return user
}
async function getAllUsers() {
......@@ -111,6 +240,22 @@ async function updateData(msg, data) {
}
async function updateBalance(user) {
return await User.update(
{
toman_balance: user.toman_balance,
dollar_balance: user.dollar_balance,
euro_balance: user.euro_balance,
lir_balance: user.lir_balance,
aed_balance: user.aed_balance
},
{where: {id: user.id}})
}
async function updateStatus(msg, status) {
return await Admin.update(
{
......@@ -133,8 +278,6 @@ async function findAdmin(msg) {
})
}
async function findRequest(id) {
......
......@@ -9,10 +9,12 @@ async function simpleMessage(bot, msg, text) {
const opts = {
reply_markup: {
keyboard: [
['💷 لیست اعضا']
['💷 لیست اعضا'],
],
resize_keyboard: true,
one_time_keyboard: true
}
};
......@@ -32,17 +34,7 @@ async function listUserMessage(bot, msg, users) {
for (let i = 0; i < users.length; i++) {
let text = users[i].first_name + " " + users[i].last_name + ' ' + 'اعتبار:'
text += generateText('تومان', users[i].toman_balance)
if (users[i].dollar_balance !== 0)
text += generateText('دلار', users[i].dollar_balance)
if (users[i].euro_balance !== 0)
text += generateText('یورو', users[i].euro_balance)
if (users[i].lir_balance !== 0)
text += generateText('لیر', users[i].lir_balance)
if (users[i].aed_balance !== 0)
text += generateText('درهم', users[i].aed_balance)
let text = users[i].first_name + " " + users[i].last_name
opts.reply_markup.inline_keyboard.push([{
text: text,
......@@ -63,6 +55,11 @@ function generateText(type, balance) {
}
async function deleteMessage(bot,msg)
{
await bot.deleteMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, typeof msg.message_id !== 'undefined' ? msg.message_id :msg.message.message_id)
}
function userShowMessage(bot, msg, user) {
var opts = {
......@@ -110,9 +107,53 @@ function userShowMessage(bot, msg, user) {
chat_id: msg.message.chat.id,
message_id: msg.message.message_id
};
let text = '💷 '
text += Math.abs(user.toman_balance) + ' ' + 'تومان' + ' '
text += user.toman_balance === 0 ? '' :
user.toman_balance > 0 ? 'بدهکار' : 'طلبکار'
text += '\n'
text += '\n'
if (user.dollar_balance !== 0) {
text += '💵 '
text += Math.abs(user.dollar_balance) + ' ' + 'دلار' + ' '
text += user.dollar_balance === 0 ? '' :
user.dollar_balance > 0 ? 'بدهکار' : 'طلبکار'
text += '\n'
text += '\n'
}
if (user.euro_balance !== 0) {
text += '💶 '
text += Math.abs(user.euro_balance) + ' ' + 'یورو' + ' '
text += user.euro_balance === 0 ? '' :
user.euro_balance > 0 ? 'بدهکار' : 'طلبکار'
text += '\n'
text += '\n'
}
if (user.lir_balance !== 0) {
text += '💴 '
text += Math.abs(user.lir_balance) + ' ' + 'لیر' + ' '
text += user.lir_balance === 0 ? '' :
user.lir_balance > 0 ? 'بدهکار' : 'طلبکار'
text += '\n'
text += '\n'
}
if (user.aed_balance !== 0) {
bot.editMessageText('لطفا کاربر مورد نظر را انتخاب کنید.', opts)
text += '💰 ' + Math.abs(user.aed_balance) + ' ' + 'درهم' + " "
text += user.aed_balance === 0 ? '' :
user.aed_balance > 0 ? 'بدهکار' : 'طلبکار'
}
bot.editMessageText(text, opts)
}
async function showCurrencyMessage(bot, msg, user) {
......@@ -196,6 +237,14 @@ async function userSubmitRequest(bot, msg, user) {
callback_data: 'sell_' + user.id
}
],
[
{
text: 'واریز',
callback_data: 'variz_' + user.id
}
]
......@@ -211,14 +260,17 @@ async function userSubmitRequest(bot, msg, user) {
}
async function showCountMessage(bot, msg) {
async function showCountMessage(bot, msg, variz) {
var opts = {
chat_id: msg.message.chat.id,
message_id: msg.message.message_id
};
await db.updateStatus(msg, constant.input1)
if (variz)
bot.editMessageText('لطفا مبلغ فیش واریزی را وارد نمایید.', opts)
else
bot.editMessageText('لطفا مقدار مورد نیاز را وارد نمایید.', opts)
bot.editMessageText('لطفا مقدار مورد نیاز را وارد نمایید.', opts)
}
async function showFeeMessage(bot, msg) {
......@@ -302,7 +354,7 @@ async function sendDocMessage(bot, msg, user) {
{
text: 'خیر',
callback_data: 'no_' + user.id
callback_data: 'no_' + user
}
],
......@@ -322,16 +374,32 @@ async function sendDocMessage(bot, msg, user) {
async function listRequest(bot, msg, reqs) {
const opts = {
reply_markup: {
inline_keyboard: []
inline_keyboard: [],
}
};
let print="ℹ️ ";
print+='تعداد کل فیش ها: '
print+=reqs.length +"\n"
reqs = reqs.reverse()
for (let i = 0; i < reqs.length; i++) {
let text = "فیش شماره "
text += reqs[i].id
let text = ''
text += reqs[i].type.replace("variz", "واریز").replace("buy", "خرید").replace("sell", "فروش") + " "
text += reqs[i].count + " "
text += reqs[i].unit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم') + " "
if (reqs[i].type !== 'variz') {
text += 'با مبلغ '
text += ' '
text += reqs[i].fee
text += ' '
text += reqs[i].toUnit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
}
opts.reply_markup.inline_keyboard.push([{
text: text,
......@@ -340,33 +408,55 @@ async function listRequest(bot, msg, reqs) {
}
await db.updateStatus(msg, constant.start)
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, 'لطفا فیش مورد نظر را انتخاب کنید.', opts)
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, print, opts)
}
async function showDocument(bot, msg, req) {
let d = await bot.downloadFile(req.file_id, 'img')
return await bot.sendPhoto(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, d)
}
async function showRequest(bot, msg, req) {
let text = "فیش شماره "
text += req.id
text += '\n'
text += 'تعداد '
text += req.type.replace("sell", 'فروش').replace("buy", "خرید")
text += ' '
text += req.count
text += ' '
text += req.unit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
text += 'با مبلغ '
text += ' '
text += req.fee
text += ' '
text += req.toUnit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
let text = ''
if (req.type !== 'variz') {
text += 'تعداد '
text += req.type.replace("sell", 'فروش').replace("buy", "خرید")
text += ' '
text += req.count
text += ' '
text += req.unit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
text += 'با مبلغ '
text += ' '
text += req.fee
text += ' '
text += req.toUnit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
}
else {
text += ' واریز مبلغ '
text += ' '
text += req.count
text += ' '
text += req.unit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
}
await await db.updateStatus(msg, constant.input3)
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, text)
if (req.file_id !== null && req.file_id !== '')
await showDocument(bot, msg, req)
}
......@@ -384,6 +474,7 @@ module.exports = {
forwardCurrencyMessage: forwardCurrencyMessage,
sendDocMessage: sendDocMessage,
listRequest: listRequest,
showRequest: showRequest
showRequest: showRequest,
deleteMessage:deleteMessage
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment