Skip to content

Commit 3d22501

Browse files
committed
scripts for hackgt12 registration
1 parent 03dedf4 commit 3d22501

6 files changed

Lines changed: 501 additions & 0 deletions

File tree

scripts/src/getAccepted.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { MongoClient, ObjectId } from "mongodb";
2+
import * as fs from "fs";
3+
// Import the built-in File System module
4+
const outputFilePath = "accepted.csv"; // Define the output file name
5+
6+
// Throw and show a stack trace on an unhandled Promise rejection
7+
process.on("unhandledRejection", err => {
8+
throw err;
9+
});
10+
11+
const client = new MongoClient("mongodb://localhost:7777");
12+
const applicationBranchIds = [
13+
new ObjectId("683f9bb6d58f3f52fa01c515"), // Participant (Early Bird, No Reimbursement)
14+
new ObjectId("683f9c55d58f3f52fa01c51a"), // Participant (Travel Reimbursement)
15+
new ObjectId("683fcf83d58f3f52fa01c541"), // Participant (Regular, No Reimbursement)
16+
new ObjectId("6864951aa334a6b56af14000"), // Priority Application
17+
];
18+
19+
/**
20+
* Escapes a field for CSV formatting.
21+
* If a field contains a comma, double quote, or newline, it will be enclosed in double quotes.
22+
* Existing double quotes within the field will be escaped by doubling them.
23+
* @param {any} field - The data to escape.
24+
* @returns {string} The CSV-safe string.
25+
*/
26+
const escapeCsvField = (field: string) => {
27+
// Return an empty string for null or undefined values
28+
if (field === null || field === undefined) {
29+
return "";
30+
}
31+
const stringField = String(field);
32+
// Check if the field contains characters that need escaping
33+
if (/[",\n\r]/.test(stringField)) {
34+
const escapedField = stringField.replace(/"/g, '""');
35+
return `"${escapedField}"`;
36+
}
37+
return stringField;
38+
};
39+
40+
const exportApplicantsToCsv = async () => {
41+
await client.connect();
42+
const db = client.db("registration");
43+
const collection = db.collection<any>("applications");
44+
45+
console.log("Fetching applicants...");
46+
47+
// Use a projection to retrieve only the necessary fields
48+
49+
// Find all documents in the collection with the specified projection
50+
const applicants = await collection
51+
.find({
52+
hexathon: new ObjectId("683f9a9ab75ad31cd0f2ec67"),
53+
applicationBranch: { $in: applicationBranchIds },
54+
status: {
55+
$in: ["CONFIRMED", "ACCEPTED"],
56+
},
57+
})
58+
.toArray();
59+
60+
// 1. Start with the header row
61+
const headers = ["name", "email", "school"];
62+
const csvRows = [headers.join(",")];
63+
64+
// 2. Add each applicant as a new CSV row
65+
applicants.forEach(applicant => {
66+
const row = [
67+
escapeCsvField(applicant.name),
68+
escapeCsvField(applicant.email),
69+
escapeCsvField(applicant.applicationData.school),
70+
];
71+
csvRows.push(row.join(","));
72+
});
73+
74+
// 3. Join all rows with a newline character to form the final CSV content
75+
const csvContent = csvRows.join("\n");
76+
77+
// 4. Write the content to the specified file
78+
try {
79+
fs.writeFileSync(outputFilePath, csvContent);
80+
console.log(`✅ Success! ${applicants.length} records written to ${outputFilePath}`);
81+
} catch (error) {
82+
console.error("❌ Error writing to file:", error);
83+
}
84+
};
85+
86+
(async () => {
87+
try {
88+
await exportApplicantsToCsv();
89+
// Use console.error for info message so it doesn't mix with CSV output
90+
} catch (err) {
91+
console.error("\nAn error occurred:", err);
92+
} finally {
93+
// Ensure the client is closed when the script finishes or errors out
94+
await client.close();
95+
}
96+
})();

scripts/src/getApplicants.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { MongoClient, ObjectId } from "mongodb";
2+
import * as fs from "fs";
3+
// Import the built-in File System module
4+
const outputFilePath = "applicants.csv"; // Define the output file name
5+
6+
// Throw and show a stack trace on an unhandled Promise rejection
7+
process.on("unhandledRejection", err => {
8+
throw err;
9+
});
10+
11+
const client = new MongoClient("mongodb://localhost:7777");
12+
const applicationBranchIds = [
13+
new ObjectId("683f9bb6d58f3f52fa01c515"), // Participant (Early Bird, No Reimbursement)
14+
new ObjectId("683f9c55d58f3f52fa01c51a"), // Participant (Travel Reimbursement)
15+
new ObjectId("683fcf83d58f3f52fa01c541"), // Participant (Regular, No Reimbursement)
16+
new ObjectId("6864951aa334a6b56af14000"), // Priority Application
17+
];
18+
19+
/**
20+
* Escapes a field for CSV formatting.
21+
* If a field contains a comma, double quote, or newline, it will be enclosed in double quotes.
22+
* Existing double quotes within the field will be escaped by doubling them.
23+
* @param {any} field - The data to escape.
24+
* @returns {string} The CSV-safe string.
25+
*/
26+
const escapeCsvField = (field: string) => {
27+
// Return an empty string for null or undefined values
28+
if (field === null || field === undefined) {
29+
return "";
30+
}
31+
const stringField = String(field);
32+
// Check if the field contains characters that need escaping
33+
if (/[",\n\r]/.test(stringField)) {
34+
const escapedField = stringField.replace(/"/g, '""');
35+
return `"${escapedField}"`;
36+
}
37+
return stringField;
38+
};
39+
40+
const exportApplicantsToCsv = async () => {
41+
await client.connect();
42+
const db = client.db("registration");
43+
const collection = db.collection<any>("applications");
44+
45+
console.log("Fetching applicants...");
46+
47+
// Use a projection to retrieve only the necessary fields
48+
49+
// Find all documents in the collection with the specified projection
50+
const applicants = await collection
51+
.find({
52+
hexathon: new ObjectId("683f9a9ab75ad31cd0f2ec67"),
53+
applicationBranch: { $in: applicationBranchIds },
54+
status: {
55+
$ne: "DRAFT",
56+
},
57+
})
58+
.toArray();
59+
60+
// 1. Start with the header row
61+
const headers = ["name", "country", "school"];
62+
const csvRows = [headers.join(",")];
63+
64+
// 2. Add each applicant as a new CSV row
65+
applicants.forEach(applicant => {
66+
const row = [
67+
escapeCsvField(applicant.name),
68+
escapeCsvField(applicant.applicationData.countryOfResidence),
69+
escapeCsvField(applicant.applicationData.school),
70+
];
71+
csvRows.push(row.join(","));
72+
});
73+
74+
// 3. Join all rows with a newline character to form the final CSV content
75+
const csvContent = csvRows.join("\n");
76+
77+
// 4. Write the content to the specified file
78+
try {
79+
fs.writeFileSync(outputFilePath, csvContent);
80+
console.log(`✅ Success! ${applicants.length} records written to ${outputFilePath}`);
81+
} catch (error) {
82+
console.error("❌ Error writing to file:", error);
83+
}
84+
};
85+
86+
(async () => {
87+
try {
88+
await exportApplicantsToCsv();
89+
// Use console.error for info message so it doesn't mix with CSV output
90+
} catch (err) {
91+
console.error("\nAn error occurred:", err);
92+
} finally {
93+
// Ensure the client is closed when the script finishes or errors out
94+
await client.close();
95+
}
96+
})();

scripts/src/getConfirmed.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { MongoClient, ObjectId } from "mongodb";
2+
import * as fs from "fs";
3+
// Import the built-in File System module
4+
const outputFilePath = "confirmed.csv"; // Define the output file name
5+
6+
// Throw and show a stack trace on an unhandled Promise rejection
7+
process.on("unhandledRejection", err => {
8+
throw err;
9+
});
10+
11+
const client = new MongoClient("mongodb://localhost:7777");
12+
const applicationBranchIds = [
13+
new ObjectId("683f9bb6d58f3f52fa01c515"), // Participant (Early Bird, No Reimbursement)
14+
new ObjectId("683f9c55d58f3f52fa01c51a"), // Participant (Travel Reimbursement)
15+
new ObjectId("683fcf83d58f3f52fa01c541"), // Participant (Regular, No Reimbursement)
16+
new ObjectId("6864951aa334a6b56af14000"), // Priority Application
17+
];
18+
19+
/**
20+
* Escapes a field for CSV formatting.
21+
* If a field contains a comma, double quote, or newline, it will be enclosed in double quotes.
22+
* Existing double quotes within the field will be escaped by doubling them.
23+
* @param {any} field - The data to escape.
24+
* @returns {string} The CSV-safe string.
25+
*/
26+
const escapeCsvField = (field: string) => {
27+
// Return an empty string for null or undefined values
28+
if (field === null || field === undefined) {
29+
return "";
30+
}
31+
const stringField = String(field);
32+
// Check if the field contains characters that need escaping
33+
if (/[",\n\r]/.test(stringField)) {
34+
const escapedField = stringField.replace(/"/g, '""');
35+
return `"${escapedField}"`;
36+
}
37+
return stringField;
38+
};
39+
40+
const exportApplicantsToCsv = async () => {
41+
await client.connect();
42+
const db = client.db("registration");
43+
const collection = db.collection<any>("applications");
44+
45+
console.log("Fetching applicants...");
46+
47+
// Use a projection to retrieve only the necessary fields
48+
49+
// Find all documents in the collection with the specified projection
50+
const applicants = await collection
51+
.find({
52+
hexathon: new ObjectId("683f9a9ab75ad31cd0f2ec67"),
53+
applicationBranch: { $in: applicationBranchIds },
54+
status: "CONFIRMED",
55+
})
56+
.toArray();
57+
58+
// 1. Start with the header row
59+
const headers = ["name", "email", "school"];
60+
const csvRows = [headers.join(",")];
61+
62+
// 2. Add each applicant as a new CSV row
63+
applicants.forEach(applicant => {
64+
const row = [
65+
escapeCsvField(applicant.name),
66+
escapeCsvField(applicant.email),
67+
escapeCsvField(applicant.applicationData.school),
68+
];
69+
csvRows.push(row.join(","));
70+
});
71+
72+
// 3. Join all rows with a newline character to form the final CSV content
73+
const csvContent = csvRows.join("\n");
74+
75+
// 4. Write the content to the specified file
76+
try {
77+
fs.writeFileSync(outputFilePath, csvContent);
78+
console.log(`✅ Success! ${applicants.length} records written to ${outputFilePath}`);
79+
} catch (error) {
80+
console.error("❌ Error writing to file:", error);
81+
}
82+
};
83+
84+
(async () => {
85+
try {
86+
await exportApplicantsToCsv();
87+
// Use console.error for info message so it doesn't mix with CSV output
88+
} catch (err) {
89+
console.error("\nAn error occurred:", err);
90+
} finally {
91+
// Ensure the client is closed when the script finishes or errors out
92+
await client.close();
93+
}
94+
})();

scripts/src/mlhDataHackGT12.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { MongoClient, ObjectId } from "mongodb";
2+
import fs from "fs";
3+
import path from "path";
4+
5+
// Throw and show a stack trace on an unhandled Promise rejection instead of logging an unhelpful warning
6+
process.on("unhandledRejection", err => {
7+
throw err;
8+
});
9+
10+
const client = new MongoClient("mongodb://localhost:7777");
11+
const applicationBranchIds = [
12+
new ObjectId("683f9bb6d58f3f52fa01c515"), // Participant (Early Bird, No Reimbursement)
13+
new ObjectId("683f9c55d58f3f52fa01c51a"), // Participant (Travel Reimbursement)
14+
new ObjectId("683fcf83d58f3f52fa01c541"), // Participant (Regular, No Reimbursement)
15+
new ObjectId("6864951aa334a6b56af14000"), // Priority Application
16+
];
17+
18+
const statuses = ["ACCEPTED", "CONFIRMED"];
19+
20+
(async () => {
21+
await client.connect();
22+
23+
const db = client.db("registration");
24+
const collection = db.collection("applications");
25+
26+
const res = await collection
27+
.find({
28+
status: {
29+
$in: statuses,
30+
},
31+
applicationBranch: {
32+
$in: applicationBranchIds,
33+
},
34+
})
35+
.toArray();
36+
37+
console.log(`Found ${res.length} users.`);
38+
39+
let output =
40+
"FirstName,LastName,Email,PhoneNumber,DateOfBirth,Country,School,LevelOfStudy,MLHCodeOfConduct,MlhEventLogistics,MLHCommunicationEmails\n";
41+
42+
for (const app of res) {
43+
const fullName = app.name.trim().split(" ");
44+
const firstName = fullName.slice(0, -1).join(" ");
45+
const lastName = fullName.slice(-1).join(" ");
46+
47+
const email = app.email || "";
48+
const phoneNumber = app.applicationData.phoneNumber?.replaceAll(",", "") || "";
49+
const dateOfBirth = app.applicationData.dateOfBirth?.replaceAll(",", "") || "";
50+
const country = app.applicationData.countryOfResidence?.replaceAll(",", "") || "";
51+
const school = app.applicationData.school?.replaceAll(",", "") || "";
52+
const levelOfStudy = app.applicationData.levelOfStudy?.replaceAll(",", "") || "";
53+
const mlhCodeOfConduct = app.applicationData?.confirmChecks?.["MLH-Code-Of-Conduct"]
54+
? "Yes"
55+
: "No";
56+
const mlhEventLogistics = app.applicationData?.confirmChecks?.["MLH-Terms-and-Conditions"]
57+
? "Yes"
58+
: "No";
59+
const mlhCommunicationEmails = app.applicationData?.confirmChecks?.["MLH-Information-Emails"]
60+
? "Yes"
61+
: "No";
62+
63+
if (
64+
!app.applicationData?.confirmChecks ||
65+
!app.applicationData?.confirmChecks?.["MLH-Code-Of-Conduct"]
66+
) {
67+
console.warn(
68+
`Warning: Applicant ${firstName} ${lastName} (${email}) has not accepted the MLH Code of Conduct.`
69+
);
70+
}
71+
72+
output += `${firstName},${lastName},${email},${phoneNumber},${dateOfBirth},${country},${school},${levelOfStudy},${mlhCodeOfConduct},${mlhEventLogistics},${mlhCommunicationEmails}\n`;
73+
}
74+
75+
fs.mkdir(path.resolve(__dirname, "../output"), { recursive: true }, err => {
76+
if (err) throw err;
77+
78+
fs.writeFileSync(path.resolve(__dirname, "../output/mlhDataHackGT12.csv"), output);
79+
});
80+
81+
console.log(`Exported ${res.length} users data`);
82+
83+
console.info("\nDone.");
84+
})();

0 commit comments

Comments
 (0)