Skip to content

Commit

Permalink
Fixed sort for Currency type (#6333)
Browse files Browse the repository at this point in the history
Fixes : #6056 

- Refactored the logic to get order by query variables for field types
- Added a case for Currency field type
  • Loading branch information
lucasbordeau authored Jul 19, 2024
1 parent d488b7f commit b380064
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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,
];
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand All @@ -9,20 +9,7 @@ export const formatFieldMetadataItemsAsSortDefinitions = ({
fields: Array<ObjectMetadataItem['fields'][0]>;
}): 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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 [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FieldMetadataItem, 'id' | 'name' | 'type'>,
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',
},
];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<Field, 'id' | 'name' | 'type'>[] =
const fields: Pick<FieldMetadataItem, 'id' | 'name' | 'type'>[] =
objectMetadataItem?.fields ?? [];

const fieldsById = mapArrayToObject(fields, ({ id }) => id);

const sortsOrderBy = sorts
.map((sort) => {
const correspondingField = fieldsById[sort.fieldMetadataId];
Expand All @@ -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<Field, 'id' | 'name' | 'type'>,
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();
};

0 comments on commit b380064

Please sign in to comment.