From b2e10c9b1874fa0a036e50330c67caf278729061 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 19 Nov 2024 16:52:52 -0500 Subject: [PATCH 1/6] Add short reflink --- vercel.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vercel.json b/vercel.json index c432aef6..1b264597 100644 --- a/vercel.json +++ b/vercel.json @@ -8,5 +8,17 @@ "path": "/api/cron/every-day", "schedule": "0 18 * * *" } + ], + "rewrites": [ + { + "source": "/:ref_id*", + "has": [ + { + "type": "host", + "value": "ahoy.hack.club" + } + ], + "destination": "/?ref=:path*" + } ] } From 83f5adec43b2c9259f7b73a16e00312d7fe44078 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 19 Nov 2024 17:20:49 -0500 Subject: [PATCH 2/6] Update path reflink --- src/app/api/referral/[slackId]/route.ts | 12 ++++++++++++ vercel.json | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/app/api/referral/[slackId]/route.ts diff --git a/src/app/api/referral/[slackId]/route.ts b/src/app/api/referral/[slackId]/route.ts new file mode 100644 index 00000000..566a12ad --- /dev/null +++ b/src/app/api/referral/[slackId]/route.ts @@ -0,0 +1,12 @@ +'use server' + +import { redirect } from 'next/navigation' +import { NextRequest } from 'next/server' + +export async function GET( + _request: NextRequest, + { params }: { params: { slackId: string } }, +) { + + redirect('/?slackId=' + params.slackId) +} \ No newline at end of file diff --git a/vercel.json b/vercel.json index 1b264597..a0d518b1 100644 --- a/vercel.json +++ b/vercel.json @@ -18,7 +18,7 @@ "value": "ahoy.hack.club" } ], - "destination": "/?ref=:path*" + "destination": "/api/referral/path*" } ] } From 9b6105efeea947f07bc3b85adb2de4af967ab892 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 19 Nov 2024 17:31:58 -0500 Subject: [PATCH 3/6] Bun fmt --- src/app/api/referral/[slackId]/route.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/api/referral/[slackId]/route.ts b/src/app/api/referral/[slackId]/route.ts index 566a12ad..c1689910 100644 --- a/src/app/api/referral/[slackId]/route.ts +++ b/src/app/api/referral/[slackId]/route.ts @@ -7,6 +7,5 @@ export async function GET( _request: NextRequest, { params }: { params: { slackId: string } }, ) { - redirect('/?slackId=' + params.slackId) -} \ No newline at end of file +} From ec1f98b40cea85413bbfc9dcd1195db6f70e4c0c Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 19 Nov 2024 17:39:04 -0500 Subject: [PATCH 4/6] Switch to using autonumber --- src/app/api/referral/[autonum]/route.ts | 17 ++++++++++++ src/app/api/referral/[slackId]/route.ts | 11 -------- src/app/utils/airtable.ts | 35 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 src/app/api/referral/[autonum]/route.ts delete mode 100644 src/app/api/referral/[slackId]/route.ts diff --git a/src/app/api/referral/[autonum]/route.ts b/src/app/api/referral/[autonum]/route.ts new file mode 100644 index 00000000..e4f96953 --- /dev/null +++ b/src/app/api/referral/[autonum]/route.ts @@ -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('/') + } else { + redirect('/?ref=' + slackId) + } +} diff --git a/src/app/api/referral/[slackId]/route.ts b/src/app/api/referral/[slackId]/route.ts deleted file mode 100644 index c1689910..00000000 --- a/src/app/api/referral/[slackId]/route.ts +++ /dev/null @@ -1,11 +0,0 @@ -'use server' - -import { redirect } from 'next/navigation' -import { NextRequest } from 'next/server' - -export async function GET( - _request: NextRequest, - { params }: { params: { slackId: string } }, -) { - redirect('/?slackId=' + params.slackId) -} diff --git a/src/app/utils/airtable.ts b/src/app/utils/airtable.ts index 6f4471dc..0c10af73 100644 --- a/src/app/utils/airtable.ts +++ b/src/app/utils/airtable.ts @@ -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 From d3ed931caa900794dcfcbbb7580deff00dd2823f Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 19 Nov 2024 17:55:48 -0500 Subject: [PATCH 5/6] Whoops! --- src/app/api/referral/[autonum]/route.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/api/referral/[autonum]/route.ts b/src/app/api/referral/[autonum]/route.ts index e4f96953..86208e80 100644 --- a/src/app/api/referral/[autonum]/route.ts +++ b/src/app/api/referral/[autonum]/route.ts @@ -10,8 +10,8 @@ export async function GET( ) { const slackId = (await getPersonByAuto(params.autonum))?.slackId if (slackId) { - redirect('/') - } else { redirect('/?ref=' + slackId) + } else { + redirect('/') } } From 93ee6ac27660db9ebaa91bba394044b8084d343c Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 19 Nov 2024 17:57:10 -0500 Subject: [PATCH 6/6] Redirect ahoy.hack.club -> highseas.hackclub.com --- vercel.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vercel.json b/vercel.json index a0d518b1..195841e4 100644 --- a/vercel.json +++ b/vercel.json @@ -18,7 +18,7 @@ "value": "ahoy.hack.club" } ], - "destination": "/api/referral/path*" + "destination": "https://highseas.hackclub.com/api/referral/path*" } ] }