"use server";
import { authOption } from "@/lib/auth";
import { db } from "@/lib/db";
import * as schema from "@/lib/db/schema";
import { eq } from "drizzle-orm";
import { getServerSession } from "next-auth";
import { zact } from "zact/server";
export const deleteAccount = zact()(async () => {
const session = await getServerSession(authOption);
if (!session || !session.user) {
throw new Error("Unauthorized");
}
const userId = session.user.id;
try {
const [userInfo] = await db
.select({ sheeId: schema.users.id, email: schema.users.email })
.from(schema.users)
.where(eq(schema.users.id, userId))
.limit(1);
const sheetId = userInfo.sheeId as string;
await db.transaction(async (tx) => {
//delete all users
const deleteUser = tx
.delete(schema.users)
.where(eq(schema.users.id, userId));
//delete all accounts
const deleteAccounts = tx
.delete(schema.accounts)
.where(eq(schema.accounts.userId, userId));
//delete all sessions
const deleteSessions = tx
.delete(schema.sessions)
.where(eq(schema.sessions.userId, userId));
//delete all question solved
const deleteQuestions = tx
.delete(schema.questions)
.where(eq(schema.questions.sheet_id, sheetId));
///remove all tracking questions
const deleteTrackingQuestions = tx
.delete(schema.trackingQuestions)
.where(eq(schema.trackingQuestions.userId, userId));
//remove all verification token
const deleteVerificationTokens = tx
.delete(schema.verificationTokens)
.where(
eq(schema.verificationTokens.identifier, userInfo.email as string)
);
//remove all reminders
"use server";
import { authOption } from "@/lib/auth";
import { db } from "@/lib/db";
import * as schema from "@/lib/db/schema";
import { eq } from "drizzle-orm";
import { getServerSession } from "next-auth";
import { zact } from "zact/server";
export const deleteAccount = zact()(async () => {
const session = await getServerSession(authOption);
if (!session || !session.user) {
throw new Error("Unauthorized");
}
const userId = session.user.id;
try {
const [userInfo] = await db
.select({ sheeId: schema.users.id, email: schema.users.email })
.from(schema.users)
.where(eq(schema.users.id, userId))
.limit(1);
const sheetId = userInfo.sheeId as string;
await db.transaction(async (tx) => {
//delete all users
const deleteUser = tx
.delete(schema.users)
.where(eq(schema.users.id, userId));
//delete all accounts
const deleteAccounts = tx
.delete(schema.accounts)
.where(eq(schema.accounts.userId, userId));
//delete all sessions
const deleteSessions = tx
.delete(schema.sessions)
.where(eq(schema.sessions.userId, userId));
//delete all question solved
const deleteQuestions = tx
.delete(schema.questions)
.where(eq(schema.questions.sheet_id, sheetId));
///remove all tracking questions
const deleteTrackingQuestions = tx
.delete(schema.trackingQuestions)
.where(eq(schema.trackingQuestions.userId, userId));
//remove all verification token
const deleteVerificationTokens = tx
.delete(schema.verificationTokens)
.where(
eq(schema.verificationTokens.identifier, userInfo.email as string)
);
//remove all reminders