From dec6de839803ded75cffaf3d0f89f93d01f4569d Mon Sep 17 00:00:00 2001 From: smyalygames Date: Wed, 1 Mar 2023 13:03:17 +0000 Subject: [PATCH] feat(arabot): add updating statistics for outreach groups --- .../20230228200640_stat_role/migration.sql | 10 ++++ src/commands/outreach/outreach.ts | 53 ++++++++++++++++++- src/utils/database/outreach.ts | 38 +++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20230228200640_stat_role/migration.sql diff --git a/prisma/migrations/20230228200640_stat_role/migration.sql b/prisma/migrations/20230228200640_stat_role/migration.sql new file mode 100644 index 0000000..c98bcf2 --- /dev/null +++ b/prisma/migrations/20230228200640_stat_role/migration.sql @@ -0,0 +1,10 @@ +-- CreateTable +CREATE TABLE "StatRole" ( + "statId" INTEGER NOT NULL, + "roleId" TEXT NOT NULL, + + CONSTRAINT "StatRole_pkey" PRIMARY KEY ("statId") +); + +-- AddForeignKey +ALTER TABLE "StatRole" ADD CONSTRAINT "StatRole_statId_fkey" FOREIGN KEY ("statId") REFERENCES "Stat"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/src/commands/outreach/outreach.ts b/src/commands/outreach/outreach.ts index a3e3239..ee7d8f6 100644 --- a/src/commands/outreach/outreach.ts +++ b/src/commands/outreach/outreach.ts @@ -26,7 +26,12 @@ import { checkActiveEvent, createEvent, createStat, - getCurrentEvent, getStatFromLeader, getStatFromRole, getStatGroups, userInStats, + getCurrentEvent, + getStatFromLeader, + getStatFromRole, + getStatGroups, + updateStats, + userInStats, } from '#utils/database/outreach'; import IDs from '#utils/ids'; @@ -96,6 +101,8 @@ export class OutreachCommand extends Subcommand { .setDescription('How many said would go vegan?')) .addIntegerOption((option) => option.setName('considered') .setDescription('How many seriously considered being vegan?')) + .addIntegerOption((option) => option.setName('anti-vegan') + .setDescription('How many people had anti-vegan viewpoints?')) .addIntegerOption((option) => option.setName('thanked') .setDescription('How many thanked you for the conversation?')) .addIntegerOption((option) => option.setName('documentary') @@ -280,4 +287,48 @@ export class OutreachCommand extends Subcommand { content: `Added ${user} to the group!`, }); } + + public async groupUpdate(interaction: Subcommand.ChatInputCommandInteraction) { + const vegan = interaction.options.getInteger('vegan'); + const considered = interaction.options.getInteger('considered'); + const antiVegan = interaction.options.getInteger('anti-vegan'); + const thanked = interaction.options.getInteger('thanked'); + const documentary = interaction.options.getInteger('documentary'); + const educated = interaction.options.getInteger('educated'); + const leader = interaction.user; + + const stats = { + vegan: vegan !== null ? vegan : 0, + considered: considered !== null ? considered : 0, + antiVegan: antiVegan !== null ? antiVegan : 0, + thanked: thanked !== null ? thanked : 0, + documentary: documentary !== null ? documentary : 0, + educated: educated !== null ? educated : 0, + }; + + if (leader === null) { + await interaction.reply({ + content: 'Could not find your user!', + ephemeral: true, + }); + return; + } + + await interaction.deferReply({ ephemeral: true }); + + const stat = await getStatFromLeader(leader.id); + + if (stat === null) { + await interaction.editReply({ + content: 'You\'re not the leader of a group!', + }); + return; + } + + await updateStats(stat.id, stats); + + await interaction.editReply({ + content: 'Updated the database with the stats!', + }); + } } diff --git a/src/utils/database/outreach.ts b/src/utils/database/outreach.ts index 15f0074..64c8b1f 100644 --- a/src/utils/database/outreach.ts +++ b/src/utils/database/outreach.ts @@ -93,6 +93,44 @@ export async function createStat(eventId: number, leaderId: Snowflake, roleId: S }); } +export async function updateStats( + statId: number, + stats: { + vegan: number, + considered: number, + antiVegan: number, + thanked: number, + documentary: number, + educated: number, + }, +) { + await container.database.stat.update({ + where: { + id: statId, + }, + data: { + vegan: { + increment: stats.vegan, + }, + considered: { + increment: stats.considered, + }, + antivegan: { + increment: stats.antiVegan, + }, + thanked: { + increment: stats.thanked, + }, + documentary: { + increment: stats.documentary, + }, + educated: { + increment: stats.educated, + }, + }, + }); +} + export async function getStatGroups(eventId: number) { const stats = await container.database.stat.findMany({ where: {