@flumens/models 0.11.0 → 0.11.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),n=require("lodash"),a=require("../Model.js"),i=require("./Media.js"),r=require("./helpers.js"),o=function(a){function o(n){void 0===n&&(n={});var r=this,s=n.metadata,d=void 0===s?{}:s,u=n.media,c=void 0===u?[]:u,l=n.Media,p=void 0===l?i.default:l,v=t.__rest(n,["metadata","media","Media"]);v.createdAt=v.createdAt||(null==d?void 0:d.createdOn),v.updatedAt=v.updatedAt||(null==d?void 0:d.updatedOn),v.syncedAt=v.syncedAt||(null==d?void 0:d.syncedOn),(r=a.call(this,v)||this).debouncedValue=300,r.keys=function(){var e;return t.__assign(t.__assign({},o.keys),(e=r.getSurvey().attrs||{},Object.keys(e).reduce((function(n,a){var i;return t.__assign(t.__assign({},n),((i={})[a]=e[a].remote||e[a],i))}),{})))},r.Media=p,r.metadata=e.observable(d);var m,f,_=c.map((m=r.Media,void 0===f&&(f={}),function(e){return e instanceof m?e:new m(t.__assign(t.__assign({},e),f))}));r.media=e.observable(_);var h=function(t){return t.parent=r};r.media.forEach(h);return e.intercept(r.media,(function(t){var e;return(null===(e=t.added)||void 0===e?void 0:e.length)?(t.added.forEach(h),r.setUpdatedAtTimestamp(Date.now())):t.removedCount&&r.setUpdatedAtTimestamp(Date.now()),t})),r}return t.__extends(o,a),o.fromElasticDTO=function(e,n,a){var i,o,s,d,u,c,l,p=e.id,v=e.metadata,m=e.event,f=e.occurrence,_=e.taxon,h=e.identification,y=new Date(v.created_on).getTime(),g=new Date(v.updated_on).getTime(),b=Date.now(),A=m.parent_event_id?null===(o=null===(i=a.smp)||void 0===i?void 0:i.occ)||void 0===o?void 0:o.attrs:null===(s=a.occ)||void 0===s?void 0:s.attrs;A||(A=(null===(u=null===(d=a.smp)||void 0===d?void 0:d.occ)||void 0===u?void 0:u.attrs)||(null===(c=a.occ)||void 0===c?void 0:c.attrs)||{});var w=r.parseRemoteAttrs(A,(null==f?void 0:f.attributes)||[]),O=(null===(l=null==f?void 0:f.media)||void 0===l?void 0:l.map((function(t){var e=t.path;return{id:e,createdAt:y,updatedAt:g,syncedAt:b,data:{data:"".concat(n,"/upload/").concat(e)}}})))||[],x=_.species||_.taxon_name,N=x!==_.taxon_name?_.taxon_name:"";return new this({id:p,cid:p,createdAt:y,updatedAt:g,syncedAt:b,metadata:{verification:h},data:t.__assign(t.__assign({},w),{taxon:{warehouse_id:parseInt(_.taxa_taxon_list_id,10),scientific_name:x,scientificName:x,common_name:N,commonName:N,found_in_name:N?"common_name":"scientific_name",foundInName:N?"commonName":"scientificName"},comment:null==f?void 0:f.occurrence_remarks}),media:O})},o.prototype.setUpdatedAtTimestamp=function(t){var e;a.prototype.setUpdatedAtTimestamp.call(this,t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)},o.prototype.save=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){if(!this.parent)throw new Error("Trying to save locally without a parent");return this.parent.save(),[2]}))}))},o.prototype.destroy=function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:if(!this.parent)throw new Error("Trying to destroy locally without a parent");return this.parent.occurrences.remove(this),[4,Promise.all(this.media.map((function(t){return t.destroy(!0)})))];case 1:return t.sent(),e?[2]:[4,this.parent.save()];case 2:return t.sent(),[2]}}))}))},o.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)]}))}))},o.prototype.toJSON=function(){var n,i=a.prototype.toJSON.call(this);return JSON.parse(JSON.stringify(t.__assign(t.__assign({},i),{media:(null===(n=this.media)||void 0===n?void 0:n.map((function(t){return t.toJSON()})))||[],metadata:e.toJS(this.metadata)||{}})))},o.prototype.getSurvey=function(){return this.parent&&this.parent.getSurvey().occ||{}},Object.defineProperty(o.prototype,"isUploaded",{get:function(){if(!this.parent)throw new Error("No occurrence parent to return disabled status.");return this.parent.isUploaded},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"isDisabled",{get:function(){return this.isUploaded},enumerable:!1,configurable:!0}),o.prototype.toDTO=function(e){var a=this;void 0===e&&(e={});var i="function"==typeof this.keys?this.keys():this.keys,r=t.__assign(t.__assign({},o.keys),i),s={values:{external_key:this.cid},media:[]};this.id&&(s.values.id=this.id);Object.keys(this.data).forEach((function(t){var e=a.data[t],i=function(t){return null==t};if(!i(e)){if(!r[t])return t=t.includes("occAttr:")?t:n.snakeCase(t),void(s.values[t]=e);var o=r[t].id||t;if(e=function(t,e){var n=r[t].values;if(!n)return e;if("function"==typeof n)return n(e,s,a);if(n instanceof Array){var i=n.find((function(t){return t.value===e}));if(!i||!i.id)throw new Error('A "'.concat(t,'" attribute "').concat(e,'" value could not be mapped to a remote database field.'));return i.id}return e instanceof Array?e.map((function(t){return n[t]})):n[e]}(t,e),!i(e)){var d=Number.isNaN(Number(o))?o:"occAttr:".concat(o);d=d.includes("occAttr:")?d:n.snakeCase(d),s.values[d]=e}}})),this.media.forEach((function(t){var n=t.toDTO(e);n&&s.media.push(n)}));var d=this.getSurvey();return d.modifySubmission?d.modifySubmission(s,this):s},o.keys={taxon:{id:"taxa_taxon_list_id"}},o}(a.default);exports.default=o;
|
package/dist/Indicia/Sample.d.ts
CHANGED
|
@@ -18,7 +18,6 @@ export interface Options<T = any, S = any> extends OptionsOriginal<T> {
|
|
|
18
18
|
occurrences?: any[];
|
|
19
19
|
samples?: any[];
|
|
20
20
|
media?: any[];
|
|
21
|
-
Sample?: typeof Sample<any, any>;
|
|
22
21
|
Occurrence?: typeof Occurrence<any, any>;
|
|
23
22
|
Media?: typeof Media<any, any>;
|
|
24
23
|
skipStore?: boolean;
|
|
@@ -87,7 +86,6 @@ declare class Sample<T extends Data = Data, S extends Metadata = Metadata> exten
|
|
|
87
86
|
getAccessToken: () => Promise<string>;
|
|
88
87
|
};
|
|
89
88
|
protected survey?: Survey;
|
|
90
|
-
Sample: typeof Sample<any, any>;
|
|
91
89
|
Occurrence: typeof Occurrence<any, any>;
|
|
92
90
|
Media: typeof Media<any, any>;
|
|
93
91
|
/**
|
|
@@ -104,7 +102,7 @@ declare class Sample<T extends Data = Data, S extends Metadata = Metadata> exten
|
|
|
104
102
|
media: IObservableArray<Media>;
|
|
105
103
|
parent?: Sample<T, S>;
|
|
106
104
|
debouncedValue: number;
|
|
107
|
-
constructor({ metadata, samples, occurrences, media,
|
|
105
|
+
constructor({ metadata, samples, occurrences, media, Occurrence: OccurrenceClass, Media: MediaClass, skipStore, ...options }?: Options);
|
|
108
106
|
setUpdatedAtTimestamp(newUpdatedAt: number): void;
|
|
109
107
|
/**
|
|
110
108
|
* Returns a clean (no observables) JSON representation of the model.
|
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"),c=require("./ElasticSample.js"),u=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 h=p(r),m=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 y(e,t){var r={values:{id:t||-1}};return e.occurrences&&(r.occurrences=e.occurrences.map((function(e){return y(e)}))),e.samples&&(r.samples=e.samples.map((function(e){return y(e)}))),e.media&&(r.media=e.media.map((function(e){return y(e)}))),r}function g(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,c=s.samples,p=void 0===c?[]:c,h=s.occurrences,m=void 0===h?[]:h,v=s.media,_=void 0===v?[]:v,y=s.Sample,g=void 0===y?a:y,w=s.Occurrence,b=void 0===w?d.default:w,A=s.Media,O=void 0===A?u.default:A,E=s.skipStore,S=e.__rest(s,["metadata","samples","occurrences","media","Sample","Occurrence","Media","skipStore"]);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);var T=E?void 0:S.store;(n=r.call(this,e.__assign(e.__assign({},S),{store:T}))||this).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=g,n.Occurrence=b,n.Media=O,n.remote=t.observable({synchronising:!1,url:S.url,getAccessToken:S.getAccessToken}),n.metadata=t.observable(o);var k=p.map(f(n.Sample));n.samples=t.observable(k);var D=m.map(f(n.Occurrence,{Media:n.Media}));n.occurrences=t.observable(D);var q=_.map(f(n.Media));n.media=t.observable(q);var j=function(e){return e.parent=n};n.samples.forEach(j),n.occurrences.forEach(j),n.media.forEach(j);var N=function(e){var t;return(null===(t=e.added)||void 0===t?void 0:t.length)?(e.added.forEach(j),n.setUpdatedAtTimestamp(Date.now())):e.removedCount&&n.setUpdatedAtTimestamp(Date.now()),e};return t.intercept(n.samples,N),t.intercept(n.occurrences,N),t.intercept(n.media,N),n}return e.__extends(a,r),a.fromElasticDTO=function(t,r,s){var a,n;void 0===r&&(r={});var i=t.id,o=t.event,c=t.location,u=t.metadata,d=r.url,p=new Date(o.date_start),h=new Date(u.updated_on).getTime(),f=Date.now(),v=c.point.split(",").map(parseFloat),_=v[0],y=v[1],g="OSGB"===c.output_sref_system?c.output_sref:"",w=c.geom?m.default.parse(c.geom):null,b=o.parent_event_id,A=l.parseRemoteAttrs(b?null==s?void 0:s.smp.attrs:null==s?void 0:s.attrs,o.attributes||[]),O=null===(a=o.media)||void 0===a?void 0:a.map((function(e){var t=e.path;return{id:t,metadata:{updatedOn:p,createdOn:p,syncedOn:p},data:{data:"".concat(d,"/upload/").concat(t)}}}));return new this(e.__assign({id:i,cid:o.source_system_key||i,createdAt:p.getTime(),updatedAt:h,syncedAt:f,metadata:{saved:!0},data:e.__assign(e.__assign({},A),{surveyId:u.survey.id,date:p.toISOString(),location:{code:c.code,name:c.name||c.verbatim_locality,latitude:_,longitude:y,shape:w,gridref:g},enteredSrefSystem:4326,groupId:null===(n=u.group)||void 0===n?void 0:n.id,comment:o.event_remarks,training:"true"===u.trial}),media:O},r))},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,c;return e.__generator(this,(function(u){switch(u.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"]),c=e.__assign(e.__assign({},o),{data:{data:r,metadata:s,occurrences:a,samples:n,media:i}}),[4,this.store.save(c)];case 1:return u.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.toDTO(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,c,u,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,h.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===(c=i.response)||void 0===c?void 0:c.data.duplicate_of))return[2,y(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 g(t),new Error(l.getErrorMessageFromObject(null===(d=null===(u=i.response)||void 0===u?void 0:u.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,u,d,l,p,h,m,f,v,_,y,g,w,b,A,O=this;return e.__generator(this,(function(E){switch(E.label){case 0:if(!(t=this.remote.url)||t.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[4,this.remote.getAccessToken()];case 1:return r=E.sent(),[4,c.fetchElasticSample({id:this.id,cid:this.cid,url:t,accessToken:r})];case 2:return s=E.sent(),this.data.surveyId||(this.data.surveyId=s.metadata.survey.id),a=this.getSurvey(),n=this.Sample.fromElasticDTO(s,{url:t},a),this.id=n.id,this.cid=n.cid||this.cid,Object.assign(this.data,n.data),Object.assign(this.metadata,n.metadata),i=c.fetchElasticSubSamples({parentId:this.id,url:t,accessToken:r}),u=o.fetchElasticOccurrences({parentId:this.id,url:t,accessToken:r}),[4,Promise.all([i,u])];case 3:return d=E.sent(),l=d[0],p=d[1],h=(new Date).getTime(),m=function(e){var r=O.Sample.fromElasticDTO(e,{url:t},O.getSurvey());return r.syncedAt=h,r},f=l.map(m),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(),(A=this.samples).push.apply(A,f),y=function(e){var t=O.Occurrence.fromElasticDTO(e,O.remote.url,O.getSurvey());return t.syncedAt=h,[t,e.event.event_id]},g=p.map(y),w=function(e){var t=e[0],r=e[1];r!==parseInt(O.id,10)?_[r]?_[r].occurrences.push(t):console.log("Can't attach occ to missing ".concat(r," sample")):O.occurrences.push(t)},g.forEach(w),this.syncedAt=h,[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.toDTO(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,c,u,d;return e.__generator(this,(function(e){switch(e.label){case 0:return c={},a="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:c.Authorization=a.apply("Bearer ",[e.sent()]),s=c,e.label=2;case 2:return e.trys.push([2,4,,5]),[4,h.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 g(r),new Error(l.getErrorMessageFromObject(null===(d=null===(u=o.response)||void 0===u?void 0:u.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,c,u,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,h.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,c=function(r){var n,i,c,u;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 c=d.sent(),u=(new Date).getTime(),n.forEach((function(e){var t=e[0];t.syncedAt=u,t.data.queued=c[t.cid].name})),t=e.__assign(e.__assign({},t),c),[2]}}))},u=0,l.label=2;case 2:return u<a.length?[5,c(u)]:[3,5];case 3:l.sent(),l.label=4;case 4:return u+=o,[3,2];case 5:return[2,t]}}))}))},a.prototype.toDTO=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 c=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 c(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=c(e,t))){var n=i[e].id||e,u=Number.isNaN(Number(n))?n:"smpAttr:".concat(n);u=u.includes("smpAttr:")?u:s.snakeCase(u),o.values[u]=t}}})),this.samples.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.toDTO(t);s&&o.samples.push(s)})),this.occurrences.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.toDTO(t);s&&o.occurrences.push(s)})),this.media.forEach((function(e){var r=e.toDTO(t);r&&o.media.push(r)}));var u=this.getSurvey();return u.modifySubmission?u.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}),Object.defineProperty(a.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},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}}},a}(i.default);exports.default=w,exports.getMediaFormData=_;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("lodash"),n=require("wellknown"),a=require("@flumens/utils"),i=require("../Model.js"),o=require("./ElasticOccurrence.js"),c=require("./ElasticSample.js"),u=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 h=p(r),m=p(n),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,n,a,i;if(null==t?void 0:t.values)e.id=t.values.id,e.samples&&t.samples&&(null===(n=e.samples)||void 0===n||n.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===(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,n;return e.__generator(this,(function(a){switch(a.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:n=a.sent(),r.push.apply(r,n),a.label=2;case 2:return t.occurrences?[4,Promise.all(t.occurrences.map(_))]:[3,4];case 3:n=a.sent(),r.push.apply(r,n.flat()),a.label=4;case 4:return t.samples?[4,Promise.all(t.samples.map(_))]:[3,6];case 5:n=a.sent(),r.push.apply(r,n.flat()),a.label=6;case 6:return[2,r]}}))}))};function y(e,t){var r={values:{id:t||-1}};return e.occurrences&&(r.occurrences=e.occurrences.map((function(e){return y(e)}))),e.samples&&(r.samples=e.samples.map((function(e){return y(e)}))),e.media&&(r.media=e.media.map((function(e){return y(e)}))),r}function g(e){try{console.warn(JSON.stringify(e).substring(0,1e5))}catch(e){}}var w=function(r){function n(s){void 0===s&&(s={});var a=this,i=s.metadata,o=void 0===i?{}:i,c=s.samples,p=void 0===c?[]:c,h=s.occurrences,m=void 0===h?[]:h,v=s.media,_=void 0===v?[]:v,y=s.Occurrence,g=void 0===y?d.default:y,w=s.Media,b=void 0===w?u.default:w,A=s.skipStore,O=e.__rest(s,["metadata","samples","occurrences","media","Occurrence","Media","skipStore"]);O.createdAt=O.createdAt||(null==o?void 0:o.createdOn),O.updatedAt=O.updatedAt||(null==o?void 0:o.updatedOn),O.syncedAt=O.syncedAt||(null==o?void 0:o.syncedOn);var E=A?void 0:O.store;(a=r.call(this,e.__assign(e.__assign({},O),{store:E}))||this).validateRemote=l.validateRemoteModel,a.error=t.observable({message:""}),a.debouncedValue=300,a.keys=function(){var t;return e.__assign(e.__assign({},n.keys),(t=a.getSurvey().attrs||{},Object.keys(t).reduce((function(r,s){var n;return e.__assign(e.__assign({},r),((n={})[s]=t[s].remote||t[s],n))}),{})))},a.requiresRemoteSync=function(){return!a.syncedAt||a.updatedAt>a.syncedAt},a.Occurrence=g,a.Media=b,a.remote=t.observable({synchronising:!1,url:O.url,getAccessToken:O.getAccessToken}),a.metadata=t.observable(o);var S=p.map(f(a.constructor));a.samples=t.observable(S);var k=m.map(f(a.Occurrence,{Media:a.Media}));a.occurrences=t.observable(k);var T=_.map(f(a.Media));a.media=t.observable(T);var D=function(e){return e.parent=a};a.samples.forEach(D),a.occurrences.forEach(D),a.media.forEach(D);var q=function(e){var t;return(null===(t=e.added)||void 0===t?void 0:t.length)?(e.added.forEach(D),a.setUpdatedAtTimestamp(Date.now())):e.removedCount&&a.setUpdatedAtTimestamp(Date.now()),e};return t.intercept(a.samples,q),t.intercept(a.occurrences,q),t.intercept(a.media,q),a}return e.__extends(n,r),n.fromElasticDTO=function(t,r,s){var n,a;void 0===r&&(r={});var i=t.id,o=t.event,c=t.location,u=t.metadata,d=r.url,p=new Date(o.date_start),h=new Date(u.updated_on).getTime(),f=Date.now(),v=c.point.split(",").map(parseFloat),_=v[0],y=v[1],g="OSGB"===c.output_sref_system?c.output_sref:"",w=c.geom?m.default.parse(c.geom):null,b=o.parent_event_id,A=l.parseRemoteAttrs(b?null==s?void 0:s.smp.attrs:null==s?void 0:s.attrs,o.attributes||[]),O=null===(n=o.media)||void 0===n?void 0:n.map((function(e){var t=e.path;return{id:t,metadata:{updatedOn:p,createdOn:p,syncedOn:p},data:{data:"".concat(d,"/upload/").concat(t)}}}));return new this(e.__assign({id:i,cid:o.source_system_key||i,createdAt:p.getTime(),updatedAt:h,syncedAt:f,metadata:{saved:!0},data:e.__assign(e.__assign({},A),{surveyId:u.survey.id,date:p.toISOString(),location:{code:c.code,name:c.name||c.verbatim_locality,latitude:_,longitude:y,shape:w,gridref:g},enteredSrefSystem:4326,groupId:null===(a=u.group)||void 0===a?void 0:a.id,comment:o.event_remarks,training:"true"===u.trial}),media:O},r))},n.prototype.setUpdatedAtTimestamp=function(e){var t;r.prototype.setUpdatedAtTimestamp.call(this,e),null===(t=this.parent)||void 0===t||t.setUpdatedAtTimestamp(e)},n.prototype.toJSON=function(){var s,n,a,i=r.prototype.toJSON.call(this);return a=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===(n=this.media)||void 0===n?void 0:n.map((function(e){return e.toJSON()})))||[]}),JSON.parse(JSON.stringify(a))},n.prototype.getSurvey=function(){return this.survey?this.parent?this.parent.getSurvey().smp||{}:this.survey:{}},n.prototype.save=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,a,i,o,c;return e.__generator(this,(function(u){switch(u.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,n=t.occurrences,a=t.samples,i=t.media,o=e.__rest(t,["data","metadata","occurrences","samples","media"]),c=e.__assign(e.__assign({},o),{data:{data:r,metadata:s,occurrences:n,samples:a,media:i}}),[4,this.store.save(c)];case 1:return u.sent(),[2]}}))}))},n.prototype.destroy=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,n=this;return e.__generator(this,(function(e){switch(e.label){case 0:return r=function(){return Promise.all([Promise.all(n.media.map((function(e){return e.destroy(!0)}))),Promise.all(n.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]}}))}))},n.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)]}))}))},n.prototype.saveRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,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,4,,5]),this.remote.synchronising=!0,[4,this.uploadMedia()];case 2:return t=e.sent(),r=this.toDTO(t),[4,this.postRemote(r)];case 3:return s=e.sent(),this.remote.synchronising=!1,v(this,s),n=(new Date).getTime(),this.updatedAt=n,this.syncedAt=n,this.save(),[3,5];case 4:throw a=e.sent(),this.remote.synchronising=!1,a;case 5:return[2]}}))}))},n.prototype.postRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,n,i,o,c,u,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,h.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(n=e.sent(),409===(i=n).status&&(null===(c=i.response)||void 0===c?void 0:c.data.duplicate_of))return[2,y(this,i.response.data.duplicate_of.id)];if(a.isAxiosNetworkError(i))throw new a.HandledError("Request aborted because of a network issue (timeout or similar).");if(400===i.status)throw g(t),new Error(l.getErrorMessageFromObject(null===(d=null===(u=i.response)||void 0===u?void 0:u.data)||void 0===d?void 0:d.message));throw new Error(i.message);case 4:return[2]}}))}))},n.prototype.fetchRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,a,i,u,d,l,p,h,m,f,v,_,y,g,w,b,A,O=this;return e.__generator(this,(function(E){switch(E.label){case 0:if(!(t=this.remote.url)||t.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[4,this.remote.getAccessToken()];case 1:return r=E.sent(),[4,c.fetchElasticSample({id:this.id,cid:this.cid,url:t,accessToken:r})];case 2:return s=E.sent(),this.data.surveyId||(this.data.surveyId=s.metadata.survey.id),n=this.getSurvey(),a=this.constructor.fromElasticDTO(s,{url:t,skipStore:!0},n),this.id=a.id,this.cid=a.cid||this.cid,Object.assign(this.data,a.data),Object.assign(this.metadata,a.metadata),i=c.fetchElasticSubSamples({parentId:this.id,url:t,accessToken:r}),u=o.fetchElasticOccurrences({parentId:this.id,url:t,accessToken:r}),[4,Promise.all([i,u])];case 3:return d=E.sent(),l=d[0],p=d[1],h=(new Date).getTime(),m=function(e){var r=O.constructor.fromElasticDTO(e,{url:t},O.getSurvey());return r.syncedAt=h,r},f=l.map(m),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(),(A=this.samples).push.apply(A,f),y=function(e){var t=O.Occurrence.fromElasticDTO(e,O.remote.url,O.getSurvey());return t.syncedAt=h,[t,e.event.event_id]},g=p.map(y),w=function(e){var t=e[0],r=e[1];r!==parseInt(O.id,10)?_[r]?_[r].occurrences.push(t):console.log("Can't attach occ to missing ".concat(r," sample")):O.occurrences.push(t)},g.forEach(w),this.syncedAt=h,[2]}}))}))},n.prototype.updateRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,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,3,,4]),this.remote.synchronising=!0,t={},r=this.toDTO(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 n=e.sent(),this.remote.synchronising=!1,n;case 4:return[2]}}))}))},n.prototype.putRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s,n,i,o,c,u,d;return e.__generator(this,(function(e){switch(e.label){case 0:return c={},n="Bearer ".concat,[4,this.remote.getAccessToken()];case 1:c.Authorization=n.apply("Bearer ",[e.sent()]),s=c,e.label=2;case 2:return e.trys.push([2,4,,5]),[4,h.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,a.isAxiosNetworkError(o))throw new a.HandledError("Request aborted because of a network issue (timeout or similar).");if(400===o.status)throw g(r),new Error(l.getErrorMessageFromObject(null===(d=null===(u=o.response)||void 0===u?void 0:u.data)||void 0===d?void 0:d.message));throw new Error(o.message);case 5:return[2]}}))}))},n.prototype.uploadMedia=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,i,o,c,u,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,n,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,h.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"===(n=e.sent()).message)throw new a.HandledError("Request aborted because of a network issue (timeout or similar).");throw n;case 5:return[2]}}))}))},n=[],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}:n.push(e)})),o=5,c=function(r){var a,i,c,u;return e.__generator(this,(function(d){switch(d.label){case 0:return a=n.slice(r,r+o),i=new FormData,a.forEach((function(e){var t=e[1];return i.append.apply(i,t)})),[4,s(i)];case 1:return c=d.sent(),u=(new Date).getTime(),a.forEach((function(e){var t=e[0];t.syncedAt=u,t.data.queued=c[t.cid].name})),t=e.__assign(e.__assign({},t),c),[2]}}))},u=0,l.label=2;case 2:return u<n.length?[5,c(u)]:[3,5];case 3:l.sent(),l.label=4;case 4:return u+=o,[3,2];case 5:return[2,t]}}))}))},n.prototype.toDTO=function(t){var r=this;void 0===t&&(t={});var a="function"==typeof this.keys?this.keys():this.keys,i=e.__assign(e.__assign({},n.keys),a),o={values:{external_key:this.cid},media:[],samples:[],occurrences:[]};this.id&&(o.values.id=this.id);var c=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 c(e,t)}));if(s instanceof Array){var n=s.find((function(e){return e.value===t}));if(!n||!n.id)throw new Error('A "'.concat(e,'" attribute "').concat(t,'" value could not be mapped to a remote database field.'));return n.id}return s[t]};Object.keys(this.data).forEach((function(e){var t=r.data[e],n=function(e){return null==e};if(!n(t)){if(!i[e])return e=e.includes("smpAttr:")?e:s.snakeCase(e),void(o.values[e]=t);if(!n(t=c(e,t))){var a=i[e].id||e,u=Number.isNaN(Number(a))?a:"smpAttr:".concat(a);u=u.includes("smpAttr:")?u:s.snakeCase(u),o.values[u]=t}}})),this.samples.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.toDTO(t);s&&o.samples.push(s)})),this.occurrences.forEach((function(e){r.data.training&&(e.data.training=r.data.training);var s=e.toDTO(t);s&&o.occurrences.push(s)})),this.media.forEach((function(e){var r=e.toDTO(t);r&&o.media.push(r)}));var u=this.getSurvey();return u.modifySubmission?u.modifySubmission(o,this):o},Object.defineProperty(n.prototype,"isUploaded",{get:function(){return this.parent?this.parent.isUploaded:!!this.syncedAt},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isDisabled",{get:function(){return this.isUploaded},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},enumerable:!1,configurable:!0}),n.keys={location:{id:"entered_sref"},location_type:{id:"entered_sref_system",values:{british:"OSGB",irish:"OSIE",channel:"utm30ed50",latlon:4326}}},n}(i.default);exports.default=w,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"),i=require("./Sample.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=o(r),u=function(r){function n(n){var a=r.call(this,e.__assign({id:"samples",Model:i.default},n))||this;a.Occurrence=n.Occurrence,a.remote=t.observable({synchronising:!1,url:n.url,getAccessToken:n.getAccessToken});return t.observe(a.data,(function(e){e.addedCount&&e.added.forEach((function(e){return e.collection=a})),e.removedCount&&e.removed.forEach((function(e){return delete e.collection}))})),a}return e.__extends(n,r),n.prototype.fetch=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,n,a,s=this;return e.__generator(this,(function(i){switch(i.label){case 0:return this.store&&this.Model?[4,this.store.findAll()]:(this.ready.resolve(!1),[2]);case 1:return t=i.sent(),r=function(t){var r=t.data,n=r.data,a=r.metadata,i=r.occurrences,o=r.samples,c=r.media,u=r.attrs;return new s.Model(e.__assign(e.__assign({},t),{attrs:u,data:n,metadata:a,occurrences:i,samples:o,media:c}))},n=t.map(r),(a=this.data).push.apply(a,n),this.ready.resolve(!0),[2]}}))}))},n.prototype.fetchRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var n,i,o,u,d,l,f,p,h,m,v,_,y,g,b,O,E,j,w,A,
|
|
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"),i=require("./Sample.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=o(r),u=function(r){function n(n){var a=r.call(this,e.__assign({id:"samples",Model:i.default},n))||this;a.Occurrence=n.Occurrence,a.remote=t.observable({synchronising:!1,url:n.url,getAccessToken:n.getAccessToken});return t.observe(a.data,(function(e){e.addedCount&&e.added.forEach((function(e){return e.collection=a})),e.removedCount&&e.removed.forEach((function(e){return delete e.collection}))})),a}return e.__extends(n,r),n.prototype.fetch=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,n,a,s=this;return e.__generator(this,(function(i){switch(i.label){case 0:return this.store&&this.Model?[4,this.store.findAll()]:(this.ready.resolve(!1),[2]);case 1:return t=i.sent(),r=function(t){var r=t.data,n=r.data,a=r.metadata,i=r.occurrences,o=r.samples,c=r.media,u=r.attrs;return new s.Model(e.__assign(e.__assign({},t),{attrs:u,data:n,metadata:a,occurrences:i,samples:o,media:c}))},n=t.map(r),(a=this.data).push.apply(a,n),this.ready.resolve(!0),[2]}}))}))},n.prototype.fetchRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var n,i,o,u,d,l,f,p,h,m,v,_,y,g,b,O,E,S,j,w,A,T,q,k=this;return e.__generator(this,(function(x){switch(x.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 p=x.sent(),h={method:"post",url:"".concat(f,"/index.php/services/rest/es-samples/_search"),headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(p)},data:l},[4,c.default.request(h)];case 2:return m=x.sent(),v=function(e){return e._source},_=m.data.hits.hits.map(v),y=_.map((function(e){return e.id})),g=s.fetchElasticSubSamples({parentId:y,url:f,accessToken:p}),b=a.fetchElasticOccurrences({parentId:y,url:f,accessToken:p}),[4,Promise.all([g,b])];case 3:return O=x.sent(),E=O[0],S=O[1],j=function(e){return!!e},w=_.map(function(t,r,n,a,s){return function(i){var o,c,u=(new Date).getTime();try{var d=new n({skipStore:!0});d.data.surveyId=i.metadata.survey.id;var l=d.getSurvey(),f=n.fromElasticDTO(i,{url:t,skipStore:!0},l);d.id=f.id,d.cid=f.cid||d.cid,Object.assign(d.data,f.data),Object.assign(d.metadata,f.metadata);var p=function(e){return e.event.parent_event_id===d.id||e.event.event_id===d.id},h=a.filter(p).map((function(e){var r=n.fromElasticDTO(e,{url:t},l);return r.syncedAt=u,r})),m=h.reduce((function(t,r){var n;return e.__assign(e.__assign({},t),((n={})[r.id]=r,n))}),((o={})[d.id]=d,o));return s.filter(p).map((function(e){var n=r.fromElasticDTO(e,t,l);return n.syncedAt=u,[n,e.event.event_id]})).forEach((function(e){var t=e[0],r=e[1];r!==parseInt(d.id,10)?m[r]?m[r].occurrences.push(t):console.log("Can't attach occ to missing ".concat(r," sample")):d.occurrences.push(t)})),(c=d.samples).push.apply(c,h),d.syncedAt=u,d}catch(e){return console.warn(JSON.stringify(i)),console.error(e),null}}}(f,this.Occurrence,this.Model,E,S)).filter(j),A=function(e){return!k.cidMap.has(e.cid)},T=w.filter(A),(q=this.data).push.apply(q,T),[2,T]}}))}))},n.prototype.removeAllSynced=function(){var e=[];return this.data.forEach((function(t){t.syncedAt&&e.push(t.destroy())})),Promise.all(e)},n.prototype.reset=function(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return t=this.data.map((function(e){return e.destroy()})),[4,Promise.all(t)];case 1:return e.sent(),[2]}}))}))},Object.defineProperty(n.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},enumerable:!1,configurable:!0}),n}(n.default);exports.default=u;
|