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
|
|
|
|
2022-10-10 22:42:18 +02:00
|
|
|
export async function deleteAccount(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 },
|
|
|
|
select: { websiteUuid: true },
|
2022-09-01 20:11:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let websiteUuids = [];
|
|
|
|
|
|
|
|
if (websites.length > 0) {
|
2022-10-10 22:42:18 +02:00
|
|
|
websiteUuids = websites.map(a => a.websiteUuid);
|
2022-09-01 20:11:11 +02:00
|
|
|
}
|
2022-08-29 22:04:58 +02:00
|
|
|
|
|
|
|
return client
|
|
|
|
.$transaction([
|
|
|
|
client.pageview.deleteMany({
|
2022-10-10 22:42:18 +02:00
|
|
|
where: { session: { website: { userId } } },
|
2022-08-29 22:04:58 +02:00
|
|
|
}),
|
2022-10-10 22:42:18 +02:00
|
|
|
client.eventData.deleteMany({
|
|
|
|
where: { event: { session: { website: { userId } } } },
|
2022-08-29 22:04:58 +02:00
|
|
|
}),
|
|
|
|
client.event.deleteMany({
|
2022-10-10 22:42:18 +02:00
|
|
|
where: { session: { website: { userId } } },
|
2022-08-29 22:04:58 +02:00
|
|
|
}),
|
|
|
|
client.session.deleteMany({
|
2022-10-10 22:42:18 +02:00
|
|
|
where: { website: { userId } },
|
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
|
|
|
}),
|
|
|
|
client.account.delete({
|
|
|
|
where: {
|
2022-10-10 22:42:18 +02:00
|
|
|
id: userId,
|
2022-08-29 22:04:58 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
.then(async res => {
|
2022-10-26 05:17:13 +02:00
|
|
|
if (redis.enabled) {
|
2022-08-29 22:04:58 +02:00
|
|
|
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
|
|
|
}
|