Trade: Add tradehistories table
and create rows at the end of each trade. Both users traded cards are logged.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
|
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
|
||||||
const { Card, User, Character } = require("../models");
|
const { Card, User, Character, TradeHistory } = require("../models");
|
||||||
const { UserUtils, CardUtils, DbUtils, GeneralUtils } = require("../util");
|
const { UserUtils, CardUtils, DbUtils, GeneralUtils } = require("../util");
|
||||||
const { TradeStore } = require("../stores");
|
const { TradeStore } = require("../stores");
|
||||||
|
|
||||||
@@ -346,6 +346,12 @@ module.exports = {
|
|||||||
async transactCards(trade, interaction) {
|
async transactCards(trade, interaction) {
|
||||||
try {
|
try {
|
||||||
const result = await db.sequelize.transaction(async (trx) => {
|
const result = await db.sequelize.transaction(async (trx) => {
|
||||||
|
let historyData = {
|
||||||
|
userAId: trade.user1.id,
|
||||||
|
userBId: trade.user2.id,
|
||||||
|
userATraded: [],
|
||||||
|
userBTraded: []
|
||||||
|
}
|
||||||
//check and update user1 cards
|
//check and update user1 cards
|
||||||
for (let i = 0; i < trade.user1Cards.length; i++) {
|
for (let i = 0; i < trade.user1Cards.length; i++) {
|
||||||
const card = trade.user1Cards[i];
|
const card = trade.user1Cards[i];
|
||||||
@@ -359,6 +365,7 @@ module.exports = {
|
|||||||
trx.rollback();
|
trx.rollback();
|
||||||
throw new Error(`Card ${card.id} is not owned by ${trade.user1.discordId}!`);
|
throw new Error(`Card ${card.id} is not owned by ${trade.user1.discordId}!`);
|
||||||
}
|
}
|
||||||
|
historyData.userATraded.push(card.id);
|
||||||
}
|
}
|
||||||
//check and update user1 cards
|
//check and update user1 cards
|
||||||
for (let i = 0; i < trade.user2Cards.length; i++) {
|
for (let i = 0; i < trade.user2Cards.length; i++) {
|
||||||
@@ -373,8 +380,9 @@ module.exports = {
|
|||||||
trx.rollback();
|
trx.rollback();
|
||||||
throw new Error(`Card ${card.id} is not owned by ${trade.user2.discordId}!`);
|
throw new Error(`Card ${card.id} is not owned by ${trade.user2.discordId}!`);
|
||||||
}
|
}
|
||||||
|
historyData.userBTraded.push(card.id);
|
||||||
}
|
}
|
||||||
|
let history = await TradeHistory.create(historyData);
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
let logID = await GeneralUtils.generateLogID();
|
let logID = await GeneralUtils.generateLogID();
|
||||||
|
|||||||
36
migrations/20230112132055-create-trade-histories.js
Normal file
36
migrations/20230112132055-create-trade-histories.js
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
'use strict';
|
||||||
|
module.exports = {
|
||||||
|
async up(queryInterface, Sequelize) {
|
||||||
|
await queryInterface.createTable('TradeHistories', {
|
||||||
|
id: {
|
||||||
|
allowNull: false,
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
type: Sequelize.INTEGER
|
||||||
|
},
|
||||||
|
userAId: {
|
||||||
|
type: Sequelize.INTEGER
|
||||||
|
},
|
||||||
|
userBId: {
|
||||||
|
type: Sequelize.INTEGER
|
||||||
|
},
|
||||||
|
userATraded: {
|
||||||
|
type: Sequelize.JSON
|
||||||
|
},
|
||||||
|
userBTraded: {
|
||||||
|
type: Sequelize.JSON
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
allowNull: false,
|
||||||
|
type: Sequelize.DATE
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
allowNull: false,
|
||||||
|
type: Sequelize.DATE
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async down(queryInterface, Sequelize) {
|
||||||
|
await queryInterface.dropTable('TradeHistories');
|
||||||
|
}
|
||||||
|
};
|
||||||
26
models/tradehistories.js
Normal file
26
models/tradehistories.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
'use strict';
|
||||||
|
const {
|
||||||
|
Model
|
||||||
|
} = require('sequelize');
|
||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
class TradeHistory extends Model {
|
||||||
|
/**
|
||||||
|
* Helper method for defining associations.
|
||||||
|
* This method is not a part of Sequelize lifecycle.
|
||||||
|
* The `models/index` file will call this method automatically.
|
||||||
|
*/
|
||||||
|
static associate(models) {
|
||||||
|
// define association here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TradeHistory.init({
|
||||||
|
userAId: DataTypes.INTEGER,
|
||||||
|
userBId: DataTypes.INTEGER,
|
||||||
|
userATraded: DataTypes.JSON,
|
||||||
|
userBTraded: DataTypes.JSON
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'TradeHistory',
|
||||||
|
});
|
||||||
|
return TradeHistory;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user