-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Common API - create common find one query #14720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6d38850
create common find one query
etiennejouan a2d978c
Rename
etiennejouan e7e2e16
init common exception handling
etiennejouan 283fa8a
fix integ test
etiennejouan 514cd3e
error handling
etiennejouan e245024
add a dedicated context retrieval logic in rest handler
etiennejouan 9fae5c7
reorder common
etiennejouan c351e9e
remove authenticated request on rest side
etiennejouan 362969f
improve authContext typing
etiennejouan 1ba0481
fix integration test
etiennejouan 469233b
fix after review
etiennejouan 3966eae
compute selected fields before common
etiennejouan eb11ee4
fix lint
etiennejouan 2818676
fix after review
etiennejouan a2f2054
add feature flag
etiennejouan d0664d1
fix integ test
etiennejouan 3cec720
gql codegen
etiennejouan a594f8d
fix seed
etiennejouan ee4a5ce
fix clickhouse version making fail breaking change ci
etiennejouan 53b0488
fix clickhouse version
etiennejouan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
3 changes: 3 additions & 0 deletions
3
...ngine/api/common/common-args-handlers/common-query-selected-fields/common-arg-handlers.ts
This file contains hidden or 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,3 @@ | ||
import { CommonSelectedFieldsHandler } from 'src/engine/api/common/common-args-handlers/common-query-selected-fields/common-selected-fields.handler'; | ||
|
||
export const CommonArgsHandlers = [CommonSelectedFieldsHandler]; |
140 changes: 140 additions & 0 deletions
140
...ommon/common-args-handlers/common-query-selected-fields/common-selected-fields.handler.ts
This file contains hidden or 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,140 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
|
||
import { FieldMetadataType, ObjectsPermissions } from 'twenty-shared/types'; | ||
import { isDefined } from 'twenty-shared/utils'; | ||
|
||
import { CommonSelectedFieldsResult } from 'src/engine/api/common/types/common-selected-fields-result.type'; | ||
import { | ||
Depth, | ||
MAX_DEPTH, | ||
} from 'src/engine/api/rest/input-factories/depth-input.factory'; | ||
import { getAllSelectableFields } from 'src/engine/api/utils/get-all-selectable-fields.utils'; | ||
import { ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps'; | ||
import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps'; | ||
import { isFieldMetadataEntityOfType } from 'src/engine/utils/is-field-metadata-of-type.util'; | ||
|
||
@Injectable() | ||
export class CommonSelectedFieldsHandler { | ||
computeFromDepth = ({ | ||
objectsPermissions, | ||
objectMetadataMaps, | ||
objectMetadataMapItem, | ||
depth, | ||
}: { | ||
objectsPermissions: ObjectsPermissions; | ||
objectMetadataMaps: ObjectMetadataMaps; | ||
objectMetadataMapItem: ObjectMetadataItemWithFieldMaps; | ||
depth: Depth | undefined; | ||
}): CommonSelectedFieldsResult => { | ||
const restrictedFields = | ||
objectsPermissions[objectMetadataMapItem.id].restrictedFields; | ||
|
||
const { relations, relationsSelectFields } = | ||
this.getRelationsAndRelationsSelectFields({ | ||
objectMetadataMaps, | ||
objectMetadataMapItem, | ||
objectsPermissions, | ||
depth, | ||
}); | ||
|
||
const selectableFields = getAllSelectableFields({ | ||
restrictedFields, | ||
objectMetadata: { | ||
objectMetadataMapItem, | ||
}, | ||
}); | ||
|
||
return { | ||
select: { | ||
...selectableFields, | ||
...relationsSelectFields, | ||
}, | ||
relations, | ||
aggregate: {}, | ||
}; | ||
}; | ||
|
||
private getRelationsAndRelationsSelectFields({ | ||
objectMetadataMaps, | ||
objectMetadataMapItem, | ||
objectsPermissions, | ||
depth, | ||
}: { | ||
objectMetadataMaps: ObjectMetadataMaps; | ||
objectMetadataMapItem: ObjectMetadataItemWithFieldMaps; | ||
objectsPermissions: ObjectsPermissions; | ||
depth: Depth | undefined; | ||
}) { | ||
if (!isDefined(depth) || depth === 0) { | ||
return { | ||
relations: {}, | ||
relationsSelectFields: {}, | ||
}; | ||
} | ||
|
||
let relations: { [key: string]: boolean | { [key: string]: boolean } } = {}; | ||
|
||
let relationsSelectFields: { | ||
[key: string]: | ||
| boolean | ||
| { [key: string]: boolean | { [key: string]: boolean } }; | ||
} = {}; | ||
|
||
for (const field of Object.values(objectMetadataMapItem.fieldsById)) { | ||
if (!isFieldMetadataEntityOfType(field, FieldMetadataType.RELATION)) | ||
continue; | ||
|
||
const relationTargetObjectMetadata = | ||
objectMetadataMaps.byId[field.relationTargetObjectMetadataId]; | ||
|
||
if (!isDefined(relationTargetObjectMetadata)) { | ||
throw new BadRequestException( | ||
`Object metadata relation target not found for relation creation payload`, | ||
); | ||
} | ||
const relationFieldSelectFields = getAllSelectableFields({ | ||
restrictedFields: | ||
objectsPermissions[relationTargetObjectMetadata.id].restrictedFields, | ||
objectMetadata: { | ||
objectMetadataMapItem: relationTargetObjectMetadata, | ||
}, | ||
}); | ||
|
||
if (Object.keys(relationFieldSelectFields).length === 0) continue; | ||
|
||
if ( | ||
depth === MAX_DEPTH && | ||
isDefined(field.relationTargetObjectMetadataId) | ||
) { | ||
const { | ||
relations: depth2Relations, | ||
relationsSelectFields: depth2RelationsSelectFields, | ||
} = this.getRelationsAndRelationsSelectFields({ | ||
objectMetadataMaps, | ||
objectMetadataMapItem: relationTargetObjectMetadata, | ||
objectsPermissions, | ||
depth: 1, | ||
}) as { | ||
relations: { [key: string]: boolean }; | ||
relationsSelectFields: { | ||
[key: string]: boolean; | ||
}; | ||
}; | ||
|
||
relations[field.name] = depth2Relations as { | ||
[key: string]: boolean; | ||
}; | ||
|
||
relationsSelectFields[field.name] = { | ||
...relationFieldSelectFields, | ||
...depth2RelationsSelectFields, | ||
}; | ||
} else { | ||
relations[field.name] = true; | ||
relationsSelectFields[field.name] = relationFieldSelectFields; | ||
} | ||
} | ||
|
||
return { relations, relationsSelectFields }; | ||
} | ||
} |
214 changes: 214 additions & 0 deletions
214
...nty-server/src/engine/api/common/common-query-runners/common-base-query-runner.service.ts
This file contains hidden or 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,214 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
|
||
import { isDefined } from 'twenty-shared/utils'; | ||
|
||
import { WorkspaceAuthContext } from 'src/engine/api/common/interfaces/workspace-auth-context.interface'; | ||
import { type ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface'; | ||
import { type IConnection } from 'src/engine/api/graphql/workspace-query-runner/interfaces/connection.interface'; | ||
import { type IEdge } from 'src/engine/api/graphql/workspace-query-runner/interfaces/edge.interface'; | ||
|
||
import { CommonSelectedFieldsHandler } from 'src/engine/api/common/common-args-handlers/common-query-selected-fields/common-selected-fields.handler'; | ||
import { CommonQueryNames } from 'src/engine/api/common/types/common-query-args.type'; | ||
import { OBJECTS_WITH_SETTINGS_PERMISSIONS_REQUIREMENTS } from 'src/engine/api/graphql/graphql-query-runner/constants/objects-with-settings-permissions-requirements'; | ||
import { ProcessNestedRelationsHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/process-nested-relations.helper'; | ||
import { QueryResultGettersFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/query-result-getters/query-result-getters.factory'; | ||
import { QueryRunnerArgsFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/query-runner-args.factory'; | ||
import { WorkspaceQueryHookService } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.service'; | ||
import { ApiKeyRoleService } from 'src/engine/core-modules/api-key/api-key-role.service'; | ||
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type'; | ||
import { type PermissionFlagType } from 'src/engine/metadata-modules/permissions/constants/permission-flag-type.constants'; | ||
import { | ||
PermissionsException, | ||
PermissionsExceptionCode, | ||
PermissionsExceptionMessage, | ||
} from 'src/engine/metadata-modules/permissions/permissions.exception'; | ||
import { PermissionsService } from 'src/engine/metadata-modules/permissions/permissions.service'; | ||
import { ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps'; | ||
import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps'; | ||
import { UserRoleService } from 'src/engine/metadata-modules/user-role/user-role.service'; | ||
import { WorkspacePermissionsCacheService } from 'src/engine/metadata-modules/workspace-permissions-cache/workspace-permissions-cache.service'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
|
||
@Injectable() | ||
export abstract class CommonBaseQueryRunnerService< | ||
Response extends | ||
| ObjectRecord | ||
| ObjectRecord[] | ||
| IConnection<ObjectRecord, IEdge<ObjectRecord>> | ||
| IConnection<ObjectRecord, IEdge<ObjectRecord>>[], | ||
> { | ||
@Inject() | ||
protected readonly workspaceQueryHookService: WorkspaceQueryHookService; | ||
@Inject() | ||
protected readonly queryRunnerArgsFactory: QueryRunnerArgsFactory; | ||
@Inject() | ||
protected readonly queryResultGettersFactory: QueryResultGettersFactory; | ||
@Inject() | ||
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager; | ||
@Inject() | ||
protected readonly processNestedRelationsHelper: ProcessNestedRelationsHelper; | ||
@Inject() | ||
protected readonly permissionsService: PermissionsService; | ||
@Inject() | ||
protected readonly userRoleService: UserRoleService; | ||
@Inject() | ||
protected readonly apiKeyRoleService: ApiKeyRoleService; | ||
@Inject() | ||
protected readonly selectedFieldsHandler: CommonSelectedFieldsHandler; | ||
@Inject() | ||
protected readonly workspacePermissionsCacheService: WorkspacePermissionsCacheService; | ||
|
||
public async prepareQueryRunnerContext({ | ||
authContext, | ||
objectMetadataItemWithFieldMaps, | ||
}: { | ||
authContext: WorkspaceAuthContext; | ||
objectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps; | ||
}) { | ||
if (objectMetadataItemWithFieldMaps.isSystem === true) { | ||
await this.validateSettingsPermissionsOnObjectOrThrow( | ||
authContext, | ||
objectMetadataItemWithFieldMaps, | ||
); | ||
} | ||
|
||
const workspace = authContext.workspace; | ||
|
||
const workspaceDataSource = | ||
await this.twentyORMGlobalManager.getDataSourceForWorkspace({ | ||
workspaceId: workspace.id, | ||
}); | ||
|
||
const { roleId } = await this.getRoleIdAndObjectsPermissions( | ||
authContext, | ||
workspace.id, | ||
); | ||
|
||
const repository = workspaceDataSource.getRepository( | ||
objectMetadataItemWithFieldMaps.nameSingular, | ||
false, | ||
roleId, | ||
authContext, | ||
); | ||
|
||
return { | ||
workspaceDataSource, | ||
repository, | ||
isExecutedByApiKey: isDefined(authContext.apiKey), | ||
roleId, | ||
shouldBypassPermissionChecks: false, | ||
}; | ||
} | ||
|
||
public async enrichResultsWithGettersAndHooks({ | ||
results, | ||
operationName, | ||
authContext, | ||
objectMetadataItemWithFieldMaps, | ||
objectMetadataMaps, | ||
}: { | ||
results: Response; | ||
operationName: CommonQueryNames; | ||
authContext: WorkspaceAuthContext; | ||
objectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps; | ||
objectMetadataMaps: ObjectMetadataMaps; | ||
}) { | ||
const resultWithGetters = await this.queryResultGettersFactory.create( | ||
results, | ||
objectMetadataItemWithFieldMaps, | ||
authContext.workspace.id, | ||
objectMetadataMaps, | ||
); | ||
|
||
await this.workspaceQueryHookService.executePostQueryHooks( | ||
authContext, | ||
objectMetadataItemWithFieldMaps.nameSingular, | ||
operationName, | ||
resultWithGetters, | ||
); | ||
|
||
return resultWithGetters; | ||
} | ||
|
||
private async validateSettingsPermissionsOnObjectOrThrow( | ||
authContext: WorkspaceAuthContext, | ||
objectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps, | ||
) { | ||
const workspace = authContext.workspace; | ||
|
||
if ( | ||
Object.keys(OBJECTS_WITH_SETTINGS_PERMISSIONS_REQUIREMENTS).includes( | ||
objectMetadataItemWithFieldMaps.nameSingular, | ||
) | ||
) { | ||
const permissionRequired: PermissionFlagType = | ||
OBJECTS_WITH_SETTINGS_PERMISSIONS_REQUIREMENTS[ | ||
objectMetadataItemWithFieldMaps.nameSingular as keyof typeof OBJECTS_WITH_SETTINGS_PERMISSIONS_REQUIREMENTS | ||
]; | ||
|
||
const userHasPermission = | ||
await this.permissionsService.userHasWorkspaceSettingPermission({ | ||
userWorkspaceId: authContext.userWorkspaceId, | ||
setting: permissionRequired, | ||
workspaceId: workspace.id, | ||
apiKeyId: authContext.apiKey?.id, | ||
}); | ||
|
||
if (!userHasPermission) { | ||
throw new PermissionsException( | ||
PermissionsExceptionMessage.PERMISSION_DENIED, | ||
PermissionsExceptionCode.PERMISSION_DENIED, | ||
); | ||
} | ||
} | ||
} | ||
|
||
private async getRoleIdAndObjectsPermissions( | ||
authContext: AuthContext, | ||
workspaceId: string, | ||
) { | ||
let roleId: string; | ||
|
||
if ( | ||
!isDefined(authContext.apiKey) && | ||
!isDefined(authContext.userWorkspaceId) | ||
) { | ||
throw new PermissionsException( | ||
PermissionsExceptionMessage.NO_AUTHENTICATION_CONTEXT, | ||
PermissionsExceptionCode.NO_AUTHENTICATION_CONTEXT, | ||
); | ||
} | ||
|
||
if (isDefined(authContext.apiKey)) { | ||
roleId = await this.apiKeyRoleService.getRoleIdForApiKey( | ||
authContext.apiKey.id, | ||
workspaceId, | ||
); | ||
} else { | ||
const userWorkspaceRoleId = | ||
await this.userRoleService.getRoleIdForUserWorkspace({ | ||
userWorkspaceId: authContext.userWorkspaceId, | ||
workspaceId, | ||
}); | ||
|
||
if (!isDefined(userWorkspaceRoleId)) { | ||
throw new PermissionsException( | ||
PermissionsExceptionMessage.NO_ROLE_FOUND_FOR_USER_WORKSPACE, | ||
PermissionsExceptionCode.NO_ROLE_FOUND_FOR_USER_WORKSPACE, | ||
); | ||
} | ||
|
||
roleId = userWorkspaceRoleId; | ||
} | ||
|
||
const objectMetadataPermissions = | ||
await this.workspacePermissionsCacheService.getObjectRecordPermissionsForRoles( | ||
{ | ||
workspaceId: workspaceId, | ||
roleIds: [roleId], | ||
}, | ||
); | ||
|
||
return { roleId, objectsPermissions: objectMetadataPermissions[roleId] }; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.