@flumens/models 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Indicia/Sample.d.ts
CHANGED
package/dist/Indicia/Sample.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("lodash"),a=require("wellknown"),n=require("@flumens/utils"),i=require("../Model.js"),o=require("./ElasticOccurrence.js"),u=require("./ElasticSample.js"),c=require("./Media.js"),d=require("./Occurrence.js"),l=require("./helpers.js");function p(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var m=p(r),h=p(a),f=function(t,r){return void 0===r&&(r={}),function(s){return s instanceof t?s:new t(e.__assign(e.__assign({},s),r))}};function v(e,t){var r,s,a,n;if(null==t?void 0:t.values)e.id=t.values.id,e.samples&&t.samples&&(null===(s=e.samples)||void 0===s||s.forEach((function(e,r){return v(e,t.samples[r])}))),e.occurrences&&t.occurrences&&(null===(a=e.occurrences)||void 0===a||a.forEach((function(e,r){return v(e,t.occurrences[r])}))),e.media&&t.media&&(null===(n=e.media)||void 0===n||n.forEach((function(e,r){return v(e,t.media[r])})));else{if("test"===(null===(r=null===process||void 0===process?void 0:process.env)||void 0===r?void 0:r.NODE_ENV))return;console.warn("Model didn't receive an id from the server")}}var _=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,s,a;return e.__generator(this,(function(n){switch(n.label){case 0:return r=[],t.media?(s=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,t.getFormData()];case 1:return r=e.sent(),[2,[t,r]]}}))}))},[4,Promise.all(t.media.map(s))]):[3,2];case 1:a=n.sent(),r.push.apply(r,a),n.label=2;case 2:return t.occurrences?[4,Promise.all(t.occurrences.map(_))]:[3,4];case 3:a=n.sent(),r.push.apply(r,a.flat()),n.label=4;case 4:return t.samples?[4,Promise.all(t.samples.map(_))]:[3,6];case 5:a=n.sent(),r.push.apply(r,a.flat()),n.label=6;case 6:return[2,r]}}))}))};function g(e,t){var r={values:{id:t||-1}};return e.occurrences&&(r.occurrences=e.occurrences.map((function(e){return g(e)}))),e.samples&&(r.samples=e.samples.map((function(e){return g(e)}))),e.media&&(r.media=e.media.map((function(e){return g(e)}))),r}function y(e){try{console.warn(JSON.stringify(e).substring(0,1e5))}catch(e){}}var b=function(r){function a(s){void 0===s&&(s={});var n=this,i=s.metadata,o=void 0===i?{}:i,u=s.samples,p=void 0===u?[]:u,m=s.occurrences,h=void 0===m?[]:m,v=s.media,_=void 0===v?[]:v,g=s.Sample,y=void 0===g?a:g,b=s.Occurrence,w=void 0===b?d.default:b,S=s.Media,A=void 0===S?c.default:S,O=s.skipStore,E=e.__rest(s,["metadata","samples","occurrences","media","Sample","Occurrence","Media","skipStore"]);E.createdAt=E.createdAt||(null==o?void 0:o.createdOn),E.updatedAt=E.updatedAt||(null==o?void 0:o.updatedOn),E.syncedAt=E.syncedAt||(null==o?void 0:o.syncedOn);var k=O?void 0:E.store;(n=r.call(this,e.__assign(e.__assign({},E),{store:k}))||this).remote=t.observable({synchronising:!1,url:null,getAccessToken:function(){return Promise.resolve("auth token is missing")},timeout:12e4}),n.validateRemote=l.validateRemoteModel,n.error=t.observable({message:""}),n.debouncedValue=300,n.keys=function(){var t;return e.__assign(e.__assign({},a.keys),(t=n.getSurvey().attrs||{},Object.keys(t).reduce((function(r,s){var a;return e.__assign(e.__assign({},r),((a={})[s]=t[s].remote||t[s],a))}),{})))},n.requiresRemoteSync=function(){return!n.syncedAt||n.updatedAt>n.syncedAt},n.Sample=y,n.Occurrence=w,n.Media=A,n.metadata=t.observable(o);var T=p.map(f(n.Sample));n.samples=t.observable(T);var N=h.map(f(n.Occurrence,{Media:n.Media}));n.occurrences=t.observable(N);var q=_.map(f(n.Media));n.media=t.observable(q);var R=function(e){return e.parent=n};n.samples.forEach(R),n.occurrences.forEach(R),n.media.forEach(R);var j=function(e){var t;return(null===(t=e.added)||void 0===t?void 0:t.length)?(e.added.forEach(R),n.setUpdatedAtTimestamp(Date.now())):e.removedCount&&n.setUpdatedAtTimestamp(Date.now()),e};return t.intercept(n.samples,j),t.intercept(n.occurrences,j),t.intercept(n.media,j),n}return e.__extends(a,r),a.parseRemoteJSON=function(t,r,s){var a,n=t.id,i=t.event,o=t.location,u=t.metadata,c=new Date(i.date_start),d=new Date(u.updated_on).getTime(),p=Date.now(),m=o.point.split(",").map(parseFloat),f=m[0],v=m[1],_="OSGB"===o.output_sref_system?o.output_sref:"",g=o.geom?h.default.parse(o.geom):null,y=i.parent_event_id,b=l.parseRemoteAttrs(y?null==s?void 0:s.smp.attrs:null==s?void 0:s.attrs,i.attributes||[]),w=null===(a=i.media)||void 0===a?void 0:a.map((function(e){var t=e.path;return{id:t,metadata:{updatedOn:c,createdOn:c,syncedOn:c},data:{data:"".concat(r,"/upload/").concat(t)}}}));return{id:n,cid:i.source_system_key||n,createdAt:c.getTime(),updatedAt:d,syncedAt:p,metadata:{saved:!0},data:e.__assign(e.__assign({},b),{surveyId:u.survey.id,date:c.toISOString(),location:{code:o.code,name:o.name||o.verbatim_locality,latitude:f,longitude:v,shape:g,gridref:_},enteredSrefSystem:4326,group:u.group,comment:i.event_remarks,training:"true"===u.trial}),media:w}},Object.defineProperty(a.prototype,"isStored",{get:function(){return this.store},enumerable:!1,configurable:!0}),a.prototype.setUpdatedAtTimestamp=function(e){var t;r.prototype.setUpdatedAtTimestamp.call(this,e),null===(t=this.parent)||void 0===t||t.setUpdatedAtTimestamp(e)},a.prototype.toJSON=function(){var s,a,n,i=r.prototype.toJSON.call(this);return n=e.__assign(e.__assign({},i),{metadata:t.toJS(this.metadata)||{},occurrences:this.occurrences.map((function(e){return e.toJSON()}))||[],samples:(null===(s=this.samples)||void 0===s?void 0:s.map((function(e){return e.toJSON()})))||[],media:(null===(a=this.media)||void 0===a?void 0:a.map((function(e){return e.toJSON()})))||[]}),JSON.parse(JSON.stringify(n))},a.prototype.getSurvey=function(){return this.survey?this.parent?this.parent.getSurvey().smp||{}:this.survey:{}},a.prototype.save=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,n,i,o,u;return e.__generator(this,(function(c){switch(c.label){case 0:if(this.data.deleted)return[2];if(this.parent)return this.parent.save(),[2];if(!this.store)throw new Error("Trying to sync locally without a store");return t=this.toJSON(),r=t.data,s=t.metadata,a=t.occurrences,n=t.samples,i=t.media,o=e.__rest(t,["data","metadata","occurrences","samples","media"]),u=e.__assign(e.__assign({},o),{data:{data:r,metadata:s,occurrences:a,samples:n,media:i}}),[4,this.store.save(u)];case 1:return c.sent(),[2]}}))}))},a.prototype.destroy=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,a=this;return e.__generator(this,(function(e){switch(e.label){case 0:return r=function(){return Promise.all([Promise.all(a.media.map((function(e){return e.destroy(!0)}))),Promise.all(a.occurrences.map((function(e){return e.destroy(!0)})))])},this.parent?(this.parent.samples.remove(this),[4,r()]):[3,2];case 1:return e.sent(),t?[2]:(this.parent.save(),[2]);case 2:if(!this.store)throw new Error("Trying to sync locally without a store");return[4,this.store.delete(this.cid)];case 3:return e.sent(),null===(s=this.collection)||void 0===s||s.remove(this),[4,r()];case 4:return e.sent(),this.data.deleted=!0,[2]}}))}))},a.prototype.sync=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.parent?this.parent.sync():r.prototype.sync.call(this)]}))}))},a.prototype.saveRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,n;return e.__generator(this,(function(e){switch(e.label){case 0:if(!this.remote.url||this.remote.url.includes("index.php"))throw new Error('A "remote" property is not configured.');e.label=1;case 1:return e.trys.push([1,4,,5]),this.remote.synchronising=!0,[4,this.uploadMedia()];case 2:return t=e.sent(),r=this.getSubmission(t),[4,this.postRemote(r)];case 3:return s=e.sent(),this.remote.synchronising=!1,v(this,s),a=(new Date).getTime(),this.updatedAt=a,this.syncedAt=a,this.save(),[3,5];case 4:throw n=e.sent(),this.remote.synchronising=!1,n;case 5:return[2]}}))}))},a.prototype.postRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,a,i,o,u,c,d;return e.__generator(this,(function(e){switch(e.label){case 0:return e.trys.push([0,3,,4]),o={},s="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:return o.Authorization=s.apply("Bearer ",[e.sent()]),r=o,[4,m.default.post("".concat(this.remote.url,"/index.php/services/rest/samples"),t,{headers:r,timeout:12e4})];case 2:return[2,e.sent().data];case 3:if(a=e.sent(),409===(i=a).status&&(null===(u=i.response)||void 0===u?void 0:u.data.duplicate_of))return[2,g(this,i.response.data.duplicate_of.id)];if(n.isAxiosNetworkError(i))throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");if(400===i.status)throw y(t),new Error(l.getErrorMessageFromObject(null===(d=null===(c=i.response)||void 0===c?void 0:c.data)||void 0===d?void 0:d.message));throw new Error(i.message);case 4:return[2]}}))}))},a.prototype.fetchRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,n,i,c,d,l,p,m,h,f,v,_,g,y,b,w,S,A=this;return e.__generator(this,(function(O){switch(O.label){case 0:if(!this.remote.url||this.remote.url.includes("index.php"))throw new Error('A "remote" property is not configured.');if(!this.cid)throw new Error("Sample cid or id is missing for fetching remote model.");return t=this.remote.url,[4,this.remote.getAccessToken()];case 1:return r=O.sent(),[4,u.fetchElasticSample({id:this.id,cid:this.cid,url:t,accessToken:r})];case 2:return s=O.sent(),this.data.surveyId||(this.data.surveyId=s.metadata.survey.id),a=this.getSurvey(),n=this.Sample.parseRemoteJSON(s,this.remote.url,a),this.id=n.id,this.cid=n.cid||this.cid,Object.assign(this.data,n.data),Object.assign(this.metadata,n.metadata),i=u.fetchElasticSubSamples({parentId:this.id,url:t,accessToken:r}),c=o.fetchElasticOccurrences({parentId:this.id,url:t,accessToken:r}),[4,Promise.all([i,c])];case 3:return d=O.sent(),l=d[0],p=d[1],m=(new Date).getTime(),h=function(e){var t=A.Sample.parseRemoteJSON(e,A.remote.url,A.getSurvey()),r=new A.Sample(t);return r.syncedAt=m,r},f=l.map(h),v=function(t,r){var s;return e.__assign(e.__assign({},t),((s={})[r.id]=r,s))},_=f.reduce(v,((w={})[this.id]=this,w)),this.samples.clear(),(S=this.samples).push.apply(S,f),g=function(e){var t=A.Occurrence.parseRemoteJSON(e,A.remote.url,A.getSurvey()),r=new A.Occurrence(t);return r.syncedAt=m,[r,e.event.event_id]},y=p.map(g),b=function(e){var t=e[0],r=e[1];r!==parseInt(A.id,10)?_[r]?_[r].occurrences.push(t):console.error("Can't attach occ to missing ".concat(r," sample")):A.occurrences.push(t)},y.forEach(b),this.syncedAt=m,[2]}}))}))},a.prototype.updateRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a;return e.__generator(this,(function(e){switch(e.label){case 0:if(!this.remote.url||this.remote.url.includes("index.php"))throw new Error('A "remote" property is not configured.');e.label=1;case 1:return e.trys.push([1,3,,4]),this.remote.synchronising=!0,t={},r=this.getSubmission(t),[4,this.putRemote(this.id,r)];case 2:return v(this,e.sent()),s=(new Date).getTime(),this.updatedAt=s,this.syncedAt=s,this.save(),this.remote.synchronising=!1,[3,4];case 3:throw a=e.sent(),this.remote.synchronising=!1,a;case 4:return[2]}}))}))},a.prototype.putRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s,a,i,o,u,c,d;return e.__generator(this,(function(e){switch(e.label){case 0:return u={},a="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:u.Authorization=a.apply("Bearer ",[e.sent()]),s=u,e.label=2;case 2:return e.trys.push([2,4,,5]),[4,m.default.put("".concat(this.remote.url,"/samples/").concat(t),r,{headers:s,timeout:12e4})];case 3:return[2,e.sent().data];case 4:if(i=e.sent(),o=i,n.isAxiosNetworkError(o))throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");if(400===o.status)throw y(r),new Error(l.getErrorMessageFromObject(null===(d=null===(c=o.response)||void 0===c?void 0:c.data)||void 0===d?void 0:d.message));throw new Error(o.message);case 5:return[2]}}))}))},a.prototype.uploadMedia=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,i,o,u,c,d=this;return e.__generator(this,(function(l){switch(l.label){case 0:return t={},[4,_(this)];case 1:if(r=l.sent(),!r.length)return[2,t];s=function(t){return e.__awaiter(d,void 0,void 0,(function(){var r,s,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:return i={},s="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:i.Authorization=s.apply("Bearer ",[e.sent()]),r=i,e.label=2;case 2:return e.trys.push([2,4,,5]),[4,m.default.post("".concat(this.remote.url,"/media-queue"),t,{headers:r,timeout:12e4})];case 3:return[2,e.sent().data];case 4:if("timeout"===(a=e.sent()).message)throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");throw a;case 5:return[2]}}))}))},a=[],i=Date.now()-432e5,r.forEach((function(e){var r=e[0];r.syncedAt>i&&r.data.queued?t[r.cid]={name:r.data.queued}:a.push(e)})),o=5,u=function(r){var n,i,u,c;return e.__generator(this,(function(d){switch(d.label){case 0:return n=a.slice(r,r+o),i=new FormData,n.forEach((function(e){var t=e[1];return i.append.apply(i,t)})),[4,s(i)];case 1:return u=d.sent(),c=(new Date).getTime(),n.forEach((function(e){var t=e[0];t.syncedAt=c,t.data.queued=u[t.cid].name})),t=e.__assign(e.__assign({},t),u),[2]}}))},c=0,l.label=2;case 2:return c<a.length?[5,u(c)]:[3,5];case 3:l.sent(),l.label=4;case 4:return c+=o,[3,2];case 5:return[2,t]}}))}))},a.prototype.getSubmission=function(t){var r=this;void 0===t&&(t={});var n="function"==typeof this.keys?this.keys():this.keys,i=e.__assign(e.__assign({},a.keys),n),o={values:{external_key:this.cid},media:[],samples:[],occurrences:[]};this.id&&(o.values.id=this.id);var u=function(e,t){var s=i[e].values;if(!s)return t;if("function"==typeof s)return s(t,o,r);if(t instanceof Array)return t.map((function(t){return u(e,t)}));if(s instanceof Array){var a=s.find((function(e){return e.value===t}));if(!a||!a.id)throw new Error('A "'.concat(e,'" attribute "').concat(t,'" value could not be mapped to a remote database field.'));return a.id}return s[t]};Object.keys(this.data).forEach((function(e){var t=r.data[e],a=function(e){return null==e};if(!a(t)){if(!i[e])return e=e.includes("smpAttr:")?e:s.snakeCase(e),void(o.values[e]=t);if(!a(t=u(e,t))){var n=i[e].id||e,c=Number.isNaN(Number(n))?n:"smpAttr:".concat(n);c=c.includes("smpAttr:")?c:s.snakeCase(c),o.values[c]=t}}})),this.samples.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.getSubmission(t);s&&o.samples.push(s)})),this.occurrences.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.getSubmission(t);s&&o.occurrences.push(s)})),this.media.forEach((function(e){var r=e.getSubmission(t);r&&o.media.push(r)}));var c=this.getSurvey();return c.modifySubmission?c.modifySubmission(o,this):o},Object.defineProperty(a.prototype,"isUploaded",{get:function(){return this.parent?this.parent.isUploaded:!!this.syncedAt},enumerable:!1,configurable:!0}),Object.defineProperty(a.prototype,"isDisabled",{get:function(){return this.isUploaded},enumerable:!1,configurable:!0}),a.keys={location:{id:"entered_sref"},location_type:{id:"entered_sref_system",values:{british:"OSGB",irish:"OSIE",channel:"utm30ed50",latlon:4326}},group:{id:"group_id"},training:{id:"training",values:l.boolToWarehouseValue},deleted:{id:"deleted",values:l.boolToWarehouseValue}},a}(i.default);exports.default=b,exports.getMediaFormData=_;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("lodash"),a=require("wellknown"),n=require("@flumens/utils"),i=require("../Model.js"),o=require("./ElasticOccurrence.js"),u=require("./ElasticSample.js"),c=require("./Media.js"),d=require("./Occurrence.js"),l=require("./helpers.js");function p(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var m=p(r),h=p(a),f=function(t,r){return void 0===r&&(r={}),function(s){return s instanceof t?s:new t(e.__assign(e.__assign({},s),r))}};function v(e,t){var r,s,a,n;if(null==t?void 0:t.values)e.id=t.values.id,e.samples&&t.samples&&(null===(s=e.samples)||void 0===s||s.forEach((function(e,r){return v(e,t.samples[r])}))),e.occurrences&&t.occurrences&&(null===(a=e.occurrences)||void 0===a||a.forEach((function(e,r){return v(e,t.occurrences[r])}))),e.media&&t.media&&(null===(n=e.media)||void 0===n||n.forEach((function(e,r){return v(e,t.media[r])})));else{if("test"===(null===(r=null===process||void 0===process?void 0:process.env)||void 0===r?void 0:r.NODE_ENV))return;console.warn("Model didn't receive an id from the server")}}var _=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,s,a;return e.__generator(this,(function(n){switch(n.label){case 0:return r=[],t.media?(s=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,t.getFormData()];case 1:return r=e.sent(),[2,[t,r]]}}))}))},[4,Promise.all(t.media.map(s))]):[3,2];case 1:a=n.sent(),r.push.apply(r,a),n.label=2;case 2:return t.occurrences?[4,Promise.all(t.occurrences.map(_))]:[3,4];case 3:a=n.sent(),r.push.apply(r,a.flat()),n.label=4;case 4:return t.samples?[4,Promise.all(t.samples.map(_))]:[3,6];case 5:a=n.sent(),r.push.apply(r,a.flat()),n.label=6;case 6:return[2,r]}}))}))};function g(e,t){var r={values:{id:t||-1}};return e.occurrences&&(r.occurrences=e.occurrences.map((function(e){return g(e)}))),e.samples&&(r.samples=e.samples.map((function(e){return g(e)}))),e.media&&(r.media=e.media.map((function(e){return g(e)}))),r}function y(e){try{console.warn(JSON.stringify(e).substring(0,1e5))}catch(e){}}var b=function(r){function a(s){void 0===s&&(s={});var n=this,i=s.metadata,o=void 0===i?{}:i,u=s.samples,p=void 0===u?[]:u,m=s.occurrences,h=void 0===m?[]:m,v=s.media,_=void 0===v?[]:v,g=s.Sample,y=void 0===g?a:g,b=s.Occurrence,w=void 0===b?d.default:b,S=s.Media,A=void 0===S?c.default:S,O=s.skipStore,E=e.__rest(s,["metadata","samples","occurrences","media","Sample","Occurrence","Media","skipStore"]);E.createdAt=E.createdAt||(null==o?void 0:o.createdOn),E.updatedAt=E.updatedAt||(null==o?void 0:o.updatedOn),E.syncedAt=E.syncedAt||(null==o?void 0:o.syncedOn);var k=O?void 0:E.store;(n=r.call(this,e.__assign(e.__assign({},E),{store:k}))||this).remote=t.observable({synchronising:!1,url:null,getAccessToken:function(){return Promise.resolve("auth token is missing")},timeout:12e4}),n.validateRemote=l.validateRemoteModel,n.error=t.observable({message:""}),n.debouncedValue=300,n.keys=function(){var t;return e.__assign(e.__assign({},a.keys),(t=n.getSurvey().attrs||{},Object.keys(t).reduce((function(r,s){var a;return e.__assign(e.__assign({},r),((a={})[s]=t[s].remote||t[s],a))}),{})))},n.requiresRemoteSync=function(){return!n.syncedAt||n.updatedAt>n.syncedAt},n.Sample=y,n.Occurrence=w,n.Media=A,n.metadata=t.observable(o);var T=p.map(f(n.Sample));n.samples=t.observable(T);var N=h.map(f(n.Occurrence,{Media:n.Media}));n.occurrences=t.observable(N);var q=_.map(f(n.Media));n.media=t.observable(q);var R=function(e){return e.parent=n};n.samples.forEach(R),n.occurrences.forEach(R),n.media.forEach(R);var j=function(e){var t;return(null===(t=e.added)||void 0===t?void 0:t.length)?(e.added.forEach(R),n.setUpdatedAtTimestamp(Date.now())):e.removedCount&&n.setUpdatedAtTimestamp(Date.now()),e};return t.intercept(n.samples,j),t.intercept(n.occurrences,j),t.intercept(n.media,j),n}return e.__extends(a,r),a.parseRemoteJSON=function(t,r,s){var a,n,i=t.id,o=t.event,u=t.location,c=t.metadata,d=new Date(o.date_start),p=new Date(c.updated_on).getTime(),m=Date.now(),f=u.point.split(",").map(parseFloat),v=f[0],_=f[1],g="OSGB"===u.output_sref_system?u.output_sref:"",y=u.geom?h.default.parse(u.geom):null,b=o.parent_event_id,w=l.parseRemoteAttrs(b?null==s?void 0:s.smp.attrs:null==s?void 0:s.attrs,o.attributes||[]),S=null===(a=o.media)||void 0===a?void 0:a.map((function(e){var t=e.path;return{id:t,metadata:{updatedOn:d,createdOn:d,syncedOn:d},data:{data:"".concat(r,"/upload/").concat(t)}}}));return{id:i,cid:o.source_system_key||i,createdAt:d.getTime(),updatedAt:p,syncedAt:m,metadata:{saved:!0},data:e.__assign(e.__assign({},w),{surveyId:c.survey.id,date:d.toISOString(),location:{code:u.code,name:u.name||u.verbatim_locality,latitude:v,longitude:_,shape:y,gridref:g},enteredSrefSystem:4326,groupId:null===(n=c.group)||void 0===n?void 0:n.id,comment:o.event_remarks,training:"true"===c.trial}),media:S}},Object.defineProperty(a.prototype,"isStored",{get:function(){return this.store},enumerable:!1,configurable:!0}),a.prototype.setUpdatedAtTimestamp=function(e){var t;r.prototype.setUpdatedAtTimestamp.call(this,e),null===(t=this.parent)||void 0===t||t.setUpdatedAtTimestamp(e)},a.prototype.toJSON=function(){var s,a,n,i=r.prototype.toJSON.call(this);return n=e.__assign(e.__assign({},i),{metadata:t.toJS(this.metadata)||{},occurrences:this.occurrences.map((function(e){return e.toJSON()}))||[],samples:(null===(s=this.samples)||void 0===s?void 0:s.map((function(e){return e.toJSON()})))||[],media:(null===(a=this.media)||void 0===a?void 0:a.map((function(e){return e.toJSON()})))||[]}),JSON.parse(JSON.stringify(n))},a.prototype.getSurvey=function(){return this.survey?this.parent?this.parent.getSurvey().smp||{}:this.survey:{}},a.prototype.save=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,n,i,o,u;return e.__generator(this,(function(c){switch(c.label){case 0:if(this.data.deleted)return[2];if(this.parent)return this.parent.save(),[2];if(!this.store)throw new Error("Trying to sync locally without a store");return t=this.toJSON(),r=t.data,s=t.metadata,a=t.occurrences,n=t.samples,i=t.media,o=e.__rest(t,["data","metadata","occurrences","samples","media"]),u=e.__assign(e.__assign({},o),{data:{data:r,metadata:s,occurrences:a,samples:n,media:i}}),[4,this.store.save(u)];case 1:return c.sent(),[2]}}))}))},a.prototype.destroy=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,a=this;return e.__generator(this,(function(e){switch(e.label){case 0:return r=function(){return Promise.all([Promise.all(a.media.map((function(e){return e.destroy(!0)}))),Promise.all(a.occurrences.map((function(e){return e.destroy(!0)})))])},this.parent?(this.parent.samples.remove(this),[4,r()]):[3,2];case 1:return e.sent(),t?[2]:(this.parent.save(),[2]);case 2:if(!this.store)throw new Error("Trying to sync locally without a store");return[4,this.store.delete(this.cid)];case 3:return e.sent(),null===(s=this.collection)||void 0===s||s.remove(this),[4,r()];case 4:return e.sent(),this.data.deleted=!0,[2]}}))}))},a.prototype.sync=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.parent?this.parent.sync():r.prototype.sync.call(this)]}))}))},a.prototype.saveRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,n;return e.__generator(this,(function(e){switch(e.label){case 0:if(!this.remote.url||this.remote.url.includes("index.php"))throw new Error('A "remote" property is not configured.');e.label=1;case 1:return e.trys.push([1,4,,5]),this.remote.synchronising=!0,[4,this.uploadMedia()];case 2:return t=e.sent(),r=this.getSubmission(t),[4,this.postRemote(r)];case 3:return s=e.sent(),this.remote.synchronising=!1,v(this,s),a=(new Date).getTime(),this.updatedAt=a,this.syncedAt=a,this.save(),[3,5];case 4:throw n=e.sent(),this.remote.synchronising=!1,n;case 5:return[2]}}))}))},a.prototype.postRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,a,i,o,u,c,d;return e.__generator(this,(function(e){switch(e.label){case 0:return e.trys.push([0,3,,4]),o={},s="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:return o.Authorization=s.apply("Bearer ",[e.sent()]),r=o,[4,m.default.post("".concat(this.remote.url,"/index.php/services/rest/samples"),t,{headers:r,timeout:12e4})];case 2:return[2,e.sent().data];case 3:if(a=e.sent(),409===(i=a).status&&(null===(u=i.response)||void 0===u?void 0:u.data.duplicate_of))return[2,g(this,i.response.data.duplicate_of.id)];if(n.isAxiosNetworkError(i))throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");if(400===i.status)throw y(t),new Error(l.getErrorMessageFromObject(null===(d=null===(c=i.response)||void 0===c?void 0:c.data)||void 0===d?void 0:d.message));throw new Error(i.message);case 4:return[2]}}))}))},a.prototype.fetchRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,n,i,c,d,l,p,m,h,f,v,_,g,y,b,w,S,A=this;return e.__generator(this,(function(O){switch(O.label){case 0:if(!this.remote.url||this.remote.url.includes("index.php"))throw new Error('A "remote" property is not configured.');if(!this.cid)throw new Error("Sample cid or id is missing for fetching remote model.");return t=this.remote.url,[4,this.remote.getAccessToken()];case 1:return r=O.sent(),[4,u.fetchElasticSample({id:this.id,cid:this.cid,url:t,accessToken:r})];case 2:return s=O.sent(),this.data.surveyId||(this.data.surveyId=s.metadata.survey.id),a=this.getSurvey(),n=this.Sample.parseRemoteJSON(s,this.remote.url,a),this.id=n.id,this.cid=n.cid||this.cid,Object.assign(this.data,n.data),Object.assign(this.metadata,n.metadata),i=u.fetchElasticSubSamples({parentId:this.id,url:t,accessToken:r}),c=o.fetchElasticOccurrences({parentId:this.id,url:t,accessToken:r}),[4,Promise.all([i,c])];case 3:return d=O.sent(),l=d[0],p=d[1],m=(new Date).getTime(),h=function(e){var t=A.Sample.parseRemoteJSON(e,A.remote.url,A.getSurvey()),r=new A.Sample(t);return r.syncedAt=m,r},f=l.map(h),v=function(t,r){var s;return e.__assign(e.__assign({},t),((s={})[r.id]=r,s))},_=f.reduce(v,((w={})[this.id]=this,w)),this.samples.clear(),(S=this.samples).push.apply(S,f),g=function(e){var t=A.Occurrence.parseRemoteJSON(e,A.remote.url,A.getSurvey()),r=new A.Occurrence(t);return r.syncedAt=m,[r,e.event.event_id]},y=p.map(g),b=function(e){var t=e[0],r=e[1];r!==parseInt(A.id,10)?_[r]?_[r].occurrences.push(t):console.error("Can't attach occ to missing ".concat(r," sample")):A.occurrences.push(t)},y.forEach(b),this.syncedAt=m,[2]}}))}))},a.prototype.updateRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a;return e.__generator(this,(function(e){switch(e.label){case 0:if(!this.remote.url||this.remote.url.includes("index.php"))throw new Error('A "remote" property is not configured.');e.label=1;case 1:return e.trys.push([1,3,,4]),this.remote.synchronising=!0,t={},r=this.getSubmission(t),[4,this.putRemote(this.id,r)];case 2:return v(this,e.sent()),s=(new Date).getTime(),this.updatedAt=s,this.syncedAt=s,this.save(),this.remote.synchronising=!1,[3,4];case 3:throw a=e.sent(),this.remote.synchronising=!1,a;case 4:return[2]}}))}))},a.prototype.putRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s,a,i,o,u,c,d;return e.__generator(this,(function(e){switch(e.label){case 0:return u={},a="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:u.Authorization=a.apply("Bearer ",[e.sent()]),s=u,e.label=2;case 2:return e.trys.push([2,4,,5]),[4,m.default.put("".concat(this.remote.url,"/samples/").concat(t),r,{headers:s,timeout:12e4})];case 3:return[2,e.sent().data];case 4:if(i=e.sent(),o=i,n.isAxiosNetworkError(o))throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");if(400===o.status)throw y(r),new Error(l.getErrorMessageFromObject(null===(d=null===(c=o.response)||void 0===c?void 0:c.data)||void 0===d?void 0:d.message));throw new Error(o.message);case 5:return[2]}}))}))},a.prototype.uploadMedia=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,a,i,o,u,c,d=this;return e.__generator(this,(function(l){switch(l.label){case 0:return t={},[4,_(this)];case 1:if(r=l.sent(),!r.length)return[2,t];s=function(t){return e.__awaiter(d,void 0,void 0,(function(){var r,s,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:return i={},s="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:i.Authorization=s.apply("Bearer ",[e.sent()]),r=i,e.label=2;case 2:return e.trys.push([2,4,,5]),[4,m.default.post("".concat(this.remote.url,"/media-queue"),t,{headers:r,timeout:12e4})];case 3:return[2,e.sent().data];case 4:if("timeout"===(a=e.sent()).message)throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");throw a;case 5:return[2]}}))}))},a=[],i=Date.now()-432e5,r.forEach((function(e){var r=e[0];r.syncedAt>i&&r.data.queued?t[r.cid]={name:r.data.queued}:a.push(e)})),o=5,u=function(r){var n,i,u,c;return e.__generator(this,(function(d){switch(d.label){case 0:return n=a.slice(r,r+o),i=new FormData,n.forEach((function(e){var t=e[1];return i.append.apply(i,t)})),[4,s(i)];case 1:return u=d.sent(),c=(new Date).getTime(),n.forEach((function(e){var t=e[0];t.syncedAt=c,t.data.queued=u[t.cid].name})),t=e.__assign(e.__assign({},t),u),[2]}}))},c=0,l.label=2;case 2:return c<a.length?[5,u(c)]:[3,5];case 3:l.sent(),l.label=4;case 4:return c+=o,[3,2];case 5:return[2,t]}}))}))},a.prototype.getSubmission=function(t){var r=this;void 0===t&&(t={});var n="function"==typeof this.keys?this.keys():this.keys,i=e.__assign(e.__assign({},a.keys),n),o={values:{external_key:this.cid},media:[],samples:[],occurrences:[]};this.id&&(o.values.id=this.id);var u=function(e,t){var s=i[e].values;if(!s)return t;if("function"==typeof s)return s(t,o,r);if(t instanceof Array)return t.map((function(t){return u(e,t)}));if(s instanceof Array){var a=s.find((function(e){return e.value===t}));if(!a||!a.id)throw new Error('A "'.concat(e,'" attribute "').concat(t,'" value could not be mapped to a remote database field.'));return a.id}return s[t]};Object.keys(this.data).forEach((function(e){var t=r.data[e],a=function(e){return null==e};if(!a(t)){if(!i[e])return e=e.includes("smpAttr:")?e:s.snakeCase(e),void(o.values[e]=t);if(!a(t=u(e,t))){var n=i[e].id||e,c=Number.isNaN(Number(n))?n:"smpAttr:".concat(n);c=c.includes("smpAttr:")?c:s.snakeCase(c),o.values[c]=t}}})),this.samples.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.getSubmission(t);s&&o.samples.push(s)})),this.occurrences.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.getSubmission(t);s&&o.occurrences.push(s)})),this.media.forEach((function(e){var r=e.getSubmission(t);r&&o.media.push(r)}));var c=this.getSurvey();return c.modifySubmission?c.modifySubmission(o,this):o},Object.defineProperty(a.prototype,"isUploaded",{get:function(){return this.parent?this.parent.isUploaded:!!this.syncedAt},enumerable:!1,configurable:!0}),Object.defineProperty(a.prototype,"isDisabled",{get:function(){return this.isUploaded},enumerable:!1,configurable:!0}),a.keys={location:{id:"entered_sref"},location_type:{id:"entered_sref_system",values:{british:"OSGB",irish:"OSIE",channel:"utm30ed50",latlon:4326}},training:{id:"training",values:l.boolToWarehouseValue},deleted:{id:"deleted",values:l.boolToWarehouseValue}},a}(i.default);exports.default=b,exports.getMediaFormData=_;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),n=require("../Collection.js"),a=require("./ElasticOccurrence.js"),s=require("./ElasticSample.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=i(r),o=function(r){function n(n){var i=r.call(this,n)||this;i.remote=t.observable({synchronising:!1,url:null,getAccessToken:function(){return Promise.resolve("auth token is missing")},timeout:12e4}),i.fetch=function(){return e.__awaiter(i,void 0,void 0,(function(){var t,r,n,a=this;return e.__generator(this,(function(s){switch(s.label){case 0:return this.store&&this.Model?[4,this.store.findAll()]:(this.ready.resolve(!1),[2]);case 1:return t=s.sent(),r=function(t){var r=t.data,n=r.data,s=r.metadata,i=r.occurrences,c=r.samples,o=r.media;return new a.Model(e.__assign(e.__assign({},t),{data:n,metadata:s,occurrences:i,samples:c,media:o}))},n=t.map(r),this.push.apply(this,n),this.ready.resolve(!0),[2]}}))}))},i.fetchRemote=function(t,r){return e.__awaiter(i,void 0,void 0,(function(){var n,i,o,u,d,l,f,m,h,p,v,_,y,g,b,w,A,O,S,M,k,E,T=this;return e.__generator(this,(function(j){switch(j.label){case 0:return console.log("Fetching remote samples, page",t),n={bool:{must_not:{exists:{field:"event.parent_event_id"}}}},i={match:{"metadata.trial":"false"}},o=function(e){return{match:{"metadata.survey.id":e}}},u={bool:{should:r.map(o)}},d=[u,n,i],l=JSON.stringify({from:t,size:100,query:{bool:{must:d}},sort:[{"metadata.created_on":"desc"}]}),f=this.remote.url,[4,this.remote.getAccessToken()];case 1:return m=j.sent(),h={method:"post",url:"".concat(f,"/index.php/services/rest/es-samples/_search"),headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(m)},data:l},[4,c.default.request(h)];case 2:return p=j.sent(),v=function(e){return e._source},_=p.data.hits.hits.map(v),y=_.map((function(e){return e.id})),g=s.fetchElasticSubSamples({parentId:y,url:f,accessToken:m}),b=a.fetchElasticOccurrences({parentId:y,url:f,accessToken:m}),[4,Promise.all([g,b])];case 3:return w=j.sent(),A=w[0],O=w[1],S=function(e){return!!e},M=_.map(function(t,r,n,a,s){return function(i){var c,o,u=(new Date).getTime();try{var d=new n({skipStore:!0});d.data.surveyId=i.metadata.survey.id;var l=d.getSurvey(),f=n.parseRemoteJSON(i,t,l);d.id=f.id,d.cid=f.cid||d.cid,Object.assign(d.data,f.data),Object.assign(d.metadata,f.metadata);var m=function(e){return e.event.parent_event_id===d.id||e.event.event_id===d.id},h=a.filter(m).map((function(e){var r=n.parseRemoteJSON(e,t,l),a=new n(r);return a.syncedAt=u,a})),p=h.reduce((function(t,r){var n;return e.__assign(e.__assign({},t),((n={})[r.id]=r,n))}),((c={})[d.id]=d,c));return s.filter(m).map((function(e){var n=r.parseRemoteJSON(e,t,l),a=new r(n);return a.syncedAt=u,[a,e.event.event_id]})).forEach((function(e){var t=e[0],r=e[1];r!==parseInt(d.id,10)?p[r]?p[r].occurrences.push(t):console.error("Can't attach occ to missing ".concat(r," sample")):d.occurrences.push(t)})),(o=d.samples).push.apply(o,h),d.syncedAt=u,d}catch(e){return console.warn(JSON.stringify(i)),console.error(e),null}}}(f,this.Occurrence,this.Model,A,O)).filter(S),k=function(e){return!T.cidMap.has(e.cid)},E=M.filter(k),this.push.apply(this,E),[2,E]}}))}))},i.removeAllSynced=function(){var e=[];return i.forEach((function(t){t.syncedAt&&e.push(t.destroy())})),Promise.all(e)},i.comparator=function(e){return-new Date(e.createdAt).getTime()},i.reset=function(){return e.__awaiter(i,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return t=this.map((function(e){return e.destroy()})),[4,Promise.all(t)];case 1:return e.sent(),[2]}}))}))},i.Occurrence=n.Occurrence,i.remote.url=n.url,i.remote.getAccessToken=n.getAccessToken,i.cidMap=new Map;t.intercept(i,(function(e){if(e.added&&e.added.length){e.added.forEach((function(e){i.cidMap.set(e.cid,e)}))}if(e.removedCount>0){var t=Array.from(i.cidMap.keys())[e.index];i.cidMap.delete(t)}return e}));return t.observe(i,(function(e){e.addedCount&&e.added.forEach((function(e){return e.collection=i})),e.removedCount&&e.removed.forEach((function(e){return delete e.collection}))})),i}return e.__extends(n,r),n}(n.default);exports.default=o;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),n=require("../Collection.js"),a=require("./ElasticOccurrence.js"),s=require("./ElasticSample.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=i(r),o=function(r){function n(n){var i=r.call(this,n)||this;i.remote=t.observable({synchronising:!1,url:null,getAccessToken:function(){return Promise.resolve("auth token is missing")},timeout:12e4}),i.fetch=function(){return e.__awaiter(i,void 0,void 0,(function(){var t,r,n,a=this;return e.__generator(this,(function(s){switch(s.label){case 0:return this.store&&this.Model?[4,this.store.findAll()]:(this.ready.resolve(!1),[2]);case 1:return t=s.sent(),r=function(t){var r=t.data,n=r.data,s=r.metadata,i=r.occurrences,c=r.samples,o=r.media,u=r.attrs;return new a.Model(e.__assign(e.__assign({},t),{attrs:u,data:n,metadata:s,occurrences:i,samples:c,media:o}))},n=t.map(r),this.push.apply(this,n),this.ready.resolve(!0),[2]}}))}))},i.fetchRemote=function(t,r){return e.__awaiter(i,void 0,void 0,(function(){var n,i,o,u,d,l,f,m,h,p,v,_,y,g,b,w,A,O,S,M,k,E,T=this;return e.__generator(this,(function(j){switch(j.label){case 0:return console.log("Fetching remote samples, page",t),n={bool:{must_not:{exists:{field:"event.parent_event_id"}}}},i={match:{"metadata.trial":"false"}},o=function(e){return{match:{"metadata.survey.id":e}}},u={bool:{should:r.map(o)}},d=[u,n,i],l=JSON.stringify({from:t,size:100,query:{bool:{must:d}},sort:[{"metadata.created_on":"desc"}]}),f=this.remote.url,[4,this.remote.getAccessToken()];case 1:return m=j.sent(),h={method:"post",url:"".concat(f,"/index.php/services/rest/es-samples/_search"),headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(m)},data:l},[4,c.default.request(h)];case 2:return p=j.sent(),v=function(e){return e._source},_=p.data.hits.hits.map(v),y=_.map((function(e){return e.id})),g=s.fetchElasticSubSamples({parentId:y,url:f,accessToken:m}),b=a.fetchElasticOccurrences({parentId:y,url:f,accessToken:m}),[4,Promise.all([g,b])];case 3:return w=j.sent(),A=w[0],O=w[1],S=function(e){return!!e},M=_.map(function(t,r,n,a,s){return function(i){var c,o,u=(new Date).getTime();try{var d=new n({skipStore:!0});d.data.surveyId=i.metadata.survey.id;var l=d.getSurvey(),f=n.parseRemoteJSON(i,t,l);d.id=f.id,d.cid=f.cid||d.cid,Object.assign(d.data,f.data),Object.assign(d.metadata,f.metadata);var m=function(e){return e.event.parent_event_id===d.id||e.event.event_id===d.id},h=a.filter(m).map((function(e){var r=n.parseRemoteJSON(e,t,l),a=new n(r);return a.syncedAt=u,a})),p=h.reduce((function(t,r){var n;return e.__assign(e.__assign({},t),((n={})[r.id]=r,n))}),((c={})[d.id]=d,c));return s.filter(m).map((function(e){var n=r.parseRemoteJSON(e,t,l),a=new r(n);return a.syncedAt=u,[a,e.event.event_id]})).forEach((function(e){var t=e[0],r=e[1];r!==parseInt(d.id,10)?p[r]?p[r].occurrences.push(t):console.error("Can't attach occ to missing ".concat(r," sample")):d.occurrences.push(t)})),(o=d.samples).push.apply(o,h),d.syncedAt=u,d}catch(e){return console.warn(JSON.stringify(i)),console.error(e),null}}}(f,this.Occurrence,this.Model,A,O)).filter(S),k=function(e){return!T.cidMap.has(e.cid)},E=M.filter(k),this.push.apply(this,E),[2,E]}}))}))},i.removeAllSynced=function(){var e=[];return i.forEach((function(t){t.syncedAt&&e.push(t.destroy())})),Promise.all(e)},i.comparator=function(e){return-new Date(e.createdAt).getTime()},i.reset=function(){return e.__awaiter(i,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return t=this.map((function(e){return e.destroy()})),[4,Promise.all(t)];case 1:return e.sent(),[2]}}))}))},i.Occurrence=n.Occurrence,i.remote.url=n.url,i.remote.getAccessToken=n.getAccessToken,i.cidMap=new Map;t.intercept(i,(function(e){if(e.added&&e.added.length){e.added.forEach((function(e){i.cidMap.set(e.cid,e)}))}if(e.removedCount>0){var t=Array.from(i.cidMap.keys())[e.index];i.cidMap.delete(t)}return e}));return t.observe(i,(function(e){e.addedCount&&e.added.forEach((function(e){return e.collection=i})),e.removedCount&&e.removed.forEach((function(e){return delete e.collection}))})),i}return e.__extends(n,r),n}(n.default);exports.default=o;
|