From ffdacc9637345201f92ccca51a82c0819cc0d121 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 18 Apr 2022 13:41:47 +0530 Subject: [PATCH] incr: type post start --- server/postStart.js | 47 --------------------------------------------- src/postStart.ts | 33 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 47 deletions(-) delete mode 100644 server/postStart.js create mode 100644 src/postStart.ts diff --git a/server/postStart.js b/server/postStart.js deleted file mode 100644 index 7383a004..00000000 --- a/server/postStart.js +++ /dev/null @@ -1,47 +0,0 @@ -import frappe from 'frappe'; -import { createNumberSeries } from 'frappe/model/naming'; -import JournalEntryServer from '../models/doctype/JournalEntry/JournalEntryServer.js'; -import PartyServer from '../models/doctype/Party/PartyServer.js'; -import PaymentServer from '../models/doctype/Payment/PaymentServer.js'; -import PurchaseInvoiceServer from '../models/doctype/PurchaseInvoice/PurchaseInvoiceServer.js'; -import SalesInvoiceServer from '../models/doctype/SalesInvoice/SalesInvoiceServer.js'; - -export default async function postStart() { - // set server-side modules - frappe.models.SalesInvoice.documentClass = SalesInvoiceServer; - frappe.models.Payment.documentClass = PaymentServer; - frappe.models.Party.documentClass = PartyServer; - frappe.models.PurchaseInvoice.documentClass = PurchaseInvoiceServer; - frappe.models.JournalEntry.documentClass = JournalEntryServer; - - frappe.metaCache = {}; - - // init naming series if missing - await createNumberSeries('SINV-', 'SalesInvoice'); - await createNumberSeries('PINV-', 'PurchaseInvoice'); - await createNumberSeries('PAY-', 'Payment'); - await createNumberSeries('JV-', 'JournalEntry'); - - // fetch singles - // so that they are available synchronously - await frappe.getSingle('SystemSettings'); - await frappe.getSingle('AccountingSettings'); - await frappe.getSingle('GetStarted'); - - // cache currency symbols for frappe.format - await setCurrencySymbols(); -} - -export async function setCurrencySymbols() { - frappe.currencySymbols = await frappe.db - .getAll({ - doctype: 'Currency', - fields: ['name', 'symbol'], - }) - .then((data) => { - return data.reduce((obj, currency) => { - obj[currency.name] = currency.symbol; - return obj; - }, {}); - }); -} diff --git a/src/postStart.ts b/src/postStart.ts new file mode 100644 index 00000000..0196b22f --- /dev/null +++ b/src/postStart.ts @@ -0,0 +1,33 @@ +import frappe from 'frappe'; +import { createNumberSeries } from 'frappe/model/naming'; +import { getValueMapFromList } from 'utils'; + +export default async function postStart() { + await createDefaultNumberSeries(); + await setSingles(); + await setCurrencySymbols(); +} + +async function createDefaultNumberSeries() { + await createNumberSeries('SINV-', 'SalesInvoice'); + await createNumberSeries('PINV-', 'PurchaseInvoice'); + await createNumberSeries('PAY-', 'Payment'); + await createNumberSeries('JV-', 'JournalEntry'); +} + +async function setSingles() { + await frappe.doc.getSingle('AccountingSettings'); + await frappe.doc.getSingle('GetStarted'); +} + +async function setCurrencySymbols() { + const currencies = (await frappe.db.getAll('Currency', { + fields: ['name', 'symbol'], + })) as { name: string; symbol: string }[]; + + frappe.currencySymbols = getValueMapFromList( + currencies, + 'name', + 'symbol' + ) as Record; +}