Skip to content

Instantly share code, notes, and snippets.

@scarney81
Last active December 9, 2021 05:42
Show Gist options
  • Save scarney81/43ce92205102d4ac29e9b8641510da2c to your computer and use it in GitHub Desktop.
Save scarney81/43ce92205102d4ac29e9b8641510da2c to your computer and use it in GitHub Desktop.
Secret Santa
import axios from "axios";
const people = {
Seth: "email@example.com"
Nick: "email@example.com",
Aman: "email@example.com",
Troy: "email@example.com",
};
const names = Object.keys(people);
const assigned = [];
let assignment = names.reduce(
(acc, name) => ({
...acc,
[name]: undefined,
}),
{}
);
const getRemaining = () => names.filter((name) => !assigned.includes(name));
const getRecipient = (secretSantaName: string) => {
const list = getRemaining();
const remaining = list.filter((name) => secretSantaName !== name);
return remaining[Math.floor(Math.random() * remaining.length)];
};
const assignSecretSantas = async () => {
for (const santa in assignment) {
const recipient = getRecipient(santa);
assigned.push(recipient);
assignment[santa] = recipient;
}
for (const santa in assignment) {
if (!santa) {
console.log("list borked. regenerating.");
await assignSecretSantas();
return;
}
}
for (const name of names) {
const email = people[name];
const response = await axios({
data: {
message: {
to: {
email,
},
content: {
title: "Your Secret Santa",
body: `{{name}}, your secret santa is {{assignee}}`,
},
data: {
name,
assignee: assignment[name],
},
},
},
headers: {
Authorization: `Bearer ${process.env.COURIER_API_KEY}`,
},
method: "POST",
url: "https://api.courier.com/send",
});
console.log(response.data);
}
};
assignSecretSantas();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment