Skip to content

Commit 2a11b2c

Browse files
committed
feat(*): create refund tokens for convertor
1 parent affae93 commit 2a11b2c

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { onMessageDocument } from './bot/commands/onMessageDocument.js'
5858
import { ct } from './bot/utils/createTranslate.js'
5959
import { checkTokens } from './bot/utils/checkTokens.js'
6060
import { isTokensEmpty } from './bot/commands/keyboard/empty_tokens.js'
61+
import { refundTokensIfError } from './bot/commands/admin/refundTokensIfError.js'
6162

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

165167
const app = express()
166168

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { INITIAL_SESSION } from '../../constants/index.js'
2+
import { db } from '../../db/index.js'
3+
import { Sequelize } from 'sequelize'
4+
import { ct } from '../../utils/createTranslate.js'
5+
6+
export const refundTokensIfError = bot => {
7+
bot.onText(/\/refund/, async msg => {
8+
if (msg?.chat?.id == process.env.NOTIF_GROUP) {
9+
const t = await ct(msg)
10+
const { id: chatId } = msg.chat
11+
const msgId = msg.message_id
12+
const { id } = msg.from
13+
const options = {
14+
parse_mode: 'HTML',
15+
reply_to_message_id: msgId
16+
}
17+
msg['ctx'] = INITIAL_SESSION
18+
try {
19+
const errors = await db.convertor_requests.findAll({ where: { status: 'error' } })
20+
21+
if (errors.length) {
22+
errors.map(async error => {
23+
24+
await db.convertor_requests.update(
25+
{ status: 'refund' },
26+
{ where: { document_id: error['document_id'] } }
27+
)
28+
29+
await db.subscriber.update(
30+
{ tokens: Sequelize.literal(`tokens + ${error['price_tokens']}`) },
31+
{ where: { chat_id: error['chat_id'] } }
32+
)
33+
34+
await bot.sendMessage(
35+
error['chat_id'],
36+
t('msg:refund', { tokens: error['price_tokens'] })
37+
)
38+
})
39+
40+
await bot.sendMessage(
41+
process.env.NOTIF_GROUP,
42+
`Произведен возврат токенов следующим пользователям: \n${errors.map(({
43+
chat_id,
44+
price_tokens
45+
}) => `👮‍♀️${chat_id}${price_tokens}\n`).join('')}`
46+
)
47+
} else {
48+
await bot.sendMessage(
49+
process.env.NOTIF_GROUP,
50+
'Задач со статусом завершения из-за ошибки не найдено.'
51+
)
52+
}
53+
54+
} catch (error) {
55+
await bot.sendMessage(chatId, `${error.message}`, options)
56+
}
57+
}
58+
})
59+
}

bot/commands/onMessageDocument.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ export const onMessageDocument = async (bot, msg) => {
113113
const type = fileType[fileType.length - 1]
114114
const name = fileType.map(i => i !== type ? i : '') // добавить алгоритм который будет убирать тоьлко последнюю точку
115115

116+
const { dataValues: settings } = await db.settings.findOne({ where: { user_id: 0 } })
117+
116118
const createTask = await db.convertor_requests.create({
117119
document_id: nanoid(10),
118120
chat_id: msg.from.id,
@@ -121,7 +123,7 @@ export const onMessageDocument = async (bot, msg) => {
121123
file_name: name.join(''),
122124
format_from: type,
123125
format_to: msg.data.split('-')[0],
124-
priority: 0
126+
price_tokens: settings['cost_converter']
125127
})
126128

127129
console.log('createTask', createTask)

bot/db/models/convertor_requests.model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default (sequelize, DataTypes) => {
2929
format_to: {
3030
type: DataTypes.STRING
3131
},
32-
priority: {
32+
price_tokens: {
3333
type: DataTypes.DOUBLE, // 0 - no, 1 - yes
3434
}
3535
}

0 commit comments

Comments
 (0)