Created
January 29, 2021 03:01
-
-
Save ryanhanwu/65f0caea9fe1207bbe25a940dd9ec4df to your computer and use it in GitHub Desktop.
Hubspot Code Assessment
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 fetch = require('node-fetch'); | |
const dayjs = require('dayjs') | |
const DATASET_API = "https://candidate.hubteam.com/candidateTest/v3/problem/dataset?userKey=USER_KEY" | |
const RESULT_API = "https://candidate.hubteam.com/candidateTest/v3/problem/result?userKey=USER_KEY" | |
const findIdealStartDatePartners = (country, partners) => { | |
let partnersByDate = {} | |
let mostAvailDate = null | |
let maxAttendee = -1 | |
//Loop through each partner | |
partners.forEach((p) => { | |
const { | |
availableDates | |
} = p | |
// Filter out all the potential start day and put it under availableDates | |
let availDates = availableDates.filter((dateData, index) => { | |
return availableDates[index + 1] === | |
dayjs(dateData).add(1, 'day').format("YYYY-MM-DD") //Assume availableDates are sorted | |
}) | |
if (availDates.length > 0) { | |
availDates.forEach((date) => { | |
//For each start date, add it to partnersByDate and find the most available Date | |
partnersByDate[date] = partnersByDate[date] || [] | |
const partnerCount = partnersByDate[date].push(p) | |
if (partnerCount > maxAttendee) { | |
mostAvailDate = date | |
maxAttendee = partnerCount | |
} | |
}) | |
} | |
}, []) | |
//Return the expected object format | |
return { | |
"attendeeCount": partnersByDate[mostAvailDate].length, | |
"attendees": partnersByDate[mostAvailDate].map((p) => p.email), | |
"name": country, | |
"startDate": mostAvailDate, | |
} | |
} | |
(async () => { | |
try { | |
const data_api_res = await fetch(DATASET_API) | |
const result = await data_api_res.json() | |
//Group the partner data by country since its all separated calculation | |
let groupByCountries = result.partners.reduce((accu, partner) => { | |
const { | |
country | |
} = partner | |
accu[country] = accu[country] || [] | |
accu[country].push(partner) | |
return accu | |
}, {}) | |
let countries = [] | |
for (const [country, partners] of Object.entries(groupByCountries)) { | |
countries.push(findIdealStartDatePartners(country, partners)) | |
} | |
const result_api_res = await fetch(RESULT_API, { | |
method: 'POST', | |
body: JSON.stringify({ | |
countries | |
}), | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
}) | |
const result_api_data = await result_api_res.json() | |
console.dir(result_api_data) | |
} catch (e) { | |
console.error(e) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment