@flumens/models 0.17.2 → 0.17.3

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"),a=require("lodash"),i=require("../Model.js"),s=require("./Media.js"),n=require("./helpers.js");class r extends i.default{static fromElasticDTO(t,e,a){var i,s,r,d,o,c,u;const{id:l,metadata:m,event:v,occurrence:p,taxon:h,identification:f}=t,y=new Date(m.created_on).getTime(),b=new Date(m.updated_on).getTime(),_=Date.now();let O=v.parent_event_id?null===(s=null===(i=a.smp)||void 0===i?void 0:i.occ)||void 0===s?void 0:s.attrs:null===(r=a.occ)||void 0===r?void 0:r.attrs;O||(O=(null===(o=null===(d=a.smp)||void 0===d?void 0:d.occ)||void 0===o?void 0:o.attrs)||(null===(c=a.occ)||void 0===c?void 0:c.attrs)||{});const g=n.parseRemoteAttrs(O,(null==p?void 0:p.attributes)||[]),A=(null===(u=null==p?void 0:p.media)||void 0===u?void 0:u.map((({path:t})=>({id:t,createdAt:y,updatedAt:b,syncedAt:_,data:{data:`${e}/upload/${t}`}}))))||[],w=h.species||h.taxon_name,j=w!==h.taxon_name?h.taxon_name:"";return new this({id:l,cid:l,createdAt:y,updatedAt:b,syncedAt:_,metadata:{verification:f},data:Object.assign(Object.assign({},g),{taxon:{warehouse_id:parseInt(h.taxa_taxon_list_id,10),scientific_name:w,scientificName:w,common_name:j,commonName:j,found_in_name:j?"common_name":"scientific_name",foundInName:j?"commonName":"scientificName"},comment:null==p?void 0:p.occurrence_remarks}),media:A})}constructor(a={}){var{metadata:i={},media:n=[],Media:d=s.default}=a,o=t.__rest(a,["metadata","media","Media"]);o.createdAt=o.createdAt||(null==i?void 0:i.createdOn),o.updatedAt=o.updatedAt||(null==i?void 0:i.updatedOn),o.syncedAt=o.syncedAt||(null==i?void 0:i.syncedOn),super(o),this.debouncedValue=300,this.keys=()=>{return Object.assign(Object.assign({},r.keys),(t=this.getSurvey().attrs||{},Object.keys(t).reduce(((e,a)=>{var i;return Object.assign(Object.assign({},e),{[a]:(null===(i=t[a])||void 0===i?void 0:i.remote)||t[a]})}),{})));var t},this.Media=d,this.metadata=e.observable(i);const c=n.map(((t,e={})=>a=>a instanceof t?a:new t(Object.assign(Object.assign({},a),e)))(this.Media));this.media=e.observable(c);const u=t=>t.parent=this;this.media.forEach(u);e.intercept(this.media,(t=>{var e;return(null===(e=t.added)||void 0===e?void 0:e.length)?(t.added.forEach(u),this.setUpdatedAtTimestamp(Date.now())):t.removedCount&&this.setUpdatedAtTimestamp(Date.now()),t}))}setUpdatedAtTimestamp(t){var e;super.setUpdatedAtTimestamp(t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)}save(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.parent)throw new Error("Trying to save locally without a parent");this.parent.save()}))}destroy(e){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.parent)throw new Error("Trying to destroy locally without a parent");this.parent.occurrences.remove(this),yield Promise.all(this.media.map((t=>t.destroy(!0)))),e||(yield this.parent.save())}))}sync(){const e=Object.create(null,{sync:{get:()=>super.sync}});return t.__awaiter(this,void 0,void 0,(function*(){return this.parent?this.parent.sync():e.sync.call(this)}))}toJSON(){var t;const a=super.toJSON();return JSON.parse(JSON.stringify(Object.assign(Object.assign({},a),{media:(null===(t=this.media)||void 0===t?void 0:t.map((t=>t.toJSON())))||[],metadata:e.toJS(this.metadata)||{}})))}getSurvey(){if(!this.parent)return{};return this.parent.getSurvey().occ||{}}get isUploaded(){if(!this.parent)throw new Error("No occurrence parent to return disabled status.");return this.parent.isUploaded}get isDisabled(){return this.isUploaded}toDTO(t={}){const e="function"==typeof this.keys?this.keys():this.keys,i=Object.assign(Object.assign({},r.keys),e),s={values:{external_key:this.cid},media:[]};this.id&&(s.values.id=this.id);const n=(t,e)=>{const a=i[t].values;if(!a)return e;if("function"==typeof a)return a(e,s,this);if(a instanceof Array){const i=a.find((({value:t})=>t===e));if(!i||!i.id)throw new Error(`A "${t}" attribute "${e}" value could not be mapped to a remote database field.`);return i.id}return e instanceof Array?e.map((t=>a[t])):a[e]};Object.keys(this.data).forEach((t=>{let e=this.data[t];const r=t=>null==t;if(r(e))return;if(!i[t])return t=t.includes("occAttr:")?t:a.snakeCase(t),void(s.values[t]=e);const d=i[t].id||t;if(e=n(t,e),r(e))return;let o=Number.isNaN(Number(d))?d:`occAttr:${d}`;o=o.includes("occAttr:")?o:a.snakeCase(o),s.values[o]=e})),this.media.forEach((e=>{const a=e.toDTO(t);a&&s.media.push(a)}));const d=this.getSurvey();return d.modifySubmission?d.modifySubmission(s,this):s}}r.keys={taxon:{id:"taxa_taxon_list_id"}},exports.default=r;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),a=require("lodash"),i=require("../Model.js"),s=require("./Media.js"),n=require("./helpers.js");class r extends i.default{static fromElasticDTO(t,e,a){var i,s,r,d,o,c,u;const{id:l,metadata:m,event:v,occurrence:p,taxon:h,identification:f}=t,y=new Date(m.created_on).getTime(),_=new Date(m.updated_on).getTime(),b=Date.now();let O=v.parent_event_id?null===(s=null===(i=a.smp)||void 0===i?void 0:i.occ)||void 0===s?void 0:s.attrs:null===(r=a.occ)||void 0===r?void 0:r.attrs;O||(O=(null===(o=null===(d=a.smp)||void 0===d?void 0:d.occ)||void 0===o?void 0:o.attrs)||(null===(c=a.occ)||void 0===c?void 0:c.attrs)||{});const g=n.parseRemoteAttrs(O,(null==p?void 0:p.attributes)||[]),A=(null===(u=null==p?void 0:p.media)||void 0===u?void 0:u.map((({path:t})=>({id:t,createdAt:y,updatedAt:_,syncedAt:b,data:{data:`${e}/upload/${t}`}}))))||[],w=h.species||h.taxon_name,j=w!==h.taxon_name?h.taxon_name:"";return new this({id:l,cid:l,createdAt:y,updatedAt:_,syncedAt:b,metadata:{verification:f},data:Object.assign(Object.assign({},g),{taxon:{warehouseId:parseInt(h.taxa_taxon_list_id,10),warehouse_id:parseInt(h.taxa_taxon_list_id,10),scientific_name:w,scientificName:w,common_name:j,commonName:j,found_in_name:j?"common_name":"scientific_name",foundInName:j?"commonName":"scientificName"},comment:null==p?void 0:p.occurrence_remarks}),media:A})}constructor(a={}){var{metadata:i={},media:n=[],Media:d=s.default}=a,o=t.__rest(a,["metadata","media","Media"]);o.createdAt=o.createdAt||(null==i?void 0:i.createdOn),o.updatedAt=o.updatedAt||(null==i?void 0:i.updatedOn),o.syncedAt=o.syncedAt||(null==i?void 0:i.syncedOn),super(o),this.debouncedValue=300,this.keys=()=>{return Object.assign(Object.assign({},r.keys),(t=this.getSurvey().attrs||{},Object.keys(t).reduce(((e,a)=>{var i;return Object.assign(Object.assign({},e),{[a]:(null===(i=t[a])||void 0===i?void 0:i.remote)||t[a]})}),{})));var t},this.Media=d,this.metadata=e.observable(i);const c=n.map(((t,e={})=>a=>a instanceof t?a:new t(Object.assign(Object.assign({},a),e)))(this.Media));this.media=e.observable(c);const u=t=>t.parent=this;this.media.forEach(u);e.intercept(this.media,(t=>{var e;return(null===(e=t.added)||void 0===e?void 0:e.length)?(t.added.forEach(u),this.setUpdatedAtTimestamp(Date.now())):t.removedCount&&this.setUpdatedAtTimestamp(Date.now()),t}))}setUpdatedAtTimestamp(t){var e;super.setUpdatedAtTimestamp(t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)}save(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.parent)throw new Error("Trying to save locally without a parent");this.parent.save()}))}destroy(e){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.parent)throw new Error("Trying to destroy locally without a parent");this.parent.occurrences.remove(this),yield Promise.all(this.media.map((t=>t.destroy(!0)))),e||(yield this.parent.save())}))}sync(){const e=Object.create(null,{sync:{get:()=>super.sync}});return t.__awaiter(this,void 0,void 0,(function*(){return this.parent?this.parent.sync():e.sync.call(this)}))}toJSON(){var t;const a=super.toJSON();return JSON.parse(JSON.stringify(Object.assign(Object.assign({},a),{media:(null===(t=this.media)||void 0===t?void 0:t.map((t=>t.toJSON())))||[],metadata:e.toJS(this.metadata)||{}})))}getSurvey(){if(!this.parent)return{};return this.parent.getSurvey().occ||{}}get isUploaded(){if(!this.parent)throw new Error("No occurrence parent to return disabled status.");return this.parent.isUploaded}get isDisabled(){return this.isUploaded}toDTO(t={}){const e="function"==typeof this.keys?this.keys():this.keys,i=Object.assign(Object.assign({},r.keys),e),s={values:{external_key:this.cid},media:[]};this.id&&(s.values.id=this.id);const n=(t,e)=>{const a=i[t].values;if(!a)return e;if("function"==typeof a)return a(e,s,this);if(a instanceof Array){const i=a.find((({value:t})=>t===e));if(!i||!i.id)throw new Error(`A "${t}" attribute "${e}" value could not be mapped to a remote database field.`);return i.id}return e instanceof Array?e.map((t=>a[t])):a[e]};Object.keys(this.data).forEach((t=>{let e=this.data[t];const r=t=>null==t;if(r(e))return;if(!i[t])return t=t.includes("occAttr:")?t:a.snakeCase(t),void(s.values[t]=e);const d=i[t].id||t;if(e=n(t,e),r(e))return;let o=Number.isNaN(Number(d))?d:`occAttr:${d}`;o=o.includes("occAttr:")?o:a.snakeCase(o),s.values[o]=e})),this.media.forEach((e=>{const a=e.toDTO(t);a&&s.media.push(a)}));const d=this.getSurvey();return d.modifySubmission?d.modifySubmission(s,this):s}}r.keys={taxon:{id:"taxa_taxon_list_id"}},exports.default=r;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.17.2",
3
+ "version": "0.17.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -9,6 +9,7 @@
9
9
  "test:eslint": "eslint src --quiet --ext .jsx --ext .ts --ext .tsx --ext .js && echo '\\033[37;42;1m PASS \\033[00m'",
10
10
  "prepare": "npm run build",
11
11
  "postpublish": "TAG_NAME=models-v\"$npm_package_version\" && git tag $TAG_NAME && git push origin \"$TAG_NAME\"",
12
+ "check": "npx tsc --noEmit --skipLibCheck && npm run test:eslint",
12
13
  "test": "jest --passWithNoTests",
13
14
  "test:watch": "jest --watchAll"
14
15
  },