189 lines
6.2 KiB
JavaScript
189 lines
6.2 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Partner Integration Setup Script
|
|
*
|
|
* This script demonstrates how to set up and use the partner integration system.
|
|
* Run this after setting up your environment variables.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const { DBConnection } = require('./helpers/db/connect');
|
|
const { Partner, PartnerSystemUser } = require('./model/partner');
|
|
const { UserTypes } = require('./helpers/constants');
|
|
const User = require('./model/user');
|
|
|
|
async function setupPartnerIntegration() {
|
|
console.log('🚀 Setting up Partner Integration System...\n');
|
|
|
|
try {
|
|
// Connect to database
|
|
const db = new DBConnection('Partner Setup');
|
|
await db.connect();
|
|
|
|
// 1. Create SatLoc Partner Organization
|
|
console.log('1. Creating SatLoc Partner Organization...');
|
|
|
|
let satlocPartner = await Partner.findOne({ partnerCode: 'SATLOC' });
|
|
|
|
if (!satlocPartner) {
|
|
satlocPartner = new Partner({
|
|
kind: UserTypes.PARTNER,
|
|
name: 'SatLoc Cloud',
|
|
username: 'satloc_partner',
|
|
partnerCode: 'SATLOC',
|
|
active: true
|
|
});
|
|
|
|
await satlocPartner.save();
|
|
console.log('✅ SatLoc Partner created:', satlocPartner._id);
|
|
} else {
|
|
console.log('✅ SatLoc Partner already exists:', satlocPartner._id);
|
|
}
|
|
|
|
// 2. Create a sample customer
|
|
console.log('\n2. Creating sample customer...');
|
|
|
|
let sampleCustomer = await User.findOne({ username: 'sample_customer' });
|
|
|
|
if (!sampleCustomer) {
|
|
sampleCustomer = new User({
|
|
kind: UserTypes.CLIENT,
|
|
name: 'Sample Customer',
|
|
username: 'sample_customer',
|
|
email: 'customer@example.com',
|
|
active: true
|
|
});
|
|
|
|
await sampleCustomer.save();
|
|
console.log('✅ Sample customer created:', sampleCustomer._id);
|
|
} else {
|
|
console.log('✅ Sample customer already exists:', sampleCustomer._id);
|
|
}
|
|
|
|
// 3. Create Partner System User (Customer's SatLoc Account)
|
|
console.log('\n3. Creating Partner System User...');
|
|
|
|
let partnerSystemUser = await PartnerSystemUser.findOne({
|
|
customerId: sampleCustomer._id,
|
|
partner: satlocPartner._id
|
|
});
|
|
|
|
if (!partnerSystemUser) {
|
|
partnerSystemUser = new PartnerSystemUser({
|
|
kind: UserTypes.PARTNER_SYSTEM_USER,
|
|
name: 'Sample Customer SatLoc Account',
|
|
username: `satloc_${sampleCustomer._id}`,
|
|
partner: satlocPartner._id,
|
|
customer: sampleCustomer._id,
|
|
partnerUserId: 'sample_satloc_user_id',
|
|
partnerUsername: 'sample_customer_username',
|
|
companyId: 'sample_company_123',
|
|
// Note: In production, use environment variables for credentials
|
|
apiKey: process.env.SATLOC_CUSTOMER_API_KEY || 'sample_api_key',
|
|
apiSecret: process.env.SATLOC_CUSTOMER_API_SECRET || 'sample_api_secret',
|
|
active: true,
|
|
metadata: {
|
|
setupDate: new Date(),
|
|
source: 'setup_script'
|
|
}
|
|
});
|
|
|
|
await partnerSystemUser.save();
|
|
console.log('✅ Partner System User created:', partnerSystemUser._id);
|
|
} else {
|
|
console.log('✅ Partner System User already exists:', partnerSystemUser._id);
|
|
}
|
|
|
|
// 4. Test Partner Service Availability
|
|
console.log('\n4. Testing Partner Service Availability...');
|
|
|
|
try {
|
|
const partnerSyncService = require('./services/partner_sync_service');
|
|
const availablePartners = partnerSyncService.getAvailablePartners();
|
|
|
|
console.log('✅ Available Partner Services:', availablePartners);
|
|
|
|
if (availablePartners.includes('SATLOC')) {
|
|
console.log('✅ SatLoc service is configured and available');
|
|
} else {
|
|
console.log('⚠️ SatLoc service not available - check configuration');
|
|
console.log(' Note: SatLoc now uses customer-specific credentials in PartnerSystemUser records');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.log('⚠️ Partner services not available:', error.message);
|
|
}
|
|
|
|
// 5. Display Configuration Summary
|
|
console.log('\n📋 Configuration Summary:');
|
|
console.log('─'.repeat(50));
|
|
console.log(`Partner ID: ${satlocPartner._id}`);
|
|
console.log(`Customer ID: ${sampleCustomer._id}`);
|
|
console.log(`Partner System User ID: ${partnerSystemUser._id}`);
|
|
console.log('');
|
|
console.log('🎯 Next Steps:');
|
|
console.log('1. Update PartnerSystemUser records with customer-specific SatLoc credentials');
|
|
console.log('2. Set environment variables in .env file:');
|
|
console.log(' SATLOC_API_ENDPOINT=https://www.satloccloud.com/api/Satloc');
|
|
console.log(' SATLOC_API_TIMEOUT=30000');
|
|
console.log('');
|
|
console.log('3. Create job assignments using Partner System User ID:');
|
|
console.log(` POST /api/jobs/assign with user: "${partnerSystemUser._id}"`);
|
|
console.log('');
|
|
console.log('4. Monitor system health:');
|
|
console.log(' GET /api/health');
|
|
console.log(' GET /api/health/partner-stats');
|
|
|
|
console.log('\n✨ Partner Integration Setup Complete!');
|
|
|
|
} catch (error) {
|
|
console.error('❌ Setup failed:', error);
|
|
} finally {
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
// Show usage instructions
|
|
function showUsage() {
|
|
console.log(`
|
|
🔧 Partner Integration Setup
|
|
|
|
Usage:
|
|
node setup_partners.js
|
|
|
|
Notes:
|
|
- SatLoc authentication now uses customer-specific credentials
|
|
- Credentials are stored in PartnerSystemUser records, not environment variables
|
|
- Global SatLoc credentials (SATLOC_EMAIL, SATLOC_PASSWORD) are deprecated
|
|
|
|
Environment Variables (Optional):
|
|
SATLOC_API_ENDPOINT - SatLoc API endpoint (default: https://www.satloccloud.com/api/Satloc)
|
|
SATLOC_API_TIMEOUT - API timeout in ms (default: 30000)
|
|
SATLOC_RETRY_ATTEMPTS - Retry attempts (default: 3)
|
|
PARTNER_SYNC_INTERVAL - Sync interval in ms (default: 300000)
|
|
|
|
Example .env file:
|
|
SATLOC_API_ENDPOINT=https://www.satloccloud.com/api/Satloc
|
|
SATLOC_API_TIMEOUT=30000
|
|
PARTNER_SYNC_INTERVAL=300000
|
|
|
|
After setup, restart your application to load the partner services.
|
|
`);
|
|
}
|
|
|
|
// Main execution
|
|
if (require.main === module) {
|
|
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
showUsage();
|
|
} else {
|
|
setupPartnerIntegration();
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
setupPartnerIntegration,
|
|
showUsage
|
|
};
|