@flumens/models 0.11.1 → 0.11.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.
@@ -5,7 +5,7 @@ export interface Options<T extends Group> extends BaseOptions<T> {
5
5
  getAccessToken: () => Promise<string>;
6
6
  }
7
7
  type RemoteFetchParams = {
8
- type?: 'member' | 'joinable';
8
+ type?: 'member' | 'joinable' | 'pending';
9
9
  form?: string[];
10
10
  location?: number;
11
11
  };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),n=require("@flumens/utils/dist/errors"),i=require("../Collection.js"),s=require("./Group.js"),o=require("./helpers.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=a(r),c=function(r){function i(n){var i=r.call(this,e.__assign({id:"groups",Model:s.default},n))||this;return i.remote=t.observable({synchronising:!1,url:n.url,getAccessToken:n.getAccessToken}),i}return e.__extends(i,r),i.prototype.fetchRemote=function(){return e.__awaiter(this,arguments,void 0,(function(t){var r,i,s,a,c,l,d,h,f,m,p,g,b,y,w,v,_=this;return void 0===t&&(t={}),e.__generator(this,(function(e){switch(e.label){case 0:console.log("📚 Collection: ".concat(this.id," collection fetching")),this.remote.synchronising=!0,e.label=1;case 1:return e.trys.push([1,6,,7]),r=t.type,i=t.form,s=t.location,[4,this.remote.getAccessToken()];case 2:return a=e.sent(),c="".concat(this.remote.url,"/index.php/services/rest/groups"),l={},r&&(l.view=r),i&&(l.page=i.join(",")),Number.isFinite(s)&&(l.indexed_location_ids=s),d={params:l,headers:{Authorization:"Bearer ".concat(a)},timeout:8e4},[4,u.default.get(c,d)];case 3:if(h=e.sent(),f=function(e){return!e.toDate||new Date(e.toDate)>new Date},m=function(e){return!e.fromDate||new Date(e.fromDate)<new Date},p=h.data.map((function(e){return e.values})).map(o.getCamelCaseObj).filter(f).filter(m),g=p.map((function(e){return _.Model.fromDTO(e,{skipStore:"member"!==r})})),r){if("member"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&(y.destroy(),this.data.splice(b,1),b--);else if("joinable"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored||(this.data.splice(b,1),b--)}else for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&y.destroy(),this.data.splice(b,1),b--;return"member"!==r?[3,5]:[4,Promise.all(g.map((function(e){return e.save()})))];case 4:e.sent(),e.label=5;case 5:return(v=this.data).push.apply(v,g),console.log("📚 Collection: ".concat(this.id," collection fetching done ").concat(p.length," documents")),[3,7];case 6:if(w=e.sent(),u.default.isCancel(w))return[2];if(n.isAxiosNetworkError(w))throw new n.HandledError("Request aborted because of a network issue (timeout or similar).");if("issues"in w)throw new Error(w.issues.map((function(e){return"".concat(e.path.join(" ")," ").concat(e.message)})).join(" "));throw this.remote.synchronising=!1,w;case 7:return this.remote.synchronising=!1,[2]}}))}))},Object.defineProperty(i.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},enumerable:!1,configurable:!0}),i}(i.default);exports.byGroupMembershipStatus=function(e){return function(t){var r=t.data.userIsMember||t.data.userIsPending;return"member"===e?r:!r}},exports.default=c;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("@flumens/utils/dist/errors"),i=require("../Collection.js"),n=require("./Group.js"),o=require("./helpers.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=a(r),c=function(r){function i(s){var i=r.call(this,e.__assign({id:"groups",Model:n.default},s))||this;return i.remote=t.observable({synchronising:!1,url:s.url,getAccessToken:s.getAccessToken}),i}return e.__extends(i,r),i.prototype.fetchRemote=function(){return e.__awaiter(this,arguments,void 0,(function(t){var r,i,n,a,c,l,d,h,f,m,p,g,b,y,w,v,_=this;return void 0===t&&(t={}),e.__generator(this,(function(e){switch(e.label){case 0:console.log("📚 Collection: ".concat(this.id," collection fetching")),this.remote.synchronising=!0,e.label=1;case 1:return e.trys.push([1,6,,7]),r=t.type,i=t.form,n=t.location,[4,this.remote.getAccessToken()];case 2:return a=e.sent(),c="".concat(this.remote.url,"/index.php/services/rest/groups"),l={},r&&(l.view=r),i&&(l.page=i.join(",")),Number.isFinite(n)&&(l.indexed_location_ids=n),d={params:l,headers:{Authorization:"Bearer ".concat(a)},timeout:8e4},[4,u.default.get(c,d)];case 3:if(h=e.sent(),f=function(e){return!e.toDate||new Date(e.toDate)>new Date},m=function(e){return!e.fromDate||new Date(e.fromDate)<new Date},p=h.data.map((function(e){return e.values})).map(o.getCamelCaseObj).filter(f).filter(m),g=p.map((function(e){return _.Model.fromDTO(e,{skipStore:"member"!==r})})),r){if("member"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&y.data.userIsMember&&(y.destroy(),this.data.splice(b,1),b--);else if("pending"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&y.data.userIsPending&&(y.destroy(),this.data.splice(b,1),b--);else if("joinable"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored||(this.data.splice(b,1),b--)}else for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&y.destroy(),this.data.splice(b,1),b--;return"member"!==r?[3,5]:[4,Promise.all(g.map((function(e){return e.save()})))];case 4:e.sent(),e.label=5;case 5:return(v=this.data).push.apply(v,g),console.log("📚 Collection: ".concat(this.id," collection fetching done ").concat(p.length," documents")),[3,7];case 6:if(w=e.sent(),u.default.isCancel(w))return[2];if(s.isAxiosNetworkError(w))throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");if("issues"in w)throw new Error(w.issues.map((function(e){return"".concat(e.path.join(" ")," ").concat(e.message)})).join(" "));throw this.remote.synchronising=!1,w;case 7:return this.remote.synchronising=!1,[2]}}))}))},Object.defineProperty(i.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},enumerable:!1,configurable:!0}),i}(i.default);exports.byGroupMembershipStatus=function(e){return function(t){var r=t.data.userIsMember||t.data.userIsPending;return"member"===e?r:!r}},exports.default=c;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),a=require("lodash"),n=require("../Model.js"),i=require("./Media.js"),r=require("./helpers.js"),o=function(n){function o(a){void 0===a&&(a={});var r=this,s=a.metadata,d=void 0===s?{}:s,u=a.media,c=void 0===u?[]:u,p=a.Media,l=void 0===p?i.default:p,m=t.__rest(a,["metadata","media","Media"]);m.createdAt=m.createdAt||(null==d?void 0:d.createdOn),m.updatedAt=m.updatedAt||(null==d?void 0:d.updatedOn),m.syncedAt=m.syncedAt||(null==d?void 0:d.syncedOn),(r=n.call(this,m)||this).debouncedValue=300,r.keys=function(){var e;return t.__assign(t.__assign({},o.keys),(e=r.getSurvey().attrs||{},Object.keys(e).reduce((function(a,n){var i;return t.__assign(t.__assign({},a),((i={})[n]=e[n].remote||e[n],i))}),{})))},r.Media=l,r.metadata=e.observable(d);var v,f,_=c.map((v=r.Media,void 0===f&&(f={}),function(e){return e instanceof v?e:new v(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,n),o.fromElasticDTO=function(e,a,n){var i,o,s,d,u,c,p,l=e.id,m=e.metadata,v=e.event,f=e.occurrence,_=e.taxon,h=e.identification,y=new Date(m.created_on).getTime(),g=new Date(m.updated_on).getTime(),b=Date.now(),A=v.parent_event_id?null===(o=null===(i=n.smp)||void 0===i?void 0:i.occ)||void 0===o?void 0:o.attrs:null===(s=n.occ)||void 0===s?void 0:s.attrs;A||(A=(null===(u=null===(d=n.smp)||void 0===d?void 0:d.occ)||void 0===u?void 0:u.attrs)||(null===(c=n.occ)||void 0===c?void 0:c.attrs)||{});var w=r.parseRemoteAttrs(A,f.attributes||[]),O=null===(p=f.media)||void 0===p?void 0:p.map((function(t){var e=t.path;return{id:e,createdAt:y,updatedAt:g,syncedAt:b,data:{data:"".concat(a,"/upload/").concat(e)}}})),x=_.species||_.taxon_name,N=x!==_.taxon_name?_.taxon_name:"";return new this({id:l,cid:l,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:f.occurrence_remarks}),media:O})},o.prototype.setUpdatedAtTimestamp=function(t){var e;n.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():n.prototype.sync.call(this)]}))}))},o.prototype.toJSON=function(){var a,i=n.prototype.toJSON.call(this);return JSON.parse(JSON.stringify(t.__assign(t.__assign({},i),{media:(null===(a=this.media)||void 0===a?void 0:a.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 n=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=n.data[t],i=function(t){return null==t};if(!i(e)){if(!r[t])return t=t.includes("occAttr:")?t:a.snakeCase(t),void(s.values[t]=e);var o=r[t].id||t;if(e=function(t,e){var a=r[t].values;if(!a)return e;if("function"==typeof a)return a(e,s,n);if(a instanceof Array){var i=a.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 a[t]})):a[e]}(t,e),!i(e)){var d=Number.isNaN(Number(o))?o:"occAttr:".concat(o);d=d.includes("occAttr:")?d:a.snakeCase(d),s.values[d]=e}}})),this.media.forEach((function(t){var a=t.toDTO(e);a&&s.media.push(a)}));var d=this.getSurvey();return d.modifySubmission?d.modifySubmission(s,this):s},o.keys={taxon:{id:"taxa_taxon_list_id"}},o}(n.default);exports.default=o;
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {