Skip to content

Commit

Permalink
fix: Org Seeder - to better represent production scenario and fix adm…
Browse files Browse the repository at this point in the history
…in org delete cleanup (#15157)

* Fix seeder and update message to inform well about org deletion implication

* Cleanup tempOrgRedirect as well

---------

Co-authored-by: Keith Williams <[email protected]>
  • Loading branch information
hariombalhara and keithwillcode authored May 23, 2024
1 parent 8831ff1 commit 1a38169
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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
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,
},
})
)
);
}

0 comments on commit 1a38169

Please sign in to comment.