2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00
books/utils/misc.ts
18alantom 8d3d0a29ee incr: add logUnexpected
- prevent undefined rootType, log it instead
2022-09-20 22:49:09 +05:30

58 lines
1.5 KiB
TypeScript

import { DateTime } from 'luxon';
import countryInfo from '../fixtures/countryInfo.json';
import { CUSTOM_EVENTS } from './messages';
import { CountryInfoMap, UnexpectedLogObject } from './types';
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;
}
export function getFiscalYear(date: string, isStart: boolean) {
if (!date) {
return '';
}
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 })
.toISODate();
}
return dateTime
.plus({ year: [1, 2, 3].includes(today.month) ? 0 : 1 })
.toISODate();
}
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);
}