47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import { Queue } from "quirrel/blitz";
|
|
|
|
import db, { MembershipRole } from "../../../../db";
|
|
import appLogger from "../../../../integrations/logger";
|
|
|
|
const logger = appLogger.child({ queue: "delete-user-data" });
|
|
|
|
type Payload = {
|
|
userId: string;
|
|
};
|
|
|
|
const deleteUserData = Queue<Payload>("api/queue/delete-user-data", async ({ userId }) => {
|
|
const user = await db.user.findFirst({
|
|
where: { id: userId },
|
|
include: {
|
|
memberships: {
|
|
include: {
|
|
organization: {
|
|
include: { memberships: { include: { user: true } } },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (!user) {
|
|
return;
|
|
}
|
|
|
|
switch (user.memberships[0]!.role) {
|
|
case MembershipRole.OWNER: {
|
|
const organization = user.memberships[0]!.organization;
|
|
await db.organization.delete({ where: { id: organization.id } });
|
|
await db.user.delete({ where: { id: user.id } });
|
|
break;
|
|
}
|
|
case MembershipRole.USER: {
|
|
await db.user.delete({ where: { id: user.id } });
|
|
break;
|
|
}
|
|
case MembershipRole.ADMIN:
|
|
// nothing to do here?
|
|
break;
|
|
}
|
|
});
|
|
|
|
export default deleteUserData;
|