Skip to content

Instantly share code, notes, and snippets.

@trevorhreed
Last active July 24, 2021 03:41
Show Gist options
  • Save trevorhreed/960eb915e529ddbd9929e02f933b20b8 to your computer and use it in GitHub Desktop.
Save trevorhreed/960eb915e529ddbd9929e02f933b20b8 to your computer and use it in GitHub Desktop.
LCR
{
console.clear()
;(async function run() {
copyMembersWithoutCallings()
})()
async function copyMembersWithoutCallings() {
const urls = [
'https://lcr.churchofjesuschrist.org/services/orgs/members-without-callings?filterSubOrgId=8108306&lang=eng&unitNumber=2130475',
'https://lcr.churchofjesuschrist.org/services/orgs/members-without-callings?filterSubOrgId=8283027&lang=eng&unitNumber=2130475',
'https://lcr.churchofjesuschrist.org/services/orgs/members-without-callings?filterSubOrgId=8108317&lang=eng&unitNumber=2130475',
'https://lcr.churchofjesuschrist.org/services/orgs/members-without-callings?filterSubOrgId=8283011&lang=eng&unitNumber=2130475'
]
const [
elders1,
elders2,
sisters1,
sisters2
] = await Promise.all(
urls.map(url => {
return fetch(url)
.then(res => res.json())
.then(people => {
return people.map(person => {
return {
name: person.spokenName,
phone: formatPhone(person.phone),
email: person.email,
// gender: person.genderLabelShort
}
})
})
})
)
copyArrAsCSV([
{ c1: 'Relief Society 2' },
{ c1: 'Name', c2: 'Phone', c3: 'Email' },
...sisters2,
{ },
{ c1: 'Relief Society 1' },
{ c1: 'Name', c2: 'Phone', c3: 'Email' },
...sisters1,
{ },
{ c1: 'Elders Quorum 2' },
{ c1: 'Name', c2: 'Phone', c3: 'Email' },
...elders2,
{ },
{ c1: 'Elders Quorum 1' },
{ c1: 'Name', c2: 'Phone', c3: 'Email' },
...elders1
])
}
async function copyActivityKids() {
const urls = [
'https://lcr.churchofjesuschrist.org/services/orgs/sub-orgs-with-callings?lang=eng&subOrgId=8613166',
'https://lcr.churchofjesuschrist.org/services/orgs/sub-orgs-with-callings?lang=eng&subOrgId=2683111&unitNumber=559644'
]
const [boys, girls] = await Promise.all(
urls.map(u => {
return fetch(u)
.then(r => r.json())
.then(d => {
return d[0].members
.sort((a, b) => {
return a.birthDate.localeCompare(b.birthDate)
})
.map(x => {
const { birthDate, gender } = x
const [_, lastName, firstName] = /([a-z]+),\s+([a-z]+)/i.exec(x.name) || []
const year = birthDate.substring(0, 4)
const month = birthDate.substring(4, 6)
const day = birthDate.substring(6, 8)
const birth = `${month}-${day}-${year}`
const digits = (x.phone || '')
.replace(/\D+/g, '')
.slice(-10)
const phoneArea = digits.substring(0, 3)
const phoneLocal = digits.substring(3, 6)
const phoneLast = digits.substring(6, 10)
const [address, cityStateZip] = x.address.split('<br />')
return {
name: `${lastName}, ${firstName}`,
birth,
phone: digits
? `(${phoneArea}) ${phoneLocal}-${phoneLast}`
: '',
address,//: address.replace(/<br\s*\/>/g, ' ')
gender
}
})
})
})
)
const bIndex = boys.findIndex(x => x.birth.endsWith('2011'))
const boys1 = boys.slice(0, bIndex)
const boys2 = boys.slice(bIndex)
const gIndex = girls.findIndex(x => x.birth.endsWith('2011'))
const girls1 = girls.slice(0, gIndex)
const girls2 = girls.slice(gIndex)
const arr = [
{ c1: 'Boys 2009-2010' },
{ c1: 'Name', c2: 'Birthday', c3: 'Phone', c4: 'Address', c5: 'Gender' },
...boys1,
{ },
{ c1: 'Boys 2011-2012' },
{ c1: 'Name', c2: 'Birthday', c3: 'Phone', c4: 'Address', c5: 'Gender' },
...boys2,
{ },
{ c1: 'Girls 2009-2010' },
{ c1: 'Name', c2: 'Birthday', c3: 'Phone', c4: 'Address', c5: 'Gender' },
...girls1,
{ },
{ c1: 'Girls 2011-2012' },
{ c1: 'Name', c2: 'Birthday', c3: 'Phone', c4: 'Address', c5: 'Gender' },
...girls2
]
copyArrAsCSV(arr)
}
/////////////////////////////////////////////////////
// GENERIC UTILITY FUNCTIONS
//
function formatPhone(phone) {
const digits = (phone || '')
.replace(/\D+/g, '')
.slice(-10)
const phoneArea = digits.substring(0, 3)
const phoneLocal = digits.substring(3, 6)
const phoneLast = digits.substring(6, 10)
return digits
? `(${phoneArea}) ${phoneLocal}-${phoneLast}`
: ''
}
function copyArrAsCSV(members) {
showCopy(toCSV(members))
}
function toCSV(members) {
return members
.map(member => Object.keys(member).map(m => member[m]).join('\t'))
.join('\n')
}
function copy (text) {
const textarea = document.createElement('textarea')
document.body.appendChild(textarea)
textarea.value = text
textarea.select()
document.execCommand('copy')
textarea.remove()
}
function showCopy (text) {
const div = document.createElement('div')
div.style.position = 'fixed'
div.style.top = '0'
div.style.left = '0'
div.style.bottom = '0'
div.style.right = '0'
div.style.backgroundColor = '#fff'
div.style.display = 'flex'
div.style.justifyContent = 'center'
div.style.alignItems = 'center'
div.style.zIndex = '999'
const button = document.createElement('button')
button.textContent = 'Copy'
button.style.padding = '1em 2em'
button.addEventListener('click', e => {
copy(text)
div.remove()
})
div.appendChild(button)
document.body.appendChild(div)
}
async function getMembersByOrg() {
const eq1Url = `https://lcr.churchofjesuschrist.org/services/orgs/sub-orgs-with-callings?lang=eng&subOrgId=8108306`
const rs1Url = `https://lcr.churchofjesuschrist.org/services/orgs/sub-orgs-with-callings?lang=eng&subOrgId=8108317`
const eq2Url = `https://lcr.churchofjesuschrist.org/services/orgs/sub-orgs-with-callings?lang=eng&subOrgId=8283027`
const rs2Url = `https://lcr.churchofjesuschrist.org/services/orgs/sub-orgs-with-callings?lang=eng&subOrgId=8283011`
const orgs = await Promise.all([
fetch(eq1Url).then(res => res.json()).then(data => data[0]),
fetch(rs1Url).then(res => res.json()).then(data => data[0]),
fetch(eq2Url).then(res => res.json()).then(data => data[0]),
fetch(rs2Url).then(res => res.json()).then(data => data[0])
])
return orgs.flatMap(org => {
return org.members.map(member => {
member.org = org.name
return member
})
})
}
async function getData() {
const url = `https://lcr.churchofjesuschrist.org/services/umlu/report/member-list?lang=eng&unitNumber=2130475`
const members = fetch(membersUrl).then(res => res.json())
return members
// console.dir(res)
// const members = data.shift().members
// console.dir(members)
// const listText = members
// .map(member => {
// let { spokenName: name, phone, email } = member
// phone = (phone || '').replace(/\D/g, '')
// email = (email || '').toLowerCase()
// return `${name}\t${phone}\t${email}`
// })
// .join('\n')
// showCopy(listText)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment