[NITPICK] From args list to record args RecordPositionFactory (#10215)
# Introduction Just some nitpicking while debugging a bug that wasn't one
This commit is contained in:
@ -6,7 +6,10 @@ import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-qu
|
|||||||
import { ResolverArgsType } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
|
import { ResolverArgsType } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
|
||||||
|
|
||||||
import { QueryRunnerArgsFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/query-runner-args.factory';
|
import { QueryRunnerArgsFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/query-runner-args.factory';
|
||||||
import { RecordPositionFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/record-position.factory';
|
import {
|
||||||
|
RecordPositionFactory,
|
||||||
|
RecordPositionFactoryCreateArgs,
|
||||||
|
} from 'src/engine/api/graphql/workspace-query-runner/factories/record-position.factory';
|
||||||
import { FieldMetadataMap } from 'src/engine/metadata-modules/types/field-metadata-map';
|
import { FieldMetadataMap } from 'src/engine/metadata-modules/types/field-metadata-map';
|
||||||
|
|
||||||
describe('QueryRunnerArgsFactory', () => {
|
describe('QueryRunnerArgsFactory', () => {
|
||||||
@ -104,12 +107,14 @@ describe('QueryRunnerArgsFactory', () => {
|
|||||||
ResolverArgsType.CreateMany,
|
ResolverArgsType.CreateMany,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(recordPositionFactory.create).toHaveBeenCalledWith(
|
const expectedArgs: RecordPositionFactoryCreateArgs = {
|
||||||
'last',
|
value: 'last',
|
||||||
{ isCustom: true, nameSingular: 'testNumber' },
|
objectMetadata: { isCustom: true, nameSingular: 'testNumber' },
|
||||||
workspaceId,
|
workspaceId,
|
||||||
0,
|
index: 0,
|
||||||
);
|
};
|
||||||
|
|
||||||
|
expect(recordPositionFactory.create).toHaveBeenCalledWith(expectedArgs);
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
id: 'uuid',
|
id: 'uuid',
|
||||||
data: [{ position: 2, testNumber: 1 }],
|
data: [{ position: 2, testNumber: 1 }],
|
||||||
@ -128,12 +133,14 @@ describe('QueryRunnerArgsFactory', () => {
|
|||||||
ResolverArgsType.CreateMany,
|
ResolverArgsType.CreateMany,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(recordPositionFactory.create).toHaveBeenCalledWith(
|
const expectedArgs: RecordPositionFactoryCreateArgs = {
|
||||||
'first',
|
value: 'first',
|
||||||
{ isCustom: true, nameSingular: 'testNumber' },
|
objectMetadata: { isCustom: true, nameSingular: 'testNumber' },
|
||||||
workspaceId,
|
workspaceId,
|
||||||
0,
|
index: 0,
|
||||||
);
|
};
|
||||||
|
|
||||||
|
expect(recordPositionFactory.create).toHaveBeenCalledWith(expectedArgs);
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
id: 'uuid',
|
id: 'uuid',
|
||||||
data: [{ position: 2, testNumber: 1 }],
|
data: [{ position: 2, testNumber: 1 }],
|
||||||
|
|||||||
@ -46,21 +46,33 @@ describe('RecordPositionFactory', () => {
|
|||||||
it('should return the value when value is a number', async () => {
|
it('should return the value when value is a number', async () => {
|
||||||
const value = 1;
|
const value = 1;
|
||||||
|
|
||||||
const result = await factory.create(value, objectMetadata, workspaceId);
|
const result = await factory.create({
|
||||||
|
value,
|
||||||
|
objectMetadata,
|
||||||
|
workspaceId,
|
||||||
|
});
|
||||||
|
|
||||||
expect(result).toEqual(value);
|
expect(result).toEqual(value);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the existing position -1 when value is first', async () => {
|
it('should return the existing position -1 when value is first', async () => {
|
||||||
const value = 'first';
|
const value = 'first';
|
||||||
const result = await factory.create(value, objectMetadata, workspaceId);
|
const result = await factory.create({
|
||||||
|
value,
|
||||||
|
objectMetadata,
|
||||||
|
workspaceId,
|
||||||
|
});
|
||||||
|
|
||||||
expect(result).toEqual(0);
|
expect(result).toEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the existing position + 1 when value is last', async () => {
|
it('should return the existing position + 1 when value is last', async () => {
|
||||||
const value = 'last';
|
const value = 'last';
|
||||||
const result = await factory.create(value, objectMetadata, workspaceId);
|
const result = await factory.create({
|
||||||
|
value,
|
||||||
|
objectMetadata,
|
||||||
|
workspaceId,
|
||||||
|
});
|
||||||
|
|
||||||
expect(result).toEqual(2);
|
expect(result).toEqual(2);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -177,6 +177,7 @@ export class QueryRunnerArgsFactory {
|
|||||||
return Promise.resolve({});
|
return Promise.resolve({});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const workspaceId = options.authContext.workspace.id;
|
||||||
let isFieldPositionPresent = false;
|
let isFieldPositionPresent = false;
|
||||||
|
|
||||||
const createArgByArgKeyPromises: Promise<[string, any]>[] = Object.entries(
|
const createArgByArgKeyPromises: Promise<[string, any]>[] = Object.entries(
|
||||||
@ -192,16 +193,16 @@ export class QueryRunnerArgsFactory {
|
|||||||
case FieldMetadataType.POSITION: {
|
case FieldMetadataType.POSITION: {
|
||||||
isFieldPositionPresent = true;
|
isFieldPositionPresent = true;
|
||||||
|
|
||||||
const newValue = await this.recordPositionFactory.create(
|
const newValue = await this.recordPositionFactory.create({
|
||||||
value,
|
value,
|
||||||
{
|
workspaceId,
|
||||||
|
objectMetadata: {
|
||||||
isCustom: options.objectMetadataItemWithFieldMaps.isCustom,
|
isCustom: options.objectMetadataItemWithFieldMaps.isCustom,
|
||||||
nameSingular:
|
nameSingular:
|
||||||
options.objectMetadataItemWithFieldMaps.nameSingular,
|
options.objectMetadataItemWithFieldMaps.nameSingular,
|
||||||
},
|
},
|
||||||
options.authContext.workspace.id,
|
index: argPositionBackfillInput.argIndex,
|
||||||
argPositionBackfillInput.argIndex,
|
});
|
||||||
);
|
|
||||||
|
|
||||||
return [key, newValue];
|
return [key, newValue];
|
||||||
}
|
}
|
||||||
@ -248,16 +249,16 @@ export class QueryRunnerArgsFactory {
|
|||||||
...newArgEntries,
|
...newArgEntries,
|
||||||
[
|
[
|
||||||
'position',
|
'position',
|
||||||
await this.recordPositionFactory.create(
|
await this.recordPositionFactory.create({
|
||||||
'first',
|
value: 'first',
|
||||||
{
|
workspaceId,
|
||||||
|
objectMetadata: {
|
||||||
isCustom: options.objectMetadataItemWithFieldMaps.isCustom,
|
isCustom: options.objectMetadataItemWithFieldMaps.isCustom,
|
||||||
nameSingular:
|
nameSingular:
|
||||||
options.objectMetadataItemWithFieldMaps.nameSingular,
|
options.objectMetadataItemWithFieldMaps.nameSingular,
|
||||||
},
|
},
|
||||||
options.authContext.workspace.id,
|
index: argPositionBackfillInput.argIndex,
|
||||||
argPositionBackfillInput.argIndex,
|
}),
|
||||||
),
|
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,12 @@ import {
|
|||||||
} from 'src/engine/api/graphql/workspace-query-builder/factories/record-position-query.factory';
|
} from 'src/engine/api/graphql/workspace-query-builder/factories/record-position-query.factory';
|
||||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||||
|
|
||||||
|
export type RecordPositionFactoryCreateArgs = {
|
||||||
|
value: number | 'first' | 'last';
|
||||||
|
objectMetadata: { isCustom: boolean; nameSingular: string };
|
||||||
|
workspaceId: string;
|
||||||
|
index?: number;
|
||||||
|
};
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RecordPositionFactory {
|
export class RecordPositionFactory {
|
||||||
constructor(
|
constructor(
|
||||||
@ -16,12 +22,12 @@ export class RecordPositionFactory {
|
|||||||
private readonly recordPositionQueryFactory: RecordPositionQueryFactory,
|
private readonly recordPositionQueryFactory: RecordPositionQueryFactory,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(
|
async create({
|
||||||
value: number | 'first' | 'last',
|
objectMetadata,
|
||||||
objectMetadata: { isCustom: boolean; nameSingular: string },
|
value,
|
||||||
workspaceId: string,
|
workspaceId,
|
||||||
index = 0,
|
index = 0,
|
||||||
): Promise<number> {
|
}: RecordPositionFactoryCreateArgs): Promise<number> {
|
||||||
const dataSourceSchema =
|
const dataSourceSchema =
|
||||||
this.workspaceDataSourceService.getSchemaName(workspaceId);
|
this.workspaceDataSourceService.getSchemaName(workspaceId);
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,8 @@ import { Injectable, Logger } from '@nestjs/common';
|
|||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import { isDefined } from 'class-validator';
|
import { isDefined } from 'class-validator';
|
||||||
import { Repository } from 'typeorm';
|
|
||||||
import { FieldMetadataType } from 'twenty-shared';
|
import { FieldMetadataType } from 'twenty-shared';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
RecordPositionQueryFactory,
|
RecordPositionQueryFactory,
|
||||||
@ -73,14 +73,14 @@ export class RecordPositionBackfillService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const position = await this.recordPositionFactory.create(
|
const position = await this.recordPositionFactory.create({
|
||||||
'last',
|
objectMetadata: {
|
||||||
{
|
|
||||||
isCustom: objectMetadata.isCustom,
|
isCustom: objectMetadata.isCustom,
|
||||||
nameSingular: objectMetadata.nameSingular,
|
nameSingular: objectMetadata.nameSingular,
|
||||||
},
|
},
|
||||||
|
value: 'last',
|
||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
});
|
||||||
|
|
||||||
for (
|
for (
|
||||||
let recordIndex = 0;
|
let recordIndex = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user