2022-08-28 04:38:35 +00:00
|
|
|
import prisma from 'lib/prisma';
|
2022-11-08 06:35:51 +00:00
|
|
|
import cache from 'lib/cache';
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2022-11-01 06:42:37 +00:00
|
|
|
export async function deleteUser(userId) {
|
2022-08-28 04:38:35 +00:00
|
|
|
const { client } = prisma;
|
|
|
|
|
|
2022-09-01 18:11:11 +00:00
|
|
|
const websites = await client.website.findMany({
|
2022-10-10 20:42:18 +00:00
|
|
|
where: { userId },
|
2022-09-01 18:11:11 +00:00
|
|
|
});
|
|
|
|
|
|
2022-11-08 19:55:02 +00:00
|
|
|
let websiteIds = [];
|
|
|
|
|
|
|
|
|
|
if (websites.length > 0) {
|
|
|
|
|
websiteIds = websites.map(a => a.id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 20:04:58 +00:00
|
|
|
return client
|
|
|
|
|
.$transaction([
|
2022-11-10 06:46:50 +00:00
|
|
|
client.websiteEvent.deleteMany({
|
2022-11-08 19:55:02 +00:00
|
|
|
where: { websiteId: { in: websiteIds } },
|
2022-08-29 20:04:58 +00:00
|
|
|
}),
|
|
|
|
|
client.session.deleteMany({
|
2022-11-08 19:55:02 +00:00
|
|
|
where: { websiteId: { in: websiteIds } },
|
2022-08-29 20:04:58 +00:00
|
|
|
}),
|
|
|
|
|
client.website.deleteMany({
|
2022-10-10 20:42:18 +00:00
|
|
|
where: { userId },
|
2022-08-29 20:04:58 +00:00
|
|
|
}),
|
2022-11-01 06:42:37 +00:00
|
|
|
client.user.delete({
|
2022-08-29 20:04:58 +00:00
|
|
|
where: {
|
2022-10-10 20:42:18 +00:00
|
|
|
id: userId,
|
2022-08-29 20:04:58 +00:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
])
|
2022-11-08 06:35:51 +00:00
|
|
|
.then(async data => {
|
|
|
|
|
if (cache.enabled) {
|
|
|
|
|
const ids = websites.map(a => a.id);
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < ids.length; i++) {
|
|
|
|
|
await cache.deleteWebsite(`website:${ids[i]}`);
|
2022-08-29 20:04:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 06:35:51 +00:00
|
|
|
return data;
|
2022-08-29 20:04:58 +00:00
|
|
|
});
|
2022-07-12 21:14:36 +00:00
|
|
|
}
|