Skip to content

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

4.1 KB
Raw Blame
import { FastifyPluginCallbackZod } from 'fastify-type-provider-zod';
import { z } from 'zod/v4';

import {
  ApiErrorCode,
  UserStatus,
  userRoleUpdateInputSchema,
  apiErrorOutputSchema,
  userOutputSchema,
  WorkspaceStatus,
} from '@colanode/core';
import { database } from '@colanode/server/data/database';
import { eventBus } from '@colanode/server/lib/event-bus';

export const userRoleUpdateRoute: FastifyPluginCallbackZod = (
  instance,
  _,
  done
) => {
  instance.route({
    method: 'PATCH',
    url: '/:userId/role',
    schema: {
      params: z.object({
        userId: z.string(),
      }),
      body: userRoleUpdateInputSchema,
      response: {
        200: userOutputSchema,
        400: apiErrorOutputSchema,
        403: apiErrorOutputSchema,
        404: apiErrorOutputSchema,
      },
    },
    handler: async (request, reply) => {
      const userId = request.params.userId;
      const input = request.body;
      const workspace = request.workspace;

      if (workspace.status === WorkspaceStatus.Readonly) {
        return reply.code(403).send({
          code: ApiErrorCode.WorkspaceReadonly,
          message: 'Workspace is readonly and you cannot update user roles.',
        });
      }

      if (workspace.user.role !== 'owner' && workspace.user.role !== 'admin') {
        return reply.code(403).send({
          code: ApiErrorCode.UserUpdateNoAccess,
          message: 'You do not have access to update users to this workspace.',
        });
      }

      const userToUpdate = await database
        .selectFrom('users')
        .selectAll()
        .where('id', '=', userId)
        .where('workspace_id', '=', workspace.id)
        .executeTakeFirst();

      if (!userToUpdate) {
        return reply.code(404).send({
          code: ApiErrorCode.UserNotFound,
          message: 'User not found.',
        });
      }

      if (userToUpdate.id === workspace.user.id) {
        return reply.code(403).send({
          code: ApiErrorCode.UserUpdateNoAccess,
          message: 'You cannot modify your own role.',
        });
      }

      if (userToUpdate.role === 'owner') {
        return reply.code(403).send({
          code: ApiErrorCode.UserUpdateNoAccess,
          message: 'The workspace owner role cannot be modified.',
        });
      }

      if (input.role === 'owner') {
        return reply.code(403).send({
          code: ApiErrorCode.UserUpdateNoAccess,
          message: 'The owner role cannot be assigned through this endpoint.',
        });
      }

      if (
        workspace.user.role !== 'owner' &&
        (userToUpdate.role === 'admin' || input.role === 'admin')
      ) {
        return reply.code(403).send({
          code: ApiErrorCode.UserUpdateNoAccess,
          message: 'Only the workspace owner can manage admins.',
        });
      }

      const status =
        input.role === 'none' ? UserStatus.Removed : UserStatus.Active;

      const updatedUser = await database
        .updateTable('users')
        .returningAll()
        .set({
          role: input.role,
          status,
          updated_at: new Date(),
          updated_by: request.account.id,
        })
        .where('id', '=', userToUpdate.id)
        .where('workspace_id', '=', workspace.id)
        .executeTakeFirst();

      if (!updatedUser) {
        return reply.code(400).send({
          code: ApiErrorCode.UserNotFound,
          message: 'User not found.',
        });
      }

      eventBus.publish({
        type: 'user.updated',
        userId: userToUpdate.id,
        accountId: userToUpdate.account_id,
        workspaceId: userToUpdate.workspace_id,
      });

      return {
        id: updatedUser.id,
        email: updatedUser.email,
        name: updatedUser.name,
        avatar: updatedUser.avatar,
        role: updatedUser.role,
        customName: updatedUser.custom_name,
        customAvatar: updatedUser.custom_avatar,
        createdAt: updatedUser.created_at.toISOString(),
        updatedAt: updatedUser.updated_at?.toISOString() ?? null,
        revision: updatedUser.revision,
        status: updatedUser.status,
      };
    },
  });

  done();
};