Skip to content

Instantly share code, notes, and snippets.

@kale-bogdanovs
Created September 1, 2021 22:00
Show Gist options
  • Save kale-bogdanovs/285b45bd3022f0d2df40db55dc500fce to your computer and use it in GitHub Desktop.
Save kale-bogdanovs/285b45bd3022f0d2df40db55dc500fce to your computer and use it in GitHub Desktop.
const createRecord = (worker) => {
row = {}
// Map top level fields - simple
row.id = worker.Worker_Reference_Employee_ID
row.user_id = worker.Worker_Data.User_ID
row.birthday = worker.Worker_Data.Personal_Data.Birth_Date
// Map name - nested
const preferred_name = worker.Worker_Data.Personal_Data.Name_Data.Preferred_Name_Data.Name_Detail_Data
row.first_name = preferred_name.First_Name
row.last_name = preferred_name.Last_Name
//Map contact data - semi-structured
const contact_data = worker.Worker_Data.Personal_Data.Contact_Data
contact_data.Address_Data.forEach(ad => {
const street_address_line_1 = ad.Address_Line_Data[0]
const street_address_line_2 = ad.Address_Line_Data[1]
const city = ad.Municipality
const state = ad["Country_Region_Reference_ISO_3166-2_Code"]
const zip = ad.Postal_Code
const last_modified = new Date(ad.Last_Modified).getTime()
const home_address_last_modified = row.home_address_last_modified || 0
const work_address_last_modified = row.work_address_last_modified || 0
ad.Usage_Data.forEach(ud => {
ud.Type_Data.forEach(td => {
if (td.Type_Reference_Communication_Usage_Type_ID === "HOME" && home_address_last_modified < last_modified) {
row.home_address_street_address_line_1 = street_address_line_1
row.home_address_street_address_line_2 = street_address_line_2
row.home_address_city = city
row.home_address_state = state
row.home_address_zip = zip
row.home_address_last_modified = last_modified
}
if (td.Type_Reference_Communication_Usage_Type_ID === "WORK" && work_address_last_modified < last_modified) {
row.work_address_street_address_line_1 = street_address_line_1
row.work_address_street_address_line_2 = street_address_line_2
row.work_address_city = city
row.work_address_state = state
row.work_address_zip = zip
row.work_address_last_modified = last_modified
}
})
})
})
contact_data.Phone_Data.forEach(pd => {
const phone_number = pd._amp_National_Formatted_Phone
pd.Usage_Data.forEach(ud => {
ud.Type_Data.forEach(td => {
if (td.Type_Reference_Communication_Usage_Type_ID === "HOME") {
row.home_phone = phone_number
}
if (td.Type_Reference_Communication_Usage_Type_ID === "WORK") {
row.work_phone = phone_number
}
})
})
})
contact_data.Email_Address_Data.forEach(ed => {
const email = ed.Email_Address
ed.Usage_Data.forEach(ud => {
ud.Type_Data.forEach(td => {
if (td.Type_Reference_Communication_Usage_Type_ID === "HOME") {
row.personal_email = email
}
if (td.Type_Reference_Communication_Usage_Type_ID === "WORK") {
row.work_email = email
}
})
})
})
return row
}
exports.main = ({ input_json }) => {
const input = JSON.parse(input_json)
const rows = []
input.forEach(worker => {
rows.push(createRecord(worker))
})
return {rows: rows}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment