umami/queries/admin/user/deleteUser.js

46 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-08-28 06:38:35 +02:00
import prisma from 'lib/prisma';
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;
const websites = await client.website.findMany({
where: { userId },
});
2022-08-29 22:04:58 +02:00
return client
.$transaction([
client.pageview.deleteMany({
where: { session: { website: { userId } } },
2022-08-29 22:04:58 +02:00
}),
client.eventData.deleteMany({
where: { event: { session: { website: { userId } } } },
2022-08-29 22:04:58 +02:00
}),
client.event.deleteMany({
where: { session: { website: { userId } } },
2022-08-29 22:04:58 +02:00
}),
client.session.deleteMany({
where: { website: { userId } },
2022-08-29 22:04:58 +02:00
}),
client.website.deleteMany({
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: {
id: userId,
2022-08-29 22:04:58 +02: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
}
}
return data;
2022-08-29 22:04:58 +02:00
});
2022-07-12 23:14:36 +02:00
}