Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/components/features/query/query-filters.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MultiSelect } from "@/components/ui/multi-select";
import { Menu, Filter, ArrowUpDown, Group } from "lucide-react";
import { GroupBy } from "./popovers/group-by";
import { FilterRows } from "./popovers/filter-rows";
import { SortBy } from "./popovers/sort-by";
import { useQuery } from "@/providers/query-provider";
import { ArrowUpDown, Filter, Group, Menu } from "lucide-react";
import { useMemo } from "react";
import { FilterRows } from "./popovers/filter-rows";
import { GroupBy } from "./popovers/group-by";
import { SortBy } from "./popovers/sort-by";

interface QueryFiltersProps {
availableColumns: string[];
Expand Down Expand Up @@ -37,34 +37,31 @@ export function QueryFilters({ availableColumns }: QueryFiltersProps) {
value: column,
}));


const columns = applicants[0] ? Object.keys(applicants[0]) : [];

const columnTypes = useMemo(() => {
return Object.fromEntries(
columns.map(col => [col, typeof applicants[0]?.[col]])
);
return Object.fromEntries(columns.map((col) => [col, typeof applicants[0]?.[col]]));
}, [columns, applicants]);


/**
* Definitions to determine which columns are groupable and aggreagtable.
* For example, SUM/AVG can only be applied to numeric columns.
*/
const countColumns = columns;

const { sumAvgColumns, minMaxColumns, groupableColumns } = useMemo(() => {
const sumAvg = columns.filter(col => columnTypes[col] === "number");
const minMax = columns.filter(col =>
columnTypes[col] === "number" ||
columnTypes[col] === "string" ||
applicants[0]?.[col] instanceof Date
const sumAvg = columns.filter((col) => columnTypes[col] === "number");
const minMax = columns.filter(
(col) =>
columnTypes[col] === "number" ||
columnTypes[col] === "string" ||
applicants[0]?.[col] instanceof Date,
);
const groupable = columns.filter(col => {
const groupable = columns.filter((col) => {
const val = applicants[0]?.[col];
return typeof val === "string" || typeof val === "boolean";
return typeof val === "string" || typeof val === "boolean" || typeof val === "number";
});

return { sumAvgColumns: sumAvg, minMaxColumns: minMax, groupableColumns: groupable };
}, [columns, columnTypes, applicants]);

Expand Down Expand Up @@ -120,7 +117,6 @@ export function QueryFilters({ availableColumns }: QueryFiltersProps) {
onRemoveFilter={onFilterRemove}
onFilterOperatorChange={onFilterOperatorChange}
/>

</div>

<div className="flex flex-col gap-2">
Expand Down
1 change: 1 addition & 0 deletions src/lib/firebase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export interface Applicant {
breakfast?: Timestamp[];
lunch?: Timestamp[];
dinner?: Timestamp[];
snack?: Timestamp[];
};
day2?: {
breakfast?: Timestamp[];
Expand Down
131 changes: 67 additions & 64 deletions src/services/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db } from "@/lib/firebase/client";
import type { Applicant, HackathonDayOf } from "@/lib/firebase/types";
import { collection, onSnapshot, query, getDocs, where } from "firebase/firestore";
import { returnTrueKey, createStringFromSelection, splitHackathon } from "@/lib/utils";
import { createStringFromSelection, returnTrueKey, splitHackathon } from "@/lib/utils";
import { collection, getDocs, onSnapshot, query, where } from "firebase/firestore";

/**
* Utility function that returns Applicants collection realtime data for a specific hackathon
Expand All @@ -14,12 +14,12 @@ export const subscribeToApplicants = (hackathon: string, callback: (docs: Applic
onSnapshot(
query(
collection(db, "Hackathons", hackathon, "Applicants"),
where("status.applicationStatus", "!=", "inProgress")
where("status.applicationStatus", "!=", "inProgress"),
),
(querySnapshot) => {
const docs = querySnapshot.docs.map((doc) => ({ _id: doc.id, ...doc.data() } as Applicant));
const docs = querySnapshot.docs.map((doc) => ({ _id: doc.id, ...doc.data() }) as Applicant);
callback(docs);
}
},
);

/**
Expand All @@ -28,20 +28,22 @@ export const subscribeToApplicants = (hackathon: string, callback: (docs: Applic
* @param hackathon - The hackathon name to determine data format
* @returns a flattened object with all properties at the top level
*/
export const flattenApplicantData = (applicant: Applicant, hackathon?: string): FlattenedApplicant => {

export const flattenApplicantData = (
applicant: Applicant,
hackathon?: string,
): FlattenedApplicant => {
const [, year] = hackathon ? splitHackathon(hackathon) : [undefined, undefined];
const hackathonYear = year ? Number.parseInt(year) : 2025;
const isLegacyFormat = hackathonYear < 2024 || hackathon === "nwHacks2024";
const isLegacyFormat = hackathonYear < 2024 || hackathon === "nwHacks2024";

const computedIsOfLegalAge = (applicant: Applicant) => {
const rawAge = applicant.basicInfo?.ageByHackathon
if (rawAge == "<=16") return false
if (rawAge == ">24") return true
const rawAge = applicant.basicInfo?.ageByHackathon;
if (rawAge == "<=16") return false;
if (rawAge == ">24") return true;

const numericAge = typeof rawAge === "number" ? rawAge : Number(rawAge)
return numericAge >= 19
}
const numericAge = typeof rawAge === "number" ? rawAge : Number(rawAge);
return numericAge >= 19;
};

const flattened: FlattenedApplicant = {
// Basic Info
Expand All @@ -50,30 +52,30 @@ export const flattenApplicantData = (applicant: Applicant, hackathon?: string):
email: applicant.basicInfo?.email || "",
phoneNumber: applicant.basicInfo?.phoneNumber || "",
school: applicant.basicInfo?.school || "",
major: isLegacyFormat
? (applicant.basicInfo?.major as string || '')
major: isLegacyFormat
? (applicant.basicInfo?.major as string) || ""
: createStringFromSelection(
applicant.basicInfo?.major as Record<string, boolean> | undefined,
''
"",
),
educationLevel: applicant.basicInfo?.educationLevel || "",
graduation: applicant.basicInfo?.graduation || "",
gender: typeof applicant.basicInfo?.gender === 'string'
? applicant.basicInfo.gender
: createStringFromSelection(
applicant.basicInfo?.gender as Record<string, boolean> | undefined,
''
),
gender:
typeof applicant.basicInfo?.gender === "string"
? applicant.basicInfo.gender
: createStringFromSelection(
applicant.basicInfo?.gender as Record<string, boolean> | undefined,
"",
),
isOfLegalAge: computedIsOfLegalAge(applicant),
culturalBackground: returnTrueKey(applicant.basicInfo?.ethnicity || applicant.basicInfo?.culturalBackground),
dietaryRestriction: createStringFromSelection(
applicant.basicInfo?.dietaryRestriction,
''
culturalBackground: returnTrueKey(
applicant.basicInfo?.ethnicity || applicant.basicInfo?.culturalBackground,
),

dietaryRestriction: createStringFromSelection(applicant.basicInfo?.dietaryRestriction, ""),

// Application Status
applicationStatus: applicant.status?.applicationStatus || "",

// Skills
role: returnTrueKey(applicant.skills?.contributionRole),
github: applicant.skills?.github || "",
Expand All @@ -82,41 +84,40 @@ export const flattenApplicantData = (applicant: Applicant, hackathon?: string):
resume: applicant.skills?.resume || "",

firstTimeHacker: applicant.skills?.numHackathonsAttended === "0" || false,

// Engagement source
engagementSource: isLegacyFormat
? (applicant.questionnaire?.engagementSource as string || '')
? (applicant.questionnaire?.engagementSource as string) || ""
: createStringFromSelection(
applicant.questionnaire?.engagementSource as Record<string, boolean> | undefined,
applicant.questionnaire?.otherEngagementSource || ''
applicant.questionnaire?.otherEngagementSource || "",
),
friendEmail: applicant.questionnaire?.friendEmail || "",


// Terms and conditions
MLHCodeOfConduct: applicant.termsAndConditions?.MLHCodeOfConduct || false,
nwPlusPrivacyPolicy: applicant.termsAndConditions?.nwPlusPrivacyPolicy || false,
shareWithSponsors: applicant.termsAndConditions?.shareWithSponsors || false,
shareWithnwPlus: applicant.termsAndConditions?.shareWithnwPlus || false,


// Score info
resumeScore: applicant.score?.scores?.ResumeScore?.score || 0,
totalScore: applicant.score?.totalScore || 0,
totalZScore: applicant.score?.totalZScore || -Infinity,
scoreComment: applicant.score?.comment || "",

// Day-of info
day1Breakfast: applicant.dayOf?.day1?.breakfast?.length || 0,
day1Lunch: applicant.dayOf?.day1?.lunch?.length || 0,
day1Dinner: applicant.dayOf?.day1?.dinner?.length || 0,
day1Snack: applicant.dayOf?.day1?.snack?.length || 0,
day2Breakfast: applicant.dayOf?.day2?.breakfast?.length || 0,
day2Lunch: applicant.dayOf?.day2?.lunch?.length || 0,
day2Dinner: applicant.dayOf?.day2?.dinner?.length || 0,
checkedIn: applicant.dayOf?.checkedIn || false,
attendedEvents: applicant.dayOf?.events?.map((e: { eventName: string }) => e.eventName).join(', ') || '',
attendedEvents:
applicant.dayOf?.events?.map((e: { eventName: string }) => e.eventName).join(", ") || "",
points: 0,

};

return flattened;
Expand Down Expand Up @@ -189,7 +190,7 @@ export const getAvailableColumns = (): string[] => {
northAmerica: false,
other: false,
preferNot: false,
}
},
},
status: {
applicationStatus: "inProgress",
Expand Down Expand Up @@ -218,7 +219,7 @@ export const getAvailableColumns = (): string[] => {
shareWithnwPlus: false,
},
};

const flattenedApplicant = flattenApplicantData(sampleApplicant);
return Object.keys(flattenedApplicant);
};
Expand All @@ -240,88 +241,90 @@ export interface FlattenedApplicant {
isOfLegalAge: boolean;
culturalBackground: string;
dietaryRestriction: string;

// Application Status
applicationStatus: string;

// Skills
role: string;
github: string;
linkedin: string;
portfolio: string;
resume: string;
firstTimeHacker: boolean;

// Questionnaire
engagementSource: string;
friendEmail: string;

// Terms and conditions
MLHCodeOfConduct: boolean;
nwPlusPrivacyPolicy: boolean;
shareWithSponsors: boolean;
shareWithnwPlus: boolean;

// Score info
resumeScore: number;
totalScore: number;
totalZScore: number;
scoreComment: string;

// Day-of info
day1Breakfast: number;
day1Lunch: number;
day1Dinner: number;
day1Snack: number;
day2Breakfast: number;
day2Lunch: number;
day2Dinner: number;
checkedIn: boolean;
attendedEvents: string;
points: number;

[key: string]: string | number | boolean | Date | null | Record<string, boolean> | undefined; // extra keys for group-by results
}


/**
* Calculates all hackers' points from day-of events asynchronously
*
*
* @param applicants - array of unflattened applicant data
* @param hackathon - hackathon ID
* @returns Promise that resolves to a map of applicant email : points
*/
export const calculateApplicantPoints = async (
applicants: Applicant[],
hackathon: string
applicants: Applicant[],
hackathon: string,
): Promise<Record<string, number>> => {
const pointsMap: Record<string, number> = {};

try {
const dayOfSnapshot = await getDocs(collection(db, "Hackathons", hackathon, "DayOf"));
const allEvents = dayOfSnapshot.docs.map(doc => ({
id: doc.id,
...doc.data()
} as HackathonDayOf));

const allEvents = dayOfSnapshot.docs.map(
(doc) =>
({
id: doc.id,
...doc.data(),
}) as HackathonDayOf,
);

for (const applicant of applicants) {
let points = 0;

if (applicant.dayOf?.events && applicant.dayOf.events.length > 0) {
points = applicant.dayOf.events.reduce((acc, attendedEvent) => {
const eventDoc = allEvents.find(event => event.eventID === attendedEvent.eventId);
const eventDoc = allEvents.find((event) => event.eventID === attendedEvent.eventId);
return acc + Number(eventDoc?.points ?? 0);
}, 0);
}
pointsMap[applicant.basicInfo?.email || ''] = points;

pointsMap[applicant.basicInfo?.email || ""] = points;
}
} catch (error) {
console.error('Error calculating applicant points:', error);
console.error("Error calculating applicant points:", error);
for (const applicant of applicants) {
pointsMap[applicant.basicInfo?.email || ''] = 0;
pointsMap[applicant.basicInfo?.email || ""] = 0;
}
}

return pointsMap;
};