Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short reflink #811

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/app/api/referral/[autonum]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use server'

import { getPersonByAuto } from '@/app/utils/airtable'
import { redirect } from 'next/navigation'
import { NextRequest } from 'next/server'

export async function GET(
_request: NextRequest,
{ params }: { params: { autonum: string } },
) {
const slackId = (await getPersonByAuto(params.autonum))?.slackId
if (slackId) {
redirect('/?ref=' + slackId)
} else {
redirect('/')
}
}
35 changes: 35 additions & 0 deletions src/app/utils/airtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@ export const getSignpostUpdates = async () => {
return data.records
}

export async function getPersonByAuto(num: string): Promise<{
slackId: string
} | null> {
const baseId = process.env.BASE_ID
const apiKey = process.env.AIRTABLE_API_KEY
const table = 'people'

const url = `https://middleman.hackclub.com/airtable/v0/${baseId}/${table}?filterByFormula={autonumber}='${encodeURIComponent(num)}'`

const response = await fetch(url, {
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'User-Agent': 'highseas.hackclub.com (getPersonByMagicToken)',
},
})

if (!response.ok) {
const err = new Error(`Airtable API error: ${await response.text()}`)
console.error(err)
throw err
}

const data = await response.json()
if (!data.records || data.records.length === 0) return null

const id = data.records[0].id
const email = data.records[0].fields.email
const slackId = data.records[0].fields.slack_id

if (!id || !email || !slackId) return null

return { slackId }
}

export async function getPersonByMagicToken(token: string): Promise<{
id: string
email: string
Expand Down
12 changes: 12 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@
"path": "/api/cron/every-day",
"schedule": "0 18 * * *"
}
],
"rewrites": [
{
"source": "/:ref_id*",
"has": [
{
"type": "host",
"value": "ahoy.hack.club"
}
],
"destination": "https://highseas.hackclub.com/api/referral/path*"
}
]
}
Loading