@flumens/models 0.11.3 → 0.12.0

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.
@@ -30,6 +30,8 @@ export interface Data extends GenericModelData {
30
30
  expires_in: number;
31
31
  token_type: string;
32
32
  };
33
+ /** A flag that is set when the full profile is downloaded. */
34
+ profileFetched?: boolean;
33
35
  }
34
36
  export interface Options extends GenericModelOptions {
35
37
  /**
@@ -100,6 +102,7 @@ export default class DrupalUserModel<T extends Data = Data> extends Model<T> {
100
102
  register(email: string, password: string, otherFields: any): Promise<void>;
101
103
  resetPassword(email: string): Promise<void>;
102
104
  delete(): Promise<void>;
105
+ updateRemote(values: Record<string, any>): Promise<void>;
103
106
  /**
104
107
  * Gets full user profile inc. fresh new tokens.
105
108
  */
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("axios"),r=require("jwt-decode"),s=require("zod"),i=require("@flumens/utils"),n=require("../Model.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=a(t);function c(e,t){Object.keys(t).forEach((function(r){if(r.match(/^field_/)){var s=r.replace("field_","").replace(/_([a-z])/g,(function(e){return e[1].toUpperCase()}));if(!t[r])return;if(Array.isArray(t[r])&&t[r].length)return void(e[s]=t[r][0].value);e[s]=t[r]}}))}var d=function(e){var t;if(e.response&&e.response.data&&e.response.data.message){if(e.response.data.message.includes("is already taken"))throw new i.HandledError("This email is already taken.");if("The user credentials were incorrect."===e.response.data.message)throw new i.HandledError("Incorrect password or email");if("Unrecognized username or email address."===e.response.data.message)throw new i.HandledError("Unrecognized email address.");if("This account is already activated"===e.response.data.message)throw new i.HandledError("This account is already activated.");if(e.response.data.message.includes("not been activated"))throw new i.HandledError("The user has not been activated or is blocked.");throw new Error(e.response.data.message)}if(null===(t=e.message)||void 0===t?void 0:t.includes("timeout"))throw new i.HandledError("Timeout");throw e},u=6e4;var h={email:"",tokens:void 0,verified:!1,iss:void 0,indiciaUserId:void 0},l=function(t){function i(r){var s=this,i=r.config,n=e.__rest(r,["config"]);return(s=t.call(this,e.__assign(e.__assign({},n),{data:e.__assign(e.__assign({},h),n.data)}))||this).config=i,s}return e.__extends(i,t),i.prototype.logIn=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this._exchangePasswordToTokens(t,r)];case 1:return s=e.sent(),this.data.tokens=s,[4,this.refreshProfile()];case 2:return e.sent(),[2,this.save()]}}))}))},i.prototype.register=function(t,r,s){return e.__awaiter(this,void 0,void 0,(function(){var n,a,h,l;return e.__generator(this,(function(f){switch(f.label){case 0:return n=JSON.stringify(e.__assign({name:[{value:t}],pass:[{value:r}],mail:[{value:t}]},s)),a={method:"post",url:"".concat(this.config.url,"/user/register-with-password?_format=json"),headers:{"content-type":"application/json"},data:n,timeout:u},[4,o.default(a).catch(d)];case 1:if(h=f.sent().data,!!i.registerSchemaBackend.safeParse(h).error)throw new Error("Invalid backend response.");return[4,this._exchangePasswordToTokens(t,r)];case 2:return l=f.sent(),this.data.tokens=l,this.id=h.uid[0].value,this.data.email=t,c(this.data,h),[2,this.save()]}}))}))},i.prototype.resetPassword=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,i;return e.__generator(this,(function(e){switch(e.label){case 0:r=JSON.stringify({mail:t}),s={method:"post",url:"".concat(this.config.url,"/user/password?_format=json"),headers:{"content-type":"application/json"},data:r},e.label=1;case 1:return e.trys.push([1,3,,6]),[4,o.default(s).catch(d)];case 2:return e.sent(),[3,6];case 3:return"The user has not been activated or is blocked."!==(i=e.sent()).message?[3,5]:[4,this._sendVerificationEmail(t)];case 4:return e.sent(),[2];case 5:return d(i),[3,6];case 6:return[2]}}))}))},i.prototype.delete=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.getAccessToken(!0)];case 1:return t=e.sent(),r={method:"delete",url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),headers:{Authorization:"Bearer ".concat(t)}},[4,o.default(r).catch(d)];case 2:return e.sent(),this.logOut(),[2]}}))}))},i.prototype.refreshProfile=function(){return e.__awaiter(this,void 0,void 0,(function(){var t=this;return e.__generator(this,(function(r){return this._refreshingProfilePromise||(this._refreshingProfilePromise=Promise.resolve().then((function(){return e.__awaiter(t,void 0,void 0,(function(){var t,r,s;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.getAccessToken(!0)];case 1:return t=e.sent(),this.data.verified?(r={url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),headers:{Authorization:"Bearer ".concat(t)},timeout:u},[4,o.default(r).catch(d)]):[3,3];case 2:s=e.sent().data,this.data.email=s.mail[0].value,c(this.data,s),e.label=3;case 3:return[4,this.save()];case 4:return e.sent(),delete this._refreshingProfilePromise,[2]}}))}))})).catch((function(e){throw delete t._refreshingProfilePromise,e}))),[2,this._refreshingProfilePromise]}))}))},i.prototype.hasLogIn=function(){return console.warn("hasLogIn is deprecated, please use isLoggedIn instead."),this.isLoggedIn()},i.prototype.isLoggedIn=function(){return this.data.email,!!this.id},i.prototype.logOut=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.reset()]}))}))},i.prototype.reset=function(r){return t.prototype.reset.call(this,e.__assign(e.__assign({},h),r))},i.prototype.getAccessToken=function(t){return e.__awaiter(this,void 0,void 0,(function(){var s,i,n,a,o;return e.__generator(this,(function(e){switch(e.label){case 0:if(!t&&!this.isLoggedIn())throw new Error("User is not logged in.");if(s=(this.data.tokens||{}).access_token,i=function(e){if(!e)return!0;var t=1e3*r.jwtDecode(e).exp;return Date.now()+12e4>t}(s),!(!s||i||t))return[3,6];e.label=1;case 1:return e.trys.push([1,3,,6]),[4,this._refreshAccessToken()];case 2:return e.sent(),[3,6];case 3:return n=e.sent(),console.error(n),a="The refresh token is invalid."===n.message,o="Token has no user email."===n.message,a||o?[4,this.logOut()]:[3,5];case 4:throw e.sent(),new Error("User re-login is required.");case 5:throw n;case 6:return[2,this.data.tokens.access_token]}}))}))},i.prototype._sendVerificationEmail=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s;return e.__generator(this,(function(e){return r=JSON.stringify({mail:[{value:t||this.data.email}]}),s={method:"post",url:"".concat(this.config.url,"/user/register-with-password?_format=json&resendVerificationEmail=true"),headers:{"content-type":"application/json"},data:r,timeout:u},[2,o.default(s).catch(d)]}))}))},i.prototype._exchangePasswordToTokens=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s,i,n;return e.__generator(this,(function(e){switch(e.label){case 0:return(s=new FormData).append("grant_type","password"),s.append("username",t),s.append("password",r),s.append("client_id",this.config.clientId),this.config.clientPass&&s.append("client_secret",this.config.clientPass),(null===(n=this.config.scopes)||void 0===n?void 0:n.length)&&s.append("scope",this.config.scopes.join(" ")),i={method:"post",url:"".concat(this.config.url,"/oauth/token"),data:s},[4,o.default(i).catch(d)];case 1:return[2,e.sent().data]}}))}))},i.prototype._refreshAccessToken=function(){return e.__awaiter(this,void 0,void 0,(function(){var t=this;return e.__generator(this,(function(s){return this._refreshingTokenPromise||(this._refreshingTokenPromise=Promise.resolve().then((function(){return e.__awaiter(t,void 0,void 0,(function(){var t,s,i,n,a,h;return e.__generator(this,(function(l){switch(l.label){case 0:if(!(t=(this.data.tokens||{}).refresh_token))throw new Error("No user session refresh token was found");return(s=new FormData).append("grant_type","refresh_token"),s.append("refresh_token",t),s.append("client_id",this.config.clientId),this.config.clientPass&&s.append("client_secret",this.config.clientPass),i={method:"post",url:"".concat(this.config.url,"/oauth/token"),data:s,timeout:u},[4,o.default(i).catch(d)];case 1:if(n=l.sent().data,this.data.tokens=e.__assign(e.__assign({},this.data.tokens),n),a=r.jwtDecode(null===(h=this.data.tokens)||void 0===h?void 0:h.access_token),this.id=a.sub,this.data.email=a.email,!this.data.email)throw new Error("Token has no user email.");return this.data.verified=a.email_verified,this.data.iss=a.iss,this.data.indiciaUserId=a["http://indicia.org.uk/user:id"],a.scopes&&(this.data.roles=a.scopes),c(this.data,a),[4,this.save()];case 2:return l.sent(),delete this._refreshingTokenPromise,[2]}}))}))})).catch((function(e){throw delete t._refreshingTokenPromise,e}))),[2,this._refreshingTokenPromise]}))}))},i.loginSchema=s.object({email:s.z.string().email("Please fill in"),password:s.z.string().min(1,"Please fill in")}),i.loginSchemaBackend=s.object({id:s.z.number(),email:s.z.string().email()}),i.resetSchema=s.object({email:s.z.string().email("Please fill in")}),i.registerSchema=s.object({email:s.z.string().email("Please fill in"),password:s.z.string().min(1,"Please fill in")}),i.registerSchemaBackend=s.object({uid:s.z.array(s.object({value:s.z.number()}))}),i}(n.default);exports.default=l;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("axios"),r=require("jwt-decode"),s=require("zod"),n=require("@flumens/utils"),i=require("../Model.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=a(t);function c(e){var t={};return Object.keys(e).forEach((function(r){if(r.match(/^field_/)){var s=r.replace("field_","").replace(/_([a-z])/g,(function(e){return e[1].toUpperCase()}));if(!e[r])return;if(Array.isArray(e[r])&&e[r].length)return void(t[s]=e[r][0].value);t[s]=e[r]}})),t}var u=function(e){var t;if(e.response&&e.response.data&&e.response.data.message){if(e.response.data.message.includes("is already taken"))throw new n.HandledError("This email is already taken.");if("The user credentials were incorrect."===e.response.data.message)throw new n.HandledError("Incorrect password or email");if("Unrecognized username or email address."===e.response.data.message)throw new n.HandledError("Unrecognized email address.");if("This account is already activated"===e.response.data.message)throw new n.HandledError("This account is already activated.");if(e.response.data.message.includes("not been activated"))throw new n.HandledError("The user has not been activated or is blocked.");throw new Error(e.response.data.message)}if(null===(t=e.message)||void 0===t?void 0:t.includes("timeout"))throw new n.HandledError("Timeout");throw e},d=6e4;var h={email:"",tokens:void 0,verified:!1,iss:void 0,indiciaUserId:void 0,profileFetched:!1},l=function(t){function n(r){var s=this,n=r.config,i=e.__rest(r,["config"]);return(s=t.call(this,e.__assign(e.__assign({},i),{data:e.__assign(e.__assign({},h),i.data)}))||this).config=n,s}return e.__extends(n,t),n.prototype.logIn=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this._exchangePasswordToTokens(t,r)];case 1:return s=e.sent(),this.data.tokens=s,[4,this.refreshProfile()];case 2:return e.sent(),[2,this.save()]}}))}))},n.prototype.register=function(t,r,s){return e.__awaiter(this,void 0,void 0,(function(){var i,a,h,l,f;return e.__generator(this,(function(_){switch(_.label){case 0:return i=JSON.stringify(e.__assign({name:[{value:t}],pass:[{value:r}],mail:[{value:t}]},s)),a={method:"post",url:"".concat(this.config.url,"/user/register-with-password?_format=json"),headers:{"content-type":"application/json"},data:i,timeout:d},[4,o.default(a).catch(u)];case 1:if(h=_.sent().data,!!n.registerSchemaBackend.safeParse(h).error)throw new Error("Invalid backend response.");return[4,this._exchangePasswordToTokens(t,r)];case 2:return l=_.sent(),this.id=h.uid[0].value,f=c(h),Object.assign(this.data,e.__assign({email:t,tokens:l,profileFetched:!0},f)),[2,this.save()]}}))}))},n.prototype.resetPassword=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,n;return e.__generator(this,(function(e){switch(e.label){case 0:r=JSON.stringify({mail:t}),s={method:"post",url:"".concat(this.config.url,"/user/password?_format=json"),headers:{"content-type":"application/json"},data:r},e.label=1;case 1:return e.trys.push([1,3,,6]),[4,o.default(s).catch(u)];case 2:return e.sent(),[3,6];case 3:return"The user has not been activated or is blocked."!==(n=e.sent()).message?[3,5]:[4,this._sendVerificationEmail(t)];case 4:return e.sent(),[2];case 5:return u(n),[3,6];case 6:return[2]}}))}))},n.prototype.delete=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.getAccessToken(!0)];case 1:return t=e.sent(),r={method:"delete",url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),headers:{Authorization:"Bearer ".concat(t)}},[4,o.default(r).catch(u)];case 2:return e.sent(),this.logOut(),[2]}}))}))},n.prototype.updateRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.getAccessToken()];case 1:return r=e.sent(),s={method:"patch",url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),data:t,headers:{Authorization:"Bearer ".concat(r)},timeout:d},[4,o.default(s)];case 2:return e.sent(),[4,this._refreshAccessToken()];case 3:return e.sent(),this.save(),[2]}}))}))},n.prototype.refreshProfile=function(){return e.__awaiter(this,void 0,void 0,(function(){var t=this;return e.__generator(this,(function(r){return this._refreshingProfilePromise||(this._refreshingProfilePromise=Promise.resolve().then((function(){return e.__awaiter(t,void 0,void 0,(function(){var t,r,s,n;return e.__generator(this,(function(i){switch(i.label){case 0:return[4,this.getAccessToken(!0)];case 1:return t=i.sent(),this.data.verified?(r={url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),headers:{Authorization:"Bearer ".concat(t)},timeout:d},[4,o.default(r).catch(u)]):[3,3];case 2:s=i.sent().data,n=c(s),Object.assign(this.data,e.__assign({email:s.mail[0].value,profileFetched:!0},n)),i.label=3;case 3:return[4,this.save()];case 4:return i.sent(),delete this._refreshingProfilePromise,[2]}}))}))})).catch((function(e){throw delete t._refreshingProfilePromise,e}))),[2,this._refreshingProfilePromise]}))}))},n.prototype.hasLogIn=function(){return console.warn("hasLogIn is deprecated, please use isLoggedIn instead."),this.isLoggedIn()},n.prototype.isLoggedIn=function(){return this.data.email,!!this.id},n.prototype.logOut=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.reset()]}))}))},n.prototype.reset=function(r){return t.prototype.reset.call(this,e.__assign(e.__assign({},h),r))},n.prototype.getAccessToken=function(t){return e.__awaiter(this,void 0,void 0,(function(){var s,n,i,a,o;return e.__generator(this,(function(e){switch(e.label){case 0:if(!t&&!this.isLoggedIn())throw new Error("User is not logged in.");if(s=(this.data.tokens||{}).access_token,n=function(e){if(!e)return!0;var t=1e3*r.jwtDecode(e).exp;return Date.now()+12e4>t}(s),!(!s||n||t))return[3,6];e.label=1;case 1:return e.trys.push([1,3,,6]),[4,this._refreshAccessToken()];case 2:return e.sent(),[3,6];case 3:return i=e.sent(),console.error(i),a="The refresh token is invalid."===i.message,o="Token has no user email."===i.message,a||o?[4,this.logOut()]:[3,5];case 4:throw e.sent(),new Error("User re-login is required.");case 5:throw i;case 6:return[2,this.data.tokens.access_token]}}))}))},n.prototype._sendVerificationEmail=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s;return e.__generator(this,(function(e){return r=JSON.stringify({mail:[{value:t||this.data.email}]}),s={method:"post",url:"".concat(this.config.url,"/user/register-with-password?_format=json&resendVerificationEmail=true"),headers:{"content-type":"application/json"},data:r,timeout:d},[2,o.default(s).catch(u)]}))}))},n.prototype._exchangePasswordToTokens=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s,n,i;return e.__generator(this,(function(e){switch(e.label){case 0:return(s=new FormData).append("grant_type","password"),s.append("username",t),s.append("password",r),s.append("client_id",this.config.clientId),this.config.clientPass&&s.append("client_secret",this.config.clientPass),(null===(i=this.config.scopes)||void 0===i?void 0:i.length)&&s.append("scope",this.config.scopes.join(" ")),n={method:"post",url:"".concat(this.config.url,"/oauth/token"),data:s},[4,o.default(n).catch(u)];case 1:return[2,e.sent().data]}}))}))},n.prototype._refreshAccessToken=function(){return e.__awaiter(this,void 0,void 0,(function(){var t=this;return e.__generator(this,(function(s){return this._refreshingTokenPromise||(this._refreshingTokenPromise=Promise.resolve().then((function(){return e.__awaiter(t,void 0,void 0,(function(){var t,s,n,i,a,h,l,f,_,g,p,m;return e.__generator(this,(function(v){switch(v.label){case 0:if(!(t=(this.data.tokens||{}).refresh_token))throw new Error("No user session refresh token was found");return(s=new FormData).append("grant_type","refresh_token"),s.append("refresh_token",t),s.append("client_id",this.config.clientId),this.config.clientPass&&s.append("client_secret",this.config.clientPass),n={method:"post",url:"".concat(this.config.url,"/oauth/token"),data:s,timeout:d},[4,o.default(n).catch(u)];case 1:if(i=v.sent().data,a=e.__assign(e.__assign({},this.data.tokens),i),h=r.jwtDecode(a.access_token),!(l=h.email))throw new Error("Token has no user email.");return f=h.iss,_=h.email_verified,g=h["http://indicia.org.uk/user:id"],p=h.scopes,m=c(h),this.id=h.sub,Object.assign(this.data,e.__assign({email:l,roles:p,verified:_,iss:f,indiciaUserId:g,tokens:a},m)),[4,this.save()];case 2:return v.sent(),delete this._refreshingTokenPromise,[2]}}))}))})).catch((function(e){throw delete t._refreshingTokenPromise,e}))),[2,this._refreshingTokenPromise]}))}))},n.loginSchema=s.object({email:s.z.string().email("Please fill in"),password:s.z.string().min(1,"Please fill in")}),n.loginSchemaBackend=s.object({id:s.z.number(),email:s.z.string().email()}),n.resetSchema=s.object({email:s.z.string().email("Please fill in")}),n.registerSchema=s.object({email:s.z.string().email("Please fill in"),password:s.z.string().min(1,"Please fill in")}),n.registerSchemaBackend=s.object({uid:s.z.array(s.object({value:s.z.number()}))}),n}(i.default);exports.default=l;
@@ -1 +1 @@
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
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("@flumens/utils/dist/errors"),n=require("../Collection.js"),i=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 n(s){var n=r.call(this,e.__assign({id:"groups",Model:i.default},s))||this;return n.remote=t.observable({synchronising:!1,url:s.url,getAccessToken:s.getAccessToken}),n}return e.__extends(n,r),n.prototype.fetchRemote=function(){return e.__awaiter(this,arguments,void 0,(function(t){var r,n,i,a,c,l,d,h,f,m,p,g,b,y,v,w,j=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,8,,9]),r=t.type,n=t.form,i=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),n&&(l.page=n.join(",")),Number.isFinite(i)&&(l.indexed_location_ids=i),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 j.Model.fromDTO(e,{skipStore:"joinable"===r})})),r){if("member"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&"t"===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&&"t"===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"pending"!==r?[3,7]:[4,Promise.all(g.map((function(e){return e.save()})))];case 6:e.sent(),e.label=7;case 7:return(w=this.data).push.apply(w,g),console.log("📚 Collection: ".concat(this.id," collection fetching done ").concat(p.length," documents")),[3,9];case 8:if(v=e.sent(),u.default.isCancel(v))return[2];if(s.isAxiosNetworkError(v))throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");if("issues"in v)throw new Error(v.issues.map((function(e){return"".concat(e.path.join(" ")," ").concat(e.message)})).join(" "));throw this.remote.synchronising=!1,v;case 9:return this.remote.synchronising=!1,[2]}}))}))},Object.defineProperty(n.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},enumerable:!1,configurable:!0}),n}(n.default);exports.byGroupMembershipStatus=function(e){return function(t){var r=t.data.userIsMember||t.data.userIsPending;return"member"===e?r:!r}},exports.default=c;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.11.3",
3
+ "version": "0.12.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {