Skip to content

Commit

Permalink
fix no token set
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Dec 10, 2024
1 parent 8a6bdeb commit 1d5dfa5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
84 changes: 84 additions & 0 deletions web/app/api/cron/redeploy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { NextResponse } from "next/server";
import { db, vercelTokens } from "@/drizzle/schema";
import { Vercel } from "@vercel/sdk";
import { headers } from "next/headers";

const CRON_SECRET = process.env.CRON_SECRET;

export async function GET(request: Request) {
console.log("Redeploy cron job started");
// Verify the request is from Vercel Cron
const headersList = headers();
const authHeader = headersList.get('authorization');

if (authHeader !== `Bearer ${CRON_SECRET}`) {
return new NextResponse('Unauthorized', { status: 401 });
}

try {
// Get all tokens from the database
const tokens = await db
.select()
.from(vercelTokens);

console.log(`Found ${tokens.length} tokens to process`);

const results = await Promise.allSettled(
tokens.map(async (tokenRecord) => {
try {
const vercel = new Vercel({
bearerToken: tokenRecord.token,
});

if (!tokenRecord.projectId) {
console.log(`No project ID for user ${tokenRecord.userId}`);
return;
}

// Create new deployment with correct properties
const deployment = await vercel.deployments.createDeployment({
requestBody: {
name: `file-organizer-redeploy-${Date.now()}`,
target: "production",
project: tokenRecord.projectId,
gitSource: {
type: "github",
repo: "different-ai/file-organizer-2000",
ref: "master",
org: "different-ai",
},
projectSettings: {
framework: "nextjs",
buildCommand: "pnpm build:self-host",
installCommand: "pnpm install",
outputDirectory: ".next",
rootDirectory: "web",
},
},
});

console.log(`Redeployed project ${tokenRecord.projectId} for user ${tokenRecord.userId}`);
return deployment;
} catch (error) {
console.error(`Failed to redeploy for user ${tokenRecord.userId}:`, error);
throw error;
}
})
);

const successful = results.filter((r) => r.status === "fulfilled").length;
const failed = results.filter((r) => r.status === "rejected").length;

return NextResponse.json({
message: `Processed ${tokens.length} tokens`,
stats: {
total: tokens.length,
successful,
failed,
},
});
} catch (error) {
console.error("Error in redeploy cron:", error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}
7 changes: 2 additions & 5 deletions web/app/dashboard/lifetime/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { db, vercelTokens } from "@/drizzle/schema";
import { eq } from "drizzle-orm";
import { createLicenseKeyFromUserId } from "@/app/actions";

const VERCEL_TOKEN = process.env.VERCEL_TOKEN;
if (!VERCEL_TOKEN) {
throw new Error("VERCEL_TOKEN environment variable is not set");
}

const GITHUB_ORG = "different-ai";
const GITHUB_REPO = "file-organizer-2000";
const GITHUB_BRANCH = "master";
Expand Down Expand Up @@ -45,12 +40,14 @@ export async function setupProject(vercelToken: string, openaiKey: string): Prom
.where(eq(vercelTokens.userId, userId));

if (existingToken) {
console.log("Updating existing token");
// Update existing token
await db
.update(vercelTokens)
.set({ token: vercelToken, updatedAt: new Date() })
.where(eq(vercelTokens.userId, userId));
} else {
console.log("Inserting new token");
// Insert new token
await db.insert(vercelTokens).values({
userId,
Expand Down

0 comments on commit 1d5dfa5

Please sign in to comment.