-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UIPQB-126] Convert local date to UTC with respect to tenant zone (#180)
* [UIPQB-126] Convert local date to UTC with respect to tenant zone * cleanup diff * Most tests * more coverage * Refactor * coverage
- Loading branch information
1 parent
dcf2d01
commit 229c653
Showing
10 changed files
with
180 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { userLocaleConfig, useStripes, useOkapiKy } from '@folio/stripes/core'; | ||
import { useQuery } from 'react-query'; | ||
|
||
export function getQueryWarning(tenantTimezone, userTimezone) { | ||
if (tenantTimezone === userTimezone) { | ||
return null; | ||
} | ||
if (!tenantTimezone || !userTimezone) { | ||
return null; | ||
} | ||
|
||
// TODO: add a warning here!, and also use this... | ||
return 'a warning should go here! TODO [UIPQB-155]'; | ||
} | ||
|
||
export async function getConfigEntryTimezone(ky, query) { | ||
return JSON.parse( | ||
( | ||
await ky | ||
.get('configurations/entries', { | ||
searchParams: { | ||
query, | ||
}, | ||
}) | ||
.json() | ||
).configs?.[0].value ?? '{}', | ||
).timezone; | ||
} | ||
|
||
/** | ||
* Determines the timezone that should be used when building a query and displaying results. | ||
* | ||
* We specifically do NOT want the user's timezone, as this may cause weird inconsistencies | ||
* if the same queries are run by different users in the same tenant. As such, we will always | ||
* use the tenant's timezone. | ||
* | ||
* Additionally, the backend will always use the tenant's timezone when exporting queries, so | ||
* we want to ensure expectations are met across the board. | ||
* | ||
* @returns `{ | ||
* userTimezone?: string; | ||
* tenantTimezone?: string; | ||
* timezoneQueryWarning: ReactNode; | ||
* }` | ||
* `userTimezone`: The timezone of the current user if an override exists, the timezone of the | ||
* tenant if no override exists, or undefined if queries are still resolving. | ||
* `tenantTimezone`: The timezone of the tenant, or undefined if the query is still resolving. | ||
* `timezoneQueryWarning`: A warning message that should be displayed if the query contains | ||
* date fields. Will only be present iff userTimezone ≠ tenantTimezone. | ||
*/ | ||
export default function useTenantTimezone() { | ||
const stripes = useStripes(); | ||
const ky = useOkapiKy(); | ||
|
||
const tenantTimezone = useQuery({ | ||
queryKey: ['@folio/plugin-query-builder', 'timezone-config', 'tenant'], | ||
queryFn: () => getConfigEntryTimezone( | ||
ky, | ||
`(${[ | ||
'module==ORG', | ||
'configName == localeSettings', | ||
'(cql.allRecords=1 NOT userId="" NOT code="")', | ||
].join(' AND ')})`, | ||
), | ||
refetchOnMount: false, | ||
}); | ||
|
||
const userTimezone = useQuery({ | ||
queryKey: ['@folio/plugin-query-builder', 'timezone-config', 'user'], | ||
queryFn: () => getConfigEntryTimezone( | ||
ky, | ||
`(${Object.entries({ ...userLocaleConfig, userId: stripes.user.user.id }) | ||
.map(([k, v]) => `"${k}"=="${v}"`) | ||
.join(' AND ')})`, | ||
), | ||
refetchOnMount: false, | ||
}); | ||
|
||
return { | ||
userTimezone: userTimezone.data ?? tenantTimezone.data, | ||
tenantTimezone: tenantTimezone.data, | ||
timezoneQueryWarning: getQueryWarning(tenantTimezone.data, userTimezone.data), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getQueryWarning } from './useTenantTimezone'; | ||
|
||
describe('timezone query warning', () => { | ||
it.each([ | ||
[null, null], | ||
[null, undefined], | ||
['', null], | ||
['', undefined], | ||
['Narnia', null], | ||
['Narnia', undefined], | ||
['Narnia', 'Narnia'], | ||
])('returns no warning for [%s,%s]', (a, b) => { | ||
expect(getQueryWarning(a, b)).toBeNull(); | ||
expect(getQueryWarning(b, a)).toBeNull(); | ||
}); | ||
|
||
it('returns a warning for different timezones', () => { | ||
expect(getQueryWarning('Narnia', 'Atlantis')).not.toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters