|
| 1 | +import { DataType } from "sequelize-typescript"; |
| 2 | +import { v4 as uuidv4 } from "uuid"; |
| 3 | +import { Migration } from "../umzug"; |
| 4 | + |
| 5 | +import allApplications from "./applicationlist.json"; |
| 6 | +import applicants from "./separateJSONs"; |
| 7 | +import { |
| 8 | + ApplicationStatus, |
| 9 | + SkillCategory, |
| 10 | + ApplicantRole, |
| 11 | + EngineeringPositionTitles, |
| 12 | + DesignPositionTitles, |
| 13 | + ProductPositionTitles, |
| 14 | + CommunityPositionTitles, |
| 15 | +} from "../types"; |
| 16 | + |
| 17 | +const ALL_POSITION_TITLES = [ |
| 18 | + ...EngineeringPositionTitles, |
| 19 | + ...DesignPositionTitles, |
| 20 | + ...ProductPositionTitles, |
| 21 | + ...CommunityPositionTitles, |
| 22 | +]; |
| 23 | + |
| 24 | +const MAX_SHORT_ANSWER_LENGTH = 255; |
| 25 | + |
| 26 | +function normalizeApplicantRole(role: string): string { |
| 27 | + if (!role) return "President"; |
| 28 | + const trimmed = role.trim(); |
| 29 | + if (trimmed.toLowerCase() === "project developer") return "Developer"; |
| 30 | + // Capitalize each word for comparison |
| 31 | + const formatted = trimmed.replace(/\b\w/g, (c) => c.toUpperCase()); |
| 32 | + if (ALL_POSITION_TITLES.includes(formatted as any)) return formatted; |
| 33 | + return "President"; |
| 34 | +} |
| 35 | + |
| 36 | +type ApplicantRecord = { |
| 37 | + id: string; |
| 38 | + applicantId: string; |
| 39 | + position: string; |
| 40 | + roleSpecificQuestions: string[] | null; |
| 41 | + choice: number; |
| 42 | + status: ApplicationStatus; |
| 43 | + skillCategory: SkillCategory | null; |
| 44 | + extraInfo: string | null; |
| 45 | +}; |
| 46 | + |
| 47 | +const applicantRecords: ApplicantRecord[] = []; |
| 48 | +allApplications.forEach((app, idx) => { |
| 49 | + const applicantId = applicants[idx].id; |
| 50 | + |
| 51 | + // First choice |
| 52 | + if (app.firstChoiceRole) { |
| 53 | + const roleSpecificQs = |
| 54 | + app.roleSpecificQuestions |
| 55 | + ?.filter((q) => q.role === app.firstChoiceRole) |
| 56 | + ?.flatMap( |
| 57 | + (q) => |
| 58 | + q.questions?.map((question) => |
| 59 | + JSON.stringify(question).slice(0, MAX_SHORT_ANSWER_LENGTH), |
| 60 | + ) || [], |
| 61 | + ) || []; |
| 62 | + applicantRecords.push({ |
| 63 | + id: uuidv4(), |
| 64 | + applicantId, |
| 65 | + position: normalizeApplicantRole(app.firstChoiceRole), |
| 66 | + roleSpecificQuestions: roleSpecificQs.length > 0 ? roleSpecificQs : null, |
| 67 | + choice: 1, |
| 68 | + status: (app.status as ApplicationStatus) || "Applied", |
| 69 | + skillCategory: null, |
| 70 | + extraInfo: JSON.stringify({ adminReview: "seeded_admin_review" }), |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + // Second choice |
| 75 | + if (app.secondChoiceRole) { |
| 76 | + const roleSpecificQs = |
| 77 | + app.roleSpecificQuestions |
| 78 | + ?.filter((q) => q.role === app.secondChoiceRole) |
| 79 | + ?.flatMap( |
| 80 | + (q) => |
| 81 | + q.questions?.map((question) => |
| 82 | + JSON.stringify(question).slice(0, MAX_SHORT_ANSWER_LENGTH), |
| 83 | + ) || [], |
| 84 | + ) || []; |
| 85 | + applicantRecords.push({ |
| 86 | + id: uuidv4(), |
| 87 | + applicantId, |
| 88 | + position: normalizeApplicantRole(app.secondChoiceRole), |
| 89 | + roleSpecificQuestions: roleSpecificQs.length > 0 ? roleSpecificQs : null, |
| 90 | + choice: 2, |
| 91 | + status: (app.status as ApplicationStatus) || "Applied", |
| 92 | + skillCategory: null, |
| 93 | + extraInfo: JSON.stringify({ adminReview: "seeded_admin_review" }), |
| 94 | + }); |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +const TABLE_NAME = "applicant_records"; |
| 99 | + |
| 100 | +export const up: Migration = async ({ context: sequelize }) => { |
| 101 | + await sequelize.getQueryInterface().createTable(TABLE_NAME, { |
| 102 | + id: { |
| 103 | + type: DataType.STRING, |
| 104 | + allowNull: false, |
| 105 | + primaryKey: true, |
| 106 | + }, |
| 107 | + applicantId: { |
| 108 | + type: DataType.STRING, |
| 109 | + allowNull: false, |
| 110 | + references: { |
| 111 | + model: "applicants", |
| 112 | + key: "id", |
| 113 | + }, |
| 114 | + }, |
| 115 | + position: { |
| 116 | + type: DataType.STRING, |
| 117 | + allowNull: true, |
| 118 | + references: { |
| 119 | + model: "positions", |
| 120 | + key: "title", |
| 121 | + }, |
| 122 | + }, |
| 123 | + roleSpecificQuestions: { |
| 124 | + type: DataType.ARRAY(DataType.STRING), |
| 125 | + allowNull: true, |
| 126 | + }, |
| 127 | + choice: { |
| 128 | + type: DataType.INTEGER, |
| 129 | + allowNull: false, |
| 130 | + }, |
| 131 | + status: { |
| 132 | + type: DataType.STRING, |
| 133 | + allowNull: false, |
| 134 | + }, |
| 135 | + skillCategory: { |
| 136 | + type: DataType.STRING, |
| 137 | + allowNull: true, |
| 138 | + }, |
| 139 | + extraInfo: { |
| 140 | + type: DataType.JSONB, |
| 141 | + allowNull: true, |
| 142 | + }, |
| 143 | + }); |
| 144 | + await sequelize.getQueryInterface().bulkInsert(TABLE_NAME, applicantRecords); |
| 145 | +}; |
| 146 | + |
| 147 | +export const down: Migration = async ({ context: sequelize }) => { |
| 148 | + await sequelize.getQueryInterface().dropTable(TABLE_NAME); |
| 149 | +}; |
0 commit comments