Migrate role and role target to v2 (#16009)
# Introduction close https://github.com/twentyhq/core-team-issues/issues/1930 close https://github.com/twentyhq/core-team-issues/issues/1929 Migrating role and roleTarget entities to the v2 core engine, allowing v2 caching leverage and allow migrating agent to v2 that needs role target in prior After agent we should be able to pass twenty standard app totally though workspace migration ## Role target assignation Please note that role target have 3 creation entrypoints: - Agent - User workspace - ApiKey Refactored all 3 of them to pass through a new role-target.service.ts that consumes the v2 under the hood. --------- Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
import { ROLE_TARGET_FOREIGN_KEY_PROPERTIES } from 'src/engine/metadata-modules/flat-role-target/constants/role-target-foreign-key-properties.constant';
|
||||
import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';
|
||||
|
||||
export const FLAT_ROLE_TARGET_EDITABLE_PROPERTIES = [
|
||||
'roleId',
|
||||
...ROLE_TARGET_FOREIGN_KEY_PROPERTIES,
|
||||
] as const satisfies (keyof FlatRoleTarget)[];
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';
|
||||
|
||||
export const ROLE_TARGET_FOREIGN_KEY_PROPERTIES = [
|
||||
'userWorkspaceId',
|
||||
'apiKeyId',
|
||||
'agentId',
|
||||
] as const satisfies (keyof FlatRoleTarget)[];
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';
|
||||
import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
|
||||
import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';
|
||||
import { createEmptyFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-flat-entity-maps.constant';
|
||||
import { type FlatRoleTargetMaps } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target-maps.type';
|
||||
import { fromRoleTargetsEntityToFlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/utils/from-role-target-entity-to-flat-role-target.util';
|
||||
import { RoleTargetsEntity } from 'src/engine/metadata-modules/role/role-targets.entity';
|
||||
import { WorkspaceFlatMapCache } from 'src/engine/workspace-flat-map-cache/decorators/workspace-flat-map-cache.decorator';
|
||||
import { WorkspaceFlatMapCacheService } from 'src/engine/workspace-flat-map-cache/services/workspace-flat-map-cache.service';
|
||||
import { addFlatEntityToFlatEntityMapsThroughMutationOrThrow } from 'src/engine/workspace-manager/workspace-migration-v2/utils/add-flat-entity-to-flat-entity-maps-through-mutation-or-throw.util';
|
||||
|
||||
@Injectable()
|
||||
@WorkspaceFlatMapCache('flatRoleTargetMaps')
|
||||
export class WorkspaceFlatRoleTargetMapCacheService extends WorkspaceFlatMapCacheService<FlatRoleTargetMaps> {
|
||||
constructor(
|
||||
@InjectCacheStorage(CacheStorageNamespace.EngineWorkspace)
|
||||
cacheStorageService: CacheStorageService,
|
||||
@InjectRepository(RoleTargetsEntity)
|
||||
private readonly roleTargetsRepository: Repository<RoleTargetsEntity>,
|
||||
) {
|
||||
super(cacheStorageService);
|
||||
}
|
||||
|
||||
protected async computeFlatMap({
|
||||
workspaceId,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
}): Promise<FlatRoleTargetMaps> {
|
||||
const roleTargets = await this.roleTargetsRepository.find({
|
||||
where: {
|
||||
workspaceId,
|
||||
},
|
||||
withDeleted: true,
|
||||
});
|
||||
|
||||
const flatRoleTargetMaps = createEmptyFlatEntityMaps();
|
||||
|
||||
for (const roleTargetEntity of roleTargets) {
|
||||
const flatRoleTarget =
|
||||
fromRoleTargetsEntityToFlatRoleTarget(roleTargetEntity);
|
||||
|
||||
addFlatEntityToFlatEntityMapsThroughMutationOrThrow({
|
||||
flatEntity: flatRoleTarget,
|
||||
flatEntityMapsToMutate: flatRoleTargetMaps,
|
||||
});
|
||||
}
|
||||
|
||||
return flatRoleTargetMaps;
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';
|
||||
|
||||
export type FlatRoleTargetMaps = FlatEntityMaps<FlatRoleTarget>;
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import { type FlatEntityFrom } from 'src/engine/metadata-modules/flat-entity/types/flat-entity.type';
|
||||
import { type RoleTargetsEntity } from 'src/engine/metadata-modules/role/role-targets.entity';
|
||||
|
||||
export const roleTargetsEntityRelationProperties = ['role', 'apiKey'] as const;
|
||||
|
||||
export type RoleTargetsEntityRelationProperties =
|
||||
(typeof roleTargetsEntityRelationProperties)[number];
|
||||
|
||||
export type FlatRoleTarget = FlatEntityFrom<
|
||||
RoleTargetsEntity,
|
||||
RoleTargetsEntityRelationProperties
|
||||
>;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { type ROLE_TARGET_FOREIGN_KEY_PROPERTIES } from 'src/engine/metadata-modules/flat-role-target/constants/role-target-foreign-key-properties.constant';
|
||||
|
||||
export type RoleTargetForeignKeyProperties =
|
||||
(typeof ROLE_TARGET_FOREIGN_KEY_PROPERTIES)[number];
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
|
||||
import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';
|
||||
import { type RoleTargetForeignKeyProperties } from 'src/engine/metadata-modules/flat-role-target/types/role-target-foreign-key-properties.type';
|
||||
|
||||
type FindAllFlatRoleTargetOfArgs = {
|
||||
targetMetadataForeignKey: RoleTargetForeignKeyProperties;
|
||||
targetId: string;
|
||||
} & Pick<AllFlatEntityMaps, 'flatRoleTargetMaps'>;
|
||||
export const findFlatRoleTargetFromForeignKey = ({
|
||||
flatRoleTargetMaps,
|
||||
targetMetadataForeignKey,
|
||||
targetId,
|
||||
}: FindAllFlatRoleTargetOfArgs): FlatRoleTarget | undefined => {
|
||||
const allRoleTargets = Object.values(flatRoleTargetMaps.byId).filter(
|
||||
isDefined,
|
||||
);
|
||||
|
||||
return allRoleTargets.find(
|
||||
(roleTarget) => roleTarget[targetMetadataForeignKey] === targetId,
|
||||
);
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';
|
||||
import { type RoleTargetsEntity } from 'src/engine/metadata-modules/role/role-targets.entity';
|
||||
|
||||
export const fromRoleTargetsEntityToFlatRoleTarget = (
|
||||
roleTarget: RoleTargetsEntity,
|
||||
): FlatRoleTarget => {
|
||||
return {
|
||||
id: roleTarget.id,
|
||||
workspaceId: roleTarget.workspaceId,
|
||||
roleId: roleTarget.roleId,
|
||||
userWorkspaceId: roleTarget.userWorkspaceId,
|
||||
agentId: roleTarget.agentId,
|
||||
apiKeyId: roleTarget.apiKeyId,
|
||||
applicationId: roleTarget.applicationId,
|
||||
universalIdentifier: roleTarget.universalIdentifier ?? roleTarget.id,
|
||||
createdAt: roleTarget.createdAt,
|
||||
updatedAt: roleTarget.updatedAt,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user