Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-diaz committed Oct 16, 2024
1 parent d765105 commit 8d835fe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
58 changes: 44 additions & 14 deletions src/pages/api/admin/participant/identities/generate.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
import { findMostRecentOfmi, findParticipants } from "@/lib/ofmi";
import {
ContestantParticipationInput,
ContestantParticipationInputSchema,
} from "@/types/participation.schema";
import { VolunteerParticipationInputSchema } from "@/types/participation.schema";
import { Value } from "@sinclair/typebox/build/cjs/value";
import { NextApiRequest, NextApiResponse } from "next";
import countries from "@/lib/address/iso-3166-countries.json";
import { capitalizeInitials, getMexStateCode } from "@/utils";
import { capitalizeInitials, filterNull, getMexStateCode } from "@/utils";

async function generateIdentitiesHandler(
req: NextApiRequest,
res: NextApiResponse,
): Promise<void> {
const ofmi = await findMostRecentOfmi();
const allParticipants = await findParticipants(ofmi);
const onlyContestants = allParticipants.filter((participant) => {
return Value.Check(
ContestantParticipationInputSchema,
participant.userParticipation,
);
});
const onlyContestants = filterNull(
allParticipants.map((participant) => {
if (
Value.Check(
VolunteerParticipationInputSchema,
participant.userParticipation,
)
) {
return null;
}
return {
ofmiEdition: participant.ofmiEdition,
user: participant.user,
userParticipation: participant.userParticipation,
};
}),
);

const states = new Map<string, number>();

const getMaxContestantsCount = (
contestants: typeof onlyContestants,
): number => {
let maxi = 0;
const states = new Map<string, number>();
// Mexican contestants are divided by their state
// International contestants are divided by their country
// Among all these different groups, find the one with the greatest amount of participants.
// This is done to make sure participant usernames are as short as possible.
for (const contestant of contestants) {
let state = contestant.userParticipation.schoolCountry;
if (state === "Mexico") {
state = contestant.userParticipation.schoolState;
}
const count = (states.get(state) || 0) + 1;
maxi = Math.max(maxi, count);
states.set(state, count);
}
return maxi;
};

const states = new Map();
const minDigits = Math.log10(onlyContestants.length);
const minDigits = Math.log10(getMaxContestantsCount(onlyContestants));

const generateUsername = (state: string): string => {
const rawNumber = (states.get(state) || 0) + 1;
Expand All @@ -34,7 +64,7 @@ async function generateIdentitiesHandler(

onlyContestants.map((contestant) => {
const { user, userParticipation } = contestant;
const participation = userParticipation as ContestantParticipationInput;
const participation = userParticipation;
const country = countries.find((country) => {
return country.name === participation.schoolCountry;
}) || {
Expand Down
2 changes: 1 addition & 1 deletion src/types/participation.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const ContestantParticipationInputSchema = Type.Object({
schoolState: Type.String({ minLength: 1 }),
});

const VolunteerParticipationInputSchema = Type.Object({
export const VolunteerParticipationInputSchema = Type.Object({
role: Type.Literal(ParticipationRole.VOLUNTEER),
educationalLinkageOptIn: Type.Boolean(),
fundraisingOptIn: Type.Boolean(),
Expand Down

0 comments on commit 8d835fe

Please sign in to comment.