diff --git a/apps/platform/trpc/routers/spaceRouter/spaceSettingsRouter.ts b/apps/platform/trpc/routers/spaceRouter/spaceSettingsRouter.ts index 4023efe5..3f8d053c 100644 --- a/apps/platform/trpc/routers/spaceRouter/spaceSettingsRouter.ts +++ b/apps/platform/trpc/routers/spaceRouter/spaceSettingsRouter.ts @@ -1,12 +1,12 @@ +import { iCanHazCallerFactory } from '../orgRouter/iCanHaz/iCanHazRouter'; import { router, orgProcedure } from '~platform/trpc/trpc'; -import { z } from 'zod'; - import { spaceTypeArray } from '@u22n/utils/spaces'; import { isOrgMemberSpaceMember } from './utils'; import { spaces } from '@u22n/database/schema'; import { uiColors } from '@u22n/utils/colors'; import { eq, and } from '@u22n/database/orm'; import { TRPCError } from '@trpc/server'; +import { z } from 'zod'; export const spaceSettingsRouter = router({ getSpacesSettings: orgProcedure @@ -272,6 +272,34 @@ export const spaceSettingsRouter = router({ }); } + if ( + spaceMembershipResponse.isPersonalSpace && + input.spaceType === 'open' + ) { + throw new TRPCError({ + code: 'FORBIDDEN', + message: 'You cannot change the type of a personal space to open' + }); + } + + if ( + !spaceMembershipResponse.isPersonalSpace && + input.spaceType === 'private' + ) { + const iCanHazCaller = iCanHazCallerFactory(ctx); + const canHazSpace = await iCanHazCaller.space({ + orgShortcode: input.orgShortcode + }); + + if (input.spaceType === 'private' && !canHazSpace.private) { + throw new TRPCError({ + code: 'FORBIDDEN', + message: + 'You cannot change the type of a space to private on your current plan' + }); + } + } + try { await db .update(spaces) @@ -288,7 +316,7 @@ export const spaceSettingsRouter = router({ console.error(error); throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', - message: 'Error while updating Space description' + message: 'Error while updating Space type' }); } diff --git a/apps/platform/trpc/routers/spaceRouter/utils.ts b/apps/platform/trpc/routers/spaceRouter/utils.ts index b3cbd50a..d92be1f0 100644 --- a/apps/platform/trpc/routers/spaceRouter/utils.ts +++ b/apps/platform/trpc/routers/spaceRouter/utils.ts @@ -125,6 +125,7 @@ type IsOrgMemberSpaceMemberResponse = { role: SpaceMemberRole | null; spaceId: number; type: SpaceType; + isPersonalSpace: boolean; permissions: { canCreate: boolean; canRead: boolean; @@ -157,7 +158,8 @@ export async function isOrgMemberSpaceMember({ columns: { id: true, publicId: true, - type: true + type: true, + personalSpace: true }, with: { members: { @@ -231,6 +233,7 @@ export async function isOrgMemberSpaceMember({ role: null, spaceId: spaceQueryResponse.id, type: spaceQueryResponse.type, + isPersonalSpace: spaceQueryResponse.personalSpace, permissions: { canCreate: false, canRead: false, diff --git a/apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx b/apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx index 66e1a4c4..13d730f1 100644 --- a/apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx +++ b/apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx @@ -166,15 +166,20 @@ export default function SettingsPage() { - + {/* Show visibility field only if the space is not a personal space or if it is a personal space and it is open so that users can change the visibility */} + {!spaceSettings.settings.personalSpace || + spaceSettings.settings.type === 'open' ? ( + + ) : null} void; }) { const orgShortcode = useOrgShortcode(); @@ -483,7 +490,9 @@ function VisibilityField({ + className="hover:bg-base-3 w-full rounded-sm" + // Don't allow changing the visibility of a personal space to open + disabled={personalSpace === true}>
@@ -494,7 +503,7 @@ function VisibilityField({
- {canAddSpace?.private ? ( + {personalSpace === true || canAddSpace?.private ? (