BLUE
PHP 7.4.33
Path:
/var/www/web-koncept/server/src/services
Run
Logout
Edit File
Size: 6.24 KB
Close
/var/www/web-koncept/server/src/services/syncPodioData.js
Text
Base64
import podio from "@phasesdk/api-client-for-podio"; import { promises as fsPromises } from "fs"; import { dirname, join } from "path"; import { fileURLToPath } from "url"; import leadModel from "../models/leadModel.js"; import modeBookerModel from "../models/modeBookerModel.js"; import salesModel from "../models/salesModel.js"; import { udpateSyncTime } from "../services/updateLastSync.js"; import Log from "../utils/logger.js"; import { GetFormatedLeadsFields } from "./leadsServices.js"; import { GetFormatedModeBookerFields } from "./modeBookerServices.js"; import { appAuthentication } from "./podio/podioAuth.js"; import { getFormatedSalesFields } from "./salesServices.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const fileName = join(__dirname, "/../storage/public/lastSyncDate.json"); const lastSyncData = await fsPromises.readFile(fileName, "utf-8"); const jsonData = JSON.parse(lastSyncData); const time = new Date(); export const leadSync = async (appId, isInitialSync = false) => { try { const auth = await appAuthentication( process.env.LEADS_APP_ID, process.env.LEADS_APP_TOKEN ); let offset = 0; let limit = 100; let total = 0; while (offset <= total) { let attributes = { offset: offset, limit: limit, }; // If it's not an initial sync, apply the filter to fetch data since the last sync if (!isInitialSync && jsonData.lead_last_sync !== "") { attributes.filters = { last_edit_on: { from: jsonData.lead_last_sync }, }; } Log.log("Fetching data limit :", attributes); let filterResp = await podio.api.item(auth).filter(appId, attributes); let items = filterResp.data.items; total = filterResp.data.total; for (const item of items) { let formatedValue = await GetFormatedLeadsFields(item, item.item_id); try { if (formatedValue) await findOrCreateLead(formatedValue); } catch (error) { Log.error( `Error saving lead with item_id ${formatedValue.item_id}: ${error}` ); } } offset += limit; // Move to the next set of records } await udpateSyncTime(time, "lead_last_sync"); } catch (error) { Log.error(error); } }; const findOrCreateLead = async (insertArray) => { const existingLead = await leadModel.findOne({ item_id: insertArray.item_id, }); if (existingLead) { // Update the existing lead await leadModel.updateOne({ item_id: insertArray.item_id }, insertArray); return { lead: existingLead, isCreated: false }; } else { // Create a new lead const newLead = new leadModel(insertArray); await newLead.save(); return { lead: newLead, isCreated: true }; } }; export const salesSync = async (appId, isInitialSync = false) => { try { const auth = await appAuthentication( process.env.SALES_APP_ID, process.env.SALES_APP_TOKEN ); let offset = 0; let limit = 100; let total = 0; while (offset <= total) { let attributes = { offset: offset, limit: limit, }; // Apply the filter only if it's not an initial sync if (!isInitialSync && jsonData.sale_last_sync !== "") { attributes.filters = { last_edit_on: { from: jsonData.sale_last_sync }, }; } Log.log("Fetching data from sales app - limit :", attributes); let filterResp = await podio.api.item(auth).filter(appId, attributes); let items = filterResp.data.items; total = filterResp.data.total; if (items.length !== 0) { await Promise.all( items.map(async (item) => { let formatedValue = await getFormatedSalesFields( item.fields, item.item_id ); if (formatedValue) await findOrCreateSale(formatedValue); }) ); } offset += limit; } const time = new Date(); await udpateSyncTime(time, "sale_last_sync"); } catch (error) { Log.error(error); } }; export const findOrCreateSale = async (insertArray) => { const existingSale = await salesModel.findOne({ item_id: insertArray.item_id, }); if (existingSale) { // Update the existing sale await salesModel.updateOne({ item_id: insertArray.item_id }, insertArray); return { sale: existingSale, isCreated: false }; } else { // Create a new sale const newSale = new salesModel(insertArray); await newSale.save(); return { sale: newSale, isCreated: true }; } }; export const modeBookerSync = async (appId, isInitialSync = false) => { try { const auth = await appAuthentication(appId, process.env.MODE_BOOKER_TOKEN); let offset = 0; let limit = 100; let total = 0; while (offset <= total) { let attributes = { offset: offset, limit: limit, }; // Apply the filter only if it's not an initial sync if (!isInitialSync && jsonData.mode_booker_last_sync !== "") { attributes.filters = { last_edit_on: { from: jsonData.mode_booker_last_sync }, }; } Log.log("Fetching data from mode booker app - limit :", attributes); let filterResp = await podio.api.item(auth).filter(appId, attributes); let items = filterResp.data.items; if (items.length !== 0) { await Promise.all( items.map(async (item) => { let formatedValue = await GetFormatedModeBookerFields( item, item.item_id ); if (formatedValue) await findOrCreateModeBooker(formatedValue); }) ); } offset += limit; } const time = new Date(); await udpateSyncTime(time, "mode_booker_last_sync"); } catch (error) { Log.error(error); } }; const findOrCreateModeBooker = async (insertArray) => { const existing = await modeBookerModel.findOne({ item_id: insertArray.item_id, }); if (existing) { await modeBookerModel.updateOne( { item_id: insertArray.item_id }, insertArray ); return { mdoeBooker: existing, isCreated: false }; } else { // Create a new sale const newData = new modeBookerModel(insertArray); await newData.save(); return { mdoeBooker: newData, isCreated: true }; } };
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 1 × Files: 8
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
podio
DIR
-
drwxr-xr-x
2024-11-05 04:29:08
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
initialSyncPodio.js
3.93 KB
lrw-r--r--
2024-11-05 04:29:08
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
leadsServices.js
2.55 KB
lrw-r--r--
2024-11-04 12:03:48
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
modeBookerServices.js
1.00 KB
lrw-r--r--
2024-11-05 04:29:08
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
salesServices.js
2.79 KB
lrw-r--r--
2024-11-05 04:29:08
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
syncPodioData.js
6.24 KB
lrw-r--r--
2024-11-05 04:29:08
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
tokenHandler.js
770 B
lrw-r--r--
2024-11-04 12:03:48
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
updateLastSync.js
1.16 KB
lrw-r--r--
2024-11-04 12:03:48
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
users.js
988 B
lrw-r--r--
2024-11-04 12:03:48
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).