[GroupBy] Fix order by date granularity (#17573)
https://discord.com/channels/1130383047699738754/1466472357496623165/1466472357496623165 before <img width="1262" height="598" alt="image" src="https://github.com/user-attachments/assets/e6fb9a13-58c0-408d-b3e9-a486ec04c33c" /> after <img width="670" height="267" alt="image" src="https://github.com/user-attachments/assets/b4b5ab19-1144-4300-9801-b8220ce928f9" />
This commit is contained in:
+3
-3
@@ -21,7 +21,7 @@ import {
|
||||
type GroupByField,
|
||||
type GroupByRegularField,
|
||||
} from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/types/group-by-field.types';
|
||||
import { getGroupByExpression } from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/utils/get-group-by-expression.util';
|
||||
import { getGroupByOrderExpression } from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/utils/get-group-by-order-expression.util';
|
||||
import { ProcessAggregateHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/process-aggregate.helper';
|
||||
import {
|
||||
type AggregationField,
|
||||
@@ -493,7 +493,7 @@ export class GraphqlQueryOrderGroupByParser {
|
||||
)[0]
|
||||
}"`;
|
||||
|
||||
const expression = getGroupByExpression({
|
||||
const expression = getGroupByOrderExpression({
|
||||
groupByField: associatedGroupByField,
|
||||
columnNameWithQuotes,
|
||||
});
|
||||
@@ -620,7 +620,7 @@ export class GraphqlQueryOrderGroupByParser {
|
||||
|
||||
const columnNameWithQuotes = `"${joinAlias}"."${nestedColumnName}"`;
|
||||
|
||||
const expression = getGroupByExpression({
|
||||
const expression = getGroupByOrderExpression({
|
||||
groupByField: associatedGroupByField,
|
||||
columnNameWithQuotes,
|
||||
});
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
FirstDayOfTheWeek,
|
||||
ObjectRecordGroupByDateGranularity,
|
||||
} from 'twenty-shared/types';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { type GroupByField } from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/types/group-by-field.types';
|
||||
import { getGroupByExpression } from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/utils/get-group-by-expression.util';
|
||||
import { isGroupByDateField } from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/utils/is-group-by-date-field.util';
|
||||
import { isGroupByRelationField } from 'src/engine/api/graphql/graphql-query-runner/group-by/resolvers/utils/is-group-by-relation-field.util';
|
||||
|
||||
const DAYS_OF_WEEK = [
|
||||
'Monday',
|
||||
'Tuesday',
|
||||
'Wednesday',
|
||||
'Thursday',
|
||||
'Friday',
|
||||
'Saturday',
|
||||
'Sunday',
|
||||
] as const;
|
||||
|
||||
const getDayOfWeekOrderExpression = (
|
||||
groupByExpression: string,
|
||||
weekStartDay?: FirstDayOfTheWeek,
|
||||
): string => {
|
||||
const startDay = weekStartDay ?? FirstDayOfTheWeek.MONDAY;
|
||||
|
||||
const startIndex =
|
||||
startDay === FirstDayOfTheWeek.SUNDAY
|
||||
? 6
|
||||
: startDay === FirstDayOfTheWeek.SATURDAY
|
||||
? 5
|
||||
: 0;
|
||||
|
||||
const orderedDays = [
|
||||
...DAYS_OF_WEEK.slice(startIndex),
|
||||
...DAYS_OF_WEEK.slice(0, startIndex),
|
||||
];
|
||||
|
||||
const caseConditions = orderedDays
|
||||
.map((day, index) => `WHEN '${day}' THEN ${index + 1}`)
|
||||
.join(' ');
|
||||
|
||||
return `CASE ${groupByExpression} ${caseConditions} END`;
|
||||
};
|
||||
|
||||
export const getGroupByOrderExpression = ({
|
||||
groupByField,
|
||||
columnNameWithQuotes,
|
||||
}: {
|
||||
groupByField: GroupByField;
|
||||
columnNameWithQuotes: string;
|
||||
}): string => {
|
||||
if (
|
||||
!(isGroupByDateField(groupByField) || isGroupByRelationField(groupByField))
|
||||
) {
|
||||
return getGroupByExpression({ groupByField, columnNameWithQuotes });
|
||||
}
|
||||
|
||||
const dateGranularity = groupByField.dateGranularity;
|
||||
|
||||
if (!isDefined(dateGranularity)) {
|
||||
return getGroupByExpression({ groupByField, columnNameWithQuotes });
|
||||
}
|
||||
|
||||
const groupByExpression = getGroupByExpression({
|
||||
groupByField,
|
||||
columnNameWithQuotes,
|
||||
});
|
||||
|
||||
switch (dateGranularity) {
|
||||
case ObjectRecordGroupByDateGranularity.DAY_OF_THE_WEEK:
|
||||
return getDayOfWeekOrderExpression(
|
||||
groupByExpression,
|
||||
groupByField.weekStartDay,
|
||||
);
|
||||
case ObjectRecordGroupByDateGranularity.MONTH_OF_THE_YEAR:
|
||||
return `CASE ${groupByExpression} WHEN 'January' THEN 1 WHEN 'February' THEN 2 WHEN 'March' THEN 3 WHEN 'April' THEN 4 WHEN 'May' THEN 5 WHEN 'June' THEN 6 WHEN 'July' THEN 7 WHEN 'August' THEN 8 WHEN 'September' THEN 9 WHEN 'October' THEN 10 WHEN 'November' THEN 11 WHEN 'December' THEN 12 END`;
|
||||
default:
|
||||
return groupByExpression;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user