Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.
Draft
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
25 changes: 12 additions & 13 deletions lib/rota_alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class RotaAlerts {
const rotation = await this.upcoming(this.type);

if (rotation) {
await this.sendMessage(rotation.first_line.email, rotation.start_date);
await this.sendMessage(rotation.second_line.email, rotation.start_date);
await this.sendMessage(rotation.members[0], rotation.start_date);
await this.sendMessage(rotation.members[1], rotation.start_date);
}
}

Expand All @@ -22,22 +22,21 @@ class RotaAlerts {
return await rotationFinder.find();
}

async sendMessage(email, startDate) {
async sendMessage(membership, startDate) {
const date = moment(startDate).format("dddd Do MMMM");
const text = this.message(date);
const slackMessage = new SlackMessage(email, text);
const emailMessage = new EmailMessage(email, text);
const text = this.message(membership, date);
const slackMessage = new SlackMessage(membership.email, text);
const emailMessage = new EmailMessage(membership.email, text);

await Promise.all([slackMessage.send(), emailMessage.send()]);
}

message(date) {
let typeDescription = {
ooh: "out of hours",
support: "support",
}[this.type];

return `You have an upcoming ${typeDescription} allocation on ${date}. Can't do it? Ask in #dxw-tech-team and see if someone wants to swap`;
message(membership, date) {
return (
`You have an upcoming **${membership.rota}** allocation on ${date}. ` +
`Your role is **${membership.role}**. ` +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
}

static async runner() {
Expand Down
62 changes: 60 additions & 2 deletions lib/upcoming_rotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const moment = require("moment");

class UpcomingRotation {
constructor(type) {
this.type = type;
this.date = moment().add(1, "week").format("YYYY-MM-DD");
this.url = {
ooh: "https://dxw-support-rota.herokuapp.com/out-of-hours/rota.json",
Expand All @@ -16,10 +17,67 @@ class UpcomingRotation {

async find() {
const response = await axios.get(this.url);

return response.data.find((rotation) => {
const inconsistentRotation = response.data.find((rotation) => {
return rotation.start_date === this.date;
});
return this.consistentRotation(inconsistentRotation);
}

consistentRotation(rotation) {
if (rotation) {
return {
start_date: rotation.start_date,
end_date: rotation.end_date,
members: this.members(rotation),
};
}
}

members(rotation) {
console.log(this.type);
switch (this.type) {
case "ooh":
return this.outOfHoursMembers(rotation);
case "support":
return this.inHoursMembers(rotation);
default:
throw new Error(`The rotation type ${this.type} is unknown.`);
}
}

outOfHoursMembers(rotation) {
return [
{
rota: "out-of-hours",
role: "first line",
name: rotation.first_line.name,
email: rotation.first_line.email,
},
{
rota: "out-of-hours",
role: "second line",
name: rotation.second_line.name,
email: rotation.second_line.email,
},
];
}

inHoursMembers(rotation) {
console.log(rotation);
return [
{
rota: "in-hours",
role: "developer",
name: rotation.developer.name,
email: rotation.developer.email,
},
{
rota: "in-hours",
role: "ops",
name: rotation.ops.name,
email: rotation.ops.email,
},
];
}
}

Expand Down
151 changes: 113 additions & 38 deletions spec/rota_alerts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,122 @@ describe("RotaAlerts", () => {
EmailMessage.mockClear();
});

test("sends notifications when a rotation is available", async () => {
UpcomingRotation.mockImplementationOnce(() => {
return {
find: () => {
return Promise.resolve({
start_date: "2020-01-09",
end_date: "2020-01-16",
first_line: {
name: "Eve",
email: "eve@example.com",
},
second_line: {
name: "Trent",
email: "trent@example.com",
},
});
},
};
describe("when the rotation has the OOH (first_line/second_line) shape", () => {
test("sends notifications when a out of hours rotation is available", async () => {
UpcomingRotation.mockImplementationOnce(() => {
return {
find: () => {
return Promise.resolve({
start_date: "2020-01-09",
end_date: "2020-01-16",
members: [
{
role: "first line",
rota: "out-of-hours",
name: "Eve",
email: "eve@example.com",
},
{
role: "second line",
rota: "out-of-hours",
name: "Trent",
email: "trent@example.com",
},
],
});
},
};
});

const rotaAlerts = new RotaAlerts("ooh");
await rotaAlerts.run();

expect(SlackMessage).toHaveBeenCalledTimes(2);
expect(EmailMessage).toHaveBeenCalledTimes(2);
expect(SlackMessage).toHaveBeenCalledWith(
"eve@example.com",
"You have an upcoming **out-of-hours** allocation on Thursday 9th January. " +
"Your role is **first line**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(SlackMessage).toHaveBeenCalledWith(
"trent@example.com",
"You have an upcoming **out-of-hours** allocation on Thursday 9th January. " +
"Your role is **second line**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(EmailMessage).toHaveBeenCalledWith(
"eve@example.com",
"You have an upcoming **out-of-hours** allocation on Thursday 9th January. " +
"Your role is **first line**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(EmailMessage).toHaveBeenCalledWith(
"trent@example.com",
"You have an upcoming **out-of-hours** allocation on Thursday 9th January. " +
"Your role is **second line**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
});
});

const rotaAlerts = new RotaAlerts("ooh");
await rotaAlerts.run();
describe("when the rotation has the Support (developer/ops) shape", () => {
test("sends notifications when a out of hours rotation is available", async () => {
UpcomingRotation.mockImplementationOnce(() => {
return {
find: () => {
return Promise.resolve({
start_date: "2020-01-09",
end_date: "2020-01-16",
members: [
{
role: "developer",
rota: "in-hours",
name: "Sandy",
email: "sandy@example.com",
},
{
role: "ops",
rota: "in-hours",
name: "Frances",
email: "frances@example.com",
},
],
});
},
};
});

const rotaAlerts = new RotaAlerts("support");
await rotaAlerts.run();

expect(SlackMessage).toHaveBeenCalledTimes(2);
expect(EmailMessage).toHaveBeenCalledTimes(2);
expect(SlackMessage).toHaveBeenCalledWith(
"eve@example.com",
"You have an upcoming out of hours allocation on Thursday 9th January. Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(SlackMessage).toHaveBeenCalledWith(
"trent@example.com",
"You have an upcoming out of hours allocation on Thursday 9th January. Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(EmailMessage).toHaveBeenCalledWith(
"eve@example.com",
"You have an upcoming out of hours allocation on Thursday 9th January. Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(EmailMessage).toHaveBeenCalledWith(
"trent@example.com",
"You have an upcoming out of hours allocation on Thursday 9th January. Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(SlackMessage).toHaveBeenCalledTimes(2);
expect(EmailMessage).toHaveBeenCalledTimes(2);
expect(SlackMessage).toHaveBeenCalledWith(
"sandy@example.com",
"You have an upcoming **in-hours** allocation on Thursday 9th January. " +
"Your role is **developer**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(SlackMessage).toHaveBeenCalledWith(
"frances@example.com",
"You have an upcoming **in-hours** allocation on Thursday 9th January. " +
"Your role is **ops**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(EmailMessage).toHaveBeenCalledWith(
"sandy@example.com",
"You have an upcoming **in-hours** allocation on Thursday 9th January. " +
"Your role is **developer**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
expect(EmailMessage).toHaveBeenCalledWith(
"frances@example.com",
"You have an upcoming **in-hours** allocation on Thursday 9th January. " +
"Your role is **ops**. " +
"Can't do it? Ask in #dxw-tech-team and see if someone wants to swap"
);
});
});

test("does not send Slack notifications when there is no rotation", async () => {
Expand Down
Loading