-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from pvdthings/web-shifts-page
Volunteer Module
- Loading branch information
Showing
25 changed files
with
1,372 additions
and
939 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { findMember } = require("../../../services/borrowers"); | ||
const { fetchJobs, updateJobAssignments } = require("../../../services/jobs/service"); | ||
const { sendJobsNotification } = require("../../../services/messages"); | ||
|
||
const dateFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }; | ||
|
||
const formatTime = (date) => { | ||
let hours = date.getHours(); | ||
let minutes = date.getMinutes(); | ||
let ampm = hours >= 12 ? 'pm' : 'am'; | ||
|
||
hours = hours % 12; hours = hours ? hours : 12; | ||
minutes = minutes < 10 ? '0' + minutes : minutes; | ||
return hours + ':' + minutes + ampm; | ||
}; | ||
|
||
const getShifts = async ({ email }) => { | ||
const member = await getMember(email); | ||
const jobs = await fetchJobs(); | ||
|
||
return jobs.map((j) => { | ||
const date = new Date(j.startTime); | ||
let endDate = new Date(j.startTime); | ||
endDate.setSeconds(date.getSeconds() + j.duration); | ||
|
||
return { | ||
id: j.id, | ||
title: j.title, | ||
date: date.toLocaleString('en-US', dateFormatOptions), | ||
timespan: `${formatTime(date)} - ${formatTime(endDate)}`, | ||
description: j.description, | ||
enrolled: j.members.some((m) => m.id === member?.id), | ||
volunteers: j.members.filter((m) => m.id !== member?.id).map((m) => ({ | ||
name: m.name, | ||
firstName: m.name.split(' ')?.[0], | ||
keyholder: m.keyholder | ||
})) | ||
}; | ||
}); | ||
}; | ||
|
||
const getMember = async (email) => { | ||
return !!email ? await findMember({ email }) : undefined; | ||
}; | ||
|
||
const enroll = async (email, shifts) => { | ||
try { | ||
updateJobAssignments(email, shifts).then(async () => { | ||
const newShifts = shifts.filter((s) => !s.remove); | ||
if (newShifts.length) { | ||
await sendJobsNotification({ recipient: email, ids: newShifts.map((s) => s.id) }); | ||
} | ||
}); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
module.exports = { enroll, getShifts }; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { fetchJobs } = require('./service'); | ||
|
||
module.exports = { fetchJobs }; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const { base, Table } = require('../../db'); | ||
|
||
const jobs = base(Table.Jobs); | ||
const members = base(Table.Borrowers); | ||
|
||
const fetchJobs = async () => { | ||
const records = await jobs.select({ view: 'Future' }).all(); | ||
|
||
return await Promise.all(records.map(async (r) => { | ||
const memberIds = r.get('Members') || []; | ||
|
||
return { | ||
id: r.id, | ||
title: r.get('Title'), | ||
canceled: !!r.get('Canceled'), | ||
description: r.get('Description'), | ||
members: await Promise.all(memberIds.map(async (id) => { | ||
const memberRecord = await members.find(id); | ||
|
||
return { | ||
id: memberRecord.id, | ||
name: memberRecord.get('Name'), | ||
keyholder: !!memberRecord.get('Keyholder') | ||
}; | ||
})), | ||
startTime: r.get('Start Time'), | ||
duration: r.get('Duration') | ||
}; | ||
})); | ||
}; | ||
|
||
const updateJobAssignments = async (email, jobs) => { | ||
const matches = await members.select({ | ||
filterByFormula: `{Email} = '${email}'` | ||
}).all(); | ||
|
||
if (!matches.length) { | ||
throw new Error('Email provided does not match any member.'); | ||
} | ||
|
||
const memberRecord = matches[0]; | ||
const existingJobs = memberRecord.get('Jobs') || []; | ||
|
||
const toAdd = jobs.filter((j) => !j.remove).map((j) => j.id); | ||
const toRemove = jobs.filter((j) => !!j.remove).map((j) => j.id); | ||
|
||
const updatedJobs = [ | ||
...toAdd, | ||
...existingJobs.filter((id) => !toRemove.includes(id)) | ||
]; | ||
|
||
await memberRecord.updateFields({ | ||
'Jobs': updatedJobs | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
fetchJobs, | ||
updateJobAssignments | ||
}; |
This file contains 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
Oops, something went wrong.