{"version":3,"file":"inspectionItems-BcoYkqco.js","sources":["../../../Nitrogen.Client/src/models/InspectionItem.js","../../../Nitrogen.Client/src/api/modules/inspectionItems.js"],"sourcesContent":["import InspectionResponse from './InspectionResponse.js';\r\n\r\nexport default class InspectionItem {\r\n\tconstructor(dto) {\r\n\t\tthis.id = 0;\r\n\t\tthis.text = null;\r\n\t\tthis.passingResponse = null;\r\n\t\tthis.sortOrder = 0;\r\n\r\n\t\tif (typeof dto === 'object' && dto !== null) {\r\n\t\t\tif (typeof dto.id === 'number') {\r\n\t\t\t\tthis.id = dto.id;\r\n\t\t\t}\r\n\t\t\tif (typeof dto.text === 'string') {\r\n\t\t\t\tthis.text = dto.text;\r\n\t\t\t}\r\n\t\t\tif (typeof dto.passingResponse === 'number' && InspectionResponse.isValid(dto.passingResponse)) {\r\n\t\t\t\tthis.passingResponse = dto.passingResponse;\r\n\t\t\t}\r\n\t\t\tif (typeof dto.sortOrder === 'number') {\r\n\t\t\t\tthis.sortOrder = dto.sortOrder;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n","import { makeComparator } from '@/helpers/helpers';\r\nimport { getIdb, idbHelpers } from '@/idb';\r\nimport InspectionItem from '@/models/InspectionItem';\r\nimport { DateTime } from 'luxon';\r\nimport { fetchAllPages, fetchWrap, idbResponse, isIdbResponse, offlineResponse } from '../_helpers';\r\nimport cache from './cache';\r\n\r\nconst idbStore = 'inspectionItems';\r\n\r\nexport default {\r\n\t/**\r\n\t * Get all inspection items\r\n\t * @returns (async) Returns an array of InspectionItem objects if the request was successful, otherwise a Response.\r\n\t */\r\n\tasync getAll() {\r\n\t\tconst idb = await getIdb();\r\n\t\tlet response, data = [], timestamp;\r\n\t\tlet useIdb = await cache.isCacheHit(idbStore);\r\n\t\tif (!useIdb) {\r\n\t\t\ttry {\r\n\t\t\t\ttimestamp = DateTime.now();\r\n\t\t\t\tresponse = await fetchAllPages('/api/InspectionItems', x => data.push(x), {}, 1000);\r\n\t\t\t} catch {\r\n\t\t\t\tuseIdb = true;\r\n\t\t\t\tresponse = offlineResponse();\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (useIdb && idb) {\r\n\t\t\tdata = await idb.getAll(idbStore);\r\n\t\t\tresponse = idbResponse(200);\r\n\t\t}\r\n\t\tif (response.ok) {\r\n\t\t\tif (idb && !isIdbResponse(response)) {\r\n\t\t\t\tawait idbHelpers.replaceAll(idb, idbStore, data);\r\n\t\t\t\tawait cache.setTimestamp(idbStore, timestamp);\r\n\t\t\t}\r\n\t\t\tdata.sort(makeComparator('sortOrder', 'id'));\r\n\t\t\treturn data.map(x => new InspectionItem(x));\r\n\t\t} else {\r\n\t\t\tthrow response;\r\n\t\t}\r\n\t},\r\n\t/**\r\n\t * Get an inspection item\r\n\t * @param {Number} id InspectionItem ID\r\n\t * @returns (async) Returns an InspectionItem if the request was successful, otherwise a Response.\r\n\t */\r\n\tasync getById(id) {\r\n\t\tconst idb = await getIdb();\r\n\t\tlet response, data = null;\r\n\t\tlet useIdb = await cache.isCacheHit(idbStore);\r\n\t\tif (!useIdb && await cache.canCache()) {\r\n\t\t\t// cache all for future re-use\r\n\t\t\tif ((data = (await this.getAll()).find(x => x.id === id) ?? null)) {\r\n\t\t\t\treturn data;\r\n\t\t\t} else {\r\n\t\t\t\tthrow idbResponse(404);\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (!useIdb) {\r\n\t\t\ttry {\r\n\t\t\t\tresponse = await fetchWrap('/api/InspectionItems/' + id);\r\n\t\t\t\tif (response.ok) { data = await response.json(); }\r\n\t\t\t} catch {\r\n\t\t\t\tuseIdb = true;\r\n\t\t\t\tresponse = offlineResponse();\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (useIdb && idb) {\r\n\t\t\tdata = await idb.get(idbStore, id);\r\n\t\t\tresponse = data ? idbResponse(200) : idbResponse(404);\r\n\t\t}\r\n\t\tif (response.ok) {\r\n\t\t\tif (idb && !isIdbResponse(response)) {\r\n\t\t\t\tawait idb.put(idbStore, data);\r\n\t\t\t}\r\n\t\t\treturn new InspectionItem(data);\r\n\t\t} else {\r\n\t\t\tthrow response;\r\n\t\t}\r\n\t},\r\n\t/**\r\n\t * Create an inspection item\r\n\t * @param {InspectionItem} model inspection item to create.\r\n\t * @returns (async) Returns the new InspectionItem if the request was successful, otherwise a Response.\r\n\t */\r\n\tasync create(model) {\r\n\t\tlet response;\r\n\t\ttry {\r\n\t\t\tresponse = await fetchWrap('/api/InspectionItems', {\r\n\t\t\t\tmethod: 'POST',\r\n\t\t\t\theaders: { 'Content-Type': 'application/json' },\r\n\t\t\t\tbody: JSON.stringify(model),\r\n\t\t\t});\r\n\t\t} catch {\r\n\t\t\tresponse = offlineResponse();\r\n\t\t}\r\n\t\tif (response.ok) {\r\n\t\t\tawait cache.clearTimestamp(idbStore);\r\n\t\t\treturn new InspectionItem(await response.json());\r\n\t\t} else {\r\n\t\t\treturn response;\r\n\t\t}\r\n\t},\r\n\t/**\r\n\t * Update an inspection item\r\n\t * @param {InspectionItem} model inspection item to update.\r\n\t * @returns (async) Returns the updated InspectionItem if the request was successful, otherwise a Response.\r\n\t */\r\n\tasync update(model) {\r\n\t\tlet response;\r\n\t\ttry {\r\n\t\t\tresponse = await fetchWrap('/api/InspectionItems/' + model.id, {\r\n\t\t\t\tmethod: 'PUT',\r\n\t\t\t\theaders: { 'Content-Type': 'application/json' },\r\n\t\t\t\tbody: JSON.stringify(model),\r\n\t\t\t});\r\n\t\t} catch {\r\n\t\t\tresponse = offlineResponse();\r\n\t\t}\r\n\t\tif (response.ok) {\r\n\t\t\tawait cache.clearTimestamp(idbStore);\r\n\t\t\treturn new InspectionItem(model);\r\n\t\t} else {\r\n\t\t\treturn response;\r\n\t\t}\r\n\t},\r\n\t/**\r\n\t * Delete an inspection item\r\n\t * @param {Number} id InspectionItem ID to delete.\r\n\t * @returns (async) Returns true if the request was successful (or not found), false if the inspection item could not be deleted, otherwise a Response.\r\n\t */\r\n\tasync deleteById(id) {\r\n\t\tlet response;\r\n\t\ttry {\r\n\t\t\tresponse = await fetchWrap('/api/InspectionItems/' + id, { method: 'DELETE' });\r\n\t\t} catch {\r\n\t\t\treturn offlineResponse();\r\n\t\t}\r\n\t\tif (response.ok) {\r\n\t\t\tawait cache.clearTimestamp(idbStore);\r\n\t\t\treturn true;\r\n\t\t} else if (response.status === 404) {\r\n\t\t\treturn true;\r\n\t\t} else if (response.status === 409) {\r\n\t\t\treturn false;\r\n\t\t} else {\r\n\t\t\treturn response;\r\n\t\t}\r\n\t}\r\n};\r\n"],"names":["InspectionItem","dto","InspectionResponse","idbStore","inspectionItemsApi","idb","getIdb","response","data","timestamp","useIdb","cache","DateTime","fetchAllPages","x","offlineResponse","idbResponse","isIdbResponse","idbHelpers","makeComparator","id","fetchWrap","model"],"mappings":"mNAEe,MAAMA,CAAe,CACnC,YAAYC,EAAK,CAChB,KAAK,GAAK,EACV,KAAK,KAAO,KACZ,KAAK,gBAAkB,KACvB,KAAK,UAAY,EAEb,OAAOA,GAAQ,UAAYA,IAAQ,OAClC,OAAOA,EAAI,IAAO,WACrB,KAAK,GAAKA,EAAI,IAEX,OAAOA,EAAI,MAAS,WACvB,KAAK,KAAOA,EAAI,MAEb,OAAOA,EAAI,iBAAoB,UAAYC,EAAmB,QAAQD,EAAI,eAAe,IAC5F,KAAK,gBAAkBA,EAAI,iBAExB,OAAOA,EAAI,WAAc,WAC5B,KAAK,UAAYA,EAAI,WAGvB,CACF,CCjBA,MAAME,EAAW,kBAEFC,EAAA,CAKd,MAAM,QAAS,CACd,MAAMC,EAAM,MAAMC,IAClB,IAAIC,EAAUC,EAAO,CAAE,EAAEC,EACrBC,EAAS,MAAMC,EAAM,WAAWR,CAAQ,EAC5C,GAAI,CAACO,EACJ,GAAI,CACHD,EAAYG,EAAS,MACrBL,EAAW,MAAMM,EAAc,uBAAwBC,GAAKN,EAAK,KAAKM,CAAC,EAAG,GAAI,GAAI,CACtF,MAAW,CACPJ,EAAS,GACTH,EAAWQ,EAAe,CAC1B,CAMF,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