From b38006455d60d55e274dede110fd219e1a9b6549 Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Fri, 19 Jul 2024 15:27:28 +0200 Subject: [PATCH] Fixed sort for Currency type (#6333) Fixes : #6056 - Refactored the logic to get order by query variables for field types - Added a case for Currency field type --- .../constants/SortableFieldMetadataTypes.ts | 15 ++++++++ ...rmatFieldMetadataItemsAsSortDefinitions.ts | 17 ++------- .../utils/getObjectOrderByField.ts | 23 +++--------- .../utils/getOrderByForFieldMetadataType.ts | 35 +++++++++++++++++++ .../utils/turnSortsIntoOrderBy.ts | 35 +++++++------------ 5 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 packages/twenty-front/src/modules/object-metadata/constants/SortableFieldMetadataTypes.ts create mode 100644 packages/twenty-front/src/modules/object-metadata/utils/getOrderByForFieldMetadataType.ts diff --git a/packages/twenty-front/src/modules/object-metadata/constants/SortableFieldMetadataTypes.ts b/packages/twenty-front/src/modules/object-metadata/constants/SortableFieldMetadataTypes.ts new file mode 100644 index 000000000000..8779f49e016c --- /dev/null +++ b/packages/twenty-front/src/modules/object-metadata/constants/SortableFieldMetadataTypes.ts @@ -0,0 +1,15 @@ +import { FieldMetadataType } from '~/generated-metadata/graphql'; + +export const SORTABLE_FIELD_METADATA_TYPES = [ + FieldMetadataType.DateTime, + FieldMetadataType.Date, + FieldMetadataType.Number, + FieldMetadataType.Text, + FieldMetadataType.Boolean, + FieldMetadataType.Select, + FieldMetadataType.Phone, + FieldMetadataType.Email, + FieldMetadataType.FullName, + FieldMetadataType.Rating, + FieldMetadataType.Currency, +]; diff --git a/packages/twenty-front/src/modules/object-metadata/utils/formatFieldMetadataItemsAsSortDefinitions.ts b/packages/twenty-front/src/modules/object-metadata/utils/formatFieldMetadataItemsAsSortDefinitions.ts index 56aca5fcc579..12f68c2e9330 100644 --- a/packages/twenty-front/src/modules/object-metadata/utils/formatFieldMetadataItemsAsSortDefinitions.ts +++ b/packages/twenty-front/src/modules/object-metadata/utils/formatFieldMetadataItemsAsSortDefinitions.ts @@ -1,6 +1,6 @@ import { SortDefinition } from '@/object-record/object-sort-dropdown/types/SortDefinition'; -import { FieldMetadataType } from '~/generated-metadata/graphql'; +import { SORTABLE_FIELD_METADATA_TYPES } from '@/object-metadata/constants/SortableFieldMetadataTypes'; import { ObjectMetadataItem } from '../types/ObjectMetadataItem'; export const formatFieldMetadataItemsAsSortDefinitions = ({ @@ -9,20 +9,7 @@ export const formatFieldMetadataItemsAsSortDefinitions = ({ fields: Array; }): SortDefinition[] => fields.reduce((acc, field) => { - if ( - ![ - FieldMetadataType.DateTime, - FieldMetadataType.Date, - FieldMetadataType.Number, - FieldMetadataType.Text, - FieldMetadataType.Boolean, - FieldMetadataType.Select, - FieldMetadataType.Phone, - FieldMetadataType.Email, - FieldMetadataType.FullName, - FieldMetadataType.Rating, - ].includes(field.type) - ) { + if (!SORTABLE_FIELD_METADATA_TYPES.includes(field.type)) { return acc; } diff --git a/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts b/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts index 4b3b080f848a..d6367fcc47b5 100644 --- a/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts +++ b/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts @@ -1,8 +1,8 @@ import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { OrderBy } from '@/object-metadata/types/OrderBy'; import { getLabelIdentifierFieldMetadataItem } from '@/object-metadata/utils/getLabelIdentifierFieldMetadataItem'; +import { getOrderByForFieldMetadataType } from '@/object-metadata/utils/getOrderByForFieldMetadataType'; import { RecordGqlOperationOrderBy } from '@/object-record/graphql/types/RecordGqlOperationOrderBy'; -import { FieldMetadataType } from '~/generated-metadata/graphql'; import { isDefined } from '~/utils/isDefined'; export const getOrderByFieldForObjectMetadataItem = ( @@ -13,23 +13,10 @@ export const getOrderByFieldForObjectMetadataItem = ( getLabelIdentifierFieldMetadataItem(objectMetadataItem); if (isDefined(labelIdentifierFieldMetadata)) { - switch (labelIdentifierFieldMetadata.type) { - case FieldMetadataType.FullName: - return [ - { - [labelIdentifierFieldMetadata.name]: { - firstName: orderBy ?? 'AscNullsLast', - lastName: orderBy ?? 'AscNullsLast', - }, - }, - ]; - default: - return [ - { - [labelIdentifierFieldMetadata.name]: orderBy ?? 'AscNullsLast', - }, - ]; - } + return getOrderByForFieldMetadataType( + labelIdentifierFieldMetadata, + orderBy, + ); } else { return [ { diff --git a/packages/twenty-front/src/modules/object-metadata/utils/getOrderByForFieldMetadataType.ts b/packages/twenty-front/src/modules/object-metadata/utils/getOrderByForFieldMetadataType.ts new file mode 100644 index 000000000000..ca189703446e --- /dev/null +++ b/packages/twenty-front/src/modules/object-metadata/utils/getOrderByForFieldMetadataType.ts @@ -0,0 +1,35 @@ +import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; +import { OrderBy } from '@/object-metadata/types/OrderBy'; +import { RecordGqlOperationOrderBy } from '@/object-record/graphql/types/RecordGqlOperationOrderBy'; +import { FieldMetadataType } from '~/generated-metadata/graphql'; + +export const getOrderByForFieldMetadataType = ( + field: Pick, + direction: OrderBy | null | undefined, +): RecordGqlOperationOrderBy => { + switch (field.type) { + case FieldMetadataType.FullName: + return [ + { + [field.name]: { + firstName: direction ?? 'AscNullsLast', + lastName: direction ?? 'AscNullsLast', + }, + }, + ]; + case FieldMetadataType.Currency: + return [ + { + [field.name]: { + amountMicros: direction ?? 'AscNullsLast', + }, + }, + ]; + default: + return [ + { + [field.name]: direction ?? 'AscNullsLast', + }, + ]; + } +}; diff --git a/packages/twenty-front/src/modules/object-record/object-sort-dropdown/utils/turnSortsIntoOrderBy.ts b/packages/twenty-front/src/modules/object-record/object-sort-dropdown/utils/turnSortsIntoOrderBy.ts index 04e877109434..5a33d53991cc 100644 --- a/packages/twenty-front/src/modules/object-record/object-sort-dropdown/utils/turnSortsIntoOrderBy.ts +++ b/packages/twenty-front/src/modules/object-record/object-sort-dropdown/utils/turnSortsIntoOrderBy.ts @@ -2,20 +2,23 @@ import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { OrderBy } from '@/object-metadata/types/OrderBy'; import { hasPositionField } from '@/object-metadata/utils/hasPositionField'; import { RecordGqlOperationOrderBy } from '@/object-record/graphql/types/RecordGqlOperationOrderBy'; -import { Field, FieldMetadataType } from '~/generated/graphql'; import { mapArrayToObject } from '~/utils/array/mapArrayToObject'; import { isDefined } from '~/utils/isDefined'; import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; +import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; +import { getOrderByForFieldMetadataType } from '@/object-metadata/utils/getOrderByForFieldMetadataType'; import { Sort } from '../types/Sort'; export const turnSortsIntoOrderBy = ( objectMetadataItem: ObjectMetadataItem, sorts: Sort[], ): RecordGqlOperationOrderBy => { - const fields: Pick[] = + const fields: Pick[] = objectMetadataItem?.fields ?? []; + const fieldsById = mapArrayToObject(fields, ({ id }) => id); + const sortsOrderBy = sorts .map((sort) => { const correspondingField = fieldsById[sort.fieldMetadataId]; @@ -32,26 +35,14 @@ export const turnSortsIntoOrderBy = ( .filter(isDefined); if (hasPositionField(objectMetadataItem)) { - return [...sortsOrderBy, { position: 'AscNullsFirst' }]; - } + const positionOrderBy = [ + { + position: 'AscNullsFirst', + }, + ] satisfies RecordGqlOperationOrderBy; - return sortsOrderBy; -}; - -const getOrderByForFieldMetadataType = ( - field: Pick, - direction: OrderBy, -) => { - switch (field.type) { - case FieldMetadataType.FullName: - return { - [field.name]: { - firstName: direction, - lastName: direction, - }, - }; - - default: - return { [field.name]: direction }; + return [...sortsOrderBy, ...positionOrderBy].flat(); } + + return sortsOrderBy.flat(); };