diff --git a/commands/trade.js b/commands/trade.js index c631f81..12b1841 100644 --- a/commands/trade.js +++ b/commands/trade.js @@ -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(); diff --git a/migrations/20230112132055-create-trade-histories.js b/migrations/20230112132055-create-trade-histories.js new file mode 100644 index 0000000..f4d5bfb --- /dev/null +++ b/migrations/20230112132055-create-trade-histories.js @@ -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'); + } +}; \ No newline at end of file diff --git a/models/tradehistories.js b/models/tradehistories.js new file mode 100644 index 0000000..fe320f1 --- /dev/null +++ b/models/tradehistories.js @@ -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; +}; \ No newline at end of file