2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 09:24:04 +00:00
books/fyo/index.ts

241 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-03-22 09:28:36 +00:00
import { getMoneyMaker, MoneyMaker } from 'pesa';
2023-04-13 12:39:33 +00:00
import { Field, FieldType } from 'schemas/types';
import { getIsNullOrUndef } from 'utils';
2022-03-22 09:28:36 +00:00
import { markRaw } from 'vue';
import { AuthHandler } from './core/authHandler';
import { DatabaseHandler } from './core/dbHandler';
import { DocHandler } from './core/docHandler';
import { DocValue, FyoConfig } from './core/types';
import { Config } from './demux/config';
import { Doc } from './model/doc';
import { ModelMap } from './model/types';
import { TelemetryManager } from './telemetry/telemetry';
2022-03-22 09:28:36 +00:00
import {
DEFAULT_CURRENCY,
2022-03-22 09:28:36 +00:00
DEFAULT_DISPLAY_PRECISION,
DEFAULT_INTERNAL_PRECISION,
} from './utils/consts';
import * as errors from './utils/errors';
import { format } from './utils/format';
import { t, T } from './utils/translation';
import { ErrorLog } from './utils/types';
2023-06-13 08:07:39 +00:00
import type { reports } from 'reports/index';
import type { Report } from 'reports/Report';
2022-03-22 09:28:36 +00:00
export class Fyo {
2022-03-22 09:28:36 +00:00
t = t;
T = T;
errors = errors;
isElectron: boolean;
2022-03-22 09:28:36 +00:00
pesa: MoneyMaker;
auth: AuthHandler;
doc: DocHandler;
2022-03-31 09:04:30 +00:00
db: DatabaseHandler;
2022-03-22 09:28:36 +00:00
_initialized = false;
2022-03-22 09:28:36 +00:00
2022-04-22 11:02:03 +00:00
errorLog: ErrorLog[] = [];
2022-03-22 09:28:36 +00:00
temp?: Record<string, unknown>;
2022-04-18 08:01:41 +00:00
currencyFormatter?: Intl.NumberFormat;
currencySymbols: Record<string, string | undefined> = {};
isTest: boolean;
telemetry: TelemetryManager;
config: Config;
constructor(conf: FyoConfig = {}) {
this.isTest = conf.isTest ?? false;
this.isElectron = conf.isElectron ?? true;
this.auth = new AuthHandler(this, conf.AuthDemux);
this.db = new DatabaseHandler(this, conf.DatabaseDemux);
this.doc = new DocHandler(this);
2022-03-22 09:28:36 +00:00
this.pesa = getMoneyMaker({
currency: DEFAULT_CURRENCY,
2022-03-22 09:28:36 +00:00
precision: DEFAULT_INTERNAL_PRECISION,
display: DEFAULT_DISPLAY_PRECISION,
wrapper: markRaw,
});
this.telemetry = new TelemetryManager(this);
this.config = new Config(this.isElectron && !this.isTest);
2022-03-22 09:28:36 +00:00
}
get initialized() {
return this._initialized;
}
get docs() {
return this.doc.docs;
}
get models() {
return this.doc.models;
}
2022-04-14 04:27:48 +00:00
get singles() {
return this.doc.singles;
}
get schemaMap() {
return this.db.schemaMap;
}
2022-03-22 09:28:36 +00:00
get fieldMap() {
return this.db.fieldMap;
}
2023-04-13 12:39:33 +00:00
format(value: unknown, field: FieldType | Field, doc?: Doc) {
return format(value, field, doc ?? null, this);
}
setIsElectron() {
try {
2023-07-10 09:12:20 +00:00
this.isElectron = !!window?.ipc;
} catch {
this.isElectron = false;
}
}
async initializeAndRegister(
models: ModelMap = {},
regionalModels: ModelMap = {},
force = false
) {
if (this._initialized && !force) return;
2022-03-22 09:28:36 +00:00
await this.#initializeModules();
await this.#initializeMoneyMaker();
2022-03-22 09:28:36 +00:00
this.doc.registerModels(models, regionalModels);
await this.doc.getDoc('SystemSettings');
this._initialized = true;
}
2022-03-22 09:28:36 +00:00
async #initializeModules() {
2022-03-22 09:28:36 +00:00
// temp params while calling routes
this.temp = {};
this.doc.init();
this.auth.init();
await this.db.init();
2022-03-22 09:28:36 +00:00
}
async #initializeMoneyMaker() {
2022-03-22 09:28:36 +00:00
const values =
(await this.db?.getSingleValues(
{
fieldname: 'internalPrecision',
parent: 'SystemSettings',
},
{
fieldname: 'displayPrecision',
parent: 'SystemSettings',
},
{
fieldname: 'currency',
parent: 'SystemSettings',
2022-03-22 09:28:36 +00:00
}
)) ?? [];
const acc = values.reduce((acc, sv) => {
acc[sv.fieldname] = sv.value as string | number | undefined;
return acc;
}, {} as Record<string, string | number | undefined>);
const precision: number =
(acc.internalPrecision as number) ?? DEFAULT_INTERNAL_PRECISION;
const display: number =
(acc.displayPrecision as number) ?? DEFAULT_DISPLAY_PRECISION;
const currency: string = (acc.currency as string) ?? DEFAULT_CURRENCY;
2022-03-22 09:28:36 +00:00
this.pesa = getMoneyMaker({
currency,
precision,
display,
wrapper: markRaw,
});
}
async close() {
await this.db.close();
2022-03-22 09:28:36 +00:00
}
getField(schemaName: string, fieldname: string) {
return this.fieldMap[schemaName]?.[fieldname];
}
async getValue(
schemaName: string,
name: string,
fieldname?: string
): Promise<DocValue | Doc[]> {
if (fieldname === undefined && this.schemaMap[schemaName]?.isSingle) {
fieldname = name;
name = schemaName;
}
if (getIsNullOrUndef(name) || getIsNullOrUndef(fieldname)) {
return undefined;
}
let doc: Doc;
let value: DocValue | Doc[];
try {
doc = await this.doc.getDoc(schemaName, name);
value = doc.get(fieldname);
} catch (err) {
value = undefined;
}
if (value === undefined && schemaName === name) {
const sv = await this.db.getSingleValues({
fieldname: fieldname,
parent: schemaName,
});
return sv?.[0]?.value;
}
return value;
}
2022-07-30 11:03:09 +00:00
async purgeCache() {
2022-04-22 11:02:03 +00:00
this.pesa = getMoneyMaker({
currency: DEFAULT_CURRENCY,
precision: DEFAULT_INTERNAL_PRECISION,
display: DEFAULT_DISPLAY_PRECISION,
wrapper: markRaw,
});
this._initialized = false;
this.temp = {};
this.currencyFormatter = undefined;
this.currencySymbols = {};
this.errorLog = [];
this.temp = {};
2022-07-30 11:03:09 +00:00
await this.db.purgeCache();
this.doc.purgeCache();
2022-04-22 11:02:03 +00:00
}
2022-03-22 09:28:36 +00:00
store = {
isDevelopment: false,
skipTelemetryLogging: false,
2022-03-22 09:28:36 +00:00
appVersion: '',
2022-07-07 12:29:06 +00:00
platform: '',
language: '',
instanceId: '',
deviceId: '',
openCount: -1,
appFlags: {} as Record<string, boolean>,
2023-06-13 08:07:39 +00:00
reports: {} as Record<keyof typeof reports, Report | undefined>,
2022-03-22 09:28:36 +00:00
};
}
export { T, t };