Skip to content

Commit

Permalink
refactor(core): update naming and fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Jun 20, 2024
1 parent 07da791 commit 8253827
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/libraries/organization-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class OrganizationInvitationLibrary {
});

if (entity.organizationRoles.length > 0) {
await organizationQueries.relations.rolesUsers.insert(
await organizationQueries.relations.usersRoles.insert(

Check warning on line 177 in packages/core/src/libraries/organization-invitation.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/organization-invitation.ts#L177

Added line #L177 was not covered by tests
...entity.organizationRoles.map((role) => ({
organizationId: entity.organizationId,
organizationRoleId: role.id,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/libraries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const createUserLibrary = (queries: Queries) => {
}))
);
if (data.length > 0) {
await organizationQueries.relations.rolesUsers.insert(...data);
await organizationQueries.relations.usersRoles.insert(...data);

Check warning on line 172 in packages/core/src/libraries/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/user.ts#L172

Added line #L172 was not covered by tests
}

return organizations;
Expand Down Expand Up @@ -235,7 +235,7 @@ export const createUserLibrary = (queries: Queries) => {
const usersRoles = await findUsersRolesByUserId(userId);
const rolesScopes = await findRolesScopesByRoleIds(usersRoles.map(({ roleId }) => roleId));
const organizationScopes = findFromOrganizations
? await organizations.relations.rolesUsers.getUserResourceScopes(
? await organizations.relations.usersRoles.getUserResourceScopes(
userId,
resourceIndicator,
organizationId
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/oidc/grants/refresh-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ describe('organization token grant', () => {

Sinon.stub(tenant.queries.organizations.relations.users, 'exists').resolves(true);
Sinon.stub(tenant.queries.applications, 'findApplicationById').resolves(mockApplication);
Sinon.stub(tenant.queries.organizations.relations.rolesUsers, 'getUserScopes').resolves([
Sinon.stub(tenant.queries.organizations.relations.usersRoles, 'getUserScopes').resolves([
{ tenantId: 'default', id: 'foo', name: 'foo', description: 'foo' },
{ tenantId: 'default', id: 'bar', name: 'bar', description: 'bar' },
{ tenantId: 'default', id: 'baz', name: 'baz', description: 'baz' },
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/oidc/grants/refresh-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export const buildHandler: (
/* === RFC 0001 === */
const audience = buildOrganizationUrn(organizationId);
/** All available scopes for the user in the organization. */
const availableScopes = await queries.organizations.relations.rolesUsers
const availableScopes = await queries.organizations.relations.usersRoles
.getUserScopes(organizationId, account.accountId)
.then((scopes) => scopes.map(({ name }) => name));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type CommonQueryMethods, sql } from '@silverhand/slonik';
import RelationQueries from '#src/utils/RelationQueries.js';
import { convertToIdentifiers } from '#src/utils/sql.js';

export class RoleApplicationRelationQueries extends RelationQueries<
export class ApplicationRoleRelationQueries extends RelationQueries<
[typeof Organizations, typeof OrganizationRoles, typeof Applications]
> {
constructor(pool: CommonQueryMethods) {
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/queries/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import { TwoRelationsQueries } from '#src/utils/RelationQueries.js';
import SchemaQueries from '#src/utils/SchemaQueries.js';
import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';

import { ApplicationRoleRelationQueries } from './application-role-relations.js';
import { EmailDomainQueries } from './email-domains.js';
import { RoleApplicationRelationQueries } from './role-application-relations.js';
import { RoleUserRelationQueries } from './role-user-relations.js';
import { UserRelationQueries } from './user-relations.js';
import { UserRoleRelationQueries } from './user-role-relations.js';

/**
* The schema field keys that can be used for searching roles.
Expand Down Expand Up @@ -284,19 +284,17 @@ export default class OrganizationQueries extends SchemaQueries<
),
/** Queries for organization - user relations. */
users: new UserRelationQueries(this.pool),
// TODO: Rename to `usersRoles`
/** Queries for organization - organization role - user relations. */
rolesUsers: new RoleUserRelationQueries(this.pool),
usersRoles: new UserRoleRelationQueries(this.pool),
/** Queries for organization - application relations. */
apps: new TwoRelationsQueries(
this.pool,
OrganizationApplicationRelations.table,
Organizations,
Applications
),
// TODO: Rename to `appsRoles`
/** Queries for organization - organization role - application relations. */
rolesApps: new RoleApplicationRelationQueries(this.pool),
appsRoles: new ApplicationRoleRelationQueries(this.pool),
invitationsRoles: new TwoRelationsQueries(
this.pool,
OrganizationInvitationRoleRelations.table,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { sql, type CommonQueryMethods } from '@silverhand/slonik';
import RelationQueries from '#src/utils/RelationQueries.js';
import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';

export class RoleUserRelationQueries extends RelationQueries<
export class UserRoleRelationQueries extends RelationQueries<
[typeof Organizations, typeof OrganizationRoles, typeof Users]
> {
constructor(pool: CommonQueryMethods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async function handleSubmitRegister(
// This is only for Cloud integration tests and data alignment, OSS still uses the legacy Management API user role.
const organizationId = getTenantOrganizationId(defaultTenantId);
await organizations.relations.users.insert({ organizationId, userId: id });
await organizations.relations.rolesUsers.insert({
await organizations.relations.usersRoles.insert({
organizationId,
organizationRoleId: getTenantRole(TenantRole.Admin).id,
userId: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function applicationRoleRelationRoutes(
async (ctx, next) => {
const { id, applicationId } = ctx.guard.params;

const [totalCount, entities] = await organizations.relations.rolesApps.getEntities(
const [totalCount, entities] = await organizations.relations.appsRoles.getEntities(

Check warning on line 47 in packages/core/src/routes/organization/index.application-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.application-role-relations.ts#L47

Added line #L47 was not covered by tests
OrganizationRoles,
{
organizationId: id,
Expand All @@ -71,7 +71,7 @@ export default function applicationRoleRelationRoutes(
const { id, applicationId } = ctx.guard.params;
const { organizationRoleIds } = ctx.guard.body;

await organizations.relations.rolesApps.insert(
await organizations.relations.appsRoles.insert(

Check warning on line 74 in packages/core/src/routes/organization/index.application-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.application-role-relations.ts#L74

Added line #L74 was not covered by tests
...organizationRoleIds.map((organizationRoleId) => ({
organizationId: id,
applicationId,
Expand All @@ -97,7 +97,7 @@ export default function applicationRoleRelationRoutes(
const { id, applicationId } = ctx.guard.params;
const { organizationRoleIds } = ctx.guard.body;

await organizations.relations.rolesApps.replace(id, applicationId, organizationRoleIds);
await organizations.relations.appsRoles.replace(id, applicationId, organizationRoleIds);

Check warning on line 100 in packages/core/src/routes/organization/index.application-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.application-role-relations.ts#L100

Added line #L100 was not covered by tests

ctx.status = 204;
return next();
Expand All @@ -113,7 +113,7 @@ export default function applicationRoleRelationRoutes(
async (ctx, next) => {
const { id, applicationId, organizationRoleId } = ctx.guard.params;

await organizations.relations.rolesApps.delete({
await organizations.relations.appsRoles.delete({

Check warning on line 116 in packages/core/src/routes/organization/index.application-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.application-role-relations.ts#L116

Added line #L116 was not covered by tests
organizationId: id,
applicationId,
organizationRoleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@
"description": "The role was removed from the application in the organization successfully."
},
"422": {
"description": "The role could not be removed. The role may not exist."
"description": "The application is not associated with the organization."
},
"404": {
"description": "Cannot find the record to delete."
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function emailDomainRoutes(
`${pathname}/:emailDomain`,
koaGuard({
params: z.object({ ...params, emailDomain: z.string().min(1) }),
status: [204],
status: [204, 404],
}),
async (ctx, next) => {
const { id, emailDomain } = ctx.guard.params;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/routes/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default function organizationRoutes<T extends ManagementApiRouter>(
const { id } = ctx.guard.params;
const { userIds, organizationRoleIds } = ctx.guard.body;

await organizations.relations.rolesUsers.insert(
await organizations.relations.usersRoles.insert(

Check warning on line 132 in packages/core/src/routes/organization/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.ts#L132

Added line #L132 was not covered by tests
...organizationRoleIds.flatMap((roleId) =>
userIds.map((userId) => ({ organizationId: id, organizationRoleId: roleId, userId }))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function userRoleRelationRoutes(
async (ctx, next) => {
const { id, userId } = ctx.guard.params;

const [totalCount, entities] = await organizations.relations.rolesUsers.getEntities(
const [totalCount, entities] = await organizations.relations.usersRoles.getEntities(

Check warning on line 46 in packages/core/src/routes/organization/index.user-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.user-role-relations.ts#L46

Added line #L46 was not covered by tests
OrganizationRoles,
{
organizationId: id,
Expand All @@ -69,7 +69,7 @@ export default function userRoleRelationRoutes(
const { id, userId } = ctx.guard.params;
const { organizationRoleIds } = ctx.guard.body;

await organizations.relations.rolesUsers.insert(
await organizations.relations.usersRoles.insert(

Check warning on line 72 in packages/core/src/routes/organization/index.user-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.user-role-relations.ts#L72

Added line #L72 was not covered by tests
...organizationRoleIds.map((roleId) => ({
organizationId: id,
organizationRoleId: roleId,
Expand All @@ -93,7 +93,7 @@ export default function userRoleRelationRoutes(
const { id, userId } = ctx.guard.params;
const { organizationRoleIds } = ctx.guard.body;

await organizations.relations.rolesUsers.replace(id, userId, organizationRoleIds);
await organizations.relations.usersRoles.replace(id, userId, organizationRoleIds);

Check warning on line 96 in packages/core/src/routes/organization/index.user-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.user-role-relations.ts#L96

Added line #L96 was not covered by tests

ctx.status = 204;
return next();
Expand All @@ -109,7 +109,7 @@ export default function userRoleRelationRoutes(
async (ctx, next) => {
const { id, roleId, userId } = ctx.guard.params;

await organizations.relations.rolesUsers.delete({
await organizations.relations.usersRoles.delete({

Check warning on line 112 in packages/core/src/routes/organization/index.user-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.user-role-relations.ts#L112

Added line #L112 was not covered by tests
organizationId: id,
organizationRoleId: roleId,
userId,
Expand All @@ -130,7 +130,7 @@ export default function userRoleRelationRoutes(
async (ctx, next) => {
const { id, userId } = ctx.guard.params;

const scopes = await organizations.relations.rolesUsers.getUserScopes(id, userId);
const scopes = await organizations.relations.usersRoles.getUserScopes(id, userId);

Check warning on line 133 in packages/core/src/routes/organization/index.user-role-relations.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/organization/index.user-role-relations.ts#L133

Added line #L133 was not covered by tests

ctx.body = scopes;
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@
"204": {
"description": "The role was removed from the user successfully."
},
"422": {
"description": "The user is not a member of the organization."
},
"404": {
"description": "The user is not a member of the organization; or the user does not have the role."
"description": "Cannot find the record to delete."
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ devFeatureTest.describe('organization application APIs', () => {
);

const response = await organizationApi
.addApplicationRoles(organization.id, '0', [application.id])
.addApplicationRoles(organization.id, application.id, ['0'])
.catch((error: unknown) => error);
assert(response instanceof HTTPError);
expect(response.response.status).toBe(422);
Expand Down

0 comments on commit 8253827

Please sign in to comment.