mirror of
https://github.com/System-End/site.git
synced 2026-04-19 22:05:11 +00:00
Merge pull request #1322 from hackclub/add-active-cohort-endpoint
Add endpoint for currently active cohort to vote on
This commit is contained in:
commit
ad31913122
1 changed files with 57 additions and 0 deletions
57
pages/api/arcade/showcase/cohort/active.js
Normal file
57
pages/api/arcade/showcase/cohort/active.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import AirtablePlus from 'airtable-plus'
|
||||
|
||||
async function getCohortFromAuth(authToken) {
|
||||
const safeAuthToken = authToken?.replace(/[^a-zA-Z0-9-]/g, '')
|
||||
const airtable = new AirtablePlus({
|
||||
apiKey: process.env.AIRTABLE_API_KEY,
|
||||
baseID: 'app4kCWulfB02bV8Q',
|
||||
tableName: 'Cohorts'
|
||||
})
|
||||
const cohorts = await airtable.read({
|
||||
filterByFormula: `AND(
|
||||
FIND("${safeAuthToken}", {Allowed Voter Keys}) > 0,
|
||||
{Is Active} = true
|
||||
)`,
|
||||
maxRecords: 1
|
||||
})
|
||||
const cohort = cohorts[0]
|
||||
return cohort
|
||||
}
|
||||
|
||||
async function getShowcases(cohort) {
|
||||
const airtable = new AirtablePlus({
|
||||
apiKey: process.env.AIRTABLE_API_KEY,
|
||||
baseID: 'app4kCWulfB02bV8Q',
|
||||
tableName: 'Showcase'
|
||||
})
|
||||
|
||||
const showcaseIDs = cohort.fields['Showcases']
|
||||
const showcaseFormula = [
|
||||
'FALSE()',
|
||||
...showcaseIDs.map(id => `RECORD_ID() = '${id}'`)
|
||||
]
|
||||
|
||||
const showcases = await airtable.read({
|
||||
filterByFormula: `OR(${showcaseFormula.join(', ')})`
|
||||
})
|
||||
return showcases
|
||||
}
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const authToken = req.headers['authorization']?.replace('Bearer ', '')
|
||||
|
||||
const cohort = await getCohortFromAuth(authToken)
|
||||
if (!cohort) {
|
||||
return res.status(401).json({ error: 'No cohort found for user' })
|
||||
}
|
||||
|
||||
let showcases = await getShowcases(cohort)
|
||||
|
||||
res.status(200).json({
|
||||
cohort: {
|
||||
id: cohort.id
|
||||
},
|
||||
showcaseIDs: cohort.fields['Showcases'],
|
||||
showcases
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue