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(user_id) {
2022-08-28 06:38:35 +02:00
const { client } = prisma;
const websites = await client.website.findMany({
where: { user_id },
select: { website_uuid: true },
});
let websiteUuids = [];
if (websites.length > 0) {
websiteUuids = websites.map(a => a.website_uuid);
}
2022-08-29 22:04:58 +02:00
return client
.$transaction([
client.pageview.deleteMany({
where: { session: { website: { user_id } } },
}),
client.event_data.deleteMany({
where: { event: { session: { website: { user_id } } } },
}),
client.event.deleteMany({
where: { session: { website: { user_id } } },
}),
client.session.deleteMany({
where: { website: { user_id } },
}),
client.website.deleteMany({
where: { user_id },
}),
client.account.delete({
where: {
user_id,
},
}),
])
.then(async res => {
if (redis.client) {
for (let i = 0; i < websiteUuids.length; i++) {
await redis.client.set(`website:${websiteUuids[i]}`, DELETED);
}
}
return res;
});
2022-07-12 23:14:36 +02:00
}