From df4af84d55a5744df2af8bfe1f2554ddb94dc24a Mon Sep 17 00:00:00 2001 From: Wanjohi Date: Sat, 20 Sep 2025 03:56:51 +0300 Subject: [PATCH] fix: Resolve database issues --- cloud/infra/auth.ts | 1 + cloud/infra/secrets.ts | 1 + cloud/packages/core/src/polar/index.ts | 103 ++++++++++++------------- cloud/packages/functions/sst-env.d.ts | 4 + sst-env.d.ts | 4 + 5 files changed, 60 insertions(+), 53 deletions(-) diff --git a/cloud/infra/auth.ts b/cloud/infra/auth.ts index edb1120a..3e13203d 100644 --- a/cloud/infra/auth.ts +++ b/cloud/infra/auth.ts @@ -11,6 +11,7 @@ export const auth = new sst.cloudflare.Worker("Auth", { link: [ database, authStorage, + secret.POLAR_API_KEY, secret.DISCORD_CLIENT_ID, secret.DISCORD_CLIENT_SECRET, ], diff --git a/cloud/infra/secrets.ts b/cloud/infra/secrets.ts index 1a931592..1985a800 100644 --- a/cloud/infra/secrets.ts +++ b/cloud/infra/secrets.ts @@ -1,4 +1,5 @@ export const secret = { DISCORD_CLIENT_ID: new sst.Secret("DISCORD_CLIENT_ID"), DISCORD_CLIENT_SECRET: new sst.Secret("DISCORD_CLIENT_SECRET"), + POLAR_API_KEY: new sst.Secret("POLAR_API_KEY"), }; diff --git a/cloud/packages/core/src/polar/index.ts b/cloud/packages/core/src/polar/index.ts index 1d885c40..cbd5f521 100644 --- a/cloud/packages/core/src/polar/index.ts +++ b/cloud/packages/core/src/polar/index.ts @@ -4,60 +4,57 @@ import { Resource } from "sst"; import { Polar as PolarSdk } from "@polar-sh/sdk"; import { validateEvent } from "@polar-sh/sdk/webhooks"; -const polar = new PolarSdk({ - accessToken: Resource.PolarSecret.value, - server: Resource.App.stage !== "production" ? "sandbox" : "production" -}); - export namespace Polar { - export const client = polar; + export const client = () => + new PolarSdk({ + accessToken: Resource.POLAR_API_KEY.value, + server: Resource.App.stage !== "production" ? "sandbox" : "production", + }); - export const fromUserEmail = fn(z.string().min(1), async (email) => { - try { - const customers = await client.customers.list({ email }) + export const fromUserEmail = fn(z.string().min(1), async (email) => { + try { + const customers = await client().customers.list({ email }); - if (customers.result.items.length === 0) { - return await client.customers.create({ email}) - } else { - return customers.result.items[0] - } - - } catch (err) { - //FIXME: This is the issue [Polar.sh/#5147](https://github.com/polarsource/polar/issues/5147) - // console.log("error", err) - return undefined - } - }) - - // const getProductIDs = (plan: z.infer) => { - // switch (plan) { - // case "free": - // return [Resource.NestriFreeMonthly.value] - // case "pro": - // return [Resource.NestriProYearly.value, Resource.NestriProMonthly.value] - // case "family": - // return [Resource.NestriFamilyYearly.value, Resource.NestriFamilyMonthly.value] - // default: - // return [Resource.NestriFreeMonthly.value] - // } - // } - - export const createPortal = fn( - z.string(), - async (customerId) => { - const session = await client.customerSessions.create({ - customerId - }) - - return session.customerPortalUrl - } - ) - - //TODO: Implement this - export const handleWebhook = async (payload: ReturnType) => { - switch (payload.type) { - case "subscription.created": - const teamID = payload.data.metadata.teamID - } + if (customers.result.items.length === 0) { + return await client().customers.create({ email }); + } else { + return customers.result.items[0]; + } + } catch (err) { + //FIXME: This is the issue [Polar.sh/#5147](https://github.com/polarsource/polar/issues/5147) + // console.log("error", err) + return undefined; } -} \ No newline at end of file + }); + + // const getProductIDs = (plan: z.infer) => { + // switch (plan) { + // case "free": + // return [Resource.NestriFreeMonthly.value] + // case "pro": + // return [Resource.NestriProYearly.value, Resource.NestriProMonthly.value] + // case "family": + // return [Resource.NestriFamilyYearly.value, Resource.NestriFamilyMonthly.value] + // default: + // return [Resource.NestriFreeMonthly.value] + // } + // } + + export const createPortal = fn(z.string(), async (customerId) => { + const session = await client().customerSessions.create({ + customerId, + }); + + return session.customerPortalUrl; + }); + + //TODO: Implement this + export const handleWebhook = async ( + payload: ReturnType, + ) => { + switch (payload.type) { + case "subscription.created": + const teamID = payload.data.metadata.teamID; + } + }; +} diff --git a/cloud/packages/functions/sst-env.d.ts b/cloud/packages/functions/sst-env.d.ts index 0b9f4b11..ad516d4d 100644 --- a/cloud/packages/functions/sst-env.d.ts +++ b/cloud/packages/functions/sst-env.d.ts @@ -21,6 +21,10 @@ declare module "sst" { "type": "sst.sst.Linkable" "user": string } + "POLAR_API_KEY": { + "type": "sst.sst.Secret" + "value": string + } } } // cloudflare diff --git a/sst-env.d.ts b/sst-env.d.ts index 0b9f4b11..ad516d4d 100644 --- a/sst-env.d.ts +++ b/sst-env.d.ts @@ -21,6 +21,10 @@ declare module "sst" { "type": "sst.sst.Linkable" "user": string } + "POLAR_API_KEY": { + "type": "sst.sst.Secret" + "value": string + } } } // cloudflare