Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: personal spaces can be made private without a pro plan #776

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions apps/platform/trpc/routers/spaceRouter/spaceSettingsRouter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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'
});
}

Expand Down
5 changes: 4 additions & 1 deletion apps/platform/trpc/routers/spaceRouter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type IsOrgMemberSpaceMemberResponse = {
role: SpaceMemberRole | null;
spaceId: number;
type: SpaceType;
isPersonalSpace: boolean;
permissions: {
canCreate: boolean;
canRead: boolean;
Expand Down Expand Up @@ -157,7 +158,8 @@ export async function isOrgMemberSpaceMember({
columns: {
id: true,
publicId: true,
type: true
type: true,
personalSpace: true
},
with: {
members: {
Expand Down Expand Up @@ -231,6 +233,7 @@ export async function isOrgMemberSpaceMember({
role: null,
spaceId: spaceQueryResponse.id,
type: spaceQueryResponse.type,
isPersonalSpace: spaceQueryResponse.personalSpace,
permissions: {
canCreate: false,
canRead: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ export default function SettingsPage() {
</div>
</div>
<ColorField
initialValue={spaceSettings?.settings?.color}
initialValue={spaceSettings.settings.color}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
/>
<VisibilityField
initialValue={spaceSettings?.settings?.type}
initialValue={spaceSettings.settings.type}
showSaved={setShowSaved}
isSpaceAdmin={isSpaceAdmin}
personalSpace={spaceSettings.settings.personalSpace}
/>
<Workflows
showSaved={setShowSaved}
Expand Down Expand Up @@ -433,10 +434,12 @@ function ColorField({
function VisibilityField({
initialValue,
showSaved,
personalSpace,
isSpaceAdmin
}: {
initialValue: string;
isSpaceAdmin: boolean;
personalSpace: boolean;
showSaved: (value: boolean) => void;
}) {
const orgShortcode = useOrgShortcode();
Expand Down Expand Up @@ -483,7 +486,9 @@ function VisibilityField({
<SelectContent>
<SelectItem
value="open"
className="hover:bg-base-3 w-full rounded-sm">
className="hover:bg-base-3 w-full rounded-sm"
// Don't allow changing the visibility of a personal space to open
disabled={personalSpace === true}>
<div className="flex flex-row items-center justify-start gap-4 rounded-md p-1">
<Globe className="h-6 w-6" />
<div className="flex grow flex-col justify-start gap-2 text-left">
Expand All @@ -494,7 +499,7 @@ function VisibilityField({
</div>
</div>
</SelectItem>
{canAddSpace?.private ? (
{personalSpace === true || canAddSpace?.private ? (
<SelectItem
value="private"
className="hover:bg-base-3 w-full rounded-sm">
Expand Down