Skip to content

Commit

Permalink
Merge pull request #702 from Dokploy/canary
Browse files Browse the repository at this point in the history
v0.11.2
  • Loading branch information
Siumauricio authored Nov 14, 2024
2 parents 8162dcf + 82fc989 commit be93406
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/dokploy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dokploy",
"version": "v0.11.1",
"version": "v0.11.2",
"private": true,
"license": "Apache-2.0",
"type": "module",
Expand Down
66 changes: 46 additions & 20 deletions apps/dokploy/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export default async function handler(
.update(admins)
.set({
stripeSubscriptionId: newSubscription.id,
serversQuantity: 0,
stripeCustomerId: newSubscription.customer as string,
})
.where(eq(admins.stripeCustomerId, newSubscription.customer as string))
Expand Down Expand Up @@ -121,12 +120,6 @@ export default async function handler(
}
case "customer.subscription.updated": {
const newSubscription = event.data.object as Stripe.Subscription;
await db
.update(admins)
.set({
serversQuantity: newSubscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(eq(admins.stripeCustomerId, newSubscription.customer as string));

const admin = await findAdminByStripeCustomerId(
newSubscription.customer as string,
Expand All @@ -136,8 +129,27 @@ export default async function handler(
return res.status(400).send("Webhook Error: Admin not found");
}

const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
if (newSubscription.status === "active") {
await db
.update(admins)
.set({
serversQuantity: newSubscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(
eq(admins.stripeCustomerId, newSubscription.customer as string),
);

const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
} else {
await disableServers(admin.adminId);
await db
.update(admins)
.set({ serversQuantity: 0 })
.where(
eq(admins.stripeCustomerId, newSubscription.customer as string),
);
}

break;
}
Expand All @@ -148,6 +160,13 @@ export default async function handler(
newInvoice.subscription as string,
);

if (suscription.status !== "active") {
console.log(
`Skipping invoice.payment_succeeded for subscription ${suscription.id} with status ${suscription.status}`,
);
break;
}

await db
.update(admins)
.set({
Expand All @@ -168,22 +187,29 @@ export default async function handler(
}
case "invoice.payment_failed": {
const newInvoice = event.data.object as Stripe.Invoice;
await db
.update(admins)
.set({
serversQuantity: 0,
})
.where(eq(admins.stripeCustomerId, newInvoice.customer as string));

const admin = await findAdminByStripeCustomerId(
newInvoice.customer as string,
const subscription = await stripe.subscriptions.retrieve(
newInvoice.subscription as string,
);

if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
if (subscription.status !== "active") {
const admin = await findAdminByStripeCustomerId(
newInvoice.customer as string,
);

if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
await db
.update(admins)
.set({
serversQuantity: 0,
})
.where(eq(admins.stripeCustomerId, newInvoice.customer as string));

await disableServers(admin.adminId);
}

await disableServers(admin.adminId);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/dokploy/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default function Home({ IS_CLOUD }: Props) {
) : (
<Link
className="hover:underline text-muted-foreground"
href="https://docs.dokploy.com/docs/core/get-started/reset-password"
href="https://docs.dokploy.com/docs/core/reset-password"
target="_blank"
>
Lost your password?
Expand Down

0 comments on commit be93406

Please sign in to comment.