From 42a5102e661687b2c8bfb0a5bd20fc36e0901de2 Mon Sep 17 00:00:00 2001 From: arditdomi <32884230+arditdomi@users.noreply.github.com> Date: Thu, 25 Feb 2021 15:41:05 +0000 Subject: [PATCH] [ACA-4213] - Fix search datetime range in different timezones (#6732) * Fix search datetime range in different timezones * Add unit test checking the conversion from GMT to UTC --- .../search-datetime-range.component.spec.ts | 32 ++++++++++++++++--- .../search-datetime-range.component.ts | 4 +-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.spec.ts b/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.spec.ts index e01ba41e7f..eb4bb1d1d1 100644 --- a/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.spec.ts +++ b/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.spec.ts @@ -124,7 +124,7 @@ describe('SearchDatetimeRangeComponent', () => { expect(context.update).toHaveBeenCalled(); }); - it('should update query builder on value changes', () => { + it('should update the query in UTC format when values change', () => { const context: any = { queryFragments: {}, update() { @@ -143,10 +143,34 @@ describe('SearchDatetimeRangeComponent', () => { to: toDatetime }, true); - const startDate = moment(fromDatetime).startOf('minute').format(); - const endDate = moment(toDatetime).endOf('minute').format(); + const expectedQuery = `cm:created:['2016-10-16T12:30:00Z' TO '2017-10-16T20:00:59Z']`; - const expectedQuery = `cm:created:['${startDate}' TO '${endDate}']`; + expect(context.queryFragments[component.id]).toEqual(expectedQuery); + expect(context.update).toHaveBeenCalled(); + }); + + it('should be able to update the query in UTC format from a GMT format', () => { + const context: any = { + queryFragments: {}, + update() { + } + }; + const fromInGmt = '2021-02-24T17:00:00+02:00'; + const toInGmt = '2021-02-28T15:00:00+02:00'; + + component.id = 'createdDateRange'; + component.context = context; + component.settings = { field: 'cm:created' }; + + spyOn(context, 'update').and.stub(); + + fixture.detectChanges(); + component.apply({ + from: fromInGmt, + to: toInGmt + }, true); + + const expectedQuery = `cm:created:['2021-02-24T15:00:00Z' TO '2021-02-28T13:00:59Z']`; expect(context.queryFragments[component.id]).toEqual(expectedQuery); expect(context.update).toHaveBeenCalled(); diff --git a/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.ts b/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.ts index afa65a3993..728597bc65 100644 --- a/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.ts +++ b/lib/content-services/src/lib/search/components/search-datetime-range/search-datetime-range.component.ts @@ -130,8 +130,8 @@ export class SearchDatetimeRangeComponent implements SearchWidget, OnInit, OnDes if (isValid && this.id && this.context && this.settings && this.settings.field) { this.isActive = true; - const start = moment(model.from).startOf('minute').format(); - const end = moment(model.to).endOf('minute').format(); + const start = moment.utc(model.from).startOf('minute').format(); + const end = moment.utc(model.to).endOf('minute').format(); this.context.queryFragments[this.id] = `${this.settings.field}:['${start}' TO '${end}']`; this.context.update();