2022-03-22 09:28:36 +00:00
|
|
|
import { getMoneyMaker, MoneyMaker } from 'pesa';
|
|
|
|
import { markRaw } from 'vue';
|
2022-04-01 09:35:51 +00:00
|
|
|
import { AuthHandler } from './core/authHandler';
|
|
|
|
import { DatabaseHandler } from './core/dbHandler';
|
|
|
|
import { DocHandler } from './core/docHandler';
|
2022-04-07 13:38:17 +00:00
|
|
|
import { DatabaseDemuxConstructor } from './core/types';
|
2022-04-07 06:20:14 +00:00
|
|
|
import { ModelMap } from './model/types';
|
2022-03-22 09:28:36 +00:00
|
|
|
import {
|
2022-04-18 06:42:56 +00:00
|
|
|
DEFAULT_CURRENCY,
|
2022-03-22 09:28:36 +00:00
|
|
|
DEFAULT_DISPLAY_PRECISION,
|
|
|
|
DEFAULT_INTERNAL_PRECISION,
|
2022-04-01 09:35:51 +00:00
|
|
|
} from './utils/consts';
|
|
|
|
import * as errors from './utils/errors';
|
|
|
|
import { format } from './utils/format';
|
|
|
|
import { t, T } from './utils/translation';
|
2022-04-11 07:15:35 +00:00
|
|
|
import { ErrorLog } from './utils/types';
|
2022-03-22 09:28:36 +00:00
|
|
|
|
2022-04-07 06:20:14 +00:00
|
|
|
|
2022-03-22 09:28:36 +00:00
|
|
|
export class Frappe {
|
|
|
|
t = t;
|
|
|
|
T = T;
|
|
|
|
format = format;
|
|
|
|
|
|
|
|
errors = errors;
|
|
|
|
isElectron = false;
|
|
|
|
|
|
|
|
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: boolean = false;
|
|
|
|
|
|
|
|
errorLog?: ErrorLog[];
|
|
|
|
methods?: Record<string, Function>;
|
|
|
|
temp?: Record<string, unknown>;
|
|
|
|
|
2022-04-07 13:38:17 +00:00
|
|
|
constructor(DatabaseDemux?: DatabaseDemuxConstructor) {
|
|
|
|
/**
|
|
|
|
* `DatabaseManager` can be passed as the `DatabaseDemux` for
|
|
|
|
* testing this class without API or IPC calls.
|
|
|
|
*/
|
2022-03-22 09:28:36 +00:00
|
|
|
this.auth = new AuthHandler(this);
|
2022-04-07 13:38:17 +00:00
|
|
|
this.db = new DatabaseHandler(this, DatabaseDemux);
|
2022-04-07 06:20:14 +00:00
|
|
|
this.doc = new DocHandler(this);
|
|
|
|
|
2022-03-22 09:28:36 +00:00
|
|
|
this.pesa = getMoneyMaker({
|
|
|
|
currency: 'XXX',
|
|
|
|
precision: DEFAULT_INTERNAL_PRECISION,
|
|
|
|
display: DEFAULT_DISPLAY_PRECISION,
|
|
|
|
wrapper: markRaw,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-07 06:20:14 +00:00
|
|
|
get schemaMap() {
|
|
|
|
return this.db.schemaMap;
|
|
|
|
}
|
2022-03-22 09:28:36 +00:00
|
|
|
|
2022-04-07 06:20:14 +00:00
|
|
|
async initializeAndRegister(
|
2022-04-18 06:42:56 +00:00
|
|
|
models: ModelMap = {},
|
|
|
|
regionalModels: ModelMap = {},
|
2022-04-07 06:20:14 +00:00
|
|
|
force: boolean = false
|
|
|
|
) {
|
2022-04-18 06:42:56 +00:00
|
|
|
if (this._initialized && !force) return;
|
2022-03-22 09:28:36 +00:00
|
|
|
|
2022-04-18 06:42:56 +00:00
|
|
|
await this.#initializeModules();
|
|
|
|
await this.#initializeMoneyMaker();
|
2022-03-22 09:28:36 +00:00
|
|
|
|
2022-04-18 06:42:56 +00:00
|
|
|
this.doc.registerModels(models, regionalModels);
|
|
|
|
this._initialized = true;
|
|
|
|
}
|
2022-03-22 09:28:36 +00:00
|
|
|
|
2022-04-18 06:42:56 +00:00
|
|
|
async #initializeModules() {
|
2022-03-22 09:28:36 +00:00
|
|
|
this.methods = {};
|
|
|
|
this.errorLog = [];
|
|
|
|
|
|
|
|
// temp params while calling routes
|
|
|
|
this.temp = {};
|
|
|
|
|
2022-03-31 12:04:30 +00:00
|
|
|
await this.doc.init();
|
|
|
|
await this.auth.init();
|
|
|
|
await this.db.init();
|
2022-03-22 09:28:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 06:42:56 +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',
|
2022-04-18 06:42:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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>);
|
|
|
|
|
2022-04-18 06:42:56 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.db.close();
|
|
|
|
this.auth.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
store = {
|
|
|
|
isDevelopment: false,
|
|
|
|
appVersion: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { T, t };
|
|
|
|
export default new Frappe();
|