2022-04-24 06:48:44 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2022-09-20 17:19:09 +00:00
|
|
|
import countryInfo from '../fixtures/countryInfo.json';
|
|
|
|
import { CUSTOM_EVENTS } from './messages';
|
|
|
|
import { CountryInfoMap, UnexpectedLogObject } from './types';
|
2022-04-23 09:23:44 +00:00
|
|
|
|
|
|
|
export function getCountryInfo(): CountryInfoMap {
|
|
|
|
// @ts-ignore
|
|
|
|
return countryInfo as CountryInfoMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCountryCodeFromCountry(countryName: string): string {
|
|
|
|
const countryInfoMap = getCountryInfo();
|
|
|
|
const countryInfo = countryInfoMap[countryName];
|
|
|
|
if (countryInfo === undefined) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return countryInfo.code;
|
|
|
|
}
|
2022-04-24 06:48:44 +00:00
|
|
|
|
2022-12-14 06:57:40 +00:00
|
|
|
export function getFiscalYear(
|
|
|
|
date: string,
|
|
|
|
isStart: boolean
|
|
|
|
): undefined | Date {
|
2022-04-24 06:48:44 +00:00
|
|
|
if (!date) {
|
2022-12-14 06:57:40 +00:00
|
|
|
return undefined;
|
2022-04-24 06:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const today = DateTime.local();
|
|
|
|
const dateTime = DateTime.fromFormat(date, 'MM-dd');
|
|
|
|
if (isStart) {
|
|
|
|
return dateTime
|
|
|
|
.plus({ year: [1, 2, 3].includes(today.month) ? -1 : 0 })
|
2022-12-14 06:57:40 +00:00
|
|
|
.toJSDate();
|
2022-04-24 06:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dateTime
|
|
|
|
.plus({ year: [1, 2, 3].includes(today.month) ? 0 : 1 })
|
2022-12-14 06:57:40 +00:00
|
|
|
.toJSDate();
|
2022-04-24 06:48:44 +00:00
|
|
|
}
|
2022-09-20 17:19:09 +00:00
|
|
|
|
|
|
|
export function logUnexpected(detail: Partial<UnexpectedLogObject>) {
|
|
|
|
/**
|
|
|
|
* Raises a custom event, it's lsitener is in renderer.ts
|
|
|
|
* used to log unexpected occurances as errors.
|
|
|
|
*/
|
|
|
|
if (!window?.CustomEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
detail.name ??= 'LogUnexpected';
|
|
|
|
detail.message ??= 'Logging an unexpected occurance';
|
|
|
|
detail.stack ??= new Error().stack;
|
|
|
|
detail.more ??= {};
|
|
|
|
|
|
|
|
const event = new window.CustomEvent(CUSTOM_EVENTS.LOG_UNEXPECTED, {
|
|
|
|
detail,
|
|
|
|
});
|
|
|
|
window.dispatchEvent(event);
|
|
|
|
}
|