mirror of
https://github.com/frappe/books.git
synced 2025-03-31 23:41:32 +00:00
refactor: remove util calls from frappe
- shift demux - fix import - add init test
This commit is contained in:
parent
de7131ba1f
commit
309c5c7474
@ -1,5 +1,5 @@
|
|||||||
import { DatabaseDemux } from '@/demux/db';
|
|
||||||
import { Frappe } from 'frappe';
|
import { Frappe } from 'frappe';
|
||||||
|
import { DatabaseDemux } from 'frappe/demux/db';
|
||||||
import Money from 'pesa/dist/types/src/money';
|
import Money from 'pesa/dist/types/src/money';
|
||||||
import { FieldType, FieldTypeEnum, RawValue, SchemaMap } from 'schemas/types';
|
import { FieldType, FieldTypeEnum, RawValue, SchemaMap } from 'schemas/types';
|
||||||
import { DatabaseBase, DatabaseDemuxBase, GetAllOptions } from 'utils/db/types';
|
import { DatabaseBase, DatabaseDemuxBase, GetAllOptions } from 'utils/db/types';
|
||||||
@ -11,6 +11,7 @@ import {
|
|||||||
SingleValue,
|
SingleValue,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
|
// Return types of Bespoke Queries
|
||||||
type TopExpenses = { account: string; total: number }[];
|
type TopExpenses = { account: string; total: number }[];
|
||||||
type TotalOutstanding = { total: number; outstanding: number };
|
type TotalOutstanding = { total: number; outstanding: number };
|
||||||
type Cashflow = { inflow: number; outflow: number; 'month-year': string }[];
|
type Cashflow = { inflow: number; outflow: number; 'month-year': string }[];
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { ipcRenderer } from 'electron';
|
import { ipcRenderer } from 'electron';
|
||||||
import { SchemaMap } from 'schemas/types';
|
import { SchemaMap } from 'schemas/types';
|
||||||
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
|
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
|
||||||
|
import { DatabaseResponse } from 'utils/ipc/types';
|
||||||
import { IPC_ACTIONS } from 'utils/messages';
|
import { IPC_ACTIONS } from 'utils/messages';
|
||||||
import { DatabaseResponse } from '../../utils/ipc/types';
|
|
||||||
|
|
||||||
export class DatabaseDemux extends DatabaseDemuxBase {
|
export class DatabaseDemux extends DatabaseDemuxBase {
|
||||||
#isElectron: boolean = false;
|
#isElectron: boolean = false;
|
||||||
@ -82,7 +82,11 @@ export class DatabaseDemux extends DatabaseDemuxBase {
|
|||||||
async callBespoke(method: string, ...args: unknown[]): Promise<unknown> {
|
async callBespoke(method: string, ...args: unknown[]): Promise<unknown> {
|
||||||
let response: DatabaseResponse;
|
let response: DatabaseResponse;
|
||||||
if (this.#isElectron) {
|
if (this.#isElectron) {
|
||||||
response = await ipcRenderer.invoke(IPC_ACTIONS.DB_BESPOKE, method, ...args);
|
response = await ipcRenderer.invoke(
|
||||||
|
IPC_ACTIONS.DB_BESPOKE,
|
||||||
|
method,
|
||||||
|
...args
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// TODO: API Call
|
// TODO: API Call
|
||||||
response = { error: '', data: undefined };
|
response = { error: '', data: undefined };
|
@ -1,4 +1,3 @@
|
|||||||
import { ErrorLog } from '@/errorHandling';
|
|
||||||
import { getMoneyMaker, MoneyMaker } from 'pesa';
|
import { getMoneyMaker, MoneyMaker } from 'pesa';
|
||||||
import { markRaw } from 'vue';
|
import { markRaw } from 'vue';
|
||||||
import { AuthHandler } from './core/authHandler';
|
import { AuthHandler } from './core/authHandler';
|
||||||
@ -14,6 +13,7 @@ import {
|
|||||||
import * as errors from './utils/errors';
|
import * as errors from './utils/errors';
|
||||||
import { format } from './utils/format';
|
import { format } from './utils/format';
|
||||||
import { t, T } from './utils/translation';
|
import { t, T } from './utils/translation';
|
||||||
|
import { ErrorLog } from './utils/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Terminology
|
* Terminology
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
import { getPaddedName } from '@/utils';
|
|
||||||
import frappe from 'frappe';
|
import frappe from 'frappe';
|
||||||
import Doc from 'frappe/model/doc';
|
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 {
|
export default class NumberSeries extends Doc {
|
||||||
validate() {
|
validate() {
|
||||||
const current = this.get('current') as number | null;
|
const current = this.get('current') as number | null;
|
||||||
@ -33,6 +36,6 @@ export default class NumberSeries extends Doc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getPaddedName(next: number): string {
|
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
6
frappe/utils/types.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export interface ErrorLog {
|
||||||
|
name: string;
|
||||||
|
message: string;
|
||||||
|
stack?: string;
|
||||||
|
more?: Record<string, unknown>;
|
||||||
|
}
|
@ -10,7 +10,7 @@ import Item from './app/Item.json';
|
|||||||
import JournalEntry from './app/JournalEntry.json';
|
import JournalEntry from './app/JournalEntry.json';
|
||||||
import JournalEntryAccount from './app/JournalEntryAccount.json';
|
import JournalEntryAccount from './app/JournalEntryAccount.json';
|
||||||
import NumberSeries from './app/NumberSeries.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 Payment from './app/Payment.json';
|
||||||
import PaymentFor from './app/PaymentFor.json';
|
import PaymentFor from './app/PaymentFor.json';
|
||||||
import PrintSettings from './app/PrintSettings.json';
|
import PrintSettings from './app/PrintSettings.json';
|
||||||
|
@ -1,24 +1,18 @@
|
|||||||
import { ipcRenderer } from 'electron';
|
import { ipcRenderer } from 'electron';
|
||||||
import frappe, { t } from 'frappe';
|
import frappe, { t } from 'frappe';
|
||||||
import Document from 'frappe/model/document';
|
import Doc from 'frappe/model/doc';
|
||||||
import {
|
import {
|
||||||
DuplicateEntryError,
|
DuplicateEntryError,
|
||||||
LinkValidationError,
|
LinkValidationError,
|
||||||
MandatoryError,
|
MandatoryError,
|
||||||
ValidationError,
|
ValidationError,
|
||||||
} from 'frappe/utils/errors';
|
} from 'frappe/utils/errors';
|
||||||
|
import { ErrorLog } from 'frappe/utils/types';
|
||||||
import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages';
|
import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages';
|
||||||
import config, { ConfigKeys, TelemetrySetting } from './config';
|
import config, { ConfigKeys, TelemetrySetting } from './config';
|
||||||
import telemetry from './telemetry/telemetry';
|
import telemetry from './telemetry/telemetry';
|
||||||
import { showMessageDialog, showToast } from './utils';
|
import { showMessageDialog, showToast } from './utils';
|
||||||
|
|
||||||
export interface ErrorLog {
|
|
||||||
name: string;
|
|
||||||
message: string;
|
|
||||||
stack?: string;
|
|
||||||
more?: Record<string, unknown>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCanLog(): boolean {
|
function getCanLog(): boolean {
|
||||||
const telemetrySetting = config.get(ConfigKeys.Telemetry);
|
const telemetrySetting = config.get(ConfigKeys.Telemetry);
|
||||||
return telemetrySetting !== TelemetrySetting.dontLogAnything;
|
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.`;
|
let errorMessage = e.message || t`An error occurred.`;
|
||||||
|
|
||||||
const { doctype, name }: { doctype?: unknown; name?: unknown } = doc ?? {};
|
const { schemaName, name }: { schemaName?: string; name?: string } =
|
||||||
const canElaborate = !!(doctype && name);
|
doc ?? {};
|
||||||
|
const canElaborate = !!(schemaName && name);
|
||||||
|
|
||||||
if (e instanceof LinkValidationError && canElaborate) {
|
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) {
|
} else if (e instanceof DuplicateEntryError && canElaborate) {
|
||||||
errorMessage = t`${doctype} ${name} already exists.`;
|
errorMessage = t`${schemaName} ${name} already exists.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return errorMessage;
|
return errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleErrorWithDialog(error: Error, doc?: Document) {
|
export function handleErrorWithDialog(error: Error, doc?: Doc) {
|
||||||
const errorMessage = getErrorMessage(error, doc);
|
const errorMessage = getErrorMessage(error, doc);
|
||||||
handleError(false, error, { errorMessage, doc });
|
handleError(false, error, { errorMessage, doc });
|
||||||
|
|
||||||
|
@ -614,7 +614,3 @@ export function invertMap(map) {
|
|||||||
|
|
||||||
return inverted;
|
return inverted;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPaddedName(prefix, next, padZeros) {
|
|
||||||
return prefix + next.toString().padStart(padZeros ?? 4, '0');
|
|
||||||
}
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import * as assert from 'assert';
|
||||||
import 'mocha';
|
import 'mocha';
|
||||||
import { DatabaseManager } from '../backend/database/manager';
|
import { DatabaseManager } from '../backend/database/manager';
|
||||||
import { Frappe } from '../frappe';
|
import { Frappe } from '../frappe';
|
||||||
@ -6,7 +7,25 @@ describe('Frappe', function () {
|
|||||||
const frappe = new Frappe(DatabaseManager);
|
const frappe = new Frappe(DatabaseManager);
|
||||||
|
|
||||||
specify('Init', async function () {
|
specify('Init', async function () {
|
||||||
// await frappe.init();
|
assert.strictEqual(
|
||||||
// await frappe.db.connectToDatabase(':memory:');
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user