diff --git a/models/types.ts b/models/types.ts index bd84d599..d7ef7bda 100644 --- a/models/types.ts +++ b/models/types.ts @@ -12,6 +12,7 @@ export enum ModelNameEnum { UOM = 'UOM', JournalEntry = 'JournalEntry', JournalEntryAccount = 'JournalEntryAccount', + Misc = 'Misc', NumberSeries = 'NumberSeries', Party = 'Party', Payment = 'Payment', diff --git a/schemas/app/Misc.json b/schemas/app/Misc.json new file mode 100644 index 00000000..7d8f5c1e --- /dev/null +++ b/schemas/app/Misc.json @@ -0,0 +1,13 @@ +{ + "name": "Misc", + "label": "Misc", + "isSingle": true, + "isChild": false, + "fields": [ + { + "fieldname": "openCount", + "label": "Open Count", + "fieldtype": "Int" + } + ] +} diff --git a/schemas/schemas.ts b/schemas/schemas.ts index d9e23457..3fb6cc36 100644 --- a/schemas/schemas.ts +++ b/schemas/schemas.ts @@ -1,6 +1,7 @@ import Account from './app/Account.json'; import AccountingLedgerEntry from './app/AccountingLedgerEntry.json'; import AccountingSettings from './app/AccountingSettings.json'; +import Misc from './app/Misc.json'; import Address from './app/Address.json'; import Color from './app/Color.json'; import CompanySettings from './app/CompanySettings.json'; @@ -46,6 +47,7 @@ export const metaSchemas: SchemaStub[] = [ ]; export const appSchemas: Schema[] | SchemaStub[] = [ + Misc as Schema, SetupWizard as Schema, GetStarted as Schema, diff --git a/src/App.vue b/src/App.vue index c9f13e0e..f0708d95 100644 --- a/src/App.vue +++ b/src/App.vue @@ -48,6 +48,7 @@ import SetupWizard from './pages/SetupWizard/SetupWizard.vue'; import setupInstance from './setup/setupInstance'; import './styles/index.css'; import { checkForUpdates } from './utils/ipcCalls'; +import { updateConfigFiles } from './utils/misc'; import { Search } from './utils/search'; import { routeTo } from './utils/ui'; @@ -132,6 +133,7 @@ export default { } await initializeInstance(filePath, false, countryCode, fyo); + await updateConfigFiles(fyo); await this.setDesk(filePath); }, async setDeskRoute() { diff --git a/src/initFyo.ts b/src/initFyo.ts index 7fea1a99..eff97398 100644 --- a/src/initFyo.ts +++ b/src/initFyo.ts @@ -35,13 +35,14 @@ export async function initializeInstance( await setVersion(fyo); setDeviceId(fyo); await setInstanceId(fyo); - await incrementOpenCount(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.Misc); } async function setCreds(fyo: Fyo) { @@ -75,6 +76,7 @@ function setDeviceId(fyo: Fyo) { fyo.store.deviceId = deviceId; } + async function setInstanceId(fyo: Fyo) { const systemSettings = await fyo.doc.getDoc(ModelNameEnum.SystemSettings); if (!systemSettings.instanceId) { @@ -99,25 +101,29 @@ async function setCurrencySymbols(fyo: Fyo) { ) as Record; } -export async function incrementOpenCount(fyo: Fyo) { - const instanceId = (await fyo.getValue( - ModelNameEnum.SystemSettings, - 'instanceId' - )) as string; +async function setOpenCount(fyo: Fyo) { + const misc = await fyo.doc.getDoc(ModelNameEnum.Misc); + let openCount = misc.openCount as number | null; - let openCount = 0; - const files = (fyo.config.get(ConfigKeys.Files) ?? []) as ConfigFile[]; - for (const file of files) { - if (file.id !== instanceId) { - continue; - } - - file.openCount ??= 0; - file.openCount += 1; - openCount = file.openCount; - break; + if (typeof openCount !== 'number') { + openCount = getOpenCountFromFiles(fyo); } - fyo.config.set(ConfigKeys.Files, files); - fyo.store.openCount = openCount; + 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; } diff --git a/src/pages/DatabaseSelector.vue b/src/pages/DatabaseSelector.vue index 6d88e578..fa5102b5 100644 --- a/src/pages/DatabaseSelector.vue +++ b/src/pages/DatabaseSelector.vue @@ -214,7 +214,6 @@ import { setupDummyInstance } from 'dummy'; import { ipcRenderer } from 'electron'; import { t } from 'fyo'; -import { ConfigKeys } from 'fyo/core/types'; import { DateTime } from 'luxon'; import Button from 'src/components/Button.vue'; import LanguageSelector from 'src/components/Controls/LanguageSelector.vue'; @@ -223,7 +222,7 @@ import Loading from 'src/components/Loading.vue'; import Modal from 'src/components/Modal.vue'; import { fyo } from 'src/initFyo'; import { deleteDb, getSavePath } from 'src/utils/ipcCalls'; -import { addNewConfigFile } from 'src/utils/misc'; +import { updateConfigFiles } from 'src/utils/misc'; import { showMessageDialog } from 'src/utils/ui'; import { IPC_ACTIONS } from 'utils/messages'; @@ -288,7 +287,7 @@ export default { } this.creatingDemo = true; - const { companyName, instanceId } = await setupDummyInstance( + await setupDummyInstance( filePath, fyo, 1, @@ -299,14 +298,7 @@ export default { } ); - addNewConfigFile( - companyName, - filePath, - instanceId, - fyo.config.get(ConfigKeys.Files), - fyo - ); - + updateConfigFiles(fyo); fyo.purgeCache(); await this.setFiles(); diff --git a/src/setup/setupInstance.ts b/src/setup/setupInstance.ts index 201ea64b..40a12707 100644 --- a/src/setup/setupInstance.ts +++ b/src/setup/setupInstance.ts @@ -1,5 +1,5 @@ import { Fyo } from 'fyo'; -import { ConfigFile, DocValueMap } from 'fyo/core/types'; +import { DocValueMap } from 'fyo/core/types'; import { Doc } from 'fyo/model/doc'; import { createNumberSeries } from 'fyo/model/naming'; import { @@ -224,28 +224,9 @@ async function setDefaultAccounts(fyo: Fyo) { } async function completeSetup(companyName: string, fyo: Fyo) { - await updateInitializationConfig(companyName, fyo); await fyo.singles.AccountingSettings!.setAndSync('setupComplete', true); } -async function updateInitializationConfig(companyName: string, fyo: Fyo) { - const instanceId = (await fyo.getValue( - ModelNameEnum.SystemSettings, - 'instanceId' - )) as string; - const dbPath = fyo.db.dbPath; - const files = fyo.config.get('files', []) as ConfigFile[]; - - files.forEach((file) => { - if (file.dbPath === dbPath) { - file.companyName = companyName; - file.id = instanceId; - } - }); - - fyo.config.set('files', files); -} - async function checkAndCreateDoc( schemaName: string, docObject: DocValueMap, diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 4c688c4b..bf568aa8 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -58,22 +58,27 @@ export async function getSetupWizardDoc() { ); } -export function addNewConfigFile( - companyName: string, - dbPath: string, - instanceId: string, - files: ConfigFile[], - fyo: Fyo -): ConfigFile { - const newFile: ConfigFile = { - companyName, - dbPath, - id: instanceId, - openCount: 0, - }; +export function updateConfigFiles(fyo: Fyo): ConfigFile { + const configFiles = fyo.config.get(ConfigKeys.Files, []) as ConfigFile[]; - files.push(newFile); - fyo.config.set(ConfigKeys.Files, files); + const companyName = fyo.singles.AccountingSettings!.companyName as string; + const id = fyo.singles.SystemSettings!.instanceId as string; + const dbPath = fyo.db.dbPath!; + const openCount = fyo.singles.Misc!.openCount as number; + + const fileIndex = configFiles.findIndex((f) => f.id === id); + let newFile = { id, companyName, dbPath, openCount } as ConfigFile; + + if (fileIndex === -1) { + configFiles.push(newFile); + } else { + configFiles[fileIndex].companyName = companyName; + configFiles[fileIndex].dbPath = dbPath; + configFiles[fileIndex].openCount = openCount; + newFile = configFiles[fileIndex]; + } + + fyo.config.set(ConfigKeys.Files, configFiles); return newFile; }