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:
@ -51,31 +51,17 @@ describe('QueryRunnerArgsFactory', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('create', () => {
|
describe('create', () => {
|
||||||
it('should return the args when empty', async () => {
|
it('should simply return the args when data is an empty array', async () => {
|
||||||
const args = {};
|
const args = {
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
const result = await factory.create(args, options);
|
const result = await factory.create(args, options);
|
||||||
|
|
||||||
expect(result).toEqual(args);
|
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 () => {
|
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([
|
workspaceDataSourceService.executeRawQuery.mockResolvedValue([
|
||||||
{ position: 1 },
|
{ position: 1 },
|
||||||
@ -83,10 +69,12 @@ describe('QueryRunnerArgsFactory', () => {
|
|||||||
|
|
||||||
const result = await factory.create(args, options);
|
const result = await factory.create(args, options);
|
||||||
|
|
||||||
expect(result).toEqual([
|
expect(result).toEqual({
|
||||||
{ id: 1 },
|
data: [
|
||||||
{ position: 2 }, // Calculates 1 + 1
|
{ id: 1 },
|
||||||
]);
|
{ position: 2 }, // Calculates 1 + 1
|
||||||
|
],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
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 { 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 { ObjectMetadataInterface } from 'src/metadata/field-metadata/interfaces/object-metadata.interface';
|
||||||
|
|
||||||
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service';
|
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 { RecordPositionQueryFactory } from 'src/workspace/workspace-query-builder/factories/record-position-query.factory';
|
||||||
|
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class QueryRunnerArgsFactory {
|
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(
|
private async overrideArgByFieldMetadata(
|
||||||
args: Record<string, any>,
|
arg: Record<string, any>,
|
||||||
options: WorkspaceQueryRunnerOptions,
|
options: WorkspaceQueryRunnerOptions,
|
||||||
fieldMetadataMap: Map<string, FieldMetadataInterface>,
|
fieldMetadataMap: Map<string, FieldMetadataInterface>,
|
||||||
) {
|
) {
|
||||||
// If it's not an object, we don't need to do anything
|
const createArgPromiseByArgKey = Object.entries(arg).map(
|
||||||
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(
|
|
||||||
async ([key, value]) => {
|
async ([key, value]) => {
|
||||||
const fieldMetadata = fieldMetadataMap.get(key);
|
const fieldMetadata = fieldMetadataMap.get(key);
|
||||||
|
|
||||||
if (!fieldMetadata) {
|
if (!fieldMetadata) {
|
||||||
return [
|
return [key, await Promise.resolve(value)];
|
||||||
key,
|
|
||||||
await this.createArgsRecursive(value, options, fieldMetadataMap),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (fieldMetadata.type) {
|
switch (fieldMetadata.type) {
|
||||||
@ -72,15 +61,12 @@ export class QueryRunnerArgsFactory {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
default:
|
default:
|
||||||
return [
|
return [key, await Promise.resolve(value)];
|
||||||
key,
|
|
||||||
await this.createArgsRecursive(value, options, fieldMetadataMap),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const newArgEntries = await Promise.all(createArgPromisesByArgKey);
|
const newArgEntries = await Promise.all(createArgPromiseByArgKey);
|
||||||
|
|
||||||
return Object.fromEntries(newArgEntries);
|
return Object.fromEntries(newArgEntries);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user