Creates a new organization-user relationship
Initializes a relationship between a user and an organization with specified permissions and metadata. This relationship controls the user's access and capabilities within the organization.
Configuration data including: - org: Organization ID or data - user: User ID or data - permission: Access level ('owner', 'admin', 'member', 'guest') - metadata: Custom metadata object
Optionaluri: stringOptional custom URI path for the relationship endpoint
Get the associated user details
This getter provides access to the user's details within the context of the organization relationship. It returns a User instance that can be used to access and manage user-specific data.
User instance with full user details
Basic user access:
const user = orgUser.user;
console.log(`Member: ${user.name} (${user.email})`);
Detailed user information:
try {
const user = orgUser.user;
console.log('User Details:');
console.log(`Name: ${user.name}`);
console.log(`Email: ${user.email}`);
console.log(`Status: ${user.isActive() ? 'Active' : 'Inactive'}`);
console.log(`Last Login: ${user.last_login_at}`);
} catch (error) {
console.error('User data not available:', error.message);
}
Set the associated user details
This setter updates the user details within the organization relationship. It's typically used when reassigning the relationship to a different user or updating user details in bulk.
Complete user data including: - id: User's unique identifier - name: User's full name - email: User's email address - metadata: Additional user data
Get the associated organization details
This getter provides access to the organization's details within the context of the user relationship. It returns an Organization instance that can be used to access and manage organization-specific data.
Organization instance with full organization details
Basic organization access:
const org = orgUser.org;
console.log(`Organization: ${org.name}`);
console.log(`Description: ${org.short_description}`);
Detailed organization information:
try {
const org = orgUser.org;
console.log('Organization Details:');
console.log(`Name: ${org.name}`);
console.log(`Description: ${org.short_description}`);
console.log(`Status: ${org.isActive() ? 'Active' : 'Inactive'}`);
console.log(`Members: ${org.member_count}`);
if (org.metadata?.industry) {
console.log(`Industry: ${org.metadata.industry}`);
}
} catch (error) {
console.error('Organization data not available:', error.message);
}
Set the associated organization details
This setter updates the organization details within the user relationship. It's typically used when reassigning the relationship to a different organization or updating organization details in bulk.
Complete organization data including: - id: Organization's unique identifier - name: Organization name - short_description: Brief description - metadata: Additional organization data
Basic organization update:
orgUser.org = {
id: 'org-123',
name: 'Acme Corporation',
short_description: 'Leading tech company'
};
Detailed organization update:
orgUser.org = {
id: 'org-456',
name: 'Tech Innovators',
short_description: 'AI and ML solutions',
long_description: 'Cutting-edge AI/ML solutions for enterprises',
metadata: {
industry: 'technology',
size: 'enterprise',
founded: '2020',
locations: ['San Francisco', 'London']
}
};
// Save changes
await orgUser.save();
Check if the entity is active
This method checks the active status of the entity. Most entities in the system can be active or inactive, which affects their availability and usability in the platform.
True if the entity is active, false otherwise
Convert model instance to interface data
This method serializes the model instance to a plain object that matches the interface type. This is useful for:
The model data as a plain object matching the interface type
Convert model instance to API payload
This method creates a payload suitable for API requests by:
A clean object suitable for API requests
Update model data with new values
This method updates the model's data and instance properties with new values. It performs a shallow merge of the updates with existing data, allowing for partial updates of the model's properties.
Object containing properties to update
const user = new User({
email: 'old@example.com',
firstName: 'John'
});
// Update multiple properties
user.update({
email: 'new@example.com',
lastName: 'Doe'
});
// Save changes to API
await user.save();
This method only updates the local model instance. To persist changes to the API, call save after updating.
Save the model instance to the API
This method persists the current state of the model to the API using a PUT request. It requires the model to have an ID (existing instance). For new instances, use the collection's create method instead.
The method:
Promise resolving to the updated model data
Delete the model instance from the API
This method permanently deletes the model instance from the API and clears the local data. This operation cannot be undone.
The method:
Promise that resolves when deletion is successful
Create an authenticated session for the organization user
This method creates a new authenticated session for the user within the organization context. The session includes access tokens and configuration needed to interact with organization resources.
The method:
Promise resolving to MosaiaConfig with session details
Basic session creation:
try {
const config = await orgUser.session();
const mosaia = new Mosaia(config);
// Use the authenticated session
const agents = await mosaia.agents.get();
console.log(`Found ${agents.length} agents`);
} catch (error) {
console.error('Session failed:', error.message);
}
Advanced session usage:
try {
// Create authenticated session
const config = await orgUser.session();
const mosaia = new Mosaia(config);
// Access organization resources
const [agents, models, apps] = await Promise.all([
mosaia.agents.get(),
mosaia.models.get(),
mosaia.apps.get()
]);
console.log('Organization Resources:');
console.log(`- ${agents.length} AI agents`);
console.log(`- ${models.length} models`);
console.log(`- ${apps.length} applications`);
} catch (error) {
if (error.message.includes('inactive')) {
console.error('User access is inactive');
} else if (error.message.includes('unauthorized')) {
console.error('Invalid permissions');
} else {
console.error('Session error:', error.message);
}
}
Disable the organization-user relationship
This method deactivates the relationship between the user and organization, effectively revoking the user's access to organization resources. This is useful for:
The method:
Promise resolving when relationship is disabled
Basic deactivation:
try {
await orgUser.disable();
console.log('User access revoked successfully');
} catch (error) {
console.error('Failed to revoke access:', error.message);
}
Managed offboarding:
async function offboardUser(orgUser: OrgUser): Promise<void> {
try {
// Get user details for logging
const user = orgUser.user;
const org = orgUser.org;
// Revoke access
await orgUser.disable();
console.log('User offboarded successfully:');
console.log(`- User: ${user.name} (${user.email})`);
console.log(`- From: ${org.name}`);
console.log(`- Time: ${new Date().toISOString()}`);
} catch (error) {
console.error('Offboarding failed:', error.message);
throw error; // Re-throw for handling by caller
}
}
OrgUser class for managing organization-user relationships
This class represents the relationship between a user and an organization in the Mosaia platform. It manages permissions, roles, and access control within organizational contexts, enabling fine-grained control over user access to organizational resources.
Features:
Remarks
Organization-user relationships are crucial for:
The class supports various permission levels:
Example
Basic relationship setup:
Example
Managing access and sessions: