Trade: Add tradehistories table

and create rows at the end of each trade.
Both users traded cards are logged.
This commit is contained in:
2023-01-12 14:43:06 +01:00
parent 2ef1dfe7a7
commit 692904d462
3 changed files with 72 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
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 { TradeStore } = require("../stores");
@@ -346,6 +346,12 @@ module.exports = {
async transactCards(trade, interaction) {
try {
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
for (let i = 0; i < trade.user1Cards.length; i++) {
const card = trade.user1Cards[i];
@@ -359,6 +365,7 @@ module.exports = {
trx.rollback();
throw new Error(`Card ${card.id} is not owned by ${trade.user1.discordId}!`);
}
historyData.userATraded.push(card.id);
}
//check and update user1 cards
for (let i = 0; i < trade.user2Cards.length; i++) {
@@ -373,8 +380,9 @@ module.exports = {
trx.rollback();
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) {
let logID = await GeneralUtils.generateLogID();