Add workspace migration orchestrator + refactor builders + view/viewField action builders (#14383)
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
export class FlatEntityException extends CustomException {
|
||||
code: FlatEntityExceptionCode;
|
||||
|
||||
constructor(message: string, code: FlatEntityExceptionCode) {
|
||||
super(message, code);
|
||||
}
|
||||
}
|
||||
|
||||
export enum FlatEntityExceptionCode {
|
||||
ENTITY_ALREADY_EXISTS = 'ENTITY_ALREADY_EXISTS',
|
||||
ENTITY_NOT_FOUND = 'ENTITY_NOT_FOUND',
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { type FlatEntity } from 'src/engine/core-modules/common/types/flat-entity.type';
|
||||
|
||||
export type FlatEntityMaps<T extends FlatEntity> = {
|
||||
byId: Partial<Record<string, T>>;
|
||||
idByUniversalIdentifier: Partial<Record<string, string>>;
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface FlatEntity {
|
||||
id: string;
|
||||
universalIdentifier: string;
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
import { isDefined } from 'class-validator';
|
||||
|
||||
import {
|
||||
FlatEntityException,
|
||||
FlatEntityExceptionCode,
|
||||
} from 'src/engine/core-modules/common/exceptions/flat-entity.exception';
|
||||
import { type FlatEntityMaps } from 'src/engine/core-modules/common/types/flat-entity-maps.type';
|
||||
import { type FlatEntity } from 'src/engine/core-modules/common/types/flat-entity.type';
|
||||
|
||||
type AddFlatEntityToFlatEntityMapsOrThrowArgs<T extends FlatEntity> = {
|
||||
flatEntity: T;
|
||||
flatEntityMaps: FlatEntityMaps<T>;
|
||||
};
|
||||
|
||||
export const addFlatEntityToFlatEntityMapsOrThrow = <T extends FlatEntity>({
|
||||
flatEntity,
|
||||
flatEntityMaps,
|
||||
}: AddFlatEntityToFlatEntityMapsOrThrowArgs<T>): FlatEntityMaps<T> => {
|
||||
if (isDefined(flatEntityMaps.byId[flatEntity.id])) {
|
||||
throw new FlatEntityException(
|
||||
'addFlatEntityToFlatEntityMapsOrThrow: flat entity to add already exists',
|
||||
FlatEntityExceptionCode.ENTITY_ALREADY_EXISTS,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
byId: {
|
||||
...flatEntityMaps.byId,
|
||||
[flatEntity.id]: flatEntity,
|
||||
},
|
||||
idByUniversalIdentifier: {
|
||||
...flatEntityMaps.idByUniversalIdentifier,
|
||||
[flatEntity.universalIdentifier]: flatEntity.id,
|
||||
},
|
||||
};
|
||||
};
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { isDefined, removePropertiesFromRecord } from 'twenty-shared/utils';
|
||||
|
||||
import { type FlatEntityMaps } from 'src/engine/core-modules/common/types/flat-entity-maps.type';
|
||||
import { type FlatEntity } from 'src/engine/core-modules/common/types/flat-entity.type';
|
||||
import {
|
||||
FlatEntityException,
|
||||
FlatEntityExceptionCode,
|
||||
} from 'src/engine/core-modules/common/exceptions/flat-entity.exception';
|
||||
|
||||
export type DeleteFlatEntityFromFlatEntityMapsOrThrowArgs<
|
||||
T extends FlatEntity,
|
||||
> = {
|
||||
entityToDeleteId: string;
|
||||
flatEntityMaps: FlatEntityMaps<T>;
|
||||
};
|
||||
|
||||
export const deleteFlatEntityFromFlatEntityMapsOrThrow = <
|
||||
T extends FlatEntity,
|
||||
>({
|
||||
flatEntityMaps,
|
||||
entityToDeleteId,
|
||||
}: DeleteFlatEntityFromFlatEntityMapsOrThrowArgs<T>): FlatEntityMaps<T> => {
|
||||
if (!isDefined(flatEntityMaps.byId[entityToDeleteId])) {
|
||||
throw new FlatEntityException(
|
||||
'deleteFlatEntityFromFlatEntityMapsOrThrow: entity to delete not found',
|
||||
FlatEntityExceptionCode.ENTITY_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const updatedIdByUniversalIdentifierEntries = Object.entries(
|
||||
flatEntityMaps.idByUniversalIdentifier,
|
||||
).filter(([_universalIdentifier, id]) => id !== entityToDeleteId);
|
||||
|
||||
return {
|
||||
byId: removePropertiesFromRecord(flatEntityMaps.byId, [entityToDeleteId]),
|
||||
idByUniversalIdentifier: Object.fromEntries(
|
||||
updatedIdByUniversalIdentifierEntries,
|
||||
),
|
||||
};
|
||||
};
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import { type FlatEntityMaps } from 'src/engine/core-modules/common/types/flat-entity-maps.type';
|
||||
import { type FlatEntity } from 'src/engine/core-modules/common/types/flat-entity.type';
|
||||
import { addFlatEntityToFlatEntityMapsOrThrow } from 'src/engine/core-modules/common/utils/add-flat-entity-to-flat-entity-maps-or-throw.util';
|
||||
import { deleteFlatEntityFromFlatEntityMapsOrThrow } from 'src/engine/core-modules/common/utils/delete-flat-entity-from-flat-entity-maps-or-throw.util';
|
||||
|
||||
export type ReplaceFlatEntityInFlatEntityMapsOrThrowArgs<T extends FlatEntity> =
|
||||
{
|
||||
flatEntity: T;
|
||||
flatEntityMaps: FlatEntityMaps<T>;
|
||||
};
|
||||
|
||||
export const replaceFlatEntityInFlatEntityMapsOrThrow = <T extends FlatEntity>({
|
||||
flatEntity,
|
||||
flatEntityMaps,
|
||||
}: ReplaceFlatEntityInFlatEntityMapsOrThrowArgs<T>): FlatEntityMaps<T> => {
|
||||
const flatEntityMapsToReplace = deleteFlatEntityFromFlatEntityMapsOrThrow({
|
||||
flatEntityMaps,
|
||||
entityToDeleteId: flatEntity.id,
|
||||
});
|
||||
|
||||
return addFlatEntityToFlatEntityMapsOrThrow({
|
||||
flatEntity,
|
||||
flatEntityMaps: flatEntityMapsToReplace,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user