New Timeline (#4936)

Refactored the code to introduce two different concepts:
- AuditLogs (immutable, raw data)
- TimelineActivities (user-friendly, transformed data)

Still some work needed:
- Add message, files, calendar events to timeline (~2 hours if done
naively)
- Refactor repository to try to abstract concept when we can (tbd, wait
for Twenty ORM)
- Introduce ability to display child timelines on parent timeline with
filtering (~2 days)
- Improve UI: add links to open note/task, improve diff display, etc
(half a day)
- Decide the path forward for Task vs Notes: either introduce a new
field type "Record Type" and start going into that direction ; or split
in two objects?
- Trigger updates when a field is changed (will be solved by real-time /
websockets: 2 weeks)
- Integrate behavioral events (1 day for POC, 1 week for
clean/documented)

<img width="1248" alt="Screenshot 2024-04-12 at 09 24 49"
src="https://github.com/twentyhq/twenty/assets/6399865/9428db1a-ab2b-492c-8b0b-d4d9a36e81fa">
This commit is contained in:
Félix Malfait
2024-04-19 17:52:57 +02:00
committed by GitHub
parent 9c8cb52952
commit d145684966
56 changed files with 1314 additions and 368 deletions

View File

@ -1,7 +1,7 @@
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
export class ObjectRecordCreateEvent<T> extends ObjectRecordBaseEvent {
details: {
properties: {
after: T;
};
}

View File

@ -1,7 +1,7 @@
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
export class ObjectRecordDeleteEvent<T> extends ObjectRecordBaseEvent {
details: {
properties: {
before: T;
};
}

View File

@ -0,0 +1,11 @@
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
export class ObjectRecordJobData extends ObjectRecordBaseEvent {
getOperation() {
return this.name.split('.')[1];
}
getObjectName() {
return this.name.split('.')[0];
}
}

View File

@ -1,7 +1,7 @@
import { ObjectRecordBaseEvent } from 'src/engine/integrations/event-emitter/types/object-record.base.event';
export class ObjectRecordUpdateEvent<T> extends ObjectRecordBaseEvent {
details: {
properties: {
before: T;
after: T;
diff?: Partial<T>;

View File

@ -1,9 +1,11 @@
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
export class ObjectRecordBaseEvent {
name: string;
workspaceId: string;
recordId: string;
userId?: string;
workspaceMemberId?: string;
objectMetadata: ObjectMetadataInterface;
details: any;
properties: any;
}

View File

@ -1,11 +1,35 @@
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
import { objectRecordChangedValues } from 'src/engine/integrations/event-emitter/utils/object-record-changed-values';
const mockObjectMetadata: ObjectMetadataInterface = {
id: '1',
nameSingular: 'Object',
namePlural: 'Objects',
labelSingular: 'Object',
labelPlural: 'Objects',
description: 'Test object metadata',
targetTableName: 'test_table',
fromRelations: [],
toRelations: [],
fields: [],
isSystem: false,
isCustom: false,
isActive: true,
isRemote: false,
isAuditLogged: true,
};
describe('objectRecordChangedValues', () => {
it('detects changes in scalar values correctly', () => {
const oldRecord = { id: 1, name: 'Original Name', updatedAt: new Date() };
const newRecord = { id: 1, name: 'Updated Name', updatedAt: new Date() };
const result = objectRecordChangedValues(oldRecord, newRecord);
const result = objectRecordChangedValues(
oldRecord,
newRecord,
mockObjectMetadata,
);
expect(result).toEqual({
name: { before: 'Original Name', after: 'Updated Name' },
@ -13,20 +37,15 @@ describe('objectRecordChangedValues', () => {
});
});
it('ignores changes in properties that are objects', () => {
const oldRecord = { id: 1, details: { age: 20 } };
const newRecord = { id: 1, details: { age: 21 } };
const result = objectRecordChangedValues(oldRecord, newRecord);
expect(result).toEqual({});
});
it('ignores changes to the updatedAt field', () => {
const oldRecord = { id: 1, updatedAt: new Date('2020-01-01') };
const newRecord = { id: 1, updatedAt: new Date('2024-01-01') };
const result = objectRecordChangedValues(oldRecord, newRecord);
const result = objectRecordChangedValues(
oldRecord,
newRecord,
mockObjectMetadata,
);
expect(result).toEqual({});
});
@ -35,7 +54,11 @@ it('returns an empty object when there are no changes', () => {
const oldRecord = { id: 1, name: 'Name', value: 100 };
const newRecord = { id: 1, name: 'Name', value: 100 };
const result = objectRecordChangedValues(oldRecord, newRecord);
const result = objectRecordChangedValues(
oldRecord,
newRecord,
mockObjectMetadata,
);
expect(result).toEqual({});
});
@ -57,9 +80,14 @@ it('correctly handles a mix of changed, unchanged, and special case values', ()
};
const expectedChanges = {
name: { before: 'Original', after: 'Updated' },
config: { before: { theme: 'dark' }, after: { theme: 'light' } },
};
const result = objectRecordChangedValues(oldRecord, newRecord);
const result = objectRecordChangedValues(
oldRecord,
newRecord,
mockObjectMetadata,
);
expect(result).toEqual(expectedChanges);
});

View File

@ -1,21 +1,30 @@
import deepEqual from 'deep-equal';
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
export const objectRecordChangedValues = (
oldRecord: Record<string, any>,
newRecord: Record<string, any>,
objectMetadata: ObjectMetadataInterface,
) => {
const isObject = (value: any) => {
return typeof value === 'object' && value !== null && !Array.isArray(value);
};
const changedValues = Object.keys(newRecord).reduce(
(acc, key) => {
// Discard if values are objects (e.g. we don't want Company.AccountOwner ; we have AccountOwnerId already)
if (isObject(oldRecord[key]) || isObject(newRecord[key])) {
if (
objectMetadata.fields.find(
(field) =>
field.type === FieldMetadataType.RELATION && field.name === key,
)
) {
return acc;
}
if (!deepEqual(oldRecord[key], newRecord[key]) && key != 'updatedAt') {
if (objectMetadata.nameSingular === 'activity' && key === 'body') {
return acc;
}
if (!deepEqual(oldRecord[key], newRecord[key]) && key !== 'updatedAt') {
acc[key] = { before: oldRecord[key], after: newRecord[key] };
}

View File

@ -0,0 +1,30 @@
export function objectRecordDiffMerge(
oldRecord: Record<string, any>,
newRecord: Record<string, any>,
): Record<string, any> {
const result: Record<string, any> = { diff: {} };
// Iterate over the keys in the oldRecord diff
Object.keys(oldRecord.diff ?? {}).forEach((key) => {
if (newRecord.diff && newRecord.diff[key]) {
// If the key also exists in the newRecord, merge the 'before' from the oldRecord and the 'after' from the newRecord
result.diff[key] = {
before: oldRecord.diff[key].before,
after: newRecord.diff[key].after,
};
} else {
// If the key does not exist in the newRecord, copy it as is from the oldRecord
result.diff[key] = oldRecord.diff[key];
}
});
// Iterate over the keys in the newRecord diff to catch any that weren't in the oldRecord
Object.keys(newRecord.diff ?? {}).forEach((key) => {
if (!result.diff[key]) {
// If the key was not already added from the oldRecord, add it from the newRecord
result.diff[key] = newRecord.diff[key];
}
});
return result;
}