-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmartStoreUtils.js
More file actions
5 lines (4 loc) · 16.4 KB
/
Copy pathsmartStoreUtils.js
File metadata and controls
5 lines (4 loc) · 16.4 KB
1
2
3
4
5
/* mobilecaddy-utils - v3.2.1 - Bundle Time: 2024-10-22 11:36:49 */
/* git info: "2024-10-22 11:34:11 +0100": 4b69821f8a37db692d32cb588a22dc57e260d69b (v3) */
/* Copyright 2024 MobileCaddy Ltd */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.buildMobileTables=buildMobileTables,exports.deleteSoup=deleteSoup,exports.deleteRecordsForExternalId=deleteRecordsForExternalId,exports.deleteRecordsForExternalId2=deleteRecordsForExternalId2,exports.deleteRecordsFromSoup=deleteRecordsFromSoup,exports.insertRecords=insertRecords,exports.upsertSoupEntries=upsertSoupEntries,exports.updateRecordsWithExternalId=updateRecordsWithExternalId,exports.getProxyFieldName=getProxyFieldName,exports.listSoups=listSoups,exports.listMobileTables=listMobileTables,exports.listMobileTableColumns=listMobileTableColumns,exports.getSysDataRowMapColHeading=getSysDataRowMapColHeading,exports.getSysDataRowMapColHeadings=getSysDataRowMapColHeadings,exports.getTableDefnColumnValue=getTableDefnColumnValue,exports.setTableDefnColumnValue=setTableDefnColumnValue,exports.queryMobileTable=queryMobileTable,exports.queryMobileTableWithAnd=queryMobileTableWithAnd,exports.querySoupRecords=querySoupRecords,exports.querySoupRecsPromise=querySoupRecsPromise,exports.querySoupRecordsWithQuerySpec=querySoupRecordsWithQuerySpec,exports.smartQuerySoupRecordsWithQuerySpec=smartQuerySoupRecordsWithQuerySpec,exports.FULL=exports.BUILD=exports.ALPHA=void 0;var smartstore,mobileCaddy=_interopRequireWildcard(require("./constants")),logUtils=_interopRequireWildcard(require("./logUtils"));function _interopRequireWildcard(e){if(e&&e.__esModule)return e;var o={};if(null!=e)for(var t in e)if(Object.prototype.hasOwnProperty.call(e,t)){var r=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(e,t):{};r.get||r.set?Object.defineProperty(o,t,r):o[t]=e[t]}return o.default=e,o}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function deleteRecordsForExternalId2(e,o,t,r,n){if(0===o.length)r();else{var i="SELECT {"+e+":_soupEntryId}, {"+e+":Id} FROM {"+e+"}",u=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");smartQuerySoupRecordsWithQuerySpec(u.buildSmartQuerySpec(i,1e4)).then(function(t){var i=[];t.forEach(function(e){o.includes(e[1])&&i.push(e[0])}),i.length>0?u.removeFromSoup(e,i,function(e){r()},function(e){n(e)}):r()}).catch(function(e){n(e)})}}function deleteRecordsForExternalId(e,o,t,r,n){if(!r&&!n)return new Promise(function(r,n){if(0===o.length)r();else{var i="";"object"==_typeof(o[0])?o.forEach(function(e,o){i+=0===o?"'"+e[t]+"'":",'"+e[t]+"'"}):o.forEach(function(e,o){i+=0===o?"'"+e+"'":", '"+e+"'"});var u="SELECT {"+e+":_soupEntryId} FROM {"+e+"} WHERE {"+e+":"+t+"} IN ("+i+")",s=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");smartQuerySoupRecordsWithQuerySpec(s.buildSmartQuerySpec(u,1e4)).then(function(o){var t=o.map(function(e){return e[0]});s.removeFromSoup(e,t,function(e){r(e)},function(e){n(e)})}).catch(function(e){n(e)})}});if(0===o.length)r();else{var i=[];o.forEach(function(e){i.push(e[t])});var u="";o.forEach(function(e,o){u+=0===o?"'"+e[t]+"'":",'"+e[t]+"'"});var s="SELECT {"+e+":_soupEntryId} FROM {"+e+"} WHERE {"+e+":"+t+"} IN ("+u+")",a=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");smartQuerySoupRecordsWithQuerySpec(a.buildSmartQuerySpec(s,1e4),function(o){var t=o.map(function(e){return e[0]});a.removeFromSoup(e,t,function(e){r()},function(e){n(e)})},function(e){n(e)})}}function collectRecsFromCursor(e,o,t){var r=[];function n(e){o(r)}function i(e){if(!t)throw e;t(e)}!function e(o){o.currentPageOrderedEntries.forEach(function(e){r.push(e)}),o.currentPageIndex<o.totalPages-1?cordova.require("com.salesforce.plugin.smartstore").moveCursorToNextPage(o,e):cordova.require("com.salesforce.plugin.smartstore").closeCursor(o,n,i)}(e)}function getSoupDefinitionRows(e,o,t){querySoupRecordsWithQuerySpec("syncLib_system_data",(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).buildExactQuerySpec("Name",e,mobileCaddy.QUERY_BUFFER_SIZE),function(e){var t=[];e.forEach(function(e){e.Type!=mobileCaddy.STANDING_DATA_OBJECT&&e.Type!=mobileCaddy.DYNAMIC_DATA_OBJECT&&t.push(e)}),o(t)},t)}function getSoupObjectDefinition(e,o,t){querySoupRecordsWithQuerySpec("syncLib_system_data",cordova.require("com.salesforce.plugin.smartstore").buildExactQuerySpec("Name",e,mobileCaddy.QUERY_BUFFER_SIZE),function(e){for(var t,r=0;r<e.length;r++)if(e[r].Type==mobileCaddy.STANDING_DATA_OBJECT||e[r].Type==mobileCaddy.DYNAMIC_DATA_OBJECT){t=e[r];break}o(t)},t)}function removeSoupDefinitions(e,o,t){querySoupRecordsWithQuerySpec("syncLib_system_data",cordova.require("com.salesforce.plugin.smartstore").buildExactQuerySpec("Name",e,mobileCaddy.QUERY_BUFFER_SIZE),function(e){deleteRecordsFromSoup(e,"syncLib_system_data",function(){o()},t)},t)}function buildSoupQueryString(e,o,t){getSoupDefinitionRows(e,function(r){0===r.length?t("No soup columns defined for soup "+e):getSoupObjectDefinition(e,function(t){var n="Select ";r.forEach(function(e){n+=e.Column_Name+","}),n=n.substring(0,n.length-1),o(n+=" From "+e)},t)},t)}function getQueryAndSoupDefinition(e,o,t){var r=[],n="Select ";e.forEach(function(e){r.push({path:e.Column_Name,type:e.Column_Type}),0!==e.Column_Name.indexOf("_")&&(n+=e.Column_Name+",")}),n=n.substring(0,n.length-1),o(n+=" From "+e[0].Name,r)}function querySoupRecords(e,o,t){var r=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore"),n=r.buildAllQuerySpec("_soupEntryId",null,mobileCaddy.QUERY_BUFFER_SIZE);r.querySoup(e,n,function(e){collectRecsFromCursor(e,function(e){o(e)},t)})}function querySoupRecsPromise(e){return new Promise(function(o,t){querySoupRecords(e,function(e){o(e.filter(function(e){return"object"==_typeof(e)}))},function(e){t(e)})})}function querySoupRecordsWithQuerySpec(e,o,t,r){(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).querySoup(e,o,function(e){collectRecsFromCursor(e,function(e){t(e)},r)},r)}function smartQuerySoupRecordsWithQuerySpec(e,o,t){var r=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");if(void 0===o&&void 0===t)return new Promise(function(o,t){r.runSmartQuery(e,function(e){collectRecsFromCursor(e,function(e){o(e)},function(e){t(e)})},function(e){t(e)})});r.runSmartQuery(e,function(e){collectRecsFromCursor(e,function(e){o(e)},t)},t)}function queryMobileTable(e,o,t,r,n){var i=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");if(void 0===r&&void 0===n)return new Promise(function(r,n){var u=i.buildExactQuerySpec(o,t,mobileCaddy.QUERY_BUFFER_SIZE_SL);querySoupRecordsWithQuerySpec(e,u,function(e){r(e)},function(e){n(e)})});var u=i.buildExactQuerySpec(o,t,mobileCaddy.QUERY_BUFFER_SIZE_SL);querySoupRecordsWithQuerySpec(e,u,function(e){r(e)},n)}function queryMobileTableWithAnd(e,o,t,r,n,i,u){querySoupRecordsWithQuerySpec(e,(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).buildExactQuerySpec(o,t,mobileCaddy.QUERY_BUFFER_SIZE_SL),function(e){var o=[];e.forEach(function(e){e[r]==n&&o.push(e)}),i(o)},u)}function getDefnRowForTableName(e,o,t){getSysDataRowMapColHeading("Row Mapping PT","Mobile Table Name",function(r){queryMobileTableWithAnd("syncLib_system_data","MC_Var_String_001","Platform Table Definition",r,e,function(e){e.length,o(e[0])},t)},t)}function getTableDefnColumnValue(e,o){return new Promise(function(t,r){getDefnRowForTableName(e,function(e){getSysDataRowMapColHeading("Row Mapping PT",o,function(o){t(e[o])},function(e){r(e)})},function(e){r(e)})})}function setTableDefnColumnValue(e,o,t){return new Promise(function(r,n){var i=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");getDefnRowForTableName(e,function(e){getSysDataRowMapColHeading("Row Mapping PT",o,function(o){e[o]=t,i.upsertSoupEntries("syncLib_system_data",[e],function(){r()},function(e){n(e)})},function(e){n(e)})},function(e){n(e)})})}function getSysDataRowMapColHeading(e,o,t,r){var n=e+"_"+o,i=localStorage.getItem(n);i?t(i):queryMobileTableWithAnd("syncLib_system_data","MC_Var_String_001",e,"MC_Var_String_003",o,function(e){e.length,localStorage.setItem(n,e[0].MC_Var_String_004),t(e[0].MC_Var_String_004)},r)}function getSysDataRowMapColHeadings(e,o,t,r){querySoupRecordsWithQuerySpec("syncLib_system_data",(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).buildExactQuerySpec("MC_Var_String_001",e,mobileCaddy.QUERY_BUFFER_SIZE_SL),function(r){var n=[];o.forEach(function(o){var t=!1;if(r.forEach(function(e){if(e.MC_Var_String_003==o)return n.push(e.MC_Var_String_004),void(t=!0)}),!t)throw"No mapping found for type = "+e+" and value = "+o}),t(n)},r)}var NONE=0,ALPHA_ORDER=1,BUILD_ORDER=2;function listSoups(){return new Promise(function(e,o){Promise.resolve();var t=["Connection_Session__mc","Mobile_Log__mc"],r=["SnapShot_Connection_Session__mc","SnapShot_Mobile_Log__mc"],n=["appSoup","cacheSoup","dotsRecordTypes","recsToSync","syncLib_system_data"];soupExists("Analytics").then(function(i){i&&n.unshift("Analytics"),listMobileTables(ALPHA_ORDER,function(o){o.splice(o.indexOf("Connection_Session__mc"),1),o.splice(o.indexOf("Mobile_Log__mc"),1);var i=t.concat(o).concat(r);o.reduce(function(e,o){return e.then(function(){return getTableDefnColumnValue(o,"Snapshot Data Required")}).then(function(e){return"Yes"==e&&-1==t.indexOf(o)&&i.push("SnapShot_"+o),i}).catch(function(e){})},Promise.resolve()).then(function(o){e(o.concat(n))})},function(e){logUtils.error("Failed to listSoups",e).then(function(t){insertRecords("Mobile_Log__mc",[t],function(t){o(e)},function(t){o(e)})}).catch(function(t){o(e)})})})})}function soupExists(e){return new Promise(function(o,t){(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).soupExists("Analytics",function(e){o(e)},function(o){logUtils.error("soupExists",e,o).then(function(e){insertRecords("Mobile_Log__mc",[e],function(e){t(o)},function(e){t(e)})}).catch(function(e){t(o)})})})}function listMobileTables(e,o,t){getSysDataRowMapColHeadings("Row Mapping PT",["Mobile Table Name","Build Order"],function(r){var n=r[0],i=r[1];queryMobileTable("syncLib_system_data","MC_Var_String_001","Platform Table Definition",function(t){var r=[];e==BUILD_ORDER?(t.sort(function(e,o){var t=0,r=parseInt(e[i]),n=parseInt(o[i]);return isNaN(r)||isNaN(n)||(t=r-n),t}),t.forEach(function(e){r.push(e[n])}),null!==o&&o(r)):(t.forEach(function(e){r.push(e[n])}),e==ALPHA_ORDER&&r.sort(),null!==o&&o(r))},t)},t)}var LIST=0,BUILD_OBJECT=1,FULL_LIST=2;function listMobileTableColumns(e,o,t,r){getSysDataRowMapColHeadings("Row Mapping TC",["Parent SOUP Name","Table Column Name","Mobile Column Type","Application Field CRUD","META isNillable","Platform Column Type","Device Value Type","Proxy Ref Field Name"],function(r){var n=r[0],i=r[1],u=r[2],s=r[3],a=r[4],c=r[5],l=r[6],f=r[7];queryMobileTableWithAnd("syncLib_system_data","MC_Var_String_001","Platform Table Column",n,e,function(e){var r=[];e.forEach(function(e){o==LIST?r.push(e[i]):o==FULL_LIST?r.push({path:e[i],type:e[u],crud:e[s],nillable:e[a],platColType:e[c],appColType:e[l],proxyRefField:e[f]}):r.push({path:e[i],type:e[u]})}),t(r)},function(e){})},r)}function cacheProxyFieldNames(e,o){o.forEach(function(o){o.path.includes("MC_Proxy_ID")&&localStorage.setItem("proxyField-"+e,o.path)})}function buildMobileTables(e,o){listMobileTables(BUILD_ORDER,function(t){var r=t.length;t.forEach(function(t){listMobileTableColumns(t,BUILD_OBJECT,function(n){var i=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");i.registerSoup(t,n,function(){setTableDefnColumnValue(t,"SOUP Built Date/Time",(new Date).getTime()).then(function(){return cacheProxyFieldNames(t,n),getTableDefnColumnValue(t,"Snapshot Data Required")}).then(function(u){"Yes"==u?i.registerSoup("SnapShot_"+t,n,function(){setTableDefnColumnValue(t,"Snapshot SOUP Built Date/Time",(new Date).getTime()),0===--r&&e()},function(e){o(e)}):0===--r&&e()}).catch(function(e){o(e)})},o)},function(e){o(e)})})},o)}function deleteSoup(e){return new Promise(function(o,t){(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).removeSoup(e,function(){logUtils.log("deleteSoup",e).then(function(e){soupExists("Mobile_Log__mc").then(function(t){t?insertRecords("Mobile_Log__mc",[e],function(e){o()},function(e){o()}):o()})}).catch(function(e){t(e)})},function(e){logUtils.errorAndDispatch("deleteSoup",e).then(function(o){insertRecords("Mobile_Log__mc",[o],function(o){t(e)},function(o){t(e)})}).catch(function(o){t(e)})})})}function deleteRecordsFromSoup(e,o,t,r){if(0!==e.length){var n=[];if(e.forEach(function(e){e&&e._soupEntryId&&n.push(e._soupEntryId)}),0!==n.length)(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).removeFromSoup(o,n,function(e){t(e)},r);else t()}else t()}function upsertSoupEntries(e,o){return new Promise(function(t,r){(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).upsertSoupEntries(e,o,function(e){t(e)},function(e){r(e)})})}function insertRecords(e,o,t,r){(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).upsertSoupEntries(e,o,function(o){var n=(new Date).getTime();addProxyIds(e,o,n,function(o){var i=[];o.forEach(function(o){var t={Mobile_Table_Name:e,CRUD_Operation:"Insert",SOUP_Record_Id:o._soupEntryId,Id:o.Id,LastModifiedDateTime:n};i.push(t)}),(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).upsertSoupEntriesWithExternalId("recsToSync",i,"Id",t,r)},r)},r)}function addProxyIds(e,o,t,r,n){var i;getProxyFieldName(e).then(function(e){return i=e,getCachedCurrentValueFromAppSoup("audId")}).then(function(r){return o.forEach(function(o){var n="PROXY%"+e+"%"+t+"%"+o._soupEntryId+"%"+r;o.Id=n,o[i]=n}),upsertSoupEntries(e,o)}).then(function(e){r(e)}).catch(function(e){n(e)})}function fillOutRecords(e,o,t){var r=o.length,n=0;return new Promise(function(i,u){getProxyFieldName(e).then(function(s){o.forEach(function(a,c){queryMobileTable(e,t,a[t]).then(function(l){if(l.length>0){var f=l[0];for(var d in a)f[d]=a[d];o[c]=f,++n>=r&&i(o)}else"Id"==t&&a.Id.length>18?queryMobileTable(e,s,a.Id).then(function(e){if(e.length>0){var t=e[0];for(var s in a)"Id"!=s&&(t[s]=a[s]);o[c]=t,++n>=r&&i(o)}else u({status:101107})}):u({status:101107})}).catch(function(e){u(e)})})}).catch(function(e){u(e)})})}function updateRecordsWithExternalId(e,o,t,r,n){fillOutRecords(e,o,t).then(function(o){var i=window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore");i.upsertSoupEntriesWithExternalId(e,o,t,function(o){queryMobileTable("recsToSync","Mobile_Table_Name",e,function(t){var u=[];o.forEach(function(o){var r,n;if(t.forEach(function(e){e.Id!=o.Id||(r=e)}),void 0!==r)if(r.Current_Connection_Session&&void 0!==r.Current_Connection_Session)switch(r.CRUD_Operation){case"Insert":case"InsertUpdate":n="InsertUpdate";break;default:n="UpdateUpdate"}else n=r.CRUD_Operation;else n="Update";var i={Mobile_Table_Name:e,SOUP_Record_Id:o._soupEntryId,Id:o.Id,CRUD_Operation:n,LastModifiedDateTime:o.SystemModstamp};"InsertUpdate"!=n&&"UpdateUpdate"!=n||(i.Current_Connection_Session=r.Current_Connection_Session),u.push(i)}),i.upsertSoupEntriesWithExternalId("recsToSync",u,"Id",r,n)},n)},n)}).catch(function(e){n(e)})}function getProxyFieldName(e){return new Promise(function(o,t){var r=localStorage.getItem("proxyField-"+e);r?o(r):listMobileTableColumns(e,LIST,function(t){var r=null;t.forEach(function(o){o.includes("MC_Proxy_ID")&&(r=o,localStorage.setItem("proxyField-"+e,o))}),o(r)},function(e){o(null)})})}function getCachedCurrentValueFromAppSoup(e){var o=localStorage.getItem("appSoup-"+e);return new Promise(function(t,r){o?t(o):getCurrentValueFromAppSoup(e).then(function(o){localStorage.setItem("appSoup-"+e,o),t(o)})})}function getCurrentValueFromAppSoup(e){return new Promise(function(o,t){querySoupRecordsWithQuerySpec("appSoup",(window.smartstore?window.smartstore:cordova.require("com.salesforce.plugin.smartstore")).buildExactQuerySpec("Name",e,mobileCaddy.QUERY_BUFFER_SIZE),function(e){e[0]?o(e[0].CurrentValue):o(e[0])},function(e){t(e)})})}var ALPHA=ALPHA_ORDER;exports.ALPHA=ALPHA;var BUILD=BUILD_ORDER;exports.BUILD=BUILD;var FULL=FULL_LIST;exports.FULL=FULL;