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