feat: add updatedBy field to track last record modifier (#16807)
<!-- CURSOR_SUMMARY -->
> [!NOTE]
> Implements last-modifier tracking and unified actor injection.
>
> - New `ActorFromAuthContextService` replaces createdBy-only logic;
pre-query hooks now inject both `createdBy` and `updatedBy` on create
and `updatedBy` on update
> - Add `updatedBy` standard field to core/custom objects (e.g.,
`person`, `company`, `task`, `note`, `attachment`, `dashboard`,
`workflow`, `workflowRun`) with IDs, metadata builders, and ORM entity
fields
> - Record CRUD: `create` now sets both `createdBy` and `updatedBy`;
`update` sets `updatedBy`; workflow actions use a shared workflow actor
builder; REST base handler uses the new actor service
> - Seeder data and snapshots updated to include `updatedBy`; GraphQL
result formatting adds defaults/handling for empty composite fields
(incl. actor)
> - Frontend `useUpdateOneRecord` upserts returned record into the local
store after mutation
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
1f283778e9. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
co-authored by
Félix Malfait
Félix Malfait
parent
0da5cf29de
commit
01e0502fcf
@@ -2,6 +2,7 @@ import { isPlainObject } from '@nestjs/common/utils/shared.utils';
|
||||
|
||||
import { isNull } from '@sniptt/guards';
|
||||
import {
|
||||
FieldActorSource,
|
||||
FieldMetadataType,
|
||||
compositeTypeDefinitions,
|
||||
} from 'twenty-shared/types';
|
||||
@@ -157,6 +158,13 @@ export function formatResult<T>(
|
||||
: value;
|
||||
}
|
||||
|
||||
// After assembling composite fields, handle those with missing required subfields
|
||||
handleEmptyCompositeFields(
|
||||
newData,
|
||||
flatObjectMetadata,
|
||||
flatFieldMetadataMaps,
|
||||
);
|
||||
|
||||
const fieldMetadataItemsOfTypeDateOnly = getFlatFieldsFromFlatObjectMetadata(
|
||||
flatObjectMetadata,
|
||||
flatFieldMetadataMaps,
|
||||
@@ -250,3 +258,85 @@ function transformCompositeFieldNullValue(
|
||||
] ?? value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles composite fields with missing required subfields.
|
||||
* - For nullable fields: sets to null if all required subfields are null
|
||||
* - For non-nullable fields: provides a default value to prevent GraphQL errors
|
||||
*
|
||||
* This handles existing records that were created before the field was added
|
||||
* or records with incomplete data.
|
||||
*/
|
||||
function handleEmptyCompositeFields(
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
data: Record<string, any>,
|
||||
flatObjectMetadata: FlatObjectMetadata,
|
||||
flatFieldMetadataMaps: FlatEntityMaps<FlatFieldMetadata>,
|
||||
) {
|
||||
const compositeFieldMetadataCollection = getCompositeFieldMetadataCollection(
|
||||
flatObjectMetadata,
|
||||
flatFieldMetadataMaps,
|
||||
);
|
||||
|
||||
for (const fieldMetadata of compositeFieldMetadataCollection) {
|
||||
const fieldValue = data[fieldMetadata.name];
|
||||
|
||||
if (!isDefined(fieldValue) || !isPlainObject(fieldValue)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const compositeType = compositeTypeDefinitions.get(fieldMetadata.type);
|
||||
|
||||
if (!compositeType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const typedFieldValue = fieldValue as Record<string, any>;
|
||||
|
||||
// Check if all required properties are null/undefined
|
||||
const requiredProperties = compositeType.properties.filter(
|
||||
(prop) => prop.isRequired,
|
||||
);
|
||||
|
||||
const allRequiredPropertiesAreNull = requiredProperties.every(
|
||||
(prop) =>
|
||||
!isDefined(typedFieldValue[prop.name]) ||
|
||||
isNull(typedFieldValue[prop.name]),
|
||||
);
|
||||
|
||||
if (allRequiredPropertiesAreNull && requiredProperties.length > 0) {
|
||||
if (fieldMetadata.isNullable) {
|
||||
// Field is nullable, set to null
|
||||
data[fieldMetadata.name] = null;
|
||||
} else {
|
||||
// Field is non-nullable, provide a default value
|
||||
data[fieldMetadata.name] = getDefaultCompositeFieldValue(
|
||||
fieldMetadata.type,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a default value for non-nullable composite fields.
|
||||
*/
|
||||
function getDefaultCompositeFieldValue(
|
||||
fieldType: FieldMetadataType,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
): Record<string, any> | null {
|
||||
switch (fieldType) {
|
||||
case FieldMetadataType.ACTOR:
|
||||
return {
|
||||
source: FieldActorSource.MANUAL,
|
||||
name: '',
|
||||
workspaceMemberId: null,
|
||||
context: {},
|
||||
};
|
||||
default:
|
||||
// For other composite types, return null and let GraphQL handle the error
|
||||
// This should be extended as needed for other non-nullable composite fields
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user