Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/kind-geckos-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes incorrect start date on omnichannel reports
101 changes: 101 additions & 0 deletions apps/meteor/client/components/dashboards/getClosedPeriod.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { getClosedPeriod } from './periods';

jest.mock('moment', () => {
return () => jest.requireActual('moment')('2024-05-19T12:00:00.000Z');
});

it('should return the correct period range for this month', () => {
const monthExpectedReturn = {
start: new Date('5/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('month')(true);

expect(period.start.toISOString().split('T')[0]).toEqual(monthExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(monthExpectedReturn.end);
});

it('should return the correct period range for this year', () => {
const yearExpectedReturn = {
start: new Date('1/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('year')(true);

expect(period.start.toISOString().split('T')[0]).toEqual(yearExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(yearExpectedReturn.end);
});

it('should return the correct period range for last 6 months', () => {
const last6MonthsExpectedReturn = {
start: new Date('11/1/2023').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('month', 6)(true);

expect(period.start.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.end);
});

it('should return the correct period range for this week', () => {
const weekExpectedReturn = {
start: new Date('5/19/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('week')(true);

expect(period.start.toISOString().split('T')[0]).toEqual(weekExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(weekExpectedReturn.end);
});

it('should return the correct period range for this month using local time', () => {
const monthExpectedReturn = {
start: new Date('5/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('month')(false);

expect(period.start.toISOString().split('T')[0]).toEqual(monthExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(monthExpectedReturn.end);
});

it('should return the correct period range for this year using local time', () => {
const yearExpectedReturn = {
start: new Date('1/1/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('year')(false);

expect(period.start.toISOString().split('T')[0]).toEqual(yearExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(yearExpectedReturn.end);
});

it('should return the correct period range for last 6 months using local time', () => {
const last6MonthsExpectedReturn = {
start: new Date('11/1/2023').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('month', 6)(false);

expect(period.start.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(last6MonthsExpectedReturn.end);
});

it('should return the correct period range for this week using local time', () => {
const weekExpectedReturn = {
start: new Date('5/19/2024').toISOString().split('T')[0],
end: new Date('5/19/2024').toISOString().split('T')[0],
};

const period = getClosedPeriod('week')(false);

expect(period.start.toISOString().split('T')[0]).toEqual(weekExpectedReturn.start);
expect(period.end.toISOString().split('T')[0]).toEqual(weekExpectedReturn.end);
});
32 changes: 22 additions & 10 deletions apps/meteor/client/components/dashboards/periods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ const lastNDays =
return { start, end };
};

const getLast6Months = () => {
const last6Months = moment().subtract(6, 'months');
return moment().diff(last6Months, 'days');
};
export const getClosedPeriod =
Comment thread
aleksandernsilva marked this conversation as resolved.
Outdated
(
startOf: 'year' | 'month' | 'week',
subtract = 0,
): ((utc: boolean) => {
start: Date;
end: Date;
}) =>
(utc): { start: Date; end: Date } => {
const date = utc ? moment(new Date()).utc().toDate() : new Date();

const start = moment(date).subtract(subtract, 'months').startOf(startOf).toDate();
const end = moment(date).endOf('day').toDate();

return { start, end };
};

const periods = [
{
Expand All @@ -37,7 +49,7 @@ const periods = [
{
key: 'this week',
label: label('This_week'),
range: lastNDays(moment().day()),
range: getClosedPeriod('week'),
},
{
key: 'last 7 days',
Expand All @@ -52,7 +64,7 @@ const periods = [
{
key: 'this month',
label: label('This_month'),
range: lastNDays(moment().date()),
range: getClosedPeriod('month'),
},
{
key: 'last 30 days',
Expand All @@ -67,12 +79,12 @@ const periods = [
{
key: 'last 6 months',
label: label('Last_6_months'),
range: lastNDays(getLast6Months()),
range: getClosedPeriod('month', 6),
},
{
key: 'this year',
label: label('This_year'),
range: lastNDays(moment().dayOfYear()),
range: getClosedPeriod('year'),
},
] as const;

Expand All @@ -82,7 +94,7 @@ export const getPeriod = (key: (typeof periods)[number]['key']): Period => {
const period = periods.find((period) => period.key === key);

if (!period) {
throw new Error(`"${key}" is not a valid period key`);
return periods[0];
}

return period;
Expand All @@ -98,7 +110,7 @@ export const getPeriodRange = (
const period = periods.find((period) => period.key === key);

if (!period) {
throw new Error(`"${key}" is not a valid period key`);
return periods[0].range(utc);
}

return period.range(utc);
Expand Down