const { ethers } = require('ethers');
require('dotenv').config();
class RiseInviteSystem {
constructor(baseUrl = 'https://b2b-api.riseworks.io', jwtToken) {
this.baseUrl = baseUrl;
this.headers = {
'Authorization': `Bearer ${jwtToken}`,
'Content-Type': 'application/json'
};
}
// Regular invites (no typed data required)
async createInvite(nanoid, invites, role, anonymous = false) {
const inviteData = {
nanoid: nanoid,
invites: invites,
role: role,
anonymous: anonymous
};
const response = await fetch(`${this.baseUrl}/v2/invites`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(inviteData)
});
return response.json();
}
// Manager invites (typed data flow)
async createManagerInvite(emails, role, nanoid) {
const inviteData = {
emails: emails,
role: role,
nanoid: nanoid
};
const response = await fetch(`${this.baseUrl}/v2/invites/manager`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(inviteData)
});
return response.json();
}
async executeManagerInvite(invites, signer, typedData, signature) {
const executeData = {
invites: invites,
signer: signer,
typed_data: typedData,
signature: signature
};
const response = await fetch(`${this.baseUrl}/v2/invites/manager`, {
method: 'PUT',
headers: this.headers,
body: JSON.stringify(executeData)
});
return response.json();
}
async signManagerInvite(typedData, privateKey) {
const wallet = new ethers.Wallet(privateKey);
const signature = await wallet.signTypedData(
typedData.domain,
typedData.types,
typedData.typed_data
);
return signature;
}
async getInvites(nanoid) {
const params = new URLSearchParams({ nanoid: nanoid });
const response = await fetch(`${this.baseUrl}/v2/invites?${params}`, {
headers: this.headers
});
return response.json();
}
async sendBulkInvites(nanoid, inviteList) {
const results = [];
const errors = [];
for (const inviteData of inviteList) {
try {
const result = await this.createInvite(
nanoid,
[inviteData],
inviteData.role,
false
);
results.push(result);
console.log(`Invite sent to ${inviteData.email}`);
} catch (error) {
errors.push({
email: inviteData.email,
error: error.message
});
console.error(`Failed to invite ${inviteData.email}:`, error.message);
}
}
return { results, errors };
}
}
// Usage example
async function main() {
const inviteSystem = new RiseInviteSystem(
'https://b2b-api.riseworks.io',
process.env.JWT_TOKEN
);
try {
// Regular invites for employees
const regularInvites = await inviteSystem.createInvite(
process.env.TEAM_NANODID,
[
{
email: 'employee@company.com',
prefill: {
first_name: 'John',
last_name: 'Doe',
phone: '+1234567890',
company_name: 'Acme Corp',
job_title: 'Software Engineer',
company_size: '10-50',
company_website: 'https://acme.com',
company_industry: 'Technology',
company_address: '123 Main St',
company_city: 'San Francisco',
company_state: 'CA',
company_zip: '94105',
company_country: 'USA'
}
}
],
'team_employee',
false
);
console.log('Regular invites created:', regularInvites.data.nanoids);
// Manager invites (typed data flow)
const managerInviteData = await inviteSystem.createManagerInvite(
['admin@company.com', 'finance@company.com'],
'org_admin',
process.env.TEAM_NANODID
);
console.log('Manager invite data created:', managerInviteData.data.invites);
// Sign the typed data
const signature = await inviteSystem.signManagerInvite(
managerInviteData.data.typed_data,
process.env.WALLET_PRIVATE_KEY
);
// Execute the manager invites
const executedInvites = await inviteSystem.executeManagerInvite(
managerInviteData.data.invites,
process.env.WALLET_ADDRESS,
managerInviteData.data.typed_data.typed_data,
signature
);
console.log('Manager invites executed:', executedInvites.data.transaction);
// Bulk invites
const bulkInvites = [
{
email: 'user1@company.com',
prefill: {
first_name: 'Jane',
last_name: 'Smith',
phone: '+1234567891',
company_name: 'Acme Corp',
job_title: 'Designer',
company_size: '10-50',
company_website: 'https://acme.com',
company_industry: 'Technology',
company_address: '123 Main St',
company_city: 'San Francisco',
company_state: 'CA',
company_zip: '94105',
company_country: 'USA'
},
role: 'team_employee'
},
{
email: 'user2@company.com',
prefill: {
first_name: 'Bob',
last_name: 'Johnson',
phone: '+1234567892',
company_name: 'Acme Corp',
job_title: 'Analyst',
company_size: '10-50',
company_website: 'https://acme.com',
company_industry: 'Technology',
company_address: '123 Main St',
company_city: 'San Francisco',
company_state: 'CA',
company_zip: '94105',
company_country: 'USA'
},
role: 'team_viewer'
}
];
const bulkResult = await inviteSystem.sendBulkInvites(process.env.TEAM_NANODID, bulkInvites);
console.log('Bulk invites completed:', bulkResult);
// Get all invites
const allInvites = await inviteSystem.getInvites(process.env.TEAM_NANODID);
console.log('All invites:', allInvites.data);
} catch (error) {
console.error('Invite system error:', error);
}
}
// Run the example
if (require.main === module) {
main();
}