Skip to content

Commit 26a229d

Browse files
committed
feat: erpnext sync
1 parent 02670c4 commit 26a229d

File tree

10 files changed

+715
-5
lines changed

10 files changed

+715
-5
lines changed

fyo/model/doc.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import {
4444
ValidationMap,
4545
} from './types';
4646
import { validateOptions, validateRequired } from './validationFunction';
47+
import { getShouldDocSyncToERPNext } from 'src/utils/erpnextSync';
48+
import { ModelNameEnum } from 'models/types';
4749

4850
export class Doc extends Observable<DocValue | Doc[]> {
4951
/* eslint-disable @typescript-eslint/no-floating-promises */
@@ -247,6 +249,22 @@ export class Doc extends Observable<DocValue | Doc[]> {
247249
return true;
248250
}
249251

252+
get shouldDocSyncToERPNext() {
253+
const syncEnabled = !!this.fyo.singles.ERPNextSyncSettings?.isEnabled;
254+
if (!syncEnabled) {
255+
return false;
256+
}
257+
258+
if (!this.schemaName || !this.fyo.singles.ERPNextSyncSettings) {
259+
return false;
260+
}
261+
262+
return getShouldDocSyncToERPNext(
263+
this.fyo.singles.ERPNextSyncSettings,
264+
this
265+
);
266+
}
267+
250268
_setValuesWithoutChecks(data: DocValueMap, convertToDocValue: boolean) {
251269
for (const field of this.schema.fields) {
252270
const { fieldname, fieldtype } = field;
@@ -912,6 +930,28 @@ export class Doc extends Observable<DocValue | Doc[]> {
912930
this._notInserted = false;
913931
await this.trigger('afterSync');
914932
this.fyo.doc.observer.trigger(`sync:${this.schemaName}`, this.name);
933+
934+
if (this._addDocToSyncQueue && !!this.shouldDocSyncToERPNext) {
935+
const isDocExistsInQueue = await this.fyo.db.getAll(
936+
ModelNameEnum.ERPNextSyncQueue,
937+
{
938+
filters: {
939+
referenceType: this.schemaName,
940+
documentName: this.name as string,
941+
},
942+
}
943+
);
944+
945+
if (!isDocExistsInQueue.length) {
946+
this.fyo.doc
947+
.getNewDoc(ModelNameEnum.ERPNextSyncQueue, {
948+
referenceType: this.schemaName,
949+
documentName: this.name,
950+
})
951+
.sync();
952+
}
953+
}
954+
915955
this._syncing = false;
916956
return doc;
917957
}

main/api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import fetch, { RequestInit } from 'node-fetch';
2+
3+
export async function sendAPIRequest(
4+
endpoint: string,
5+
options: RequestInit | undefined
6+
) {
7+
return (await fetch(endpoint, options)).json() as unknown as {
8+
[key: string]: string | number | boolean;
9+
}[];
10+
}

main/preload.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ const ipc = {
180180
await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, body);
181181
},
182182

183+
async sendAPIRequest(endpoint: string, options: RequestInit | undefined) {
184+
return (await ipcRenderer.invoke(
185+
IPC_ACTIONS.SEND_API_REQUEST,
186+
endpoint,
187+
options
188+
)) as Promise<
189+
{
190+
[key: string]: string | number | boolean | Date | object | object[];
191+
}[]
192+
>;
193+
},
194+
183195
registerMainProcessErrorListener(listener: IPCRendererListener) {
184196
ipcRenderer.on(IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, listener);
185197
},

main/registerIpcMainActionListeners.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
setAndGetCleanedConfigFiles,
2727
} from './helpers';
2828
import { saveHtmlAsPdf } from './saveHtmlAsPdf';
29+
import { sendAPIRequest } from './api';
2930

3031
export default function registerIpcMainActionListeners(main: Main) {
3132
ipcMain.handle(IPC_ACTIONS.CHECK_DB_ACCESS, async (_, filePath: string) => {
@@ -209,6 +210,13 @@ export default function registerIpcMainActionListeners(main: Main) {
209210
return getTemplates();
210211
});
211212

213+
ipcMain.handle(
214+
IPC_ACTIONS.SEND_API_REQUEST,
215+
async (e, endpoint: string, options: RequestInit | undefined) => {
216+
return sendAPIRequest(endpoint, options);
217+
}
218+
);
219+
212220
/**
213221
* Database Related Actions
214222
*/

models/helpers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,6 @@ export async function getPricingRulesOfCoupons(
818818
})) as CouponCode[];
819819
}
820820

821-
console.log('pricingRuleDocNames', pricingRuleDocNames);
822-
823821
const filteredPricingRuleNames = appliedCoupons.filter(
824822
(val) => val.pricingRule === pricingRuleDocNames![0]
825823
);
@@ -842,7 +840,6 @@ export async function getPricingRulesOfCoupons(
842840
}
843841
)) as PricingRule[];
844842

845-
console.log('pricingRuleDocsForItem', pricingRuleDocsForItem);
846843
return pricingRuleDocsForItem;
847844
}
848845

@@ -1204,7 +1201,7 @@ export async function updatePricingRule(sinvDoc: SalesInvoice) {
12041201
).length;
12051202

12061203
setTimeout(() => {
1207-
(async () => {
1204+
void (async () => {
12081205
if (appliedPricingRuleCount !== applicablePricingRuleNames?.length) {
12091206
await sinvDoc.appendPricingRuleDetail(applicablePricingRuleNames);
12101207
await sinvDoc.applyProductDiscount();

models/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export enum ModelNameEnum {
6161
POSSettings = 'POSSettings',
6262
POSShift = 'POSShift',
6363

64-
ERPNextSyncSettings= 'ERPNextSyncSettings'
64+
ERPNextSyncSettings= 'ERPNextSyncSettings',
65+
ERPNextSyncQueue = 'ERPNextSyncQueue',
66+
FetchFromERPNextQueue = 'FetchFromERPNextQueue',
6567
}
6668

6769
export type ModelName = keyof typeof ModelNameEnum;

src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import { Shortcuts } from './utils/shortcuts';
7070
import { routeTo } from './utils/ui';
7171
import { useKeys } from './utils/vueUtils';
7272
import { setDarkMode } from 'src/utils/theme';
73+
import { initERPNSync, updateERPNSyncSettings } from './utils/erpnextSync';
7374
7475
enum Screen {
7576
Desk = 'Desk',
@@ -224,6 +225,8 @@ export default defineComponent({
224225
225226
await initializeInstance(filePath, false, countryCode, fyo);
226227
await updatePrintTemplates(fyo);
228+
await updateERPNSyncSettings(fyo);
229+
initERPNSync(fyo);
227230
await this.setDesk(filePath);
228231
},
229232
async handleConnectionFailed(error: Error, actionSymbol: symbol) {

src/utils/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export async function sendAPIRequest(
2+
endpoint: string,
3+
options: RequestInit | undefined
4+
) {
5+
return await ipc.sendAPIRequest(endpoint, options);
6+
}

0 commit comments

Comments
 (0)