roboto-js 1.9.7 → 1.9.9

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,4 +1,4 @@
1
1
  // Auto-generated version file
2
2
  // DO NOT EDIT - This file is automatically updated from package.json
3
- // Version: 1.9.7
4
- export var version = '1.9.7';
3
+ // Version: 1.9.9
4
+ export var version = '1.9.9';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roboto-js",
3
- "version": "1.9.7",
3
+ "version": "1.9.9",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "dist/cjs/index.cjs",
package/src/index.js CHANGED
@@ -308,6 +308,9 @@ export default class Roboto{
308
308
  async switchOrganization(orgId){
309
309
  return this.api.switchOrganization(orgId);
310
310
  }
311
+ async selectCurrentOrganization(orgId, role = 'owner'){
312
+ return this.api.selectCurrentOrganization(orgId, role);
313
+ }
311
314
  getCurrentOrganization(){
312
315
  return this.api.getCurrentOrganization();
313
316
  }
package/src/rbt_api.js CHANGED
@@ -658,6 +658,87 @@ export default class RbtApi {
658
658
  return this.currentOrganization;
659
659
  }
660
660
 
661
+ /**
662
+ * Select and link an organization to the current user
663
+ * This adds the organization to the user's organizations array (if not already there)
664
+ * and sets it as the current organization preference
665
+ *
666
+ * @param {string} orgId - Organization ID to select
667
+ * @param {string} role - Role for the user in this organization (default: 'owner')
668
+ * @returns {Promise<RbtObject>} The selected organization
669
+ */
670
+ async selectCurrentOrganization(orgId, role = 'owner') {
671
+ try {
672
+ // Ensure we have a current user
673
+ if (!this.currentUser) {
674
+ console.log('[RbtApi] Loading current user for organization selection...');
675
+ await this.loadCurrentUser();
676
+ if (!this.currentUser) {
677
+ throw new Error('Must be logged in to select organization');
678
+ }
679
+ }
680
+
681
+ console.log('[RbtApi] Selecting organization:', orgId);
682
+
683
+ // Load the organization to verify access
684
+ const org = await this.get('<@iac.organization>', orgId);
685
+ if (!org) {
686
+ throw new Error(`Organization ${orgId} not found or not accessible`);
687
+ }
688
+
689
+ // Load the user object using loadUser to get a saveable instance
690
+ const user = await this.loadUser(this.currentUser.id);
691
+ if (!user) {
692
+ throw new Error('Failed to load user object');
693
+ }
694
+
695
+ console.log('[RbtApi] User loaded:', user.id);
696
+
697
+ // Update user's organizations array
698
+ const userOrgs = user.get('organizations') || [];
699
+ const orgExists = userOrgs.some(o => o?.id === orgId);
700
+
701
+ if (!orgExists) {
702
+ console.log('[RbtApi] Adding organization to user organizations array');
703
+ userOrgs.unshift({ id: orgId, roles: [role] });
704
+ user.set('organizations', userOrgs);
705
+ } else {
706
+ console.log('[RbtApi] Organization already in user organizations array');
707
+ }
708
+
709
+ // Set as current organization preference - handle both nested path formats
710
+ const modData = user.get('mod') || {};
711
+ modData.currentOrgId = orgId;
712
+ user.set('mod', modData);
713
+
714
+ // Save user record
715
+ console.log('[RbtApi] Saving user with organization:', orgId);
716
+ await user.save();
717
+ console.log('[RbtApi] User saved successfully');
718
+
719
+ // Clear cached organization to force reload
720
+ this.currentOrganization = null;
721
+
722
+ // Update the current user reference
723
+ this.currentUser = user;
724
+
725
+ // Set the new current organization
726
+ this.currentOrganization = org;
727
+
728
+ // Update cache
729
+ if (this.localStorageAdaptor) {
730
+ await this.localStorageAdaptor.setItem('currentOrgId', orgId);
731
+ }
732
+
733
+ console.log('[RbtApi] Selected organization:', org.get('name'));
734
+
735
+ return org;
736
+ } catch (error) {
737
+ console.error('[RbtApi] Error in selectCurrentOrganization:', error);
738
+ return this._handleError(error);
739
+ }
740
+ }
741
+
661
742
 
662
743
  async loadUser(userId){
663
744
 
package/src/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // Auto-generated version file
2
2
  // DO NOT EDIT - This file is automatically updated from package.json
3
- // Version: 1.9.7
4
- export const version = '1.9.7';
3
+ // Version: 1.9.9
4
+ export const version = '1.9.9';