Skip to content

Commit 705646b

Browse files
committed
feat: deleteUser
1 parent 30c6cc4 commit 705646b

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

scripts/deleteUser.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// deletes a user and related entities like subscription
2+
// by userId or email
3+
const path = require("path")
4+
5+
require("dotenv").config({ path: path.resolve(__dirname, "../.env.local") })
6+
7+
const { PrismaClient } = require("@prisma/client")
8+
9+
const prisma = new PrismaClient()
10+
11+
const args = process.argv.slice(2)
12+
13+
if (
14+
args.length !== 2 ||
15+
(!args.includes("--userId") && !args.includes("--email"))
16+
) {
17+
console.error(
18+
"Please provide exactly one flag: --userId or --email followed by the respective value."
19+
)
20+
process.exit(1)
21+
}
22+
23+
let userId, email
24+
25+
if (args.includes("--userId")) {
26+
userId = args[args.indexOf("--userId") + 1]
27+
if (!/^\d+$/.test(userId)) {
28+
console.error("Invalid userId provided.")
29+
process.exit(1)
30+
}
31+
} else if (args.includes("--email")) {
32+
email = args[args.indexOf("--email") + 1]
33+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
34+
console.error("Invalid email provided.")
35+
process.exit(1)
36+
}
37+
}
38+
39+
const deleteUser = async ({ userId, email }) => {
40+
try {
41+
let user
42+
if (userId) {
43+
user = await prisma.user.findUnique({
44+
where: { id: parseInt(userId, 10) },
45+
})
46+
} else if (email) {
47+
user = await prisma.user.findUnique({
48+
where: { email: email },
49+
})
50+
}
51+
52+
if (!user) {
53+
console.error("User not found.")
54+
process.exit(1)
55+
}
56+
57+
const userIdToDelete = user.id
58+
59+
// Delete related UserChecklistItems first to avoid foreign key constraints
60+
await prisma.userChecklistItem.deleteMany({
61+
where: { userId: userIdToDelete },
62+
})
63+
console.log(`Deleted userChecklistItem entries for user ${userIdToDelete}`)
64+
65+
// Delete user-related entities
66+
const relatedEntities = [
67+
{ model: "userChecklist", relation: "userId" },
68+
{ model: "subscription", relation: "userId" },
69+
{ model: "session", relation: "userId" },
70+
{ model: "token", relation: "userId" },
71+
{ model: "transaction", relation: "userId" },
72+
{ model: "quizResult", relation: "userId" },
73+
{ model: "contribution", relation: "userId" },
74+
]
75+
76+
for (const entity of relatedEntities) {
77+
const deleteManyMethod = prisma[entity.model].deleteMany
78+
if (deleteManyMethod) {
79+
await deleteManyMethod({
80+
where: { [entity.relation]: userIdToDelete },
81+
})
82+
console.log(
83+
`Deleted ${entity.model} entries for user ${userIdToDelete}`
84+
)
85+
} else {
86+
console.log(`No delete method found for ${entity.model}`)
87+
}
88+
}
89+
90+
// Delete the user
91+
await prisma.user.delete({
92+
where: { id: userIdToDelete },
93+
})
94+
95+
console.log(`Deleted user ${userIdToDelete}`)
96+
} catch (error) {
97+
if (error.code === "P2025") {
98+
console.log(`Entity not found: ${error.meta.cause}`)
99+
} else {
100+
console.error("An error occurred while deleting the user:", error)
101+
}
102+
} finally {
103+
await prisma.$disconnect()
104+
}
105+
}
106+
107+
if (userId) {
108+
deleteUser({ userId })
109+
} else if (email) {
110+
deleteUser({ email })
111+
}

0 commit comments

Comments
 (0)