2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00

refactor: remove util calls from frappe

- shift demux
- fix import
- add init test
This commit is contained in:
18alantom 2022-04-11 12:45:35 +05:30
parent de7131ba1f
commit 309c5c7474
9 changed files with 51 additions and 27 deletions

View File

@ -1,5 +1,5 @@
import { DatabaseDemux } from '@/demux/db';
import { Frappe } from 'frappe';
import { DatabaseDemux } from 'frappe/demux/db';
import Money from 'pesa/dist/types/src/money';
import { FieldType, FieldTypeEnum, RawValue, SchemaMap } from 'schemas/types';
import { DatabaseBase, DatabaseDemuxBase, GetAllOptions } from 'utils/db/types';
@ -11,6 +11,7 @@ import {
SingleValue,
} from './types';
// Return types of Bespoke Queries
type TopExpenses = { account: string; total: number }[];
type TotalOutstanding = { total: number; outstanding: number };
type Cashflow = { inflow: number; outflow: number; 'month-year': string }[];

View File

@ -1,8 +1,8 @@
import { ipcRenderer } from 'electron';
import { SchemaMap } from 'schemas/types';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
import { DatabaseResponse } from 'utils/ipc/types';
import { IPC_ACTIONS } from 'utils/messages';
import { DatabaseResponse } from '../../utils/ipc/types';
export class DatabaseDemux extends DatabaseDemuxBase {
#isElectron: boolean = false;
@ -82,7 +82,11 @@ export class DatabaseDemux extends DatabaseDemuxBase {
async callBespoke(method: string, ...args: unknown[]): Promise<unknown> {
let response: DatabaseResponse;
if (this.#isElectron) {
response = await ipcRenderer.invoke(IPC_ACTIONS.DB_BESPOKE, method, ...args);
response = await ipcRenderer.invoke(
IPC_ACTIONS.DB_BESPOKE,
method,
...args
);
} else {
// TODO: API Call
response = { error: '', data: undefined };

View File

@ -1,4 +1,3 @@
import { ErrorLog } from '@/errorHandling';
import { getMoneyMaker, MoneyMaker } from 'pesa';
import { markRaw } from 'vue';
import { AuthHandler } from './core/authHandler';
@ -14,6 +13,7 @@ import {
import * as errors from './utils/errors';
import { format } from './utils/format';
import { t, T } from './utils/translation';
import { ErrorLog } from './utils/types';
/**
* Terminology

View File

@ -1,7 +1,10 @@
import { getPaddedName } from '@/utils';
import frappe from 'frappe';
import Doc from 'frappe/model/doc';
function getPaddedName(prefix: string, next: number, padZeros: number): string {
return prefix + next.toString().padStart(padZeros ?? 4, '0');
}
export default class NumberSeries extends Doc {
validate() {
const current = this.get('current') as number | null;
@ -33,6 +36,6 @@ export default class NumberSeries extends Doc {
}
getPaddedName(next: number): string {
return getPaddedName(this.name, next, this.padZeros);
return getPaddedName(this.name as string, next, this.padZeros as number);
}
}

6
frappe/utils/types.ts Normal file
View File

@ -0,0 +1,6 @@
export interface ErrorLog {
name: string;
message: string;
stack?: string;
more?: Record<string, unknown>;
}

View File

@ -10,7 +10,7 @@ import Item from './app/Item.json';
import JournalEntry from './app/JournalEntry.json';
import JournalEntryAccount from './app/JournalEntryAccount.json';
import NumberSeries from './app/NumberSeries.json';
import Party from './tests/Party.json';
import Party from './app/Party.json';
import Payment from './app/Payment.json';
import PaymentFor from './app/PaymentFor.json';
import PrintSettings from './app/PrintSettings.json';

View File

@ -1,24 +1,18 @@
import { ipcRenderer } from 'electron';
import frappe, { t } from 'frappe';
import Document from 'frappe/model/document';
import Doc from 'frappe/model/doc';
import {
DuplicateEntryError,
LinkValidationError,
MandatoryError,
ValidationError,
} from 'frappe/utils/errors';
import { ErrorLog } from 'frappe/utils/types';
import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages';
import config, { ConfigKeys, TelemetrySetting } from './config';
import telemetry from './telemetry/telemetry';
import { showMessageDialog, showToast } from './utils';
export interface ErrorLog {
name: string;
message: string;
stack?: string;
more?: Record<string, unknown>;
}
function getCanLog(): boolean {
const telemetrySetting = config.get(ConfigKeys.Telemetry);
return telemetrySetting !== TelemetrySetting.dontLogAnything;
@ -110,22 +104,23 @@ export function handleError(
}
}
export function getErrorMessage(e: Error, doc?: Document): string {
export function getErrorMessage(e: Error, doc?: Doc): string {
let errorMessage = e.message || t`An error occurred.`;
const { doctype, name }: { doctype?: unknown; name?: unknown } = doc ?? {};
const canElaborate = !!(doctype && name);
const { schemaName, name }: { schemaName?: string; name?: string } =
doc ?? {};
const canElaborate = !!(schemaName && name);
if (e instanceof LinkValidationError && canElaborate) {
errorMessage = t`${doctype} ${name} is linked with existing records.`;
errorMessage = t`${schemaName} ${name} is linked with existing records.`;
} else if (e instanceof DuplicateEntryError && canElaborate) {
errorMessage = t`${doctype} ${name} already exists.`;
errorMessage = t`${schemaName} ${name} already exists.`;
}
return errorMessage;
}
export function handleErrorWithDialog(error: Error, doc?: Document) {
export function handleErrorWithDialog(error: Error, doc?: Doc) {
const errorMessage = getErrorMessage(error, doc);
handleError(false, error, { errorMessage, doc });

View File

@ -614,7 +614,3 @@ export function invertMap(map) {
return inverted;
}
export function getPaddedName(prefix, next, padZeros) {
return prefix + next.toString().padStart(padZeros ?? 4, '0');
}

View File

@ -1,3 +1,4 @@
import * as assert from 'assert';
import 'mocha';
import { DatabaseManager } from '../backend/database/manager';
import { Frappe } from '../frappe';
@ -6,7 +7,25 @@ describe('Frappe', function () {
const frappe = new Frappe(DatabaseManager);
specify('Init', async function () {
// await frappe.init();
// await frappe.db.connectToDatabase(':memory:');
assert.strictEqual(
Object.keys(frappe.schemaMap).length,
0,
'zero schemas one'
);
await frappe.init();
assert.strictEqual(
Object.keys(frappe.schemaMap).length,
0,
'zero schemas two'
);
await frappe.db.createNewDatabase(':memory:');
await frappe.initializeAndRegister({}, true);
assert.strictEqual(
Object.keys(frappe.schemaMap).length > 0,
true,
'non zero schemas'
);
await frappe.db.close();
});
});