@flumens/models 0.9.0 → 0.9.2

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.
@@ -21,7 +21,7 @@ export interface Data extends DataOriginal {
21
21
  export interface Remote {
22
22
  synchronising?: boolean;
23
23
  url?: string | null;
24
- headers?: any;
24
+ getAccessToken?: () => Promise<string>;
25
25
  timeout?: number;
26
26
  }
27
27
  declare class Media<T extends Data = Data, S extends Metadata = Metadata> extends Model<T> {
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),a=require("axios"),r=require("@flumens/utils"),i=require("../Model.js"),n=require("./helpers.js");function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=o(a),d=r.isPlatform("hybrid"),u=function(a){function i(r){void 0===r&&(r={});var i=this,n=r.metadata,o=void 0===n?{}:n,s=t.__rest(r,["metadata"]);return s.createdAt=s.createdAt||(null==o?void 0:o.createdOn),s.updatedAt=s.updatedAt||(null==o?void 0:o.updatedOn),s.syncedAt=s.syncedAt||(null==o?void 0:o.syncedOn),(i=a.call(this,s)||this).remote={synchronising:!1,url:null,headers:{},timeout:12e4},i.debouncedValue=300,i.parent=i.parent,i.metadata=e.observable(o),i}return t.__extends(i,a),i.getDataURI=function(t,e){return void 0===e&&(e={}),new Promise((function(a,r){if("string"==typeof t){var n=t.replace(/.*\.([a-z]+)$/i,"$1");return"jpg"===n&&(n="jpeg"),void i.resize(t,n,e.width,e.height).then((function(t){var e=t[0],r=t[1];a([r,n,e.width,e.height])}))}if(window.FileReader){var o=new FileReader;o.onload=function(r){var n,o;if(e.width||e.height)i.resize(null===(n=r.target)||void 0===n?void 0:n.result,t.type,e.width,e.height).then((function(e){var r=e[0],i=e[1];a([i,t.type,r.width,r.height])}));else{var s=new window.Image;s.onload=function(){var e,i=t.type.replace(/.*\/([a-z]+)$/i,"$1");a([null===(e=r.target)||void 0===e?void 0:e.result,i,s.width,s.height])},s.src=null===(o=r.target)||void 0===o?void 0:o.result}},o.readAsDataURL(t)}else r(new Error("No File Reader"))}))},i.resize=function(t,e,a,r){return new Promise((function(i){var n=new window.Image;n.onload=function(){var t,o=n.width,s=n.height,d=null;o/=d=o>s?o/(!a||a>o?o:a):s/(!r||r>s?s:r),s/=d;var u=document.createElement("canvas");u.width=o,u.height=s,null===(t=u.getContext("2d"))||void 0===t||t.drawImage(n,0,0,o,s),i([n,u.toDataURL(e)])},n.src=t}))},i.getImageModel=function(e,a,i){return t.__awaiter(this,void 0,void 0,(function(){var n,o,s,u,h,c,l;return t.__generator(this,(function(t){switch(t.label){case 0:if(n=this,!e)throw new Error("File not found while creating image model.");return d?[4,r.createImage(e)]:[3,2];case 1:return h=t.sent(),o=h.width,s=h.height,u=e.split("/").pop(),[3,4];case 2:return[4,n.getDataURI(e,{width:2e3,height:2e3})];case 3:l=t.sent(),u=l[0],o=l[2],s=l[3],t.label=4;case 4:return c=new n({data:{data:u,type:"jpeg",width:o,height:s,path:a}}),i?[3,6]:[4,c.addThumbnail()];case 5:t.sent(),t.label=6;case 6:return[2,c]}}))}))},i.prototype.toJSON=function(){var r=a.prototype.toJSON.call(this);return JSON.parse(JSON.stringify(t.__assign(t.__assign({},r),{metadata:e.toJS(this.metadata)||{}})))},i.prototype.setupdatedAtTimestamp=function(t){var e;a.prototype.setUpdatedAtTimestamp.call(this,t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)},i.prototype.getSubmission=function(t){void 0===t&&(t={});var e=t[this.cid];if(!this.id&&!e)throw new Error("Image ID or queued ID is missing.");var a={values:{caption:this.data.caption}};return this.id?a.values.id=this.id:a.values.queued=e.name,a},i.prototype.sync=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return[2,this.parent?this.parent.sync():a.prototype.sync.call(this)]}))}))},i.prototype.uploadFile=function(){return t.__awaiter(this,arguments,void 0,(function(e){var a,r,i,n,o,d,u,h,c;return void 0===e&&(e=!1),t.__generator(this,(function(t){switch(t.label){case 0:if(this.id)throw new Error("A file of a media on the remote cannot be uploaded again.");return a=Date.now()-432e5,r=this.syncedAt>a&&this.data.queued,!e&&r?[2]:[4,this.getFormData()];case 1:return i=t.sent(),n=new FormData,(h=n).append.apply(h,i),"function"!=typeof this.remote.headers?[3,3]:[4,this.remote.headers()];case 2:return d=t.sent(),[3,4];case 3:d=this.remote.headers,t.label=4;case 4:return o=d,[4,s.default.post("".concat(this.remote.url,"/media-queue"),n,{headers:o,timeout:12e4})];case 5:if(u=t.sent(),!(null===(c=u.data[this.cid])||void 0===c?void 0:c.name))throw new Error("New remote media name was not be found.");return this.data.queued=u.data[this.cid].name,this.syncedAt=(new Date).getTime(),[2]}}))}))},i.prototype.getFormData=function(){return t.__awaiter(this,void 0,void 0,(function(){var e,a,r,i,o,s,d;return t.__generator(this,(function(t){switch(t.label){case 0:return e=this.data.type,a=e,r=e,(null==e?void 0:e.match(/image.*/))?(d=e.split("/"),a=d[1]):r="image/".concat(r),i=this.getURL(),[4,n.getBlobFromURL(i,r)];case 1:return o=t.sent(),[2,[s=this.cid,o,"".concat(s,".").concat(a)]]}}))}))},i.prototype.getRemoteURL=function(){if(!this.remote.url)throw new Error("No remote url was set.");if(!this.data.queued&&!this.data.path)throw new Error("No media queued or path attribute.");var t=this.remote.url.replace("/index.php/services/rest","");return this.data.queued?"".concat(t,"/upload-queue/").concat(this.data.queued):"".concat(t,"/upload/").concat(this.data.path)},i.prototype.getURL=function(){return this.data.data},i.prototype.resize=function(t,e){var a=this,r=this;return new Promise((function(n,o){i.resize(a.getURL(),a.data.type,t,e).then((function(t){var e=t[0],a=t[1];r.data.data=a,n([e,a])})).catch(o)}))},i.prototype.addThumbnail=function(t){var e=this,a=this;return new Promise((function(t,r){/^data:/i.test(e.getURL())?i.resize(e.getURL(),e.data.type,100,100).then((function(e){var r=e[1];a.data.thumbnail=r,t()})).catch(r):i.getDataURI(e.getURL(),{width:100,height:100}).then((function(e){a.data.thumbnail=e[0],t()})).catch(r)}))},Object.defineProperty(i.prototype,"isUploaded",{get:function(){if(!this.parent)throw new Error("No media parent to return disabled status.");return this.parent.isUploaded},enumerable:!1,configurable:!0}),Object.defineProperty(i.prototype,"isDisabled",{get:function(){return this.isUploaded},enumerable:!1,configurable:!0}),i}(i.default);exports.default=u;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),i=require("axios"),r=require("@flumens/utils"),a=require("../Model.js"),n=require("./helpers.js");function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=o(i),u=r.isPlatform("hybrid"),d=function(i){function a(r){void 0===r&&(r={});var a=this,n=r.metadata,o=void 0===n?{}:n,s=t.__rest(r,["metadata"]);return s.createdAt=s.createdAt||(null==o?void 0:o.createdOn),s.updatedAt=s.updatedAt||(null==o?void 0:o.updatedOn),s.syncedAt=s.syncedAt||(null==o?void 0:o.syncedOn),(a=i.call(this,s)||this).remote={synchronising:!1,url:null,getAccessToken:function(){return Promise.resolve("auth token is missing")},timeout:12e4},a.debouncedValue=300,a.parent=a.parent,a.metadata=e.observable(o),a}return t.__extends(a,i),a.getDataURI=function(t,e){return void 0===e&&(e={}),new Promise((function(i,r){if("string"==typeof t){var n=t.replace(/.*\.([a-z]+)$/i,"$1");return"jpg"===n&&(n="jpeg"),void a.resize(t,n,e.width,e.height).then((function(t){var e=t[0],r=t[1];i([r,n,e.width,e.height])}))}if(window.FileReader){var o=new FileReader;o.onload=function(r){var n,o;if(e.width||e.height)a.resize(null===(n=r.target)||void 0===n?void 0:n.result,t.type,e.width,e.height).then((function(e){var r=e[0],a=e[1];i([a,t.type,r.width,r.height])}));else{var s=new window.Image;s.onload=function(){var e,a=t.type.replace(/.*\/([a-z]+)$/i,"$1");i([null===(e=r.target)||void 0===e?void 0:e.result,a,s.width,s.height])},s.src=null===(o=r.target)||void 0===o?void 0:o.result}},o.readAsDataURL(t)}else r(new Error("No File Reader"))}))},a.resize=function(t,e,i,r){return new Promise((function(a){var n=new window.Image;n.onload=function(){var t,o=n.width,s=n.height,u=null;o/=u=o>s?o/(!i||i>o?o:i):s/(!r||r>s?s:r),s/=u;var d=document.createElement("canvas");d.width=o,d.height=s,null===(t=d.getContext("2d"))||void 0===t||t.drawImage(n,0,0,o,s),a([n,d.toDataURL(e)])},n.src=t}))},a.getImageModel=function(e,i,a){return t.__awaiter(this,void 0,void 0,(function(){var n,o,s,d,c,h,l;return t.__generator(this,(function(t){switch(t.label){case 0:if(n=this,!e)throw new Error("File not found while creating image model.");return u?[4,r.createImage(e)]:[3,2];case 1:return c=t.sent(),o=c.width,s=c.height,d=e.split("/").pop(),[3,4];case 2:return[4,n.getDataURI(e,{width:2e3,height:2e3})];case 3:l=t.sent(),d=l[0],o=l[2],s=l[3],t.label=4;case 4:return h=new n({data:{data:d,type:"jpeg",width:o,height:s,path:i}}),a?[3,6]:[4,h.addThumbnail()];case 5:t.sent(),t.label=6;case 6:return[2,h]}}))}))},a.prototype.toJSON=function(){var r=i.prototype.toJSON.call(this);return JSON.parse(JSON.stringify(t.__assign(t.__assign({},r),{metadata:e.toJS(this.metadata)||{}})))},a.prototype.setupdatedAtTimestamp=function(t){var e;i.prototype.setUpdatedAtTimestamp.call(this,t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)},a.prototype.getSubmission=function(t){void 0===t&&(t={});var e=t[this.cid];if(!this.id&&!e)throw new Error("Image ID or queued ID is missing.");var i={values:{caption:this.data.caption}};return this.id?i.values.id=this.id:i.values.queued=e.name,i},a.prototype.sync=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return[2,this.parent?this.parent.sync():i.prototype.sync.call(this)]}))}))},a.prototype.uploadFile=function(){return t.__awaiter(this,arguments,void 0,(function(e){var i,r,a,n,o,u,d,c,h,l;return void 0===e&&(e=!1),t.__generator(this,(function(t){switch(t.label){case 0:if(this.id)throw new Error("A file of a media on the remote cannot be uploaded again.");return i=Date.now()-432e5,r=this.syncedAt>i&&this.data.queued,!e&&r?[2]:[4,this.getFormData()];case 1:if(a=t.sent(),n=new FormData,(c=n).append.apply(c,a),!this.remote.getAccessToken)throw new Error("this.remote.getAccessToken is missing");return h={},u="Bearer ".concat,[4,this.remote.getAccessToken()];case 2:return h.Authorization=u.apply("Bearer ",[t.sent()]),o=h,[4,s.default.post("".concat(this.remote.url,"/index.php/services/rest/media-queue"),n,{headers:o,timeout:12e4})];case 3:if(d=t.sent(),!(null===(l=d.data[this.cid])||void 0===l?void 0:l.name))throw new Error("New remote media name was not be found.");return this.data.queued=d.data[this.cid].name,this.syncedAt=(new Date).getTime(),[2]}}))}))},a.prototype.getFormData=function(){return t.__awaiter(this,void 0,void 0,(function(){var e,i,r,a,o,s,u;return t.__generator(this,(function(t){switch(t.label){case 0:return e=this.data.type,i=e,r=e,(null==e?void 0:e.match(/image.*/))?(u=e.split("/"),i=u[1]):r="image/".concat(r),a=this.getURL(),[4,n.getBlobFromURL(a,r)];case 1:return o=t.sent(),[2,[s=this.cid,o,"".concat(s,".").concat(i)]]}}))}))},a.prototype.getRemoteURL=function(){if(!this.remote.url)throw new Error("No remote url was set.");if(!this.data.queued&&!this.data.path)throw new Error("No media queued or path attribute.");var t=this.remote.url.replace("/index.php/services/rest","");return this.data.queued?"".concat(t,"/upload-queue/").concat(this.data.queued):"".concat(t,"/upload/").concat(this.data.path)},a.prototype.getURL=function(){return this.data.data},a.prototype.resize=function(t,e){var i=this,r=this;return new Promise((function(n,o){a.resize(i.getURL(),i.data.type,t,e).then((function(t){var e=t[0],i=t[1];r.data.data=i,n([e,i])})).catch(o)}))},a.prototype.addThumbnail=function(t){var e=this,i=this;return new Promise((function(t,r){/^data:/i.test(e.getURL())?a.resize(e.getURL(),e.data.type,100,100).then((function(e){var r=e[1];i.data.thumbnail=r,t()})).catch(r):a.getDataURI(e.getURL(),{width:100,height:100}).then((function(e){i.data.thumbnail=e[0],t()})).catch(r)}))},Object.defineProperty(a.prototype,"isUploaded",{get:function(){if(!this.parent)throw new Error("No media parent to return disabled status.");return this.parent.isUploaded},enumerable:!1,configurable:!0}),Object.defineProperty(a.prototype,"isDisabled",{get:function(){return this.isUploaded},enumerable:!1,configurable:!0}),a}(a.default);exports.default=d;
@@ -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,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.log("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
+ "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,i;if(null==t?void 0:t.values)e.id=t.values.id,e.samples&&t.samples&&(null===(a=e.samples)||void 0===a||a.forEach((function(e,r){return v(e,t.samples[r])}))),e.occurrences&&t.occurrences&&(null===(n=e.occurrences)||void 0===n||n.forEach((function(e,r){return v(e,t.occurrences[r])}))),e.media&&t.media&&(null===(i=e.media)||void 0===i||i.forEach((function(e,r){return v(e,t.media[r])})));else{if("process"in window&&"test"===(null===(s=null===(r=window.process)||void 0===r?void 0:r.env)||void 0===s?void 0:s.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 w=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,w=s.Occurrence,b=void 0===w?d.default:w,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=b,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,w=o.parent_event_id,b=l.parseRemoteAttrs(w?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({},b),{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,w,b,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,((b={})[this.id]=this,b)),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),w=function(e){var t=e[0],r=e[1];r!==parseInt(A.id,10)?_[r]?_[r].occurrences.push(t):console.log("Can't attach occ to missing ".concat(r," sample")):A.occurrences.push(t)},y.forEach(w),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,"/index.php/services/rest/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=w,exports.getMediaFormData=_;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {