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:
Thomas Trompette 2024-03-12 17:01:24 +01:00 committed by GitHub
parent 60598bf235
commit 91f4e1a853
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 52 deletions

View File

@ -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
],
});
});
});
});

View File

@ -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);
}