2025-06-11 00:14:17 +02:00
|
|
|
import { WorkspaceMutationHandlerBase } from '@colanode/client/handlers/mutations/workspace-mutation-handler-base';
|
|
|
|
|
import { MutationHandler } from '@colanode/client/lib/types';
|
|
|
|
|
import { MutationError, MutationErrorCode } from '@colanode/client/mutations';
|
|
|
|
|
import {
|
|
|
|
|
SpaceCreateMutationInput,
|
|
|
|
|
SpaceCreateMutationOutput,
|
|
|
|
|
} from '@colanode/client/mutations/spaces/space-create';
|
2024-11-07 09:15:00 +01:00
|
|
|
import {
|
2024-12-02 00:44:02 +01:00
|
|
|
ChannelAttributes,
|
2024-12-01 23:51:33 +01:00
|
|
|
generateId,
|
|
|
|
|
IdType,
|
2024-11-07 09:15:00 +01:00
|
|
|
PageAttributes,
|
|
|
|
|
SpaceAttributes,
|
|
|
|
|
} from '@colanode/core';
|
2024-12-02 00:44:02 +01:00
|
|
|
|
2024-10-03 17:11:39 +02:00
|
|
|
export class SpaceCreateMutationHandler
|
2025-01-17 14:35:13 +01:00
|
|
|
extends WorkspaceMutationHandlerBase
|
2024-10-03 17:11:39 +02:00
|
|
|
implements MutationHandler<SpaceCreateMutationInput>
|
|
|
|
|
{
|
|
|
|
|
async handleMutation(
|
2024-11-07 09:15:00 +01:00
|
|
|
input: SpaceCreateMutationInput
|
2024-11-13 23:44:21 +01:00
|
|
|
): Promise<SpaceCreateMutationOutput> {
|
2025-11-11 07:00:14 -08:00
|
|
|
const workspace = this.getWorkspace(input.userId);
|
2024-11-11 13:45:07 +01:00
|
|
|
|
|
|
|
|
if (!workspace) {
|
2025-01-01 11:03:22 +01:00
|
|
|
throw new MutationError(
|
|
|
|
|
MutationErrorCode.WorkspaceNotFound,
|
|
|
|
|
'Workspace was not found or has been deleted.'
|
|
|
|
|
);
|
2024-11-11 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (workspace.role === 'guest' || workspace.role === 'none') {
|
2024-12-02 18:07:09 +01:00
|
|
|
throw new MutationError(
|
2025-01-01 11:03:22 +01:00
|
|
|
MutationErrorCode.SpaceCreateForbidden,
|
2024-12-02 18:07:09 +01:00
|
|
|
"You don't have permission to create spaces in this workspace."
|
|
|
|
|
);
|
2024-11-11 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-05 11:02:37 +02:00
|
|
|
const spaceId = generateId(IdType.Space);
|
2024-11-01 20:01:20 +01:00
|
|
|
const spaceAttributes: SpaceAttributes = {
|
|
|
|
|
type: 'space',
|
|
|
|
|
name: input.name,
|
2025-01-15 01:29:20 +01:00
|
|
|
visibility: 'private',
|
2024-11-01 20:01:20 +01:00
|
|
|
collaborators: {
|
2025-01-17 14:35:13 +01:00
|
|
|
[workspace.userId]: 'admin',
|
2024-11-01 20:01:20 +01:00
|
|
|
},
|
|
|
|
|
description: input.description,
|
2025-08-29 13:55:35 +02:00
|
|
|
avatar: input.avatar,
|
2024-10-25 19:02:47 +02:00
|
|
|
};
|
|
|
|
|
|
2025-02-05 22:40:35 +01:00
|
|
|
await workspace.nodes.createNode({
|
2024-12-30 21:50:08 +01:00
|
|
|
id: spaceId,
|
|
|
|
|
attributes: spaceAttributes,
|
2025-01-02 00:33:05 +01:00
|
|
|
parentId: null,
|
2024-12-30 21:50:08 +01:00
|
|
|
});
|
|
|
|
|
|
2024-10-15 23:12:52 +02:00
|
|
|
const pageId = generateId(IdType.Page);
|
2024-11-01 20:01:20 +01:00
|
|
|
const pageAttributes: PageAttributes = {
|
|
|
|
|
type: 'page',
|
|
|
|
|
name: 'Home',
|
|
|
|
|
parentId: spaceId,
|
2024-10-25 19:02:47 +02:00
|
|
|
};
|
|
|
|
|
|
2025-02-05 22:40:35 +01:00
|
|
|
await workspace.nodes.createNode({
|
2024-12-30 21:50:08 +01:00
|
|
|
id: pageId,
|
|
|
|
|
attributes: pageAttributes,
|
2025-01-02 00:33:05 +01:00
|
|
|
parentId: spaceId,
|
2024-12-30 21:50:08 +01:00
|
|
|
});
|
|
|
|
|
|
2024-10-15 23:12:52 +02:00
|
|
|
const channelId = generateId(IdType.Channel);
|
2024-11-01 20:01:20 +01:00
|
|
|
const channelAttributes: ChannelAttributes = {
|
|
|
|
|
type: 'channel',
|
|
|
|
|
name: 'Discussions',
|
|
|
|
|
parentId: spaceId,
|
2024-10-25 19:02:47 +02:00
|
|
|
};
|
|
|
|
|
|
2025-02-05 22:40:35 +01:00
|
|
|
await workspace.nodes.createNode({
|
2024-12-30 21:50:08 +01:00
|
|
|
id: channelId,
|
|
|
|
|
attributes: channelAttributes,
|
2025-01-02 00:33:05 +01:00
|
|
|
parentId: spaceId,
|
2024-12-30 21:50:08 +01:00
|
|
|
});
|
2024-10-03 17:11:39 +02:00
|
|
|
|
|
|
|
|
return {
|
2024-11-13 23:44:21 +01:00
|
|
|
id: spaceId,
|
2024-10-03 17:11:39 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|