WIP: Implement adding to the users inventory on confirm
Some checks failed
Test / build (push) Failing after 41s

This commit is contained in:
Ethan Lane 2025-03-05 19:05:10 +00:00
parent ad0d2aef08
commit 1f5a19f101
2 changed files with 32 additions and 0 deletions

View file

@ -2,6 +2,8 @@ import {ButtonInteraction} from "discord.js";
import AppLogger from "../../client/appLogger";
import EffectHelper from "../../helpers/EffectHelper";
import EmbedColours from "../../constants/EmbedColours";
import User from "../../database/entities/app/User";
import {EffectDetails} from "../../constants/EffectDetails";
export default class Buy {
public static async Execute(interaction: ButtonInteraction) {
@ -27,6 +29,13 @@ export default class Buy {
AppLogger.LogError("Buy Confirm", "Not enough parameters");
return;
}
const effectDetail = EffectDetails.get(id);
if (!effectDetail) {
AppLogger.LogError("Buy Confirm", "Effect detail not found!");
return;
}
const quantityNumber = Number(quantity);
@ -35,6 +44,25 @@ export default class Buy {
return;
}
const totalCost = effectDetail.cost * quantityNumber;
const user = await User.FetchOneById(User, interaction.user.id);
if (!user) {
AppLogger.LogError("Buy Confirm", "Unable to find user");
return;
}
if (user.Currency < totalCost) {
interaction.reply(`You don't have enough currency to buy this! You have \`${user.Currency} Currency\` and need \`${totalCost} Currency\`!`);
return;
}
user.RemoveCurrency(totalCost);
await user.Save(User, user);
await EffectHelper.AddEffectToUserInventory(interaction.user.id, id, quantityNumber);
const generatedEmbed = await EffectHelper.GenerateEffectBuyEmbed(interaction.user.id, id, quantityNumber, true);
if (typeof generatedEmbed == "string") {

View file

@ -244,6 +244,10 @@ describe("Confirm", () => {
expect(interaction.update).not.toHaveBeenCalled();
});
test.todo("GIVEN user is not found, EXPECT error");
test.todo("GIVEN user does not have enough currency, EXPECT error");
test("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied", async () => {
// Assert
interaction.customId += " id 1";