6658 workflows add a first twenty piece email sender (#6965)
This commit is contained in:
@ -0,0 +1,7 @@
|
||||
import { ObjectRecordBaseEvent } from 'src/engine/core-modules/event-emitter/types/object-record.base.event';
|
||||
|
||||
export class ObjectRecordCreateEvent<T> extends ObjectRecordBaseEvent {
|
||||
properties: {
|
||||
after: T;
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
import { ObjectRecordBaseEvent } from 'src/engine/core-modules/event-emitter/types/object-record.base.event';
|
||||
|
||||
export class ObjectRecordDeleteEvent<T> extends ObjectRecordBaseEvent {
|
||||
properties: {
|
||||
before: T;
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
import { ObjectRecordBaseEvent } from 'src/engine/core-modules/event-emitter/types/object-record.base.event';
|
||||
|
||||
export class ObjectRecordUpdateEvent<T> extends ObjectRecordBaseEvent {
|
||||
properties: {
|
||||
updatedFields: string[];
|
||||
before: T;
|
||||
after: T;
|
||||
diff?: Partial<T>;
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
|
||||
|
||||
export class ObjectRecordBaseEvent {
|
||||
recordId: string;
|
||||
userId?: string;
|
||||
workspaceMemberId?: string;
|
||||
objectMetadata: ObjectMetadataInterface;
|
||||
properties: any;
|
||||
}
|
||||
|
||||
export class ObjectRecordBaseEventWithNameAndWorkspaceId extends ObjectRecordBaseEvent {
|
||||
name: string;
|
||||
workspaceId: string;
|
||||
}
|
||||
@ -0,0 +1,119 @@
|
||||
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
|
||||
|
||||
import { objectRecordChangedValues } from 'src/engine/core-modules/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: '74316f58-29b0-4a6a-b8fa-d2b506d5516m',
|
||||
name: 'Original Name',
|
||||
updatedAt: new Date().toString(),
|
||||
};
|
||||
const newRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516m',
|
||||
name: 'Updated Name',
|
||||
updatedAt: new Date().toString(),
|
||||
};
|
||||
|
||||
const result = objectRecordChangedValues(
|
||||
oldRecord,
|
||||
newRecord,
|
||||
['name'],
|
||||
mockObjectMetadata,
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
name: { before: 'Original Name', after: 'Updated Name' },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores changes to the updatedAt field', () => {
|
||||
const oldRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516d',
|
||||
updatedAt: new Date('2020-01-01').toDateString(),
|
||||
};
|
||||
const newRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516d',
|
||||
updatedAt: new Date('2024-01-01').toDateString(),
|
||||
};
|
||||
|
||||
const result = objectRecordChangedValues(
|
||||
oldRecord,
|
||||
newRecord,
|
||||
[],
|
||||
mockObjectMetadata,
|
||||
);
|
||||
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
it('returns an empty object when there are no changes', () => {
|
||||
const oldRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516k',
|
||||
name: 'Name',
|
||||
value: 100,
|
||||
};
|
||||
const newRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516k',
|
||||
name: 'Name',
|
||||
value: 100,
|
||||
};
|
||||
|
||||
const result = objectRecordChangedValues(
|
||||
oldRecord,
|
||||
newRecord,
|
||||
['name', 'value'],
|
||||
mockObjectMetadata,
|
||||
);
|
||||
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
it('correctly handles a mix of changed, unchanged, and special case values', () => {
|
||||
const oldRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516l',
|
||||
name: 'Original',
|
||||
status: 'active',
|
||||
updatedAt: new Date(2020, 1, 1).toDateString(),
|
||||
config: { theme: 'dark' },
|
||||
};
|
||||
const newRecord = {
|
||||
id: '74316f58-29b0-4a6a-b8fa-d2b506d5516l',
|
||||
name: 'Updated',
|
||||
status: 'active',
|
||||
updatedAt: new Date(2021, 1, 1).toDateString(),
|
||||
config: { theme: 'light' },
|
||||
};
|
||||
const expectedChanges = {
|
||||
name: { before: 'Original', after: 'Updated' },
|
||||
config: { before: { theme: 'dark' }, after: { theme: 'light' } },
|
||||
};
|
||||
|
||||
const result = objectRecordChangedValues(
|
||||
oldRecord,
|
||||
newRecord,
|
||||
['name', 'config', 'status'],
|
||||
mockObjectMetadata,
|
||||
);
|
||||
|
||||
expect(result).toEqual(expectedChanges);
|
||||
});
|
||||
@ -0,0 +1,18 @@
|
||||
import deepEqual from 'deep-equal';
|
||||
|
||||
import { Record } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
|
||||
|
||||
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
|
||||
|
||||
export const objectRecordChangedProperties = <
|
||||
PRecord extends Partial<Record | BaseWorkspaceEntity> = Partial<Record>,
|
||||
>(
|
||||
oldRecord: PRecord,
|
||||
newRecord: PRecord,
|
||||
) => {
|
||||
const changedProperties = Object.keys(newRecord).filter(
|
||||
(key) => !deepEqual(oldRecord[key], newRecord[key]),
|
||||
);
|
||||
|
||||
return changedProperties;
|
||||
};
|
||||
@ -0,0 +1,41 @@
|
||||
import deepEqual from 'deep-equal';
|
||||
|
||||
import { Record as IRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
|
||||
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: Partial<IRecord>,
|
||||
newRecord: Partial<IRecord>,
|
||||
updatedKeys: string[],
|
||||
objectMetadata: ObjectMetadataInterface,
|
||||
) => {
|
||||
const fieldsByKey = new Map(
|
||||
objectMetadata.fields.map((field) => [field.name, field]),
|
||||
);
|
||||
|
||||
const changedValues = Object.keys(newRecord).reduce(
|
||||
(acc, key) => {
|
||||
const field = fieldsByKey.get(key);
|
||||
const oldRecordValue = oldRecord[key];
|
||||
const newRecordValue = newRecord[key];
|
||||
|
||||
if (
|
||||
key === 'updatedAt' ||
|
||||
!updatedKeys.includes(key) ||
|
||||
field?.type === FieldMetadataType.RELATION ||
|
||||
deepEqual(oldRecordValue, newRecordValue)
|
||||
) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
acc[key] = { before: oldRecordValue, after: newRecordValue };
|
||||
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, { before: any; after: any }>,
|
||||
);
|
||||
|
||||
return changedValues;
|
||||
};
|
||||
@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user