mirror of
https://github.com/twentyhq/twenty.git
synced 2024-12-26 05:24:04 +03:00
Fix activity creation (#4426)
* Fix activity creation * Fix tests * Remove recursive logic + fix test --------- Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
parent
60598bf235
commit
91f4e1a853
@ -51,31 +51,17 @@ describe('QueryRunnerArgsFactory', () => {
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should return the args when empty', async () => {
|
||||
const args = {};
|
||||
it('should simply return the args when data is an empty array', async () => {
|
||||
const args = {
|
||||
data: [],
|
||||
};
|
||||
const result = await factory.create(args, options);
|
||||
|
||||
expect(result).toEqual(args);
|
||||
});
|
||||
|
||||
it('should override position when of type string', async () => {
|
||||
const args = {
|
||||
position: 'first',
|
||||
};
|
||||
|
||||
workspaceDataSourceService.executeRawQuery.mockResolvedValue([
|
||||
{ position: 2 },
|
||||
]);
|
||||
|
||||
const result = await factory.create(args, options);
|
||||
|
||||
expect(result).toEqual({
|
||||
position: 1, // Calculates 2 / 2
|
||||
});
|
||||
});
|
||||
|
||||
it('should override args when of type array', async () => {
|
||||
const args = [{ id: 1 }, { position: 'last' }];
|
||||
const args = { data: [{ id: 1 }, { position: 'last' }] };
|
||||
|
||||
workspaceDataSourceService.executeRawQuery.mockResolvedValue([
|
||||
{ position: 1 },
|
||||
@ -83,10 +69,12 @@ describe('QueryRunnerArgsFactory', () => {
|
||||
|
||||
const result = await factory.create(args, options);
|
||||
|
||||
expect(result).toEqual([
|
||||
{ id: 1 },
|
||||
{ position: 2 }, // Calculates 1 + 1
|
||||
]);
|
||||
expect(result).toEqual({
|
||||
data: [
|
||||
{ id: 1 },
|
||||
{ position: 2 }, // Calculates 1 + 1
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { FieldMetadataInterface } from 'src/metadata/field-metadata/interfaces/field-metadata.interface';
|
||||
import { WorkspaceQueryRunnerOptions } from 'src/workspace/workspace-query-runner/interfaces/query-runner-option.interface';
|
||||
import { FieldMetadataInterface } from 'src/metadata/field-metadata/interfaces/field-metadata.interface';
|
||||
import { ObjectMetadataInterface } from 'src/metadata/field-metadata/interfaces/object-metadata.interface';
|
||||
|
||||
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service';
|
||||
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
|
||||
import { RecordPositionQueryFactory } from 'src/workspace/workspace-query-builder/factories/record-position-query.factory';
|
||||
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
|
||||
|
||||
@Injectable()
|
||||
export class QueryRunnerArgsFactory {
|
||||
@ -28,37 +28,26 @@ export class QueryRunnerArgsFactory {
|
||||
]),
|
||||
);
|
||||
|
||||
return this.createArgsRecursive(args, options, fieldMetadataMap);
|
||||
return {
|
||||
data: await Promise.all(
|
||||
args.data.map((arg) =>
|
||||
this.overrideArgByFieldMetadata(arg, options, fieldMetadataMap),
|
||||
),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
private async createArgsRecursive(
|
||||
args: Record<string, any>,
|
||||
private async overrideArgByFieldMetadata(
|
||||
arg: Record<string, any>,
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
fieldMetadataMap: Map<string, FieldMetadataInterface>,
|
||||
) {
|
||||
// If it's not an object, we don't need to do anything
|
||||
if (typeof args !== 'object' || args === null) {
|
||||
return args;
|
||||
}
|
||||
|
||||
// If it's an array, we need to map all items
|
||||
if (Array.isArray(args)) {
|
||||
return Promise.all(
|
||||
args.map((arg) =>
|
||||
this.createArgsRecursive(arg, options, fieldMetadataMap),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const createArgPromisesByArgKey = Object.entries(args).map(
|
||||
const createArgPromiseByArgKey = Object.entries(arg).map(
|
||||
async ([key, value]) => {
|
||||
const fieldMetadata = fieldMetadataMap.get(key);
|
||||
|
||||
if (!fieldMetadata) {
|
||||
return [
|
||||
key,
|
||||
await this.createArgsRecursive(value, options, fieldMetadataMap),
|
||||
];
|
||||
return [key, await Promise.resolve(value)];
|
||||
}
|
||||
|
||||
switch (fieldMetadata.type) {
|
||||
@ -72,15 +61,12 @@ export class QueryRunnerArgsFactory {
|
||||
),
|
||||
];
|
||||
default:
|
||||
return [
|
||||
key,
|
||||
await this.createArgsRecursive(value, options, fieldMetadataMap),
|
||||
];
|
||||
return [key, await Promise.resolve(value)];
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const newArgEntries = await Promise.all(createArgPromisesByArgKey);
|
||||
const newArgEntries = await Promise.all(createArgPromiseByArgKey);
|
||||
|
||||
return Object.fromEntries(newArgEntries);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user