diff --git a/frappe/core/dbHandler.ts b/frappe/core/dbHandler.ts index c8c45f6d..747463be 100644 --- a/frappe/core/dbHandler.ts +++ b/frappe/core/dbHandler.ts @@ -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 }[]; diff --git a/src/demux/db.ts b/frappe/demux/db.ts similarity index 93% rename from src/demux/db.ts rename to frappe/demux/db.ts index 9cdc8885..76b5b8ad 100644 --- a/src/demux/db.ts +++ b/frappe/demux/db.ts @@ -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 { 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 }; diff --git a/frappe/index.ts b/frappe/index.ts index 249116d8..77181b36 100644 --- a/frappe/index.ts +++ b/frappe/index.ts @@ -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 diff --git a/frappe/models/NumberSeries.ts b/frappe/models/NumberSeries.ts index ec13f80d..96fbcc50 100644 --- a/frappe/models/NumberSeries.ts +++ b/frappe/models/NumberSeries.ts @@ -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); } } diff --git a/frappe/utils/types.ts b/frappe/utils/types.ts new file mode 100644 index 00000000..16c1e9c9 --- /dev/null +++ b/frappe/utils/types.ts @@ -0,0 +1,6 @@ +export interface ErrorLog { + name: string; + message: string; + stack?: string; + more?: Record; +} diff --git a/schemas/schemas.ts b/schemas/schemas.ts index 527417c6..a6eb8b65 100644 --- a/schemas/schemas.ts +++ b/schemas/schemas.ts @@ -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'; diff --git a/src/errorHandling.ts b/src/errorHandling.ts index 8bf1075e..476ee615 100644 --- a/src/errorHandling.ts +++ b/src/errorHandling.ts @@ -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; -} - 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 }); diff --git a/src/utils.js b/src/utils.js index 3f8377c8..145fb17f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -614,7 +614,3 @@ export function invertMap(map) { return inverted; } - -export function getPaddedName(prefix, next, padZeros) { - return prefix + next.toString().padStart(padZeros ?? 4, '0'); -} diff --git a/tests/testFrappe.spec.ts b/tests/testFrappe.spec.ts index 5c5b92c6..22160ef6 100644 --- a/tests/testFrappe.spec.ts +++ b/tests/testFrappe.spec.ts @@ -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(); }); });