feat: v2 managed organizations (#19341)
* refactor: allow non unique PlatformBilling customerId * feat: add PlatformBilling managed organizations fields * feat: ManagedOrganization table * refactor: bill overdue based on teamId * refactor: restructure organizations module * wip: organizations endpoints * Revert "wip: organizations endpoints" This reverts commit 0e9e66fc74f31da436f12930c1b6597c650ed621. * refactor: unique index for managed organizations * wip: organizations endpoints * wip: create managed organization * remove unecessary membership check because we have guard * feat: create managed org * feat: get managed org and orgs * feat: update and delete managed orgs * wip: api key logic * feat: allow variable api key length * feat: refresh managed org api key * feat: create managed org OAuth clients using api key * finish merge main * chore: bump platform libraries * tests: fix and add more tests to api-auth.strategy.e2e * refactor: dont request managed org slug and handle metadata as object * revert: billing service and repository update overdue based on sub and customer ids * refactor: v2 OAuth client permissions (#19501) * refactor: v2 permissions as string array * refactor: frontend work with permissions as string * tests: test '*' permissions * fix:managed org creator have profile & test can fetch oauth client * fix: tests * fix: OAuthClientCard on frontend * fix: tests
This commit is contained in:
1 parent
e4ab8d87c4
commit
a44bcafcf2
192 files changed
+9103
-619
No files matched your search
@@ -1,12 +1,12 @@
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import type { PlatformOAuthClient } from "@calcom/prisma/client";
|
||||
import type { PlatformOAuthClientDto } from "@calcom/platform-types";
|
||||
import { EmptyScreen, Button } from "@calcom/ui";
|
||||
|
||||
import { OAuthClientCard } from "@components/settings/platform/oauth-clients/OAuthClientCard";
|
||||
|
||||
type OAuthClientsListProps = {
|
||||
oauthClients: PlatformOAuthClient[];
|
||||
oauthClients: PlatformOAuthClientDto[];
|
||||
isDeleting: boolean;
|
||||
handleDelete: (id: string) => Promise<void>;
|
||||
};
|
||||
|
||||
@@ -2,21 +2,20 @@ import { useRouter } from "next/navigation";
|
||||
import React from "react";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
import type { PERMISSION_MAP } from "@calcom/platform-constants";
|
||||
import { PERMISSIONS_GROUPED_MAP } from "@calcom/platform-constants";
|
||||
import type { Avatar } from "@calcom/prisma/client";
|
||||
import { Button, Icon, showToast } from "@calcom/ui";
|
||||
|
||||
import { hasPermission } from "../../../../../../packages/platform/utils/permissions";
|
||||
|
||||
type OAuthClientCardProps = {
|
||||
name: string;
|
||||
logo?: Avatar;
|
||||
redirectUris: string[];
|
||||
bookingRedirectUri: string | null;
|
||||
bookingCancelRedirectUri: string | null;
|
||||
bookingRescheduleRedirectUri: string | null;
|
||||
areEmailsEnabled: boolean;
|
||||
permissions: number;
|
||||
bookingRedirectUri: string | null | undefined;
|
||||
bookingCancelRedirectUri: string | null | undefined;
|
||||
bookingRescheduleRedirectUri: string | null | undefined;
|
||||
areEmailsEnabled: boolean | undefined;
|
||||
permissions: Array<keyof typeof PERMISSION_MAP>;
|
||||
lastItem: boolean;
|
||||
id: string;
|
||||
secret: string;
|
||||
@@ -43,26 +42,36 @@ export const OAuthClientCard = ({
|
||||
}: OAuthClientCardProps) => {
|
||||
const router = useRouter();
|
||||
|
||||
const clientPermissions = Object.values(PERMISSIONS_GROUPED_MAP).map((value, index) => {
|
||||
let permissionsMessage = "";
|
||||
const hasReadPermission = hasPermission(permissions, value.read);
|
||||
const hasWritePermission = hasPermission(permissions, value.write);
|
||||
const groupedPermissions = permissions.reduce<Record<string, { read: boolean; write: boolean }>>(
|
||||
(acc, permission) => {
|
||||
const [resource] = permission.split("_READ") as [string] | [string, string];
|
||||
const isRead = permission.endsWith("_READ");
|
||||
const isWrite = permission.endsWith("_WRITE");
|
||||
const key = resource.replace("_WRITE", "");
|
||||
|
||||
if (hasReadPermission || hasWritePermission) {
|
||||
permissionsMessage = hasReadPermission ? "read" : "write";
|
||||
}
|
||||
if (!acc[key]) acc[key] = { read: false, write: false };
|
||||
if (isRead) acc[key].read = true;
|
||||
if (isWrite) acc[key].write = true;
|
||||
|
||||
if (hasReadPermission && hasWritePermission) {
|
||||
permissionsMessage = "read/write";
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
const clientPermissions = Object.entries(groupedPermissions).map(([resource, { read, write }], index) => {
|
||||
const permissionTypes = [];
|
||||
if (read) permissionTypes.push("read");
|
||||
if (write) permissionTypes.push("write");
|
||||
|
||||
const permissionsMessage = permissionTypes.join("/");
|
||||
const groupedPermission = PERMISSIONS_GROUPED_MAP[resource as keyof typeof PERMISSIONS_GROUPED_MAP];
|
||||
const isLastItem = index === Object.keys(groupedPermissions).length - 1;
|
||||
|
||||
return (
|
||||
!!permissionsMessage && (
|
||||
<div key={value.read} className="relative text-sm">
|
||||
{permissionsMessage} {`${value.label}s`.toLocaleLowerCase()}
|
||||
{Object.values(PERMISSIONS_GROUPED_MAP).length === index + 1 ? " " : ", "}
|
||||
</div>
|
||||
)
|
||||
<div key={resource} className="relative text-sm">
|
||||
{permissionsMessage} {groupedPermission?.label.toLowerCase()}
|
||||
{!isLastItem && ", "}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
import type { ApiSuccessResponse } from "@calcom/platform-types";
|
||||
import type { PlatformOAuthClient } from "@calcom/prisma/client";
|
||||
import type { ApiSuccessResponse, PlatformOAuthClientDto } from "@calcom/platform-types";
|
||||
|
||||
export type ManagedUser = {
|
||||
id: number;
|
||||
@@ -15,7 +14,7 @@ export type ManagedUser = {
|
||||
};
|
||||
|
||||
export const useOAuthClients = () => {
|
||||
const query = useQuery<ApiSuccessResponse<PlatformOAuthClient[]>>({
|
||||
const query = useQuery<ApiSuccessResponse<PlatformOAuthClientDto[]>>({
|
||||
queryKey: ["oauth-clients"],
|
||||
queryFn: () => {
|
||||
return fetch("/api/v2/oauth-clients", {
|
||||
@@ -39,7 +38,7 @@ export const useOAuthClient = (clientId?: string) => {
|
||||
isSuccess,
|
||||
isFetchedAfterMount,
|
||||
refetch,
|
||||
} = useQuery<ApiSuccessResponse<PlatformOAuthClient>>({
|
||||
} = useQuery<ApiSuccessResponse<PlatformOAuthClientDto>>({
|
||||
queryKey: ["oauth-client", clientId],
|
||||
queryFn: () => {
|
||||
return fetch(`/api/v2/oauth-clients/${clientId}`, {
|
||||
|
||||
@@ -5,9 +5,9 @@ import type {
|
||||
ApiResponse,
|
||||
CreateOAuthClientInput,
|
||||
DeleteOAuthClientInput,
|
||||
PlatformOAuthClientDto,
|
||||
SubscribeTeamInput,
|
||||
} from "@calcom/platform-types";
|
||||
import type { OAuthClient } from "@calcom/prisma/client";
|
||||
|
||||
interface IPersistOAuthClient {
|
||||
onSuccess?: () => void;
|
||||
@@ -96,7 +96,7 @@ export const useDeleteOAuthClient = (
|
||||
},
|
||||
}
|
||||
) => {
|
||||
const mutation = useMutation<ApiResponse<OAuthClient>, unknown, DeleteOAuthClientInput>({
|
||||
const mutation = useMutation<ApiResponse<PlatformOAuthClientDto>, unknown, DeleteOAuthClientInput>({
|
||||
mutationFn: (data) => {
|
||||
const { id } = data;
|
||||
return fetch(`/api/v2/oauth-clients/${id}`, {
|
||||
|
||||
@@ -17,19 +17,6 @@ import { useGetUserAttributes } from "@components/settings/platform/hooks/useGet
|
||||
import type { FormValues } from "@components/settings/platform/oauth-clients/oauth-client-form";
|
||||
import { OAuthClientForm as EditOAuthClientForm } from "@components/settings/platform/oauth-clients/oauth-client-form";
|
||||
|
||||
import {
|
||||
hasAppsReadPermission,
|
||||
hasAppsWritePermission,
|
||||
hasBookingReadPermission,
|
||||
hasBookingWritePermission,
|
||||
hasEventTypeReadPermission,
|
||||
hasEventTypeWritePermission,
|
||||
hasProfileReadPermission,
|
||||
hasProfileWritePermission,
|
||||
hasScheduleReadPermission,
|
||||
hasScheduleWritePermission,
|
||||
} from "../../../../../../../../packages/platform/utils/permissions";
|
||||
|
||||
export default function EditOAuthClient() {
|
||||
const { t } = useLocale();
|
||||
const router = useRouter();
|
||||
@@ -102,16 +89,16 @@ export default function EditOAuthClient() {
|
||||
bookingRedirectUri: data?.bookingRedirectUri ?? "",
|
||||
bookingCancelRedirectUri: data?.bookingCancelRedirectUri ?? "",
|
||||
bookingRescheduleRedirectUri: data?.bookingRescheduleRedirectUri ?? "",
|
||||
appsRead: hasAppsReadPermission(data?.permissions),
|
||||
appsWrite: hasAppsWritePermission(data?.permissions),
|
||||
bookingRead: hasBookingReadPermission(data?.permissions),
|
||||
bookingWrite: hasBookingWritePermission(data?.permissions),
|
||||
eventTypeRead: hasEventTypeReadPermission(data?.permissions),
|
||||
eventTypeWrite: hasEventTypeWritePermission(data?.permissions),
|
||||
profileRead: hasProfileReadPermission(data?.permissions),
|
||||
profileWrite: hasProfileWritePermission(data?.permissions),
|
||||
scheduleRead: hasScheduleReadPermission(data?.permissions),
|
||||
scheduleWrite: hasScheduleWritePermission(data?.permissions),
|
||||
appsRead: data?.permissions.includes("APPS_READ"),
|
||||
appsWrite: data?.permissions.includes("APPS_WRITE"),
|
||||
bookingRead: data?.permissions.includes("BOOKING_READ"),
|
||||
bookingWrite: data?.permissions.includes("BOOKING_WRITE"),
|
||||
eventTypeRead: data?.permissions.includes("EVENT_TYPE_READ"),
|
||||
eventTypeWrite: data?.permissions.includes("EVENT_TYPE_WRITE"),
|
||||
profileRead: data?.permissions.includes("PROFILE_READ"),
|
||||
profileWrite: data?.permissions.includes("PROFILE_WRITE"),
|
||||
scheduleRead: data?.permissions.includes("SCHEDULE_READ"),
|
||||
scheduleWrite: data?.permissions.includes("SCHEDULE_WRITE"),
|
||||
}}
|
||||
onSubmit={onSubmit}
|
||||
isPending={isUpdating}
|
||||
|
||||
@@ -6,6 +6,7 @@ import Shell from "@calcom/features/shell/Shell";
|
||||
import { ErrorCode } from "@calcom/lib/errorCodes";
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { PERMISSION_MAP } from "@calcom/platform-constants";
|
||||
import { PERMISSIONS_GROUPED_MAP } from "@calcom/platform-constants/permissions";
|
||||
import { showToast } from "@calcom/ui";
|
||||
|
||||
@@ -35,22 +36,20 @@ export default function CreateOAuthClient() {
|
||||
});
|
||||
|
||||
const onSubmit = (data: FormValues) => {
|
||||
let userPermissions = 0;
|
||||
const userPermissions: Array<keyof typeof PERMISSION_MAP> = [];
|
||||
const userRedirectUris = data.redirectUris.map((uri) => uri.uri).filter((uri) => !!uri);
|
||||
|
||||
Object.keys(PERMISSIONS_GROUPED_MAP).forEach((key) => {
|
||||
const entity = key as keyof typeof PERMISSIONS_GROUPED_MAP;
|
||||
const entityKey = PERMISSIONS_GROUPED_MAP[entity].key;
|
||||
const read = PERMISSIONS_GROUPED_MAP[entity].read;
|
||||
const write = PERMISSIONS_GROUPED_MAP[entity].write;
|
||||
if (data[`${entityKey}Read`]) userPermissions |= read;
|
||||
if (data[`${entityKey}Write`]) userPermissions |= write;
|
||||
|
||||
if (data[`${entityKey}Read`]) userPermissions.push(`${entity}_READ`);
|
||||
if (data[`${entityKey}Write`]) userPermissions.push(`${entity}_WRITE`);
|
||||
});
|
||||
|
||||
save({
|
||||
name: data.name,
|
||||
permissions: userPermissions,
|
||||
// logo: data.logo,
|
||||
redirectUris: userRedirectUris,
|
||||
bookingRedirectUri: data.bookingRedirectUri,
|
||||
bookingCancelRedirectUri: data.bookingCancelRedirectUri,
|
||||
|
||||
Reference in new issue
Block a user