umami/queries/admin/account/deleteAccount.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-28 06:38:35 +02:00
import prisma from 'lib/prisma';
2022-08-29 22:04:58 +02:00
import redis, { DELETED } from 'lib/redis';
2022-07-12 23:14:36 +02:00
export async function deleteAccount(userId) {
2022-08-28 06:38:35 +02:00
const { client } = prisma;
const websites = await client.website.findMany({
where: { userId },
select: { websiteUuid: true },
});
let websiteUuids = [];
if (websites.length > 0) {
websiteUuids = websites.map(a => a.websiteUuid);
}
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
}),
client.account.delete({
where: {
id: userId,
2022-08-29 22:04:58 +02:00
},
}),
])
.then(async res => {
if (redis.client) {
for (let i = 0; i < websiteUuids.length; i++) {
2022-10-07 00:00:16 +02:00
await redis.set(`website:${websiteUuids[i]}`, DELETED);
2022-08-29 22:04:58 +02:00
}
}
return res;
});
2022-07-12 23:14:36 +02:00
}