Make positions possibly negatives (#5690)

Closes https://github.com/twentyhq/twenty/issues/5427
This commit is contained in:
Thomas Trompette
2024-05-31 14:17:49 +02:00
committed by GitHub
parent f166171a1c
commit fbd8714c76
6 changed files with 66 additions and 14 deletions

View File

@ -48,11 +48,11 @@ describe('RecordPositionFactory', () => {
expect(result).toEqual(value);
});
it('should return the existing position / 2 when value is first', async () => {
it('should return the existing position -1 when value is first', async () => {
const value = 'first';
const result = await factory.create(value, objectMetadata, workspaceId);
expect(result).toEqual(0.5);
expect(result).toEqual(0);
});
it('should return the existing position + 1 when value is last', async () => {
const value = 'last';

View File

@ -1,5 +1,7 @@
import { Injectable } from '@nestjs/common';
import { isDefined } from 'class-validator';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import {
RecordPositionQueryFactory,
@ -32,6 +34,8 @@ export class RecordPositionFactory {
dataSourceSchema,
);
// If the value was 'first', the first record will be the one with the lowest position
// If the value was 'last', the first record will be the one with the highest position
const records = await this.workspaceDataSourceService.executeRawQuery(
query,
[],
@ -39,10 +43,16 @@ export class RecordPositionFactory {
undefined,
);
return (
(value === 'first'
? records[0]?.position / 2
: records[0]?.position + 1) || 1
);
if (
!isDefined(records) ||
records.length === 0 ||
!isDefined(records[0]?.position)
) {
return 1;
}
return value === 'first'
? records[0].position - 1
: records[0].position + 1;
}
}

View File

@ -6,7 +6,7 @@ const isValidStringPosition = (value: string): boolean =>
typeof value === 'string' && (value === 'first' || value === 'last');
const isValidNumberPosition = (value: number): boolean =>
typeof value === 'number' && value >= 0;
typeof value === 'number';
const checkPosition = (value: any): PositionType => {
if (isValidNumberPosition(value) || isValidStringPosition(value)) {