mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
Merge pull request #4655 from logto-io/gao-refactor-queries
refactor(core): refactor organization queries
This commit is contained in:
commit
b2fb2f77f1
7 changed files with 36 additions and 75 deletions
|
@ -1,20 +0,0 @@
|
||||||
import {
|
|
||||||
type OrganizationRoleKeys,
|
|
||||||
OrganizationRoles,
|
|
||||||
type CreateOrganizationRole,
|
|
||||||
type OrganizationRole,
|
|
||||||
} from '@logto/schemas';
|
|
||||||
import { type CommonQueryMethods } from 'slonik';
|
|
||||||
|
|
||||||
import SchemaQueries from '#src/utils/SchemaQueries.js';
|
|
||||||
|
|
||||||
/** Class of queries for roles in the organization template. */
|
|
||||||
export default class OrganizationRoleQueries extends SchemaQueries<
|
|
||||||
OrganizationRoleKeys,
|
|
||||||
CreateOrganizationRole,
|
|
||||||
OrganizationRole
|
|
||||||
> {
|
|
||||||
constructor(pool: CommonQueryMethods) {
|
|
||||||
super(pool, OrganizationRoles);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
import {
|
|
||||||
type OrganizationScopeKeys,
|
|
||||||
OrganizationScopes,
|
|
||||||
type CreateOrganizationScope,
|
|
||||||
type OrganizationScope,
|
|
||||||
} from '@logto/schemas';
|
|
||||||
import { type CommonQueryMethods } from 'slonik';
|
|
||||||
|
|
||||||
import SchemaQueries from '#src/utils/SchemaQueries.js';
|
|
||||||
|
|
||||||
/** Class of queries for scopes in the organization template. */
|
|
||||||
export default class OrganizationScopeQueries extends SchemaQueries<
|
|
||||||
OrganizationScopeKeys,
|
|
||||||
CreateOrganizationScope,
|
|
||||||
OrganizationScope
|
|
||||||
> {
|
|
||||||
constructor(pool: CommonQueryMethods) {
|
|
||||||
super(pool, OrganizationScopes);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,9 +3,13 @@ import {
|
||||||
type CreateOrganization,
|
type CreateOrganization,
|
||||||
type OrganizationKeys,
|
type OrganizationKeys,
|
||||||
Organizations,
|
Organizations,
|
||||||
|
OrganizationRoles,
|
||||||
|
OrganizationScopes,
|
||||||
|
OrganizationRoleScopeRelations,
|
||||||
} from '@logto/schemas';
|
} from '@logto/schemas';
|
||||||
import { type CommonQueryMethods } from 'slonik';
|
import { type CommonQueryMethods } from 'slonik';
|
||||||
|
|
||||||
|
import RelationQueries from '#src/utils/RelationQueries.js';
|
||||||
import SchemaQueries from '#src/utils/SchemaQueries.js';
|
import SchemaQueries from '#src/utils/SchemaQueries.js';
|
||||||
|
|
||||||
export default class OrganizationQueries extends SchemaQueries<
|
export default class OrganizationQueries extends SchemaQueries<
|
||||||
|
@ -13,6 +17,22 @@ export default class OrganizationQueries extends SchemaQueries<
|
||||||
CreateOrganization,
|
CreateOrganization,
|
||||||
Organization
|
Organization
|
||||||
> {
|
> {
|
||||||
|
/** Queries for roles in the organization template. */
|
||||||
|
roles = new SchemaQueries(this.pool, OrganizationRoles);
|
||||||
|
/** Queries for scopes in the organization template. */
|
||||||
|
scopes = new SchemaQueries(this.pool, OrganizationScopes);
|
||||||
|
|
||||||
|
/** Queries for relations that connected with organization-related entities. */
|
||||||
|
relations = {
|
||||||
|
/** Queries for organization role - organization scope relations. */
|
||||||
|
rolesScopes: new RelationQueries(
|
||||||
|
this.pool,
|
||||||
|
OrganizationRoleScopeRelations.table,
|
||||||
|
OrganizationRoles.table,
|
||||||
|
OrganizationScopes.table
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
constructor(pool: CommonQueryMethods) {
|
constructor(pool: CommonQueryMethods) {
|
||||||
super(pool, Organizations);
|
super(pool, Organizations);
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,11 +37,16 @@ export default function organizationRoleRoutes<T extends AuthedRouter>(
|
||||||
...[
|
...[
|
||||||
originalRouter,
|
originalRouter,
|
||||||
{
|
{
|
||||||
queries: { organizationRoles, organizationRoleScopeRelations },
|
queries: {
|
||||||
|
organizations: {
|
||||||
|
roles,
|
||||||
|
relations: { rolesScopes },
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
]: RouterInitArgs<T>
|
]: RouterInitArgs<T>
|
||||||
) {
|
) {
|
||||||
const actions = new OrganizationRoleActions(organizationRoles);
|
const actions = new OrganizationRoleActions(roles);
|
||||||
const router = new SchemaRouter(OrganizationRoles, actions, { disabled: { post: true } });
|
const router = new SchemaRouter(OrganizationRoles, actions, { disabled: { post: true } });
|
||||||
|
|
||||||
/** Allows to carry an initial set of scopes for creating a new organization role. */
|
/** Allows to carry an initial set of scopes for creating a new organization role. */
|
||||||
|
@ -68,9 +73,7 @@ export default function organizationRoleRoutes<T extends AuthedRouter>(
|
||||||
const role = await actions.post(data);
|
const role = await actions.post(data);
|
||||||
|
|
||||||
if (scopeIds.length > 0) {
|
if (scopeIds.length > 0) {
|
||||||
await organizationRoleScopeRelations.insert(
|
await rolesScopes.insert(...scopeIds.map<[string, string]>((id) => [role.id, id]));
|
||||||
...scopeIds.map<[string, string]>((id) => [role.id, id])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = role;
|
ctx.body = role;
|
||||||
|
|
|
@ -1,20 +1,17 @@
|
||||||
import { UniqueIntegrityConstraintViolationError } from 'slonik';
|
import { UniqueIntegrityConstraintViolationError } from 'slonik';
|
||||||
|
|
||||||
import RequestError from '#src/errors/RequestError/index.js';
|
import RequestError from '#src/errors/RequestError/index.js';
|
||||||
import { MockTenant } from '#src/test-utils/tenant.js';
|
|
||||||
|
|
||||||
import { OrganizationScopeActions } from './organization-scopes.js';
|
import { OrganizationScopeActions } from './organization-scopes.js';
|
||||||
|
|
||||||
describe('OrganizationScopeActions', () => {
|
describe('OrganizationScopeActions', () => {
|
||||||
it('should throw RequestError if UniqueIntegrityConstraintViolationError is thrown inside', async () => {
|
it('should throw RequestError if UniqueIntegrityConstraintViolationError is thrown inside', async () => {
|
||||||
const tenantContext = new MockTenant(undefined, {
|
// @ts-expect-error for testing
|
||||||
organizationScopes: {
|
const actions = new OrganizationScopeActions({
|
||||||
insert: async () => {
|
insert: async () => {
|
||||||
throw new UniqueIntegrityConstraintViolationError(new Error('test'), 'unique');
|
throw new UniqueIntegrityConstraintViolationError(new Error('test'), 'unique');
|
||||||
},
|
},
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const actions = new OrganizationScopeActions(tenantContext.queries.organizationScopes);
|
|
||||||
|
|
||||||
await expect(actions.post({ name: 'test' })).rejects.toThrowError(
|
await expect(actions.post({ name: 'test' })).rejects.toThrowError(
|
||||||
new RequestError({ code: 'entity.duplicate_value_of_unique_field', field: 'name' })
|
new RequestError({ code: 'entity.duplicate_value_of_unique_field', field: 'name' })
|
||||||
|
|
|
@ -35,14 +35,13 @@ export default function organizationScopeRoutes<T extends AuthedRouter>(
|
||||||
...[
|
...[
|
||||||
originalRouter,
|
originalRouter,
|
||||||
{
|
{
|
||||||
queries: { organizationScopes },
|
queries: {
|
||||||
|
organizations: { scopes },
|
||||||
|
},
|
||||||
},
|
},
|
||||||
]: RouterInitArgs<T>
|
]: RouterInitArgs<T>
|
||||||
) {
|
) {
|
||||||
const router = new SchemaRouter(
|
const router = new SchemaRouter(OrganizationScopes, new OrganizationScopeActions(scopes));
|
||||||
OrganizationScopes,
|
|
||||||
new OrganizationScopeActions(organizationScopes)
|
|
||||||
);
|
|
||||||
|
|
||||||
originalRouter.use(router.routes());
|
originalRouter.use(router.routes());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
import {
|
|
||||||
OrganizationRoleScopeRelations,
|
|
||||||
OrganizationRoles,
|
|
||||||
OrganizationScopes,
|
|
||||||
} from '@logto/schemas';
|
|
||||||
import type { CommonQueryMethods } from 'slonik';
|
import type { CommonQueryMethods } from 'slonik';
|
||||||
|
|
||||||
import { type WellKnownCache } from '#src/caches/well-known.js';
|
import { type WellKnownCache } from '#src/caches/well-known.js';
|
||||||
|
@ -16,8 +11,6 @@ import { createHooksQueries } from '#src/queries/hooks.js';
|
||||||
import { createLogQueries } from '#src/queries/log.js';
|
import { createLogQueries } from '#src/queries/log.js';
|
||||||
import { createLogtoConfigQueries } from '#src/queries/logto-config.js';
|
import { createLogtoConfigQueries } from '#src/queries/logto-config.js';
|
||||||
import { createOidcModelInstanceQueries } from '#src/queries/oidc-model-instance.js';
|
import { createOidcModelInstanceQueries } from '#src/queries/oidc-model-instance.js';
|
||||||
import OrganizationRoleQueries from '#src/queries/organization-roles.js';
|
|
||||||
import OrganizationScopeQueries from '#src/queries/organization-scopes.js';
|
|
||||||
import OrganizationQueries from '#src/queries/organizations.js';
|
import OrganizationQueries from '#src/queries/organizations.js';
|
||||||
import { createPasscodeQueries } from '#src/queries/passcode.js';
|
import { createPasscodeQueries } from '#src/queries/passcode.js';
|
||||||
import { createResourceQueries } from '#src/queries/resource.js';
|
import { createResourceQueries } from '#src/queries/resource.js';
|
||||||
|
@ -28,7 +21,6 @@ import { createSignInExperienceQueries } from '#src/queries/sign-in-experience.j
|
||||||
import { createUserQueries } from '#src/queries/user.js';
|
import { createUserQueries } from '#src/queries/user.js';
|
||||||
import { createUsersRolesQueries } from '#src/queries/users-roles.js';
|
import { createUsersRolesQueries } from '#src/queries/users-roles.js';
|
||||||
import { createVerificationStatusQueries } from '#src/queries/verification-status.js';
|
import { createVerificationStatusQueries } from '#src/queries/verification-status.js';
|
||||||
import RelationQueries from '#src/utils/RelationQueries.js';
|
|
||||||
|
|
||||||
export default class Queries {
|
export default class Queries {
|
||||||
applications = createApplicationQueries(this.pool);
|
applications = createApplicationQueries(this.pool);
|
||||||
|
@ -51,16 +43,6 @@ export default class Queries {
|
||||||
domains = createDomainsQueries(this.pool);
|
domains = createDomainsQueries(this.pool);
|
||||||
dailyActiveUsers = createDailyActiveUsersQueries(this.pool);
|
dailyActiveUsers = createDailyActiveUsersQueries(this.pool);
|
||||||
organizations = new OrganizationQueries(this.pool);
|
organizations = new OrganizationQueries(this.pool);
|
||||||
/** Organization template scope queries. */
|
|
||||||
organizationScopes = new OrganizationScopeQueries(this.pool);
|
|
||||||
/** Organization template role queries. */
|
|
||||||
organizationRoles = new OrganizationRoleQueries(this.pool);
|
|
||||||
organizationRoleScopeRelations = new RelationQueries(
|
|
||||||
this.pool,
|
|
||||||
OrganizationRoleScopeRelations.table,
|
|
||||||
OrganizationRoles.table,
|
|
||||||
OrganizationScopes.table
|
|
||||||
);
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public readonly pool: CommonQueryMethods,
|
public readonly pool: CommonQueryMethods,
|
||||||
|
|
Loading…
Reference in a new issue