2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

fix: update api endpoints

This commit is contained in:
akshayitzme 2024-12-18 17:48:24 +05:30
parent faf41ee3c7
commit 9fe029ffd7

View File

@ -7,6 +7,41 @@ import { Doc } from 'fyo/model/doc';
import { ERPNextSyncQueue } from 'models/baseModels/ERPNextSyncQueue/ERPNextSyncQueue'; import { ERPNextSyncQueue } from 'models/baseModels/ERPNextSyncQueue/ERPNextSyncQueue';
import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice'; import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice';
import { StockMovementItem } from 'models/inventory/StockMovementItem'; import { StockMovementItem } from 'models/inventory/StockMovementItem';
import { getRandomString } from '../../utils';
export async function registerInstanceToERPNext(fyo: Fyo) {
const syncSettingsDoc = (await fyo.doc.getDoc(
ModelNameEnum.ERPNextSyncSettings
)) as ERPNextSyncSettings;
const endpoint = syncSettingsDoc.endpoint;
const token = syncSettingsDoc.authToken;
const deviceID = syncSettingsDoc.deviceID;
if (!endpoint || !token) {
return;
}
if (!deviceID) {
await syncSettingsDoc.setAndSync('deviceID', getRandomString());
}
try {
(await sendAPIRequest(
`${endpoint}/api/method/books_integration.api.register_instance`,
{
method: 'POST',
headers: {
Authorization: `token ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ instance: deviceID }),
}
)) as unknown as ERPNextSyncSettingsAPIResponse;
} catch (error) {
return;
}
}
export async function updateERPNSyncSettings(fyo: Fyo) { export async function updateERPNSyncSettings(fyo: Fyo) {
const syncSettingsDoc = (await fyo.doc.getDoc( const syncSettingsDoc = (await fyo.doc.getDoc(
@ -15,8 +50,9 @@ export async function updateERPNSyncSettings(fyo: Fyo) {
const endpoint = syncSettingsDoc.endpoint; const endpoint = syncSettingsDoc.endpoint;
const authToken = syncSettingsDoc.authToken; const authToken = syncSettingsDoc.authToken;
const deviceID = syncSettingsDoc.deviceID;
if (!endpoint || !authToken) { if (!endpoint || !authToken || !deviceID) {
return; return;
} }
@ -39,6 +75,7 @@ async function getERPNSyncSettings(
{ {
headers: { headers: {
Authorization: `token ${token}`, Authorization: `token ${token}`,
'Content-Type': 'application/json',
}, },
} }
)) as unknown as ERPNextSyncSettingsAPIResponse; )) as unknown as ERPNextSyncSettingsAPIResponse;
@ -76,12 +113,13 @@ export async function syncDocumentsFromERPNext(fyo: Fyo) {
const token = fyo.singles.ERPNextSyncSettings?.authToken as string; const token = fyo.singles.ERPNextSyncSettings?.authToken as string;
const endpoint = fyo.singles.ERPNextSyncSettings?.endpoint as string; const endpoint = fyo.singles.ERPNextSyncSettings?.endpoint as string;
const deviceID = fyo.singles.ERPNextSyncSettings?.deviceID as string;
if (!token || !endpoint) { if (!token || !endpoint) {
return; return;
} }
const docsToSync = await getDocsFromERPNext(endpoint, token); const docsToSync = await getDocsFromERPNext(endpoint, token, deviceID);
if (!docsToSync || !docsToSync.message.success || !docsToSync.message.data) { if (!docsToSync || !docsToSync.message.success || !docsToSync.message.data) {
return; return;
@ -143,6 +181,7 @@ export async function syncDocumentsFromERPNext(fyo: Fyo) {
await afterDocSync( await afterDocSync(
endpoint, endpoint,
token, token,
deviceID,
doc, doc,
doc.name as string, doc.name as string,
newDoc.name as string newDoc.name as string
@ -308,6 +347,7 @@ export async function syncDocumentsToERPNext(fyo: Fyo) {
const token = fyo.singles.ERPNextSyncSettings?.authToken as string; const token = fyo.singles.ERPNextSyncSettings?.authToken as string;
const endpoint = fyo.singles.ERPNextSyncSettings?.endpoint as string; const endpoint = fyo.singles.ERPNextSyncSettings?.endpoint as string;
const deviceID = fyo.singles.ERPNextSyncSettings?.deviceID as string;
if (!token || !endpoint) { if (!token || !endpoint) {
return; return;
@ -315,7 +355,7 @@ export async function syncDocumentsToERPNext(fyo: Fyo) {
const docsToSync = []; const docsToSync = [];
const syncQueueItems = (await fyo.db.getAll(ModelNameEnum.ERPNextSyncQueue, { const syncQueueItems = (await fyo.db.getAll(ModelNameEnum.ERPNextSyncQueue, {
fields: ['referenceType', 'documentName'], fields: ['name', 'referenceType', 'documentName'],
order: 'desc', order: 'desc',
})) as ERPNextSyncQueue[]; })) as ERPNextSyncQueue[];
@ -345,42 +385,25 @@ export async function syncDocumentsToERPNext(fyo: Fyo) {
try { try {
const res = (await sendAPIRequest( const res = (await sendAPIRequest(
`${endpoint}/api/method/books_integration.api.insert_docs`, `${endpoint}/api/method/books_integration.api.sync.sync_transactions`,
{ {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: `token ${token}`, Authorization: `token ${token}`,
'Content-Type': 'application/json',
}, },
body: JSON.stringify({ payload: docsToSync }), body: JSON.stringify({ instance: deviceID, records: docsToSync }),
} }
)) as unknown as InsertDocsAPIResponse; )) as unknown as InsertDocsAPIResponse;
if (res.message.success) { if (res.message.success) {
if (!res.message.success_log.length) { for (const doc of syncQueueItems) {
return; const syncQueueDoc = await fyo.doc.getDoc(
}
for (const doc of res.message.success_log) {
const filteredLogDoc = await fyo.db.getAll(
ModelNameEnum.ERPNextSyncQueue, ModelNameEnum.ERPNextSyncQueue,
{ doc.name
filters: {
referenceType: getDocTypeName(doc),
documentName: doc.name,
},
}
); );
if (!filteredLogDoc.length) { await syncQueueDoc.delete();
return;
}
const logDoc = await fyo.doc.getDoc(
ModelNameEnum.ERPNextSyncQueue,
filteredLogDoc[0].name as string
);
await logDoc.delete();
} }
} }
} catch (error) { } catch (error) {
@ -399,20 +422,22 @@ async function syncFetchFromERPNextQueue(fyo: Fyo) {
const token = fyo.singles.ERPNextSyncSettings?.authToken as string; const token = fyo.singles.ERPNextSyncSettings?.authToken as string;
const endpoint = fyo.singles.ERPNextSyncSettings?.endpoint as string; const endpoint = fyo.singles.ERPNextSyncSettings?.endpoint as string;
const deviceID = fyo.singles.ERPNextSyncSettings?.deviceID as string;
if (!token || !endpoint) { if (!token || !endpoint || !deviceID) {
return; return;
} }
try { try {
const res = (await sendAPIRequest( const res = (await sendAPIRequest(
`${endpoint}/api/method/books_integration.api.sync_queue`, `${endpoint}/api/method/books_integration.api.initiate_master_sync`,
{ {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: `token ${token}`, Authorization: `token ${token}`,
'Content-Type': 'application/json',
}, },
body: JSON.stringify({ records: docsInQueue }), body: JSON.stringify({ records: docsInQueue, instance: deviceID }),
} }
)) as unknown as ERPNSyncDocsResponse; )) as unknown as ERPNSyncDocsResponse;
@ -452,15 +477,20 @@ async function syncFetchFromERPNextQueue(fyo: Fyo) {
async function getDocsFromERPNext( async function getDocsFromERPNext(
endpoint: string, endpoint: string,
token: string token: string,
deviceID: string
): Promise<ERPNSyncDocsResponse | undefined> { ): Promise<ERPNSyncDocsResponse | undefined> {
try { try {
return (await sendAPIRequest( return (await sendAPIRequest(
`${endpoint}/api/method/books_integration.api.sync_queue`, `${endpoint}/api/method/books_integration.api.get_pending_docs`,
{ {
headers: { headers: {
Authorization: `token ${token}`, Authorization: `token ${token}`,
'Content-Type': 'application/json',
}, },
body: JSON.stringify({
instance: deviceID,
}),
} }
)) as unknown as ERPNSyncDocsResponse; )) as unknown as ERPNSyncDocsResponse;
} catch (error) { } catch (error) {
@ -471,21 +501,24 @@ async function getDocsFromERPNext(
async function afterDocSync( async function afterDocSync(
endpoint: string, endpoint: string,
token: string, token: string,
deviceID: string,
doc: Doc | DocValueMap, doc: Doc | DocValueMap,
erpnDocName: string, erpnDocName: string,
fbooksDocName: string fbooksDocName: string
) { ) {
const res = await ipc.sendAPIRequest( const res = await ipc.sendAPIRequest(
`${endpoint}/api/method/books_integration.api.perform_aftersync`, `${endpoint}/api/method/books_integration.api.update_status`,
{ {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: `token ${token}`, Authorization: `token ${token}`,
'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
doctype: getDocTypeName(doc), doctype: getDocTypeName(doc),
nameInERPNext: erpnDocName, nameInERPNext: erpnDocName,
nameInFBooks: fbooksDocName, nameInFBooks: fbooksDocName,
instance: deviceID,
doc, doc,
}), }),
} }