This program assigns secret children to each employee for a Secret Santa game. It takes a list of employees and their email IDs, along with the previous year's assignments, and generates new assignments for the current year.
- The solution uses the
fs
module andcsv-parser
library to read and parse CSV files. - The
readCsv(fileName)
function reads a CSV file and returns a promise that resolves to an array of objects, where each object represents a row in the CSV file. - The
assignSecretChildren(employees, previousAssignments)
function takes an array of employee objects and an array of previous assignment objects as input. It generates new secret Santa assignments for each employee, avoiding any conflicts with previous assignments. It returns an array of new assignment objects. - The
getRandomEmployee()
function is a helper function used byassignSecretChildren()
to randomly select an employee for assignment, ensuring that the selected employee meets all the assignment criteria. - The
isAssignedPreviously()
andisAssignedInCurrentYear()
functions are helper functions used to check if an employee has been assigned to the same employee previously or to another employee in the current year, respectively. - The
writeSecretSantaAssignments(assignments, fileName)
function takes an array of assignment objects and a file name as input. It generates a new CSV file with the assignments. - The
validateCsvData(data, expectedFields)
function is a helper function used to validate the CSV data and ensure that it has the expected format and fields. - The
runSecretSantaGame(employeeListFile, previousAssignmentsFile, resultFile)
function is the main function that orchestrates the Secret Santa game. It reads the employee list and previous assignment files, validates the data, generates new assignments, and writes the result to a new CSV file.
To use this program, follow these steps:
Clone the repository or download the code files.
Install the required dependencies by running the following command in your terminal:
npm install
To run the Secret Santa game, you need to provide two input files: Employee-List.csv and Secret-Santa-Game-Result-2023.csv. These files should be in CSV format and have specific columns.
- Employee-List.csv - This file contains the list of employees participating in the game. It should have the following columns:
- Employee_Name - The name of the employee
- Employee_EmailID - The email ID of the employee
Employee_Name,Employee_EmailID
Alice,[email protected]
Bob,[email protected]
Charlie,[email protected]
- Secret-Santa-Game-Result-2023.csv - This file contains the previous year's assignments. It should have the following columns:
- Employee_Name - The name of the employee
- Employee_EmailID - The email ID of the employee
- Secret_Child_Name - The name of the assigned secret child for the employee
- Secret_Child_EmailID - The email ID of the assigned secret child
Employee_Name,Employee_EmailID,Secret_Child_Name,Secret_Child_EmailID
Alice,[email protected],Bob,[email protected]
Bob,[email protected],Charlie,[email protected]
Charlie,[email protected],Alice,[email protected]
- Secret-Santa-Game-Result-2024.csv - This file will contain the new assignments for the current year. It will be generated by the program.
After preparing the input files, open the index.js file and modify the following line to provide the correct filenames:
runSecretSantaGame(
"Employee-List.csv",
"Secret-Santa-Game-Result-2023.csv",
"Secret-Santa-Game-Result-2024.csv"
);
Save the file, and then execute the program by running the following command:
node index.js
The program will read the input files, generate new assignments, and save them to the Secret-Santa-Game-Result-2024.csv file.
The program includes a test file named secretSanta.test.js to ensure the correctness of the assignSecretChildren function. To run the tests, execute the following command:
npm test
The tests will be executed, and the results will be displayed in the console.
The assignSecretChildren function is responsible for assigning secret children to each employee based on certain rules.
- An employee cannot choose themselves as their secret child.
- An employee cannot be assigned to the same secret child as in the previous year's Secret Santa event, if applicable.
- Each employee must have exactly one secret child.
- Each secret child should be assigned to only one employee. It takes the list of employees and previous assignments as input and returns the new assignments.
The program uses the csv-parser and fs modules to read and write CSV files, respectively. These dependencies are already installed when you run npm install.
The validateCsvData function is used to validate the CSV data and ensure it meets the expected format.
The program handles errors during file reading, writing, and data validation. If any errors occur, they will be logged to the console.
For more details, please refer to the code files and comments provided.