1/* eslint-disable no-undef */ 2importScripts("/sw-utils/data-processor.js"); 3importScripts("/sw-utils/db.js"); 4 5const DB = thenaDB(); 6 7REACT_APP_API_ROOT = "https://bolt.thena.ai"; 8 9const fetchEntityData = (model, ids) => { 10 return fetch(`${REACT_APP_API_ROOT}/v2/kanban/get-entities`, { 11 method: "POST", 12 headers: { 13 "Content-Type": "application/json", 14 }, 15 body: JSON.stringify({ 16 model, 17 ids, 18 }), 19 credentials: "include", 20 }) 21 .then((response) => response.json()) 22 .then(({ data }) => data) 23 .catch((error) => { 24 console.log("Error fetching entity data", error); 25 }); 26}; 27 28const handleRequestUpdate = async (dataList) => { 29 const processedDataList = dataList.map((data) => 30 DataProcessor.processRequestData(data) 31 ); 32 const itemsNotInView = processedDataList.filter( 33 (item) => 34 item.status === "NOT_A_REQUEST" || item.status === "CONVERTED_TO_TICKET" 35 ); 36 try { 37 if (!_.isEmpty(itemsNotInView)) { 38 sendMsgToClients({ 39 type: "SW_DB_UPDATE", 40 table: "requestsNotInView", 41 dataList: itemsNotInView, 42 subType: "PRUNE_DATA_IN_STORE", 43 }); 44 } 45 sendMsgToClients({ 46 type: "SW_DB_UPDATE", 47 table: "requests", 48 dataList: processedDataList, 49 }); 50 51 DB().then(async (db) => { 52 try { 53 if (!_.isEmpty(itemsNotInView)) { 54 await db.transaction( 55 "rw", 56 db.table("requestsNotInView"), 57 async () => { 58 const ids = itemsNotInView.map((item) => item._id); 59 60 const existingItemsArray = await db 61 .table("requestsNotInView") 62 .bulkGet(ids); 63 const existingItemsMap = existingItemsArray.reduce( 64 (acc, item) => { 65 if (item) acc[item._id] = item; 66 return acc; 67 }, 68 {} 69 ); 70 71 const itemsToUpdateOrAdd = itemsNotInView.filter((item) => { 72 const existing = existingItemsMap[item._id]; 73 // Add to the list if there's no existing item or if the item is newer 74 return ( 75 !existing || item.updated_at_date > existing.updated_at_date 76 ); 77 }); 78 79 if (itemsToUpdateOrAdd.length > 0) { 80 await db.table("requestsNotInView").bulkPut(itemsToUpdateOrAdd); 81 } 82 } 83 ); 84 } 85 await db.transaction("rw", db.table("requests"), async () => { 86 const ids = processedDataList.map((item) => item._id); 87 88 const existingItemsArray = await db.table("requests").bulkGet(ids); 89 const existingItemsMap = existingItemsArray.reduce((acc, item) => { 90 if (item) acc[item._id] = item; 91 return acc; 92 }, {}); 93 94 const itemsToUpdateOrAdd = processedDataList.filter((item) => { 95 const existing = existingItemsMap[item._id]; 96 // Add to the list if there's no existing item or if the item is newer 97 return !existing || item.updated_at_date > existing.updated_at_date; 98 }); 99 100 if (itemsToUpdateOrAdd.length > 0) { 101 await db.table("requests").bulkPut(itemsToUpdateOrAdd); 102 } 103 }); 104 } catch (error) { 105 console.error("Error in handleRequestUpdate", error); 106 } 107 }); 108 } catch (error) { 109 console.log("Error updating requests", error); 110 } 111}; 112 113const handleExternalLinkUpdate = async (data) => { 114 try { 115 const processedData = data.map((link) => 116 DataProcessor.processExternalLinkData(link) 117 ); 118 DB().then((db) => { 119 db.table("externalLinks").bulkPut(processedData); 120 }); 121 } catch (error) { 122 console.log("Error updating externalLinks", error); 123 } 124}; 125 126const handleRelationshipUpdate = async (data) => { 127 try { 128 const ids = [...new Set(data.map((item) => item._id))]; 129 if (ids.length) { 130 const response = await fetchEntityData("relationships", ids); 131 if (response?.length) { 132 const processedDataList = response.map( 133 DataProcessor.processRelationships 134 );
135 sendMsgToClients({ 136 type: "SW_DB_UPDATE", 137 table: "relationships", 138 dataList: processedDataList, 139 }); 140 141 DB().then((db) => { 142 db.table("relationships").bulkPut(processedDataList); 143 }); 144 } 145 } 146 } catch (error) { 147 console.log("Error updating relationships", error); 148 } 149}; 150 151const handleChannelUpdate = async (data) => { 152 try { 153 const processedData = data.map((channel) => 154 DataProcessor.processChannelData(channel) 155 ); 156 DB().then((db) => { 157 db.table("channels").bulkPut(processedData); 158 }); 159 } catch (error) { 160 console.log("Error updating channels", error); 161 } 162}; 163 164const handleCustomerContactUpdate = async (data) => { 165 try { 166 const processedData = data.map((contact) => 167 DataProcessor.processCustomerContactData(contact) 168 ); 169 DB().then((db) => { 170 db.table("customerContacts").bulkPut(processedData); 171 }); 172 } catch (error) { 173 console.log("Error updating customerContacts", error); 174 } 175}; 176 177const handleUserUpdate = async (data) => { 178 try { 179 const processedData = data.map((user) => 180 DataProcessor.processUserData(user) 181 ); 182 DB().then((db) => { 183 db.table("users").bulkPut(processedData); 184 }); 185 } catch (error) { 186 console.log("Error updating users", error); 187 } 188}; 189 190const handleTicketUpdate = async (dataList) => { 191 const processedDataList = dataList.map((data) => 192 DataProcessor.processTicketsDataBeforeAddingInDb(data) 193 ); 194 try { 195 sendMsgToClients({ 196 type: "SW_DB_UPDATE", 197 table: "tickets", 198 dataList: processedDataList, 199 }); 200 201 DB().then((db) => { 202 db.table("tickets").bulkPut(processedDataList); 203 }); 204 } catch (error) { 205 console.log("Error updating tickets", error); 206 } 207}; 208 209const handleKanbanActivityUpdate = async (dataList) => { 210 try { 211 DB().then((db) => { 212 sendMsgToClients({ 213 type: "SW_DB_UPDATE", 214 table: "kanban_activity", 215 dataList: dataList, 216 }); 217 db.table("kanban_activity").bulkPut(dataList); 218 }); 219 } catch (error) { 220 console.log("Error updating kanban_activity", error); 221 } 222}; 223 224const handlePusherUpdate = (dataList, entity) => { 225 // console.log(`handlePusherUpdate ${entity} data: `, dataList); 226 227 switch (entity) { 228 case "REQUEST": { 229 handleRequestUpdate(dataList); 230 break; 231 } 232 233 case "EXTERNAL_LINK": { 234 handleExternalLinkUpdate(dataList); 235 break; 236 } 237 238 case "RELATIONSHIP": { 239 handleRelationshipUpdate(dataList); 240 break; 241 } 242 243 case "CHANNEL": { 244 handleChannelUpdate(dataList); 245 break; 246 } 247 248 case "CUSTOMER_CONTACT": { 249 handleCustomerContactUpdate(dataList); 250 break; 251 } 252 253 case "USER": { 254 handleUserUpdate(dataList); 255 break; 256 } 257 258 case "EXTERNAL_TICKET": { 259 handleTicketUpdate(dataList); 260 break; 261 } 262 263 case "KANBAN_ACTIVITY": { 264 handleKanbanActivityUpdate(dataList); 265 break; 266 } 267 268 // case "CAMPAIGN": 269 // // TODO 270 // break; 271 272 default: 273 console.log("Unknown entity type: ", entity); 274 } 275};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.