Skip to content

Commit

Permalink
Merge pull request #28 from CougarCS/tutor-tutorleaderboard-dev
Browse files Browse the repository at this point in the history
Create tutor-leaderboard command
  • Loading branch information
johncoxdev authored Oct 14, 2023
2 parents 6aa828c + 5060879 commit c5d5feb
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/commands/tutor-commands/tutor-leaderboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { SlashCommandBuilder } from "discord.js";
import { Command } from "../../interfaces/Command";
import { createEmbed } from "../../utils/embeded";
import { getTutorLeaderboard } from "../../utils/supabase";
import { commandLog, sendError } from "../../utils/logs";

const buildEmbedBody = (bodyArray: string[]): string => {
return bodyArray.join('\n');
};

export const tutorleaderboard: Command = {
data: new SlashCommandBuilder()
.setName("tutor-leaderboard")
.setDescription("See the top tutor!")
.addNumberOption((option) =>
option
.setName("number")
.setDescription("Number of leaderboard spaces you want to see!")
.setMaxValue(20)
.setMinValue(1)
.setRequired(false)
),
run: async (interaction) => {
await interaction.deferReply({ ephemeral: false });
const number = interaction.options.get("number", false);
commandLog(interaction, "/tutor-leaderboard", "Green", [
{
name: "number",
value: `${number}`,
},
]);

const errorTitle = "❌ Tutor Leaderboard Canceled!";

const maxSlots = (number?.value || 10) as number;
const leaderboardResponse = await getTutorLeaderboard(maxSlots);

if (leaderboardResponse.error) {
await sendError(errorTitle, leaderboardResponse.message, interaction);
return;
}

const leaderboardString = buildEmbedBody(leaderboardResponse.data);

const returnMessage = createEmbed(
"<:tutor:1151206705913417828> Tutor Leaderboard!",
leaderboardString || "The leaderboard is empty!"
).setColor("Green");
await interaction.editReply({ embeds: [returnMessage] });
return;
},
};
2 changes: 2 additions & 0 deletions src/utils/_Commandlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { tutorlog } from "../commands/tutor-commands/tutor-log";
import { appointTutor } from "../commands/officer-commands/appoint-tutor";
import { updateProfile } from "../commands/user-commands/update-profile";
import { createProfile } from "../commands/user-commands/create-profile";
import { tutorleaderboard } from "../commands/tutor-commands/tutor-leaderboard";

export const CommandList: Command[] = [
attendance,
Expand Down Expand Up @@ -51,4 +52,5 @@ export const CommandList: Command[] = [
appointTutor,
updateProfile,
createProfile,
tutorleaderboard,
];
6 changes: 6 additions & 0 deletions src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ export interface Database {
contact_id: string
}
Returns: number
},
hour: {
Args: {
tutor_id: string
}
Returns: number
}
}
Enums: {
Expand Down
101 changes: 101 additions & 0 deletions src/utils/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,3 +1078,104 @@ export const getTutoringType = async (
message: "Successfully fetched this tutoring type!",
};
};

export const getTutorHours = async (
queryData: UniqueTutorQuery
): Promise<SupabaseResponse<number>> => {
const tutorIdResponse = await getTutorId(queryData);

if (tutorIdResponse.error) {
return tutorIdResponse;
}

const tutor_id = tutorIdResponse.data;
const hourResponses = await supabase.rpc("hour", { tutor_id })

if (hourResponses.error) {
console.error(hourResponses.error);
return {
error: true,
message: "There was an error fetching tutor hour balance!",
};
}

const balance = hourResponses.data || 0;

return {
data: balance,
error: false,
message: "Successfully fetched tutor hour balance!",
};
};


export const getTutorLeaderboard = async (
maxSlots: number
): Promise<SupabaseResponse<string[]>> => {
const transactionsResponse = await supabase
.from("tutor_logs")
.select("tutor_id");

if (transactionsResponse.error) {
return {
error: true,
message: "There was an error fetching transactions!",
};
}

const tutorUniqueBalancePairs: { tutor_id: string; tutorBalance: number }[] = [];

for (let i = 0; i < transactionsResponse.data.length; i++) {
const { tutor_id } = transactionsResponse.data[i];
if (tutorUniqueBalancePairs.find((uci) => uci.tutor_id === tutor_id)) {
continue;
}
const tutorBalanceResponse = await getTutorHours({ tutor_id });

if (tutorBalanceResponse.error) {
return tutorBalanceResponse;
}

const tutorBalance = tutorBalanceResponse.data;
tutorUniqueBalancePairs.push({ tutor_id, tutorBalance });
}

tutorUniqueBalancePairs.sort((a, b) => b.tutorBalance - a.tutorBalance);
const arrayString: string[] = [];

for (let i = 0; i < tutorUniqueBalancePairs.length; i++) {
const { tutor_id, tutorBalance } = tutorUniqueBalancePairs[i];
const tutorIdentifierResponse = await getTutor({ tutor_id });

if (tutorIdentifierResponse.error) {
continue;
}
const UUID = tutorIdentifierResponse.data.contact_id;
const identifierResponse = await getContact({ contact_id: UUID });

if (identifierResponse.error) {
return identifierResponse;
}

const { discord_snowflake, first_name, last_name } = identifierResponse.data;
const identifier = discord_snowflake ? `<@${discord_snowflake}>` : `${first_name} ${last_name}`;
let icon = "";

if (arrayString.length === 0) { icon = "🥇"; }
else if (arrayString.length === 1){ icon = "🥈"; }
else if (arrayString.length === 2){ icon = "🥉";}
else { icon = `${arrayString.length + 1}.`; }

const slot = `${icon} ${identifier}: **${tutorBalance}** hour(s)`;

arrayString.push(slot);

if (arrayString.length === maxSlots) break;
}

return {
data: arrayString,
error: false,
message: "Successfully fetched tutor leaderboard!",
};
};

0 comments on commit c5d5feb

Please sign in to comment.