@flumens/models 0.11.1 → 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;
|