sequelize-game-bank 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,6 +35,11 @@ const Account = sequelize.define('account', {
35
35
  allowNull: false,
36
36
  defaultValue: false,
37
37
  field: '2FA-status'
38
+ },
39
+ cardNumber:{
40
+ type:DataTypes.STRING,
41
+ allowNull:true,
42
+ unique:true
38
43
  }
39
44
  });
40
45
  User.hasMany(Account, { foreignKey: 'user', sourceKey: 'name' });
@@ -0,0 +1,91 @@
1
+ const path = require("path");
2
+ const bcrypt = require('bcrypt');
3
+ const Account = require(path.join(__dirname, '..', '..', 'models', 'Account'));
4
+ const User = require(path.join(__dirname, '..', '..', 'models', 'User'));
5
+ const Transaction = require(path.join(__dirname, '..', '..', 'models', 'Transaction'));
6
+ const sequelize = require(path.join(__dirname, '..', '..', 'config', 'init-db'));
7
+ const { sendBotNotification } = require(path.join(__dirname, '..', 'bot', 'bot'));
8
+ const logger = require("custom-color-logs").print;
9
+ async function getIdByCard(card) {
10
+ const result = await Account.findOne({
11
+ where:{cardNumber:card.toString()},
12
+ attributes: ['id']
13
+ });
14
+ if (!result) return false
15
+ return result
16
+ }
17
+ module.exports = async function cardTransfer(card, toId, amount, pinCode) {
18
+ const t = await sequelize.transaction();
19
+ try {
20
+ const fromId = await getIdByCard(card);
21
+ const numericAmount = parseFloat(amount);
22
+ if (isNaN(numericAmount) || numericAmount <= 0) {
23
+ await t.rollback();
24
+ return { status: false, statusCode: -2, message: 'Некорректная сумма перевода' };
25
+ }
26
+ const fromAcc = await Account.findByPk(fromId, {
27
+ transaction: t,
28
+ lock: t.LOCK.UPDATE
29
+ });
30
+ const toAcc = await Account.findByPk(toId, {
31
+ transaction: t,
32
+ lock: t.LOCK.UPDATE
33
+ });
34
+ if (!fromAcc || !toAcc) {
35
+ await t.rollback();
36
+ return { status: false, statusCode: -2, message: 'Один из счетов не найден' };
37
+ }
38
+ if (!fromAcc.active || !toAcc.active) {
39
+ await t.rollback();
40
+ return { status: false, statusCode: -11, message: 'Один из счетов заблокирован' };
41
+ }
42
+ const isPinValid = await bcrypt.compare(pinCode, fromAcc.pin_code);
43
+ if (!isPinValid) {
44
+ await t.rollback();
45
+ return { status: false, statusCode: -23, message: 'Неверный пароль счета отправителя' };
46
+ }
47
+ if (fromAcc.user !== 'root' && parseFloat(fromAcc.manny) < numericAmount) {
48
+ await t.rollback();
49
+ return { status: false, statusCode: -11, message: 'Недостаточно средств на счете' };
50
+ }
51
+ if (fromAcc.two_fa_status) {
52
+ const owner = await User.findByPk(fromAcc.user, { transaction: t });
53
+ if (owner && owner.two_fa_status) {
54
+ const isTotpValid = await bcrypt.compare(totpCode, owner.two_fa_token);
55
+ if (!isTotpValid) {
56
+ await t.rollback();
57
+ return { status: false, statusCode: -2, message: 'Неверный токен подтверждения 2FA' };
58
+ }
59
+ }
60
+ }
61
+ await fromAcc.decrement('manny', { by: numericAmount, transaction: t });
62
+ await toAcc.increment('manny', { by: numericAmount, transaction: t });
63
+ await Transaction.create({
64
+ account_id: fromId,
65
+ type: 'TRANSFER_OUT',
66
+ sender_or_receiver: `На счет #${toId} (${toAcc.user})`,
67
+ amount: numericAmount
68
+ }, { transaction: t });
69
+ await Transaction.create({
70
+ account_id: toId,
71
+ type: 'TRANSFER_IN',
72
+ sender_or_receiver: `Со счета #${fromId} (${fromAcc.user})`,
73
+ amount: numericAmount
74
+ }, { transaction: t });
75
+ await t.commit();
76
+ await fromAcc.reload();
77
+ await toAcc.reload();
78
+ try {
79
+ await sendBotNotification('transfer-from-bot', { fromId, toId, amount: numericAmount, balance: fromAcc.manny });
80
+ await sendBotNotification('transfer-to-bot', { toId, fromId, amount: numericAmount, balance: toAcc.manny });
81
+ } catch (botErr) {
82
+ console.log(logger.DatabaseError('Ошибка отправки уведомления боту:', botErr.message))
83
+ }
84
+ return { status: true, statusCode: 1 };
85
+ } catch (err) {
86
+ if (!t.finished) {
87
+ await t.rollback();
88
+ }
89
+ return { status: false, statusCode: -10, message: err.message };
90
+ }
91
+ };
@@ -12,10 +12,19 @@ module.exports = async function createAccount(username, pinCode, enable2FA) {
12
12
  pin_code: hashedPin,
13
13
  manny: 0.00,
14
14
  active: true,
15
- two_fa_status: enable2FA
15
+ two_fa_status: enable2FA,
16
+ cardNumber:null
16
17
  });
17
18
  return { status: true, statusCode: 1, accountId: newAccount.id };
18
19
  } catch (err) {
20
+ console.log(err)
21
+
22
+
23
+
24
+
25
+
26
+
27
+
19
28
  return { status: false, statusCode: -10, message: err.message };
20
29
  }
21
30
  };
@@ -0,0 +1,18 @@
1
+ const path = require("path");
2
+ const Account = require(path.join(__dirname, '..', '..', 'models', 'Account'));
3
+ const logger = require("custom-color-logs").print;
4
+ module.exports = async function linkACard(card, id, pin_code) {
5
+ try {
6
+ const account = await Account.findByPk(id);
7
+ if (!account) return { status: false, statusCode: -2, message: "?¿?" };
8
+ if(!account.active) return { status: false, statusCode: -11, message: 'Один из счетов заблокирован' };
9
+ if (account.pin_code !== pin_code) return { status: false, statusCode: -23, message: "Неправильны пароль!" };
10
+ if (account.cardNumber !== null) return { status: false, statusCode: -19, message: "Карты уже привязана" };
11
+ account.cardNumber = card;
12
+ await account.save();
13
+ return {status: true, statusCode: 1, message: 'Привязка карты удалась'};
14
+ } catch (err) {
15
+ console.log(logger.BankError(err));
16
+ return { status: false, statusCode: -10, message: err.message };
17
+ }
18
+ }
@@ -9,5 +9,7 @@ module.exports = {
9
9
  findManny: require(path.join(__dirname, 'account', 'find-manny')),
10
10
  creatCredit: require(path.join(__dirname, 'account', 'create-credit')),
11
11
  getLogs: require(path.join(__dirname, 'account', 'logs')),
12
- changeAccountStatus: require(path.join(__dirname, 'account', 'change-status'))
12
+ changeAccountStatus: require(path.join(__dirname, 'account', 'change-status')),
13
+ linkACard: require(path.join(__dirname, 'account', 'linkACard')),
14
+ cardTransfer: require(path.join(__dirname, 'account', 'cardTransfer'))
13
15
  };
package/index.js CHANGED
@@ -11,11 +11,9 @@ const { writeTemplatesIfNotExist } = require(path.join(__dirname, 'database', 's
11
11
  const registerRoutes = require(path.join(__dirname, 'server', 'server'));
12
12
  const Transaction = require(path.join(__dirname, 'database', 'models', 'Transaction'));
13
13
  const app = express();
14
-
15
14
  const { verifySession } = require(path.join(__dirname, 'server', 'server'));
16
15
  app.use(express.json());
17
16
  app.use(cookieParser());
18
- require(path.join(__dirname, 'database', 'services', 'bot', 'cron-schedule'));
19
17
  async function Bank() {
20
18
  try {
21
19
  await sequelize.sync();
@@ -53,6 +51,8 @@ async function Bank() {
53
51
  app.get('/home.html', (req, res) => res.sendFile(path.join(__dirname, 'public', 'home.html')));
54
52
  app.get('/favicon.ico', (req, res) => res.sendFile(path.join(__dirname, 'public', 'icon', 'icon.png')));
55
53
  app.get('/', (req, res) => res.redirect('/home.html'));
54
+
55
+ require(path.join(__dirname, 'database', 'services', 'bot', 'cron-schedule'));
56
56
  return configuredApp;
57
57
  } catch (err) {
58
58
  console.log(logger.ServerBankError("Критическая ошибка инициализации модулей", err));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sequelize-game-bank",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "A Sequelize-based gaming economy and banking system featuring user profiles, credits, admin management, and detailed transaction history.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/server/server.js CHANGED
@@ -210,9 +210,22 @@ module.exports = function registerRoutes(app) {
210
210
  const result = await accountsHub.getLogs(req.user.name);
211
211
  return res.json(result);
212
212
  });
213
-
214
-
215
-
213
+ app.post('/api/terminal/login', async(req, res)=>{
214
+ if(!req.body) return res.sendStatus(400)
215
+ const id = req.body.id;
216
+ const pincode = req.body.pincode;
217
+ const card = req.body.card;
218
+ const result = await accountsHub.linkACard(card, id, pincode)
219
+ if(!result.status) return res.sendStatus(422)
220
+ return res.sendStatus(200);
221
+ });
222
+ app.post('/api/terminal/transfer', async(req, res)=>{
223
+ const card = req.body.card;
224
+ const to = req.body.to;
225
+ const pincode = req.body.pincode;
226
+ const manny = req.body.pinCode;
227
+ const result = await accountsHub.cardTransfer(card, to, manny, pincode)
228
+ });
216
229
  return app;
217
230
  };
218
231
  module.exports.verifySession = verifySession;