feat: add migration

This commit is contained in:
Mauricio Siu
2025-02-09 18:19:21 -06:00
parent 6b9fd596e5
commit c04bf3c7e0
31 changed files with 5790 additions and 440 deletions

View File

@@ -0,0 +1,255 @@
CREATE TABLE "account" (
"id" text PRIMARY KEY NOT NULL,
"account_id" text NOT NULL,
"provider_id" text NOT NULL,
"user_id" text NOT NULL,
"access_token" text,
"refresh_token" text,
"id_token" text,
"access_token_expires_at" timestamp,
"refresh_token_expires_at" timestamp,
"scope" text,
"password" text,
"is2FAEnabled" boolean DEFAULT false NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
"resetPasswordToken" text,
"resetPasswordExpiresAt" text,
"confirmationToken" text,
"confirmationExpiresAt" text
);
--> statement-breakpoint
CREATE TABLE "verification" (
"id" text PRIMARY KEY NOT NULL,
"identifier" text NOT NULL,
"value" text NOT NULL,
"expires_at" timestamp NOT NULL,
"created_at" timestamp,
"updated_at" timestamp
);
-- Primero eliminar las restricciones NOT NULL y foreign keys
ALTER TABLE "user" ALTER COLUMN "adminId" DROP NOT NULL;
ALTER TABLE "user" ALTER COLUMN "authId" DROP NOT NULL;
ALTER TABLE "user" DROP CONSTRAINT IF EXISTS "user_adminId_admin_adminId_fk";
ALTER TABLE "user" DROP CONSTRAINT IF EXISTS "user_authId_auth_id_fk";
ALTER TABLE "admin" DROP CONSTRAINT IF EXISTS "admin_authId_auth_id_fk";
ALTER TABLE "project" DROP CONSTRAINT IF EXISTS "project_adminId_admin_adminId_fk";
ALTER TABLE "destination" DROP CONSTRAINT IF EXISTS "destination_adminId_admin_adminId_fk";
ALTER TABLE "certificate" DROP CONSTRAINT IF EXISTS "certificate_adminId_admin_adminId_fk";
ALTER TABLE "session" DROP CONSTRAINT IF EXISTS "session_user_id_auth_id_fk";
ALTER TABLE "registry" DROP CONSTRAINT IF EXISTS "registry_adminId_admin_adminId_fk";
ALTER TABLE "notification" DROP CONSTRAINT IF EXISTS "notification_adminId_admin_adminId_fk";
ALTER TABLE "ssh-key" DROP CONSTRAINT IF EXISTS "ssh-key_adminId_admin_adminId_fk";
ALTER TABLE "git_provider" DROP CONSTRAINT IF EXISTS "git_provider_adminId_admin_adminId_fk";
ALTER TABLE "server" DROP CONSTRAINT IF EXISTS "server_adminId_admin_adminId_fk";
-- Luego renombrar las columnas
ALTER TABLE "user" RENAME COLUMN "userId" TO "id";
ALTER TABLE "project" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "destination" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "certificate" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "registry" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "notification" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "ssh-key" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "git_provider" RENAME COLUMN "adminId" TO "userId";
ALTER TABLE "server" RENAME COLUMN "adminId" TO "userId";
-- Primero agregar todas las columnas sin restricciones
ALTER TABLE "user" ADD COLUMN "name" text;
ALTER TABLE "user" ADD COLUMN "email" text;
ALTER TABLE "user" ADD COLUMN "email_verified" boolean;
ALTER TABLE "user" ADD COLUMN "image" text;
ALTER TABLE "user" ADD COLUMN "role" text;
ALTER TABLE "user" ADD COLUMN "banned" boolean;
ALTER TABLE "user" ADD COLUMN "ban_reason" text;
ALTER TABLE "user" ADD COLUMN "ban_expires" timestamp;
ALTER TABLE "user" ADD COLUMN "updated_at" timestamp;
ALTER TABLE "user" ADD COLUMN "serverIp" text;
ALTER TABLE "user" ADD COLUMN "certificateType" "certificateType" DEFAULT 'none';
ALTER TABLE "user" ADD COLUMN "host" text;
ALTER TABLE "user" ADD COLUMN "letsEncryptEmail" text;
ALTER TABLE "user" ADD COLUMN "sshPrivateKey" text;
ALTER TABLE "user" ADD COLUMN "enableDockerCleanup" boolean DEFAULT false;
ALTER TABLE "user" ADD COLUMN "enableLogRotation" boolean DEFAULT false;
ALTER TABLE "user" ADD COLUMN "enablePaidFeatures" boolean DEFAULT false;
ALTER TABLE "user" ADD COLUMN "metricsConfig" jsonb DEFAULT '{"server":{"type":"Dokploy","refreshRate":60,"port":4500,"token":"","retentionDays":2,"cronJob":"","urlCallback":"","thresholds":{"cpu":0,"memory":0}},"containers":{"refreshRate":60,"services":{"include":[],"exclude":[]}}}';
ALTER TABLE "user" ADD COLUMN "cleanupCacheApplications" boolean DEFAULT false;
ALTER TABLE "user" ADD COLUMN "cleanupCacheOnPreviews" boolean DEFAULT false;
ALTER TABLE "user" ADD COLUMN "cleanupCacheOnCompose" boolean DEFAULT false;
ALTER TABLE "user" ALTER COLUMN "token" SET DEFAULT '';
ALTER TABLE "user" ALTER COLUMN "expirationDate" SET DEFAULT CURRENT_TIMESTAMP + INTERVAL '1 year';
ALTER TABLE "user" ALTER COLUMN "createdAt" SET DEFAULT to_char(CURRENT_TIMESTAMP, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"');
--> statement-breakpoint
-- Luego actualizar los valores nulos
UPDATE "user" SET token = '' WHERE token IS NULL;
UPDATE "user" SET "expirationDate" = CURRENT_TIMESTAMP + INTERVAL '1 year' WHERE "expirationDate" IS NULL;
UPDATE "user" SET "createdAt" = to_char(CURRENT_TIMESTAMP, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') WHERE "createdAt" IS NULL;
UPDATE "user" SET "name" = '' WHERE "name" IS NULL;
UPDATE "user" SET "email" = COALESCE("email", '') WHERE true;
UPDATE "user" SET "email_verified" = COALESCE("email_verified", false) WHERE true;
UPDATE "user" SET "role" = COALESCE("role", 'user') WHERE true;
UPDATE "user" SET "banned" = COALESCE("banned", false) WHERE true;
UPDATE "user" SET "updated_at" = COALESCE("updated_at", CURRENT_TIMESTAMP) WHERE true;
UPDATE "user" SET "certificateType" = COALESCE("certificateType", 'none') WHERE true;
UPDATE "user" SET "enableDockerCleanup" = COALESCE("enableDockerCleanup", false) WHERE true;
UPDATE "user" SET "enableLogRotation" = COALESCE("enableLogRotation", false) WHERE true;
UPDATE "user" SET "enablePaidFeatures" = COALESCE("enablePaidFeatures", false) WHERE true;
UPDATE "user" SET "metricsConfig" = COALESCE("metricsConfig", '{"server":{"type":"Dokploy","refreshRate":60,"port":4500,"token":"","retentionDays":2,"cronJob":"","urlCallback":"","thresholds":{"cpu":0,"memory":0}},"containers":{"refreshRate":60,"services":{"include":[],"exclude":[]}}}') WHERE true;
UPDATE "user" SET "cleanupCacheApplications" = COALESCE("cleanupCacheApplications", false) WHERE true;
UPDATE "user" SET "cleanupCacheOnPreviews" = COALESCE("cleanupCacheOnPreviews", false) WHERE true;
UPDATE "user" SET "cleanupCacheOnCompose" = COALESCE("cleanupCacheOnCompose", false) WHERE true;
--> statement-breakpoint
-- Migrar datos de auth a user
INSERT INTO "user" (
id,
name,
email,
email_verified,
image,
role,
updated_at
)
SELECT
id,
'' as name,
email,
true as email_verified,
image,
CASE
WHEN rol = 'admin' THEN 'admin'
ELSE 'user'
END as role,
CAST("createdAt" AS timestamp) as updated_at
FROM "auth";
-- Migrar datos de admin a user
UPDATE "user" u
SET
"serverIp" = a."serverIp",
"certificateType" = a."certificateType",
"host" = a."host",
"letsEncryptEmail" = a."letsEncryptEmail",
"sshPrivateKey" = a."sshPrivateKey",
"enableDockerCleanup" = a."enableDockerCleanup",
"enableLogRotation" = a."enableLogRotation",
"enablePaidFeatures" = a."enablePaidFeatures",
"metricsConfig" = a."metricsConfig",
"cleanupCacheApplications" = a."cleanupCacheApplications",
"cleanupCacheOnPreviews" = a."cleanupCacheOnPreviews",
"cleanupCacheOnCompose" = a."cleanupCacheOnCompose"
FROM "admin" a
WHERE u.id = a."authId";
-- Actualizar referencias en las tablas relacionadas
UPDATE "project" p
SET "userId" = a."authId"
FROM "admin" a
WHERE p."userId" = a."adminId";
UPDATE "destination" d
SET "userId" = a."authId"
FROM "admin" a
WHERE d."userId" = a."adminId";
UPDATE "certificate" c
SET "userId" = a."authId"
FROM "admin" a
WHERE c."userId" = a."adminId";
UPDATE "registry" r
SET "userId" = a."authId"
FROM "admin" a
WHERE r."userId" = a."adminId";
UPDATE "notification" n
SET "userId" = a."authId"
FROM "admin" a
WHERE n."userId" = a."adminId";
UPDATE "ssh-key" s
SET "userId" = a."authId"
FROM "admin" a
WHERE s."userId" = a."adminId";
UPDATE "git_provider" g
SET "userId" = a."authId"
FROM "admin" a
WHERE g."userId" = a."adminId";
UPDATE "server" s
SET "userId" = a."authId"
FROM "admin" a
WHERE s."userId" = a."adminId";
-- Ahora agregar las restricciones NOT NULL después de migrar los datos
ALTER TABLE "user" ALTER COLUMN "name" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "email" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "email_verified" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "updated_at" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "certificateType" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "enableDockerCleanup" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "enableLogRotation" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "enablePaidFeatures" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "metricsConfig" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "cleanupCacheApplications" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "cleanupCacheOnPreviews" SET NOT NULL;
ALTER TABLE "user" ALTER COLUMN "cleanupCacheOnCompose" SET NOT NULL;
-- Modificar session
ALTER TABLE "session" ALTER COLUMN "expires_at" SET DATA TYPE timestamp;
ALTER TABLE "session" ADD COLUMN "token" text;
ALTER TABLE "session" ADD COLUMN "created_at" timestamp;
ALTER TABLE "session" ADD COLUMN "updated_at" timestamp;
ALTER TABLE "session" ADD COLUMN "ip_address" text;
ALTER TABLE "session" ADD COLUMN "user_agent" text;
ALTER TABLE "session" ADD COLUMN "impersonated_by" text;
-- Agregar nuevas restricciones después de migrar todos los datos
ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "project" ADD CONSTRAINT "project_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "destination" ADD CONSTRAINT "destination_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "certificate" ADD CONSTRAINT "certificate_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "registry" ADD CONSTRAINT "registry_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "notification" ADD CONSTRAINT "notification_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "ssh-key" ADD CONSTRAINT "ssh-key_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "git_provider" ADD CONSTRAINT "git_provider_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "server" ADD CONSTRAINT "server_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
-- Agregar restricciones únicas
ALTER TABLE "user" ADD CONSTRAINT "user_email_unique" UNIQUE("email");
ALTER TABLE "session" ADD CONSTRAINT "session_token_unique" UNIQUE("token");
-- Eliminar columnas antiguas
ALTER TABLE "user" DROP COLUMN IF EXISTS "adminId";
ALTER TABLE "user" DROP COLUMN IF EXISTS "authId";
-- Eliminar columnas de admin
ALTER TABLE "admin" DROP COLUMN IF EXISTS "adminId";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "serverIp";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "certificateType";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "host";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "letsEncryptEmail";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "sshPrivateKey";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "enableDockerCleanup";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "enableLogRotation";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "authId";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "createdAt";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "stripeCustomerId";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "stripeSubscriptionId";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "serversQuantity";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "enablePaidFeatures";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "metricsConfig";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "cleanupCacheApplications";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "cleanupCacheOnPreviews";
ALTER TABLE "admin" DROP COLUMN IF EXISTS "cleanupCacheOnCompose";
-- Eliminar tablas antiguas
DROP TABLE IF EXISTS "auth" CASCADE;
DROP TABLE IF EXISTS "admin" CASCADE;

File diff suppressed because it is too large Load Diff

View File

@@ -463,6 +463,13 @@
"when": 1739087857244,
"tag": "0065_daily_zaladane",
"breakpoints": true
},
{
"idx": 66,
"version": "7",
"when": 1739142819089,
"tag": "0066_broad_marrow",
"breakpoints": true
}
]
}

4
apps/dokploy/lib/auth.ts Normal file
View File

@@ -0,0 +1,4 @@
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({
baseURL: "http://localhost:3000", // the base url of your auth server
});

View File

@@ -35,6 +35,7 @@
"test": "vitest --config __test__/vitest.config.ts"
},
"dependencies": {
"better-auth":"1.1.16",
"bl": "6.0.11",
"rotating-file-stream": "3.2.3",
"qrcode": "^1.5.3",

View File

@@ -0,0 +1,7 @@
import { auth } from "@dokploy/server/index";
import { toNodeHandler } from "better-auth/node";
// Disallow body parsing, we will parse it manually
export const config = { api: { bodyParser: false } };
export default toNodeHandler(auth.handler);

View File

@@ -18,224 +18,224 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (!endpointSecret) {
return res.status(400).send("Webhook Error: Missing Stripe Secret Key");
}
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2024-09-30.acacia",
maxNetworkRetries: 3,
});
// if (!endpointSecret) {
// return res.status(400).send("Webhook Error: Missing Stripe Secret Key");
// }
// const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
// apiVersion: "2024-09-30.acacia",
// maxNetworkRetries: 3,
// });
const buf = await buffer(req);
const sig = req.headers["stripe-signature"] as string;
// const buf = await buffer(req);
// const sig = req.headers["stripe-signature"] as string;
let event: Stripe.Event;
// let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(buf, sig, endpointSecret);
} catch (err) {
console.error(
"Webhook signature verification failed.",
err instanceof Error ? err.message : err,
);
return res.status(400).send("Webhook Error: ");
}
// try {
// event = stripe.webhooks.constructEvent(buf, sig, endpointSecret);
// } catch (err) {
// console.error(
// "Webhook signature verification failed.",
// err instanceof Error ? err.message : err,
// );
// return res.status(400).send("Webhook Error: ");
// }
const webhooksAllowed = [
"customer.subscription.created",
"customer.subscription.deleted",
"customer.subscription.updated",
"invoice.payment_succeeded",
"invoice.payment_failed",
"customer.deleted",
"checkout.session.completed",
];
// const webhooksAllowed = [
// "customer.subscription.created",
// "customer.subscription.deleted",
// "customer.subscription.updated",
// "invoice.payment_succeeded",
// "invoice.payment_failed",
// "customer.deleted",
// "checkout.session.completed",
// ];
if (!webhooksAllowed.includes(event.type)) {
return res.status(400).send("Webhook Error: Invalid Event Type");
}
// if (!webhooksAllowed.includes(event.type)) {
// return res.status(400).send("Webhook Error: Invalid Event Type");
// }
switch (event.type) {
case "checkout.session.completed": {
const session = event.data.object as Stripe.Checkout.Session;
const adminId = session?.metadata?.adminId as string;
// switch (event.type) {
// case "checkout.session.completed": {
// const session = event.data.object as Stripe.Checkout.Session;
// const adminId = session?.metadata?.adminId as string;
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string,
);
await db
.update(admins)
.set({
stripeCustomerId: session.customer as string,
stripeSubscriptionId: session.subscription as string,
serversQuantity: subscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(eq(admins.adminId, adminId))
.returning();
// const subscription = await stripe.subscriptions.retrieve(
// session.subscription as string,
// );
// await db
// .update(admins)
// .set({
// stripeCustomerId: session.customer as string,
// stripeSubscriptionId: session.subscription as string,
// serversQuantity: subscription?.items?.data?.[0]?.quantity ?? 0,
// })
// .where(eq(admins.adminId, adminId))
// .returning();
const admin = await findAdminById(adminId);
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
break;
}
case "customer.subscription.created": {
const newSubscription = event.data.object as Stripe.Subscription;
// const admin = await findAdminById(adminId);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
// const newServersQuantity = admin.serversQuantity;
// await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
// break;
// }
// case "customer.subscription.created": {
// const newSubscription = event.data.object as Stripe.Subscription;
await db
.update(admins)
.set({
stripeSubscriptionId: newSubscription.id,
stripeCustomerId: newSubscription.customer as string,
})
.where(eq(admins.stripeCustomerId, newSubscription.customer as string))
.returning();
// await db
// .update(admins)
// .set({
// stripeSubscriptionId: newSubscription.id,
// stripeCustomerId: newSubscription.customer as string,
// })
// .where(eq(admins.stripeCustomerId, newSubscription.customer as string))
// .returning();
break;
}
// break;
// }
case "customer.subscription.deleted": {
const newSubscription = event.data.object as Stripe.Subscription;
// case "customer.subscription.deleted": {
// const newSubscription = event.data.object as Stripe.Subscription;
await db
.update(admins)
.set({
stripeSubscriptionId: null,
serversQuantity: 0,
})
.where(eq(admins.stripeCustomerId, newSubscription.customer as string));
// await db
// .update(admins)
// .set({
// stripeSubscriptionId: null,
// serversQuantity: 0,
// })
// .where(eq(admins.stripeCustomerId, newSubscription.customer as string));
const admin = await findAdminByStripeCustomerId(
newSubscription.customer as string,
);
// const admin = await findAdminByStripeCustomerId(
// newSubscription.customer as string,
// );
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
await disableServers(admin.adminId);
break;
}
case "customer.subscription.updated": {
const newSubscription = event.data.object as Stripe.Subscription;
// await disableServers(admin.adminId);
// break;
// }
// case "customer.subscription.updated": {
// const newSubscription = event.data.object as Stripe.Subscription;
const admin = await findAdminByStripeCustomerId(
newSubscription.customer as string,
);
// const admin = await findAdminByStripeCustomerId(
// newSubscription.customer as string,
// );
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
if (newSubscription.status === "active") {
await db
.update(admins)
.set({
serversQuantity: newSubscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(
eq(admins.stripeCustomerId, newSubscription.customer as string),
);
// 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),
);
}
// 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;
}
case "invoice.payment_succeeded": {
const newInvoice = event.data.object as Stripe.Invoice;
// break;
// }
// case "invoice.payment_succeeded": {
// const newInvoice = event.data.object as Stripe.Invoice;
const suscription = await stripe.subscriptions.retrieve(
newInvoice.subscription as string,
);
// const suscription = await stripe.subscriptions.retrieve(
// newInvoice.subscription as string,
// );
if (suscription.status !== "active") {
console.log(
`Skipping invoice.payment_succeeded for subscription ${suscription.id} with status ${suscription.status}`,
);
break;
}
// if (suscription.status !== "active") {
// console.log(
// `Skipping invoice.payment_succeeded for subscription ${suscription.id} with status ${suscription.status}`,
// );
// break;
// }
await db
.update(admins)
.set({
serversQuantity: suscription?.items?.data?.[0]?.quantity ?? 0,
})
.where(eq(admins.stripeCustomerId, suscription.customer as string));
// await db
// .update(admins)
// .set({
// serversQuantity: suscription?.items?.data?.[0]?.quantity ?? 0,
// })
// .where(eq(admins.stripeCustomerId, suscription.customer as string));
const admin = await findAdminByStripeCustomerId(
suscription.customer as string,
);
// const admin = await findAdminByStripeCustomerId(
// suscription.customer as string,
// );
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
const newServersQuantity = admin.serversQuantity;
await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
break;
}
case "invoice.payment_failed": {
const newInvoice = event.data.object as Stripe.Invoice;
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
// const newServersQuantity = admin.serversQuantity;
// await updateServersBasedOnQuantity(admin.adminId, newServersQuantity);
// break;
// }
// case "invoice.payment_failed": {
// const newInvoice = event.data.object as Stripe.Invoice;
const subscription = await stripe.subscriptions.retrieve(
newInvoice.subscription as string,
);
// const subscription = await stripe.subscriptions.retrieve(
// newInvoice.subscription as string,
// );
if (subscription.status !== "active") {
const admin = await findAdminByStripeCustomerId(
newInvoice.customer as string,
);
// 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));
// 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;
}
// break;
// }
case "customer.deleted": {
const customer = event.data.object as Stripe.Customer;
// case "customer.deleted": {
// const customer = event.data.object as Stripe.Customer;
const admin = await findAdminByStripeCustomerId(customer.id);
if (!admin) {
return res.status(400).send("Webhook Error: Admin not found");
}
// const admin = await findAdminByStripeCustomerId(customer.id);
// if (!admin) {
// return res.status(400).send("Webhook Error: Admin not found");
// }
await disableServers(admin.adminId);
await db
.update(admins)
.set({
stripeCustomerId: null,
stripeSubscriptionId: null,
serversQuantity: 0,
})
.where(eq(admins.stripeCustomerId, customer.id));
// await disableServers(admin.adminId);
// await db
// .update(admins)
// .set({
// stripeCustomerId: null,
// stripeSubscriptionId: null,
// serversQuantity: 0,
// })
// .where(eq(admins.stripeCustomerId, customer.id));
break;
}
default:
console.log(`Unhandled event type: ${event.type}`);
}
// break;
// }
// default:
// console.log(`Unhandled event type: ${event.type}`);
// }
return res.status(200).json({ received: true });
}

View File

@@ -70,9 +70,9 @@ import type {
} from "next";
import Head from "next/head";
import { useRouter } from "next/router";
import { useMemo, useState, type ReactElement } from "react";
import superjson from "superjson";
import { type ReactElement, useMemo, useState } from "react";
import { toast } from "sonner";
import superjson from "superjson";
export type Services = {
appName: string;

View File

@@ -201,51 +201,51 @@ Home.getLayout = (page: ReactElement) => {
return <OnboardingLayout>{page}</OnboardingLayout>;
};
export async function getServerSideProps(context: GetServerSidePropsContext) {
if (IS_CLOUD) {
try {
const { user } = await validateRequest(context.req, context.res);
// if (IS_CLOUD) {
// try {
// const { user } = await validateRequest(context.req, context.res);
if (user) {
return {
redirect: {
permanent: true,
destination: "/dashboard/projects",
},
};
}
} catch (error) {}
// if (user) {
// return {
// redirect: {
// permanent: true,
// destination: "/dashboard/projects",
// },
// };
// }
// } catch (error) {}
return {
props: {
IS_CLOUD: IS_CLOUD,
},
};
}
const hasAdmin = await isAdminPresent();
// return {
// props: {
// IS_CLOUD: IS_CLOUD,
// },
// };
// }
// const hasAdmin = await isAdminPresent();
if (!hasAdmin) {
return {
redirect: {
permanent: true,
destination: "/register",
},
};
}
// if (!hasAdmin) {
// return {
// redirect: {
// permanent: true,
// destination: "/register",
// },
// };
// }
const { user } = await validateRequest(context.req, context.res);
// const { user } = await validateRequest(context.req, context.res);
if (user) {
return {
redirect: {
permanent: true,
destination: "/dashboard/projects",
},
};
}
// if (user) {
// return {
// redirect: {
// permanent: true,
// destination: "/dashboard/projects",
// },
// };
// }
return {
props: {
hasAdmin,
// hasAdmin,
},
};
}

View File

@@ -34,14 +34,14 @@ void app.prepare().then(async () => {
});
// WEBSOCKET
setupDrawerLogsWebSocketServer(server);
setupDeploymentLogsWebSocketServer(server);
setupDockerContainerLogsWebSocketServer(server);
setupDockerContainerTerminalWebSocketServer(server);
setupTerminalWebSocketServer(server);
if (!IS_CLOUD) {
setupDockerStatsMonitoringSocketServer(server);
}
// setupDrawerLogsWebSocketServer(server);
// setupDeploymentLogsWebSocketServer(server);
// setupDockerContainerLogsWebSocketServer(server);
// setupDockerContainerTerminalWebSocketServer(server);
// setupTerminalWebSocketServer(server);
// if (!IS_CLOUD) {
// setupDockerStatsMonitoringSocketServer(server);
// }
if (process.env.NODE_ENV === "production" && !IS_CLOUD) {
setupDirectories();