-
-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6083 from logto-io/gao-update-org-app-fetch
refactor(core): return roles in organization app get api
- Loading branch information
Showing
13 changed files
with
291 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/core/src/queries/organization/application-relations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
Organizations, | ||
Applications, | ||
OrganizationApplicationRelations, | ||
type Application, | ||
OrganizationRoles, | ||
OrganizationRoleApplicationRelations, | ||
type ApplicationWithOrganizationRoles, | ||
} from '@logto/schemas'; | ||
import { type CommonQueryMethods, sql } from '@silverhand/slonik'; | ||
|
||
import { type SearchOptions, buildSearchSql } from '#src/database/utils.js'; | ||
import { TwoRelationsQueries, type GetEntitiesOptions } from '#src/utils/RelationQueries.js'; | ||
import { convertToIdentifiers } from '#src/utils/sql.js'; | ||
|
||
import { type applicationSearchKeys } from '../application.js'; | ||
|
||
import { aggregateRoles } from './utils.js'; | ||
|
||
export class ApplicationRelationQueries extends TwoRelationsQueries< | ||
typeof Organizations, | ||
typeof Applications | ||
> { | ||
constructor(pool: CommonQueryMethods) { | ||
super(pool, OrganizationApplicationRelations.table, Organizations, Applications); | ||
} | ||
|
||
/** Get the applications of an organization with their organization roles. */ | ||
async getApplicationsByOrganizationId( | ||
organizationId: string, | ||
{ limit, offset }: GetEntitiesOptions, | ||
search?: SearchOptions<(typeof applicationSearchKeys)[number]> | ||
): Promise<[totalCount: number, applications: readonly Application[]]> { | ||
const roles = convertToIdentifiers(OrganizationRoles, true); | ||
const applications = convertToIdentifiers(Applications, true); | ||
const { fields } = convertToIdentifiers(OrganizationApplicationRelations, true); | ||
const relations = convertToIdentifiers(OrganizationRoleApplicationRelations, true); | ||
|
||
const [{ count }, entities] = await Promise.all([ | ||
this.pool.one<{ count: string }>(sql` | ||
select count(*) | ||
from ${this.table} | ||
left join ${applications.table} | ||
on ${fields.applicationId} = ${applications.fields.id} | ||
where ${fields.organizationId} = ${organizationId} | ||
${buildSearchSql(Applications, search, sql`and `)} | ||
`), | ||
this.pool.any<ApplicationWithOrganizationRoles>(sql` | ||
select | ||
${sql.join(Object.values(applications.fields), sql`, `)}, | ||
${aggregateRoles()} | ||
from ${this.table} | ||
left join ${applications.table} | ||
on ${fields.applicationId} = ${applications.fields.id} | ||
left join ${relations.table} | ||
on ${relations.fields.applicationId} = ${applications.fields.id} | ||
and ${relations.fields.organizationId} = ${fields.organizationId} | ||
left join ${roles.table} | ||
on ${relations.fields.organizationRoleId} = ${roles.fields.id} | ||
where ${fields.organizationId} = ${organizationId} | ||
${buildSearchSql(Applications, search, sql`and `)} | ||
group by ${applications.fields.id} | ||
limit ${limit} | ||
offset ${offset} | ||
`), | ||
]); | ||
|
||
return [Number(count), entities]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { OrganizationRoles } from '@logto/schemas'; | ||
import { sql } from '@silverhand/slonik'; | ||
|
||
import { convertToIdentifiers } from '#src/utils/sql.js'; | ||
|
||
/** | ||
* Build the SQL for aggregating the organization roles with basic information (id and name) | ||
* into a JSON array. | ||
* | ||
* @param as The alias of the aggregated roles. Defaults to `organizationRoles`. | ||
*/ | ||
export const aggregateRoles = (as = 'organizationRoles') => { | ||
const roles = convertToIdentifiers(OrganizationRoles, true); | ||
|
||
return sql` | ||
coalesce( | ||
json_agg( | ||
json_build_object( | ||
'id', ${roles.fields.id}, | ||
'name', ${roles.fields.name} | ||
) order by ${roles.fields.name} | ||
) filter (where ${roles.fields.id} is not null), -- left join could produce nulls as roles | ||
'[]' | ||
) as ${sql.identifier([as])} | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.