Connect, collaborate, and grow with a community of TypeScript developers
feat: add contributor role to users (#1517)
Sean Boultb86819962669
diff --git a/apps/web/global.d.ts b/apps/web/global.d.tsindex ed7b2c6b..e97829b8 100644--- a/apps/web/global.d.ts+++ b/apps/web/global.d.ts@@ -14,6 +14,7 @@ declare namespace NodeJS {RESEND_API_KEY: string;EDGE_CONFIG: string;STAGING?: string;+ CRON_SECRET?: string;NEXT_PUBLIC_ALGOLIA_APP_ID: string;NEXT_PUBLIC_ALGOLIA_API_KEY: string;}diff --git a/apps/web/src/app/api/cron/sync-contributors/route.ts b/apps/web/src/app/api/cron/sync-contributors/route.tsnew file mode 100644index 00000000..d69ab81b--- /dev/null+++ b/apps/web/src/app/api/cron/sync-contributors/route.ts@@ -0,0 +1,61 @@+import { type NextRequest } from 'next/server';+import { prisma } from '@repo/db';++import { contributors } from '../../../../../public/contributors';+import { RoleTypes } from '@repo/db/types';++export const dynamic = 'force-dynamic';++// NOTE: this is called on cron job+export async function GET(request: NextRequest) {+ const authHeader = request.headers.get('authorization');+ if (authHeader !== `Bearer ${process.env.CRON_SECRET}` && process.env.NODE_ENV === 'production') {+ return new Response('Unauthorized', {+ status: 401,+ });+ }++ const MAX_PARTITION_SIZE = 60;++ let contributorRole = await prisma.role.findFirst({+ where: {+ role: RoleTypes.CONTRIBUTOR,+ },+ });++ if (!contributorRole) {+ contributorRole = await prisma.role.create({+ data: {+ role: RoleTypes.CONTRIBUTOR,+ },+ });+ }++ const contributorIds = (+ await prisma.user.findMany({+ where: {+ id: {+ in: contributors.map((c) => `${c.id}`),+ },+ },+ select: {+ id: true,+ },+ })+ ).map((c) => c.id);++ let updateCount = 0;++ while (contributorIds.length) {+ const contributorsPartition = contributorIds.splice(0, MAX_PARTITION_SIZE);++ const values = contributorsPartition.map(+ (contributorId) => `('${contributorRole!.id}', '${contributorId}')`,+ );+ updateCount += await prisma.$executeRawUnsafe(+ `insert ignore into _RoleToUser (A, B) values ${values.join(',')}`,+ );+ }++ return Response.json({ success: true, updateCount });+}diff --git a/apps/web/vercel.json b/apps/web/vercel.jsonindex 106d6f0f..e40d89b4 100644--- a/apps/web/vercel.json+++ b/apps/web/vercel.json@@ -1,5 +1,11 @@{"ignoreCommand": "npx turbo-ignore","framework": "nextjs",- "buildCommand": "turbo build"+ "buildCommand": "turbo build",+ "crons": [+ {+ "path": "/api/cron/sync-contributors",+ "schedule": "0 0 * * *"+ }+ ]}diff --git a/turbo.json b/turbo.jsonindex a49c42bc..e6cc723e 100644--- a/turbo.json+++ b/turbo.json@@ -27,6 +27,7 @@"USERNAME","PASSWORD","CI",+ "CRON_SECRET","MAILCHIMP_API_KEY","MAILCHIMP_AUDIENCE_ID","MAILCHIMP_API_SERVER",