Created
September 16, 2021 08:55
-
-
Save joawan/8ee273986b2a4b2217ecc5a865f8706e to your computer and use it in GitHub Desktop.
Grabbing contact for people oncall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getOncallForSchedule = async (team, id, pd) => { | |
const userOnCall = await pd.schedules.listUsersOnCall(id, { | |
time_zone: 'UTC', | |
since: new Date().toISOString(), | |
until: new Date(Date.now() + 1000).toISOString(), | |
}); | |
const user = JSON.parse(userOnCall.body).users.pop(); | |
const contactMethods = await pd.users.listContactMethods(user.id); | |
const userDetails = JSON.parse(contactMethods.body); | |
const contact = userDetails.contact_methods.find((e) => e.type === 'phone_contact_method'); | |
return { | |
id: user.id, | |
name: user.name, | |
email: user.email, | |
phone: contact ? `+${contact.country_code}${contact.address}` : null, | |
time_zone: user.time_zone, | |
team, | |
}; | |
}; | |
const getOncallForTeams = async (teams, pd) => { | |
const promises = teams.map((team) => team.schedules.map(async (scheduleId) => { | |
try { | |
return await getOncallForSchedule(team.name, scheduleId, pd); | |
} catch (e) { | |
return { error: `Unexpected error when fetching oncallee for ${team.name}` }; | |
} | |
})).flat(); | |
return Promise.all(promises); | |
}; | |
// await getOncallForTeams([... filtered teams ...], PagerDutySDK) => [userX, userY, userZ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment