Skip to content

Commit

Permalink
feat(*): create refund tokens for convertor
Browse files Browse the repository at this point in the history
  • Loading branch information
aronbergman committed May 9, 2024
1 parent affae93 commit 2a11b2c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { onMessageDocument } from './bot/commands/onMessageDocument.js'
import { ct } from './bot/utils/createTranslate.js'
import { checkTokens } from './bot/utils/checkTokens.js'
import { isTokensEmpty } from './bot/commands/keyboard/empty_tokens.js'
import { refundTokensIfError } from './bot/commands/admin/refundTokensIfError.js'

const { TELEGRAM_API_KEY, SUDO_USER, NODE_REST_PORT, REACT_ADMIN_PORT, PROTOCOL, CORS_HOST } = process.env
const sudoUser = parseInt(SUDO_USER, 10)
Expand Down Expand Up @@ -161,6 +162,7 @@ setQuizModeForSubs(bot)
addSudoer(bot, sudoUser) // TODO: Удалить этот метод и таблицу
removeSudoer(bot, sudoUser) // TODO: Удалить этот метод и таблицу
listSudoers(bot, sudoUser) // TODO: Удалить этот метод и таблицу
refundTokensIfError(bot)

const app = express()

Expand Down
59 changes: 59 additions & 0 deletions bot/commands/admin/refundTokensIfError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { INITIAL_SESSION } from '../../constants/index.js'
import { db } from '../../db/index.js'
import { Sequelize } from 'sequelize'
import { ct } from '../../utils/createTranslate.js'

export const refundTokensIfError = bot => {
bot.onText(/\/refund/, async msg => {
if (msg?.chat?.id == process.env.NOTIF_GROUP) {
const t = await ct(msg)
const { id: chatId } = msg.chat
const msgId = msg.message_id
const { id } = msg.from
const options = {
parse_mode: 'HTML',
reply_to_message_id: msgId
}
msg['ctx'] = INITIAL_SESSION
try {
const errors = await db.convertor_requests.findAll({ where: { status: 'error' } })

if (errors.length) {
errors.map(async error => {

await db.convertor_requests.update(
{ status: 'refund' },
{ where: { document_id: error['document_id'] } }
)

await db.subscriber.update(
{ tokens: Sequelize.literal(`tokens + ${error['price_tokens']}`) },
{ where: { chat_id: error['chat_id'] } }
)

await bot.sendMessage(
error['chat_id'],
t('msg:refund', { tokens: error['price_tokens'] })
)
})

await bot.sendMessage(
process.env.NOTIF_GROUP,
`Произведен возврат токенов следующим пользователям: \n${errors.map(({
chat_id,
price_tokens
}) => `👮‍♀️${chat_id}${price_tokens}\n`).join('')}`
)
} else {
await bot.sendMessage(
process.env.NOTIF_GROUP,
'Задач со статусом завершения из-за ошибки не найдено.'
)
}

} catch (error) {
await bot.sendMessage(chatId, `${error.message}`, options)
}
}
})
}
4 changes: 3 additions & 1 deletion bot/commands/onMessageDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export const onMessageDocument = async (bot, msg) => {
const type = fileType[fileType.length - 1]
const name = fileType.map(i => i !== type ? i : '') // добавить алгоритм который будет убирать тоьлко последнюю точку

const { dataValues: settings } = await db.settings.findOne({ where: { user_id: 0 } })

const createTask = await db.convertor_requests.create({
document_id: nanoid(10),
chat_id: msg.from.id,
Expand All @@ -121,7 +123,7 @@ export const onMessageDocument = async (bot, msg) => {
file_name: name.join(''),
format_from: type,
format_to: msg.data.split('-')[0],
priority: 0
price_tokens: settings['cost_converter']
})

console.log('createTask', createTask)
Expand Down
2 changes: 1 addition & 1 deletion bot/db/models/convertor_requests.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default (sequelize, DataTypes) => {
format_to: {
type: DataTypes.STRING
},
priority: {
price_tokens: {
type: DataTypes.DOUBLE, // 0 - no, 1 - yes
}
}
Expand Down

0 comments on commit 2a11b2c

Please sign in to comment.