From 05e599aaf1705e138d871a20f7444aa4a9cd017f Mon Sep 17 00:00:00 2001 From: Rahul Mishra Date: Wed, 11 Sep 2024 17:31:28 +0530 Subject: [PATCH] fix: personal spaces can be made private without a pro plan (#776) ## What does this PR do? Fixes ENG-140 Personal spaces can always be made private regardless of pro plan Once affected spaces are private again, we don't allow to open them to avoid confusion ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Chore (refactoring code, technical debt, workflow improvements) - [ ] Enhancement (small improvements) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ## Checklist ### Required - [ ] Read [Contributing Guide](https://github.com/un/inbox/blob/main/CONTRIBUTING.md) - [ ] Self-reviewed my own code - [ ] Tested my code in a local environment - [ ] Commented on my code in hard-to-understand areas - [ ] Checked for warnings, there are none - [ ] Removed all `console.logs` - [ ] Merged the latest changes from main onto my branch with `git pull origin main` - [ ] My changes don't cause any responsiveness issues ### Appreciated - [ ] If a UI change was made: Added a screen recording or screenshots to this PR - [ ] Updated the UnInbox Docs if changes were necessary --- .../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 ? (