Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sort for Currency type #6333

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Weiko marked this conversation as resolved.
Show resolved Hide resolved
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();
};
Loading