Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

1.6 KB
Raw Blame
import { parseApiError } from '@colanode/client/lib/ky';
import { MutationHandler } from '@colanode/client/lib/types';
import { MutationError, MutationErrorCode } from '@colanode/client/mutations';
import {
  WorkspaceDeleteMutationInput,
  WorkspaceDeleteMutationOutput,
} from '@colanode/client/mutations/workspaces/workspace-delete';
import { AppService } from '@colanode/client/services/app-service';
import { WorkspaceOutput } from '@colanode/core';

export class WorkspaceDeleteMutationHandler
  implements MutationHandler<WorkspaceDeleteMutationInput>
{
  private readonly app: AppService;

  constructor(app: AppService) {
    this.app = app;
  }

  async handleMutation(
    input: WorkspaceDeleteMutationInput
  ): Promise<WorkspaceDeleteMutationOutput> {
    const workspaceService = this.app.getWorkspace(input.userId);
    if (!workspaceService) {
      throw new MutationError(
        MutationErrorCode.WorkspaceNotFound,
        'Workspace not found.'
      );
    }

    const accountService = this.app.getAccount(workspaceService.accountId);
    if (!accountService) {
      throw new MutationError(
        MutationErrorCode.AccountNotFound,
        'Account not found or has been logged out.'
      );
    }

    try {
      const response = await accountService.client
        .delete(`v1/workspaces/${workspaceService.workspaceId}`)
        .json<WorkspaceOutput>();

      await workspaceService.delete();

      return {
        id: response.id,
      };
    } catch (error) {
      const apiError = await parseApiError(error);
      throw new MutationError(MutationErrorCode.ApiError, apiError.message);
    }
  }
}