Skip to content

Commit

Permalink
Create new type position (twentyhq#4336)
Browse files Browse the repository at this point in the history
* Create new type position

* Remove position filter type

---------

Co-authored-by: Thomas Trompette <[email protected]>
  • Loading branch information
thomtrp and Thomas Trompette authored Mar 6, 2024
1 parent b02a9ce commit ce2901b
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/twenty-front/src/generated-metadata/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export enum FieldMetadataType {
Number = 'NUMBER',
Numeric = 'NUMERIC',
Phone = 'PHONE',
Position = 'POSITION',
Probability = 'PROBABILITY',
Rating = 'RATING',
Relation = 'RELATION',
Expand Down
1 change: 1 addition & 0 deletions packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export enum FieldMetadataType {
Number = 'NUMBER',
Numeric = 'NUMERIC',
Phone = 'PHONE',
Position = 'POSITION',
Probability = 'PROBABILITY',
Rating = 'RATING',
Relation = 'RELATION',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const generateEmptyFieldValue = (
}
case FieldMetadataType.Number:
case FieldMetadataType.Rating:
case FieldMetadataType.Position:
case FieldMetadataType.Numeric: {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum FieldMetadataType {
SELECT = 'SELECT',
MULTI_SELECT = 'MULTI_SELECT',
RELATION = 'RELATION',
POSITION = 'POSITION',
}

@Entity('fieldMetadata')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type FieldMetadataDefaultValueMapping = {
| FieldMetadataDynamicDefaultValueNow;
[FieldMetadataType.BOOLEAN]: FieldMetadataDefaultValueBoolean;
[FieldMetadataType.NUMBER]: FieldMetadataDefaultValueNumber;
[FieldMetadataType.POSITION]: FieldMetadataDefaultValueNumber;
[FieldMetadataType.NUMERIC]: FieldMetadataDefaultValueString;
[FieldMetadataType.PROBABILITY]: FieldMetadataDefaultValueNumber;
[FieldMetadataType.LINK]: FieldMetadataDefaultValueLink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function generateTargetColumnMap(
case FieldMetadataType.RATING:
case FieldMetadataType.SELECT:
case FieldMetadataType.MULTI_SELECT:
case FieldMetadataType.POSITION:
return {
value: columnName,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type BasicFieldMetadataType =
| FieldMetadataType.NUMBER
| FieldMetadataType.PROBABILITY
| FieldMetadataType.BOOLEAN
| FieldMetadataType.POSITION
| FieldMetadataType.DATE_TIME;

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const fieldMetadataTypeToColumnType = <Type extends FieldMetadataType>(
return 'numeric';
case FieldMetadataType.NUMBER:
case FieldMetadataType.PROBABILITY:
case FieldMetadataType.POSITION:
return 'float';
case FieldMetadataType.BOOLEAN:
return 'boolean';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class WorkspaceMigrationFactory {
],
[FieldMetadataType.NUMERIC, { factory: this.basicColumnActionFactory }],
[FieldMetadataType.NUMBER, { factory: this.basicColumnActionFactory }],
[FieldMetadataType.POSITION, { factory: this.basicColumnActionFactory }],
[
FieldMetadataType.PROBABILITY,
{ factory: this.basicColumnActionFactory },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PositionScalarType } from './position.scalar';
import { CursorScalarType } from './cursor.scalar';
import { BigFloatScalarType } from './big-float.scalar';
import { BigIntScalarType } from './big-int.scalar';
Expand All @@ -22,4 +23,5 @@ export const scalars = [
TimeScalarType,
UUIDScalarType,
CursorScalarType,
PositionScalarType,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { GraphQLScalarType, Kind } from 'graphql';

type PositionType = 'first' | 'last' | number;

const isValidStringPosition = (value: string): boolean =>
typeof value === 'string' && (value === 'first' || value === 'last');

const isValidNumberPosition = (value: number): boolean =>
typeof value === 'number' && value >= 0;

const checkPosition = (value: any): PositionType => {
if (isValidNumberPosition(value) || isValidStringPosition(value)) {
return value;
}
throw new Error('Invalid position found');
};

export const PositionScalarType = new GraphQLScalarType({
name: 'Position',
description:
'A custom scalar type for representing record position in a list',
serialize: checkPosition,
parseValue: checkPosition,
parseLiteral(ast): PositionType {
if (
ast.kind == Kind.STRING &&
(ast.value === 'first' || ast.value === 'last')
) {
return ast.value;
}

if (ast.kind == Kind.INT || ast.kind == Kind.FLOAT) {
return parseFloat(ast.value);
}

throw new Error('Invalid position found');
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from 'src/workspace/workspace-schema-builder/graphql-types/input';
import { OrderByDirectionType } from 'src/workspace/workspace-schema-builder/graphql-types/enum';
import { BigFloatScalarType } from 'src/workspace/workspace-schema-builder/graphql-types/scalars';
import { PositionScalarType } from 'src/workspace/workspace-schema-builder/graphql-types/scalars/position.scalar';

export interface TypeOptions<T = any> {
nullable?: boolean;
Expand Down Expand Up @@ -66,6 +67,7 @@ export class TypeMapperService {
[FieldMetadataType.NUMERIC, BigFloatScalarType],
[FieldMetadataType.PROBABILITY, GraphQLFloat],
[FieldMetadataType.RELATION, GraphQLID],
[FieldMetadataType.POSITION, PositionScalarType],
]);

return typeScalarMapping.get(fieldMetadataType);
Expand Down Expand Up @@ -96,6 +98,7 @@ export class TypeMapperService {
[FieldMetadataType.NUMERIC, BigFloatFilterType],
[FieldMetadataType.PROBABILITY, FloatFilterType],
[FieldMetadataType.RELATION, UUIDFilterType],
[FieldMetadataType.POSITION, FloatFilterType],
]);

return typeFilterMapping.get(fieldMetadataType);
Expand All @@ -118,6 +121,7 @@ export class TypeMapperService {
[FieldMetadataType.RATING, OrderByDirectionType],
[FieldMetadataType.SELECT, OrderByDirectionType],
[FieldMetadataType.MULTI_SELECT, OrderByDirectionType],
[FieldMetadataType.POSITION, OrderByDirectionType],
]);

return typeOrderByMapping.get(fieldMetadataType);
Expand Down

0 comments on commit ce2901b

Please sign in to comment.