2022-04-19 05:59:36 +00:00
|
|
|
import { Fyo } from 'fyo';
|
|
|
|
import { ConfigFile, ConfigKeys } from 'fyo/core/types';
|
|
|
|
import { DEFAULT_COUNTRY_CODE } from 'fyo/utils/consts';
|
2022-05-23 08:09:07 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
|
|
|
import { getRandomString } from 'utils';
|
|
|
|
import { UniqueId } from './types';
|
2022-04-19 05:59:36 +00:00
|
|
|
|
|
|
|
export function getCountry(fyo: Fyo): string {
|
|
|
|
return (
|
|
|
|
(fyo.singles.SystemSettings?.countryCode as string) ?? DEFAULT_COUNTRY_CODE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLanguage(fyo: Fyo): string {
|
|
|
|
return fyo.config.get('language') as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDeviceId(fyo: Fyo): UniqueId {
|
|
|
|
let deviceId = fyo.config.get(ConfigKeys.DeviceId) as string | undefined;
|
|
|
|
if (deviceId === undefined) {
|
2022-05-23 08:09:07 +00:00
|
|
|
deviceId = getRandomString();
|
2022-04-19 05:59:36 +00:00
|
|
|
fyo.config.set(ConfigKeys.DeviceId, deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return deviceId;
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:28:33 +00:00
|
|
|
export async function getInstanceId(fyo: Fyo): Promise<UniqueId> {
|
|
|
|
const instanceId = (await fyo.getValue(
|
|
|
|
ModelNameEnum.SystemSettings,
|
|
|
|
'instanceId'
|
|
|
|
)) as string;
|
|
|
|
const companyName = (await fyo.getValue(
|
|
|
|
ModelNameEnum.AccountingSettings,
|
|
|
|
'companyName'
|
|
|
|
)) as string;
|
2022-05-23 08:09:07 +00:00
|
|
|
const dbPath = fyo.db.dbPath!;
|
2022-05-23 09:28:33 +00:00
|
|
|
const files = (fyo.config.get(ConfigKeys.Files) ?? []) as ConfigFile[];
|
2022-04-19 05:59:36 +00:00
|
|
|
|
2022-05-23 08:09:07 +00:00
|
|
|
let file = files.find((f) => f.id === instanceId);
|
2022-04-19 05:59:36 +00:00
|
|
|
|
|
|
|
if (file === undefined) {
|
2022-05-23 08:09:07 +00:00
|
|
|
file = addNewConfigFile(companyName, dbPath, instanceId, files, fyo);
|
2022-04-19 05:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 08:09:07 +00:00
|
|
|
if (!file.id) {
|
|
|
|
setIdOnConfigFile(instanceId, companyName, dbPath, files, fyo);
|
2022-04-19 05:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 08:09:07 +00:00
|
|
|
return instanceId;
|
2022-04-19 05:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 08:09:07 +00:00
|
|
|
export function addNewConfigFile(
|
2022-04-19 05:59:36 +00:00
|
|
|
companyName: string,
|
2022-05-23 08:09:07 +00:00
|
|
|
dbPath: string,
|
|
|
|
instanceId: string,
|
|
|
|
files: ConfigFile[],
|
|
|
|
fyo: Fyo
|
|
|
|
): ConfigFile {
|
2022-04-19 05:59:36 +00:00
|
|
|
const newFile: ConfigFile = {
|
|
|
|
companyName,
|
2022-05-11 12:36:26 +00:00
|
|
|
dbPath,
|
2022-05-23 08:09:07 +00:00
|
|
|
id: instanceId,
|
2022-05-23 06:21:06 +00:00
|
|
|
openCount: 0,
|
2022-04-19 05:59:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
files.push(newFile);
|
|
|
|
fyo.config.set(ConfigKeys.Files, files);
|
2022-05-23 08:09:07 +00:00
|
|
|
return newFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getVersion(fyo: Fyo) {
|
|
|
|
const version = (await fyo.getValue(
|
|
|
|
ModelNameEnum.SystemSettings,
|
|
|
|
'version'
|
|
|
|
)) as string | undefined;
|
|
|
|
|
|
|
|
if (version) {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fyo.store.appVersion;
|
2022-04-19 05:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 08:09:07 +00:00
|
|
|
function setIdOnConfigFile(
|
|
|
|
instanceId: string,
|
2022-04-19 05:59:36 +00:00
|
|
|
companyName: string,
|
2022-05-23 08:09:07 +00:00
|
|
|
dbPath: string,
|
2022-04-19 05:59:36 +00:00
|
|
|
files: ConfigFile[],
|
|
|
|
fyo: Fyo
|
2022-05-23 08:09:07 +00:00
|
|
|
) {
|
2022-04-19 05:59:36 +00:00
|
|
|
for (const file of files) {
|
2022-05-23 08:09:07 +00:00
|
|
|
if (file.companyName !== companyName || file.dbPath !== dbPath) {
|
2022-04-19 05:59:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-23 08:09:07 +00:00
|
|
|
file.id = instanceId;
|
2022-04-19 05:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fyo.config.set(ConfigKeys.Files, files);
|
|
|
|
}
|