From eba755dd11df768bbc21f9d414db66d105a5dbfa Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Sat, 29 Oct 2022 13:18:37 +0530 Subject: [PATCH] refactor: move init code out of initFyo - initFyo just contains the global fyo object --- src/initFyo.ts | 132 ++---------------------------------- src/setup/setupInstance.ts | 2 +- src/utils/initialization.ts | 128 ++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 128 deletions(-) create mode 100644 src/utils/initialization.ts diff --git a/src/initFyo.ts b/src/initFyo.ts index 3be82323..547307cc 100644 --- a/src/initFyo.ts +++ b/src/initFyo.ts @@ -1,130 +1,8 @@ import { Fyo } from 'fyo'; -import { ConfigFile, ConfigKeys } from 'fyo/core/types'; -import { getRegionalModels, models } from 'models'; -import { ModelNameEnum } from 'models/types'; -import { getRandomString, getValueMapFromList } from 'utils'; -export const fyo = new Fyo({ isTest: false, isElectron: true }); +/** + * Global fyo: this is meant to be used only by the app. For + * testing purposes a separate instance of fyo should be initialized. + */ -async function closeDbIfConnected() { - if (!fyo.db.isConnected) { - return; - } - - await fyo.db.purgeCache(); -} - -export async function initializeInstance( - dbPath: string, - isNew: boolean, - countryCode: string, - fyo: Fyo -) { - if (isNew) { - await closeDbIfConnected(); - countryCode = await fyo.db.createNewDatabase(dbPath, countryCode); - } else if (!fyo.db.isConnected) { - countryCode = await fyo.db.connectToDatabase(dbPath); - } - - const regionalModels = await getRegionalModels(countryCode); - await fyo.initializeAndRegister(models, regionalModels); - - await setSingles(fyo); - await setCreds(fyo); - await setVersion(fyo); - setDeviceId(fyo); - await setInstanceId(fyo); - await setOpenCount(fyo); - await setCurrencySymbols(fyo); -} - -async function setSingles(fyo: Fyo) { - await fyo.doc.getDoc(ModelNameEnum.AccountingSettings); - await fyo.doc.getDoc(ModelNameEnum.GetStarted); - await fyo.doc.getDoc(ModelNameEnum.Defaults); - await fyo.doc.getDoc(ModelNameEnum.Misc); -} - -async function setCreds(fyo: Fyo) { - const email = (await fyo.getValue( - ModelNameEnum.AccountingSettings, - 'email' - )) as string | undefined; - const user = fyo.auth.user; - fyo.auth.user = email ?? user; -} - -async function setVersion(fyo: Fyo) { - const version = (await fyo.getValue( - ModelNameEnum.SystemSettings, - 'version' - )) as string | undefined; - - const { appVersion } = fyo.store; - if (version !== appVersion) { - const systemSettings = await fyo.doc.getDoc(ModelNameEnum.SystemSettings); - await systemSettings?.setAndSync('version', appVersion); - } -} - -function setDeviceId(fyo: Fyo) { - let deviceId = fyo.config.get(ConfigKeys.DeviceId) as string | undefined; - if (deviceId === undefined) { - deviceId = getRandomString(); - fyo.config.set(ConfigKeys.DeviceId, deviceId); - } - - fyo.store.deviceId = deviceId; -} - -async function setInstanceId(fyo: Fyo) { - const systemSettings = await fyo.doc.getDoc(ModelNameEnum.SystemSettings); - if (!systemSettings.instanceId) { - await systemSettings.setAndSync('instanceId', getRandomString()); - } - - fyo.store.instanceId = (await fyo.getValue( - ModelNameEnum.SystemSettings, - 'instanceId' - )) as string; -} - -export async function setCurrencySymbols(fyo: Fyo) { - const currencies = (await fyo.db.getAll(ModelNameEnum.Currency, { - fields: ['name', 'symbol'], - })) as { name: string; symbol: string }[]; - - fyo.currencySymbols = getValueMapFromList( - currencies, - 'name', - 'symbol' - ) as Record; -} - -async function setOpenCount(fyo: Fyo) { - const misc = await fyo.doc.getDoc(ModelNameEnum.Misc); - let openCount = misc.openCount as number | null; - - if (typeof openCount !== 'number') { - openCount = getOpenCountFromFiles(fyo); - } - - if (typeof openCount !== 'number') { - openCount = 0; - } - - openCount += 1; - await misc.setAndSync('openCount', openCount); -} - -function getOpenCountFromFiles(fyo: Fyo) { - const configFile = fyo.config.get(ConfigKeys.Files, []) as ConfigFile[]; - for (const file of configFile) { - if (file.id === fyo.singles.SystemSettings?.instanceId) { - return file.openCount ?? 0; - } - } - - return null; -} +export const fyo = new Fyo({ isTest: false, isElectron: true }); \ No newline at end of file diff --git a/src/setup/setupInstance.ts b/src/setup/setupInstance.ts index 9ab34687..f94e15f4 100644 --- a/src/setup/setupInstance.ts +++ b/src/setup/setupInstance.ts @@ -11,8 +11,8 @@ import { AccountRootTypeEnum } from 'models/baseModels/Account/types'; import { AccountingSettings } from 'models/baseModels/AccountingSettings/AccountingSettings'; import { numberSeriesDefaultsMap } from 'models/baseModels/Defaults/Defaults'; import { ModelNameEnum } from 'models/types'; -import { initializeInstance, setCurrencySymbols } from 'src/initFyo'; import { createRegionalRecords } from 'src/regional'; +import { initializeInstance, setCurrencySymbols } from 'src/utils/initialization'; import { getRandomString } from 'utils'; import { defaultUOMs } from 'utils/defaults'; import { getCountryCodeFromCountry, getCountryInfo } from 'utils/misc'; diff --git a/src/utils/initialization.ts b/src/utils/initialization.ts new file mode 100644 index 00000000..51a9df58 --- /dev/null +++ b/src/utils/initialization.ts @@ -0,0 +1,128 @@ +import { Fyo } from 'fyo'; +import { ConfigFile, ConfigKeys } from 'fyo/core/types'; +import { getRegionalModels, models } from 'models/index'; +import { ModelNameEnum } from 'models/types'; +import { getRandomString, getValueMapFromList } from 'utils/index'; + +export async function initializeInstance( + dbPath: string, + isNew: boolean, + countryCode: string, + fyo: Fyo +) { + if (isNew) { + await closeDbIfConnected(fyo); + countryCode = await fyo.db.createNewDatabase(dbPath, countryCode); + } else if (!fyo.db.isConnected) { + countryCode = await fyo.db.connectToDatabase(dbPath); + } + + const regionalModels = await getRegionalModels(countryCode); + await fyo.initializeAndRegister(models, regionalModels); + + await setSingles(fyo); + await setCreds(fyo); + await setVersion(fyo); + setDeviceId(fyo); + await setInstanceId(fyo); + await setOpenCount(fyo); + await setCurrencySymbols(fyo); +} + +async function closeDbIfConnected(fyo: Fyo) { + if (!fyo.db.isConnected) { + return; + } + + await fyo.db.purgeCache(); +} + +async function setSingles(fyo: Fyo) { + await fyo.doc.getDoc(ModelNameEnum.AccountingSettings); + await fyo.doc.getDoc(ModelNameEnum.GetStarted); + await fyo.doc.getDoc(ModelNameEnum.Defaults); + await fyo.doc.getDoc(ModelNameEnum.Misc); +} + +async function setCreds(fyo: Fyo) { + const email = (await fyo.getValue( + ModelNameEnum.AccountingSettings, + 'email' + )) as string | undefined; + const user = fyo.auth.user; + fyo.auth.user = email ?? user; +} + +async function setVersion(fyo: Fyo) { + const version = (await fyo.getValue( + ModelNameEnum.SystemSettings, + 'version' + )) as string | undefined; + + const { appVersion } = fyo.store; + if (version !== appVersion) { + const systemSettings = await fyo.doc.getDoc(ModelNameEnum.SystemSettings); + await systemSettings?.setAndSync('version', appVersion); + } +} + +function setDeviceId(fyo: Fyo) { + let deviceId = fyo.config.get(ConfigKeys.DeviceId) as string | undefined; + if (deviceId === undefined) { + deviceId = getRandomString(); + fyo.config.set(ConfigKeys.DeviceId, deviceId); + } + + fyo.store.deviceId = deviceId; +} + +async function setInstanceId(fyo: Fyo) { + const systemSettings = await fyo.doc.getDoc(ModelNameEnum.SystemSettings); + if (!systemSettings.instanceId) { + await systemSettings.setAndSync('instanceId', getRandomString()); + } + + fyo.store.instanceId = (await fyo.getValue( + ModelNameEnum.SystemSettings, + 'instanceId' + )) as string; +} + +export async function setCurrencySymbols(fyo: Fyo) { + const currencies = (await fyo.db.getAll(ModelNameEnum.Currency, { + fields: ['name', 'symbol'], + })) as { name: string; symbol: string }[]; + + fyo.currencySymbols = getValueMapFromList( + currencies, + 'name', + 'symbol' + ) as Record; +} + +async function setOpenCount(fyo: Fyo) { + const misc = await fyo.doc.getDoc(ModelNameEnum.Misc); + let openCount = misc.openCount as number | null; + + if (typeof openCount !== 'number') { + openCount = getOpenCountFromFiles(fyo); + } + + if (typeof openCount !== 'number') { + openCount = 0; + } + + openCount += 1; + await misc.setAndSync('openCount', openCount); +} + +function getOpenCountFromFiles(fyo: Fyo) { + const configFile = fyo.config.get(ConfigKeys.Files, []) as ConfigFile[]; + for (const file of configFile) { + if (file.id === fyo.singles.SystemSettings?.instanceId) { + return file.openCount ?? 0; + } + } + + return null; +}