Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Org Seeder - to better represent production scenario and fix admin org delete cleanup #15157

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/public/static/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@
"create_your_org_description": "Upgrade to Organizations and receive a subdomain, unified billing, Insights, extensive whitelabeling and more",
"create_your_enterprise_description": "Upgrade to Enterprise and get access to Active Directory Sync, SCIM Automatic User provisioning, Cal.ai Voice Agents, Admin APIs and more!",
"other_payment_app_enabled": "You can only enable one payment app per event type",
"admin_delete_organization_description": "<ul><li>Teams that are member of this organization will also be deleted along with their event-types</li><li>Users that were part of the organization will not be deleted and their event-types will also remain intact.</li><li>Usernames would be changed to allow them to exist outside the organization</li></ul>",
"admin_delete_organization_description": "<ul><li>Teams that are member of this organization will also be deleted along with their event-types</li><li>Users that were part of the organization will not be deleted but their usernames would be changed to allow them to exist outside the organization</li><li>User event-types created after the user was in the organization will be deleted</li><li>Migrated user event-types will not be deleted</li></ul>",
"admin_delete_organization_title": "Delete {{organizationName}}?",
"published": "Published",
"unpublished": "Unpublished",
Expand Down
17 changes: 16 additions & 1 deletion packages/prisma/seed.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated seeder to create TempOrgRedirect entries and set movedFromUser column

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import dailyMeta from "@calcom/app-store/dailyvideo/_metadata";
import googleMeetMeta from "@calcom/app-store/googlevideo/_metadata";
import zoomMeta from "@calcom/app-store/zoomvideo/_metadata";
import dayjs from "@calcom/dayjs";
import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains";
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
import { BookingStatus, MembershipRole, SchedulingType } from "@calcom/prisma/enums";
import { BookingStatus, MembershipRole, RedirectType, SchedulingType } from "@calcom/prisma/enums";
import type { Ensure } from "@calcom/types/utils";

import prisma from ".";
Expand Down Expand Up @@ -147,6 +148,15 @@ async function createOrganizationAndAddMembersAndTeams({
orgProfile: member.orgProfile,
};

await prisma.tempOrgRedirect.create({
data: {
fromOrgId: 0,
type: RedirectType.User,
from: member.memberData.username,
toUrl: `${getOrgFullOrigin(orgData.slug)}/${member.orgProfile.username}`,
},
});

orgMembersInDb.push(orgMemberInDb);
}
} catch (e) {
Expand Down Expand Up @@ -191,6 +201,11 @@ async function createOrganizationAndAddMembersAndTeams({
create: orgMembersInDb.map((member) => ({
uid: uuid(),
username: member.orgProfile.username,
movedFromUser: {
connect: {
id: member.id,
},
},
user: {
connect: {
id: member.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deleteDomain } from "@calcom/lib/domainManager/organization";
import logger from "@calcom/lib/logger";
import { prisma } from "@calcom/prisma";
import { RedirectType } from "@calcom/prisma/enums";

import { TRPCError } from "@trpc/server";

Expand Down Expand Up @@ -44,6 +45,8 @@ export const adminDeleteHandler = async ({ input }: AdminDeleteOption) => {
}
}

await deleteAllRedirectsForUsers(foundOrg.members.map((member) => member.user));

await renameUsersToAvoidUsernameConflicts(foundOrg.members.map((member) => member.user));
await prisma.team.delete({
where: {
Expand Down Expand Up @@ -79,3 +82,25 @@ async function renameUsersToAvoidUsernameConflicts(users: { id: number; username
});
}
}

async function deleteAllRedirectsForUsers(users: { username: string | null }[]) {
return await Promise.all(
users
.filter(
(
user
): user is {
username: string;
} => !!user.username
)
.map((user) =>
prisma.tempOrgRedirect.deleteMany({
where: {
from: user.username,
type: RedirectType.User,
fromOrgId: 0,
},
})
)
);
}
Loading