Skip to content

Commit

Permalink
Improve error catching in groups.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaKattainen committed Sep 24, 2020
1 parent 65a63ac commit 525198e
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ async function getMemberData(groupMembers) {
}
)
})
)
).catch(error => {
console.error('Failed to get member data: ', error)
})
}

function filterGroups(userNumber, groupsData) {
Expand All @@ -41,40 +43,39 @@ async function getAllGroups(userNumber) {
const member = await getMember(userNumber)
const memberGroups = await getGroupsFromKuksa(userNumber)
const groupAndMemberData = Promise.all(
memberGroups.groups.map(async group => {
const groupInfo = await getGroupInfo(userNumber, group.id)
const groupMembers = await getGroupMembers(userNumber, group.id)
const memberData = await getMemberData(groupMembers)
const ageGroupId =
(memberData[0] &&
(await getMember(memberData[0].memberId)).age_groupId) ||
4
return Object.assign(
{},
{
id: group.id,
name: group.name,
ageGroup: groupInfo.age_groups[0],
ageGroupId,
troop: groupInfo.troops.filter(
troop => troop.id === member.default_troop_id
),
members: memberData,
}
)
})
memberGroups.groups
.map(async group => {
const groupInfo = await getGroupInfo(userNumber, group.id)
const groupMembers = await getGroupMembers(userNumber, group.id)
const memberData = await getMemberData(groupMembers)
const ageGroupId =
(memberData[0] &&
(await getMember(memberData[0].memberId)).age_groupId) ||
4
return Object.assign(
{},
{
id: group.id,
name: group.name,
ageGroup: groupInfo.age_groups[0],
ageGroupId,
troop: groupInfo.troops.filter(
troop => troop.id === member.default_troop_id
),
members: memberData,
}
)
})
.catch(error => {
console.error('Failed to get all groups: ', error)
})
)

return groupAndMemberData
}

export async function getGroups(userNumber) {
try {
const allGroups = await getAllGroups(userNumber)
const filteredGroups = filterGroups(userNumber, allGroups)
return filteredGroups
} catch (error) {
console.error('Failed to fetch groups: ', error)
}
return []
const allGroups = await getAllGroups(userNumber)
const filteredGroups = filterGroups(userNumber, allGroups)
return filteredGroups || []
}

0 comments on commit 525198e

Please sign in to comment.