Skip to content

Commit

Permalink
refactor(schemas): rename tenant member role to collaborator
Browse files Browse the repository at this point in the history
  • Loading branch information
charIeszhao committed Apr 2, 2024
1 parent 36003b8 commit 24b22c5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ function EditMemberModal({ user, isOpen, onClose }: Props) {
const { currentTenantId } = useContext(TenantsContext);

const [isLoading, setIsLoading] = useState(false);
const [role, setRole] = useState(TenantRole.Member);
const [role, setRole] = useState(TenantRole.Collaborator);
const cloudApi = useAuthedCloudApi();

const roleOptions: Array<Option<TenantRole>> = useMemo(
() => [
{ value: TenantRole.Admin, title: t('admin') },
{ value: TenantRole.Member, title: t('member') },
{ value: TenantRole.Collaborator, title: t('collaborator') },
],
[t]
);

const onSubmit = async () => {
setIsLoading(true);
try {
await cloudApi.put(`/api/tenants/:tenantId/members/:userId/roles`, {
params: { tenantId: currentTenantId, userId: user.id },
body: { roleName: role },
});
// TODO: @charles Uncomment later once the Cloud APIs are updated
// await cloudApi.put(`/api/tenants/:tenantId/members/:userId/roles`, {
// params: { tenantId: currentTenantId, userId: user.id },
// body: { roleName: role },
// });
onClose();
} finally {
setIsLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function InviteMemberModal({ isOpen, onClose }: Props) {
const formMethods = useForm<InviteMemberForm>({
defaultValues: {
emails: [],
role: TenantRole.Member,
role: TenantRole.Collaborator,
},
});

Expand All @@ -63,7 +63,7 @@ function InviteMemberModal({ isOpen, onClose }: Props) {
const roleOptions: Array<Option<TenantRole>> = useMemo(
() => [
{ value: TenantRole.Admin, title: t('admin') },
{ value: TenantRole.Member, title: t('member') },
{ value: TenantRole.Collaborator, title: t('collaborator') },
],
[t]
);
Expand All @@ -84,14 +84,15 @@ function InviteMemberModal({ isOpen, onClose }: Props) {
return;
}

await Promise.all(
emails.map(async (email) =>
cloudApi.post('/api/tenants/:tenantId/invitations', {
params: { tenantId: currentTenantId },
body: { invitee: email.value, roleName: role },
})
)
);
// TODO: @charles Uncomment later once the Cloud APIs are updated
// await Promise.all(
// emails.map(async (email) =>
// cloudApi.post('/api/tenants/:tenantId/invitations', {
// params: { tenantId: currentTenantId },
// body: { invitee: email.value, roleName: role },
// })
// )
// );
toast.success(t('messages.invitation_sent'));
onClose(true);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { sql } from '@silverhand/slonik';

import type { AlterationScript } from '../lib/types/alteration.js';

const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
update organization_roles
set id = 'collaborator', name = 'collaborator', description = 'Collaborator of the tenant, who has permissions to operate the tenant data, but not the tenant settings.'
where tenant_id = 'admin' and id = 'member';
update organization_role_scope_relations
set organization_role_id = 'collaborator'
where tenant_id = 'admin' and organization_role_id = 'member';
`);
},
down: async (pool) => {
await pool.query(sql`
update organization_roles
set id = 'member', name = 'member', description = 'Member of the tenant, who has permissions to operate the tenant data, but not the tenant settings.'
where tenant_id = 'admin' and id = 'collaborator';
update organization_role_scope_relations
set organization_role_id = 'member'
where tenant_id = 'admin' and organization_role_id = 'collaborator';
`);
},
};

export default alteration;
18 changes: 9 additions & 9 deletions packages/schemas/src/types/tenant-organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,29 @@ const tenantScopeDescriptions: Readonly<Record<TenantScope, string>> = Object.fr
export enum TenantRole {
/** Admin of the tenant, who has all permissions. */
Admin = 'admin',
/** Member of the tenant, who has permissions to operate the tenant data, but not the tenant settings. */
Member = 'member',
/** Collaborator of the tenant, who has permissions to operate the tenant data, but not the tenant settings. */
Collaborator = 'collaborator',
}

const tenantRoleDescriptions: Readonly<Record<TenantRole, string>> = Object.freeze({
[TenantRole.Admin]: 'Admin of the tenant, who has all permissions.',
[TenantRole.Member]:
'Member of the tenant, who has permissions to operate the tenant data, but not the tenant settings.',
[TenantRole.Collaborator]:
'Collaborator of the tenant, who has permissions to operate the tenant data, but not the tenant settings.',
});

/**
* Given a tenant role, return the corresponding organization role data in the admin tenant.
*
* @example
* ```ts
* const role = TenantRole.Member; // 'member'
* const role = TenantRole.Collaborator; // 'collaborator'
* const roleData = getTenantRole(role);
*
* expect(roleData).toEqual({
* tenantId: 'admin',
* id: 'member',
* name: 'member',
* description: 'Member of the tenant, who has permissions to operate the tenant data, but not the tenant settings.',
* id: 'collaborator',
* name: 'collaborator',
* description: 'Collaborator of the tenant, who has permissions to operate the tenant data, but not the tenant settings.',
* });
* ```
*
Expand All @@ -167,7 +167,7 @@ export const getTenantRole = (role: TenantRole): Readonly<OrganizationRole> =>
export const tenantRoleScopes: Readonly<Record<TenantRole, Readonly<TenantScope[]>>> =
Object.freeze({
[TenantRole.Admin]: allTenantScopes,
[TenantRole.Member]: [
[TenantRole.Collaborator]: [
TenantScope.ReadData,
TenantScope.WriteData,
TenantScope.DeleteData,
Expand Down

0 comments on commit 24b22c5

Please sign in to comment.