Skip to content

Commit

Permalink
fix: more pr check fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evgongora committed Feb 6, 2025
1 parent 1df0d36 commit 5a44ab9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 58 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ jobs:
working-directory: frontend
env:
NEXT_TELEMETRY_DISABLED: 1
JWT_SECRET: ${{ secrets.JWT_SECRET || 'dummy-secret-for-ci' }}
NEXT_PUBLIC_WLD_APP_ID: ${{ secrets.NEXT_PUBLIC_WLD_APP_ID || 'app_staging_0' }}
run: pnpm build

security:
Expand Down Expand Up @@ -134,6 +136,8 @@ jobs:
env:
ANALYZE: true
NEXT_TELEMETRY_DISABLED: 1
JWT_SECRET: ${{ secrets.JWT_SECRET || 'dummy-secret-for-ci' }}
NEXT_PUBLIC_WLD_APP_ID: ${{ secrets.NEXT_PUBLIC_WLD_APP_ID || 'app_staging_0' }}
run: |
pnpm build
Expand Down
129 changes: 71 additions & 58 deletions frontend/src/app/api/confirm-payment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ interface TokenPayload extends JWTPayload {
address?: string;
}

const JWT_SECRET = process.env.JWT_SECRET;
if (!JWT_SECRET) {
throw new Error("JWT_SECRET environment variable is required");
function getSecret() {
const JWT_SECRET = process.env.JWT_SECRET;
if (!JWT_SECRET) {
throw new Error("JWT_SECRET environment variable is required");
}
return new TextEncoder().encode(JWT_SECRET);
}

const secret = new TextEncoder().encode(JWT_SECRET);

export async function POST(req: NextRequest) {
try {
const { payload } = (await req.json()) as IRequestPayload;
Expand All @@ -40,74 +41,86 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

const { payload: tokenPayload } = await jwtVerify(token, secret);
const typedPayload = tokenPayload as TokenPayload;
try {
const { payload: tokenPayload } = await jwtVerify(token, getSecret());
const typedPayload = tokenPayload as TokenPayload;

if (!typedPayload.address) {
console.error("No address in token payload");
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
}

const user = await xata.db.Users.filter({
wallet_address: typedPayload.address,
}).getFirst();

if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

// Get the latest payment_id
const latestPayment = await xata.db.Payments.sort(
"payment_id",
"desc",
).getFirst();
const nextPaymentId = (latestPayment?.payment_id || 0) + 1;

// Create payment record
await xata.db.Payments.create({
payment_id: nextPaymentId,
user: user.xata_id,
uuid: payload.transaction_id,
});

if (!typedPayload.address) {
console.error("No address in token payload");
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
}
// Check if user already has an active subscription
if (
user.subscription &&
user.subscription_expires &&
new Date(user.subscription_expires) > new Date()
) {
// Extend the existing subscription
const newExpiryDate = new Date(user.subscription_expires);
newExpiryDate.setDate(newExpiryDate.getDate() + 30);

const user = await xata.db.Users.filter({
wallet_address: typedPayload.address,
}).getFirst();
await xata.db.Users.update(user.xata_id, {
subscription_expires: newExpiryDate,
});

if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}
const response: PaymentResponse = {
success: true,
message: "Subscription extended",
next_payment_date: newExpiryDate.toISOString().split("T")[0],
};

return NextResponse.json(response);
}

// Get the latest payment_id
const latestPayment = await xata.db.Payments.sort(
"payment_id",
"desc",
).getFirst();
const nextPaymentId = (latestPayment?.payment_id || 0) + 1;

// Create payment record
await xata.db.Payments.create({
payment_id: nextPaymentId,
user: user.xata_id,
uuid: payload.transaction_id,
});

// Check if user already has an active subscription
if (
user.subscription &&
user.subscription_expires &&
new Date(user.subscription_expires) > new Date()
) {
// Extend the existing subscription
const newExpiryDate = new Date(user.subscription_expires);
newExpiryDate.setDate(newExpiryDate.getDate() + 30);
// Update user's subscription status for new subscription
const subscriptionExpiry = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);

await xata.db.Users.update(user.xata_id, {
subscription_expires: newExpiryDate,
subscription: true,
subscription_expires: subscriptionExpiry,
});

const response: PaymentResponse = {
success: true,
message: "Subscription extended",
next_payment_date: newExpiryDate.toISOString().split("T")[0],
message: "Subscription activated",
next_payment_date: subscriptionExpiry.toISOString().split("T")[0],
};

return NextResponse.json(response);
}
} catch (error) {
console.error("Error confirming payment:", error);

// Update user's subscription status for new subscription
const subscriptionExpiry = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);

await xata.db.Users.update(user.xata_id, {
subscription: true,
subscription_expires: subscriptionExpiry,
});

const response: PaymentResponse = {
success: true,
message: "Subscription activated",
next_payment_date: subscriptionExpiry.toISOString().split("T")[0],
};
const response: PaymentResponse = {
success: false,
error: "Failed to confirm payment",
details: error instanceof Error ? error.message : "Unknown error",
};

return NextResponse.json(response);
return NextResponse.json(response, { status: 500 });
}
} catch (error) {
console.error("Error confirming payment:", error);

Expand Down

0 comments on commit 5a44ab9

Please sign in to comment.