Skip to content

Commit

Permalink
Merge pull request #30 from apiaryio/miiila/unify-incidents-again
Browse files Browse the repository at this point in the history
fix: prevent duplication of schedules
  • Loading branch information
miiila authored Oct 4, 2017
2 parents 5ef17cd + b38cc4c commit 6227265
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/pagerduty.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function processSchedules(allSchedules, days = [], cb) {
const myEnd = moment.utc(entry.end);
const myUserId = entry.user.id;
const myUserName = entry.user.summary;
if (duplicities.myUserName == null) { duplicities.myUserName = []; }
if (duplicities[myUserName] == null) { duplicities[myUserName] = []; }
otherSchedules.forEach((crossSchedule) => {
crossSchedule.entries.forEach((crossCheckEntry) => {
let overlap = false;
Expand All @@ -118,13 +118,15 @@ function processSchedules(allSchedules, days = [], cb) {
const crossCheckStart = moment.utc(crossCheckEntry.start);
const crossCheckEnd = moment.utc(crossCheckEntry.end);
let message;
let overlapStart;
let overlapEnd;

// is there an overlap?
if ((crossCheckStart < myEnd && myStart < crossCheckEnd) &&
(crossCheckEntry.user.id === myUserId)) {
// find overlapping inteval
const overlapStart = moment.max(myStart, crossCheckStart);
const overlapEnd = moment.min(crossCheckEnd, myEnd);
overlapStart = moment.max(myStart, crossCheckStart);
overlapEnd = moment.min(crossCheckEnd, myEnd);

message = {
user: myUserName,
Expand Down Expand Up @@ -160,8 +162,8 @@ function processSchedules(allSchedules, days = [], cb) {
});
}

if (overlap && !duplicities.myUserName.includes(crossCheckEntry.start)) {
duplicities.myUserName.push(crossCheckEntry.start);
if (overlap && !duplicities[myUserName].includes(overlapStart.toISOString())) {
duplicities[myUserName].push(overlapStart.toISOString());
messages.push(message);
}
});
Expand Down

0 comments on commit 6227265

Please sign in to comment.