2021-09-24 23:09:20 +00:00
|
|
|
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 } });
|
2021-09-24 23:56:25 +00:00
|
|
|
await db.user.delete({ where: { id: user.id } });
|
2021-09-24 23:09:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MembershipRole.USER: {
|
2021-09-24 23:56:25 +00:00
|
|
|
await db.user.delete({ where: { id: user.id } });
|
2021-09-24 23:09:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MembershipRole.ADMIN:
|
|
|
|
// nothing to do here?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default deleteUserData;
|