From e33c5212385f16209c26300cd703fd53a8bca9a8 Mon Sep 17 00:00:00 2001 From: BlankParticle Date: Wed, 11 Sep 2024 16:19:24 +0530 Subject: [PATCH] fix: personal spaces can be made private without a pro plan --- .../spaceRouter/spaceSettingsRouter.ts | 34 +++++++++++++++++-- .../trpc/routers/spaceRouter/utils.ts | 5 ++- .../[spaceShortcode]/settings/page.tsx | 13 ++++--- 3 files changed, 44 insertions(+), 8 deletions(-) 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..034cd3cc 100644 --- a/apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx +++ b/apps/web/src/app/[orgShortcode]/[spaceShortcode]/settings/page.tsx @@ -166,14 +166,15 @@ export default function SettingsPage() { void; }) { const orgShortcode = useOrgShortcode(); @@ -483,7 +486,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 +499,7 @@ function VisibilityField({
- {canAddSpace?.private ? ( + {personalSpace === true || canAddSpace?.private ? (