lecoffre-back-mini/quick-test-rattachements.js
Sadrinho27 18936f18e7
Some checks failed
Build and Push to Registry / build-and-push (push) Failing after 40s
Remove hardcoded values & updated env file
2025-09-12 11:55:54 +02:00

160 lines
5.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fetch = require('node-fetch');
const { SDKSignerClient } = require('sdk-signer-client');
// Quick test configuration
const BASE_URL = process.env.API_BASE_URL;
const ENDPOINT = '/api/v1/idnot/user/rattachements'; // Base endpoint, idnot will be added as path parameter
const signerConfig = {
url: process.env.SIGNER_WS_URL || 'ws://localhost:9090',
apiKey: process.env.SIGNER_API_KEY || 'your-api-key-change-this',
timeout: 30000,
reconnectInterval: 5000,
maxReconnectAttempts: 3
};
// Test with a specific IDNot
async function testWithIdNot(idNot) {
if (!idNot) {
console.log('💡 Usage: node quick-test-rattachements.js [idNot]');
console.log(' Example: node quick-test-rattachements.js 12345');
console.log(' Example: node quick-test-rattachements.js (no parameter to test without idNot)');
console.log(' URL format: /api/v1/idnot/user/{idnot}/rattachements');
return;
}
console.log(`🆔 Testing with IDNot: ${idNot}`);
// Build URL with path parameter
let url = `${BASE_URL}${ENDPOINT}`;
url += `?idNot=${encodeURIComponent(idNot)}`;
console.log(`📍 URL: ${url}`);
console.log('=' .repeat(60));
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
console.log(`📊 Status: ${response.status} ${response.statusText}`);
const responseText = await response.text();
console.log(`📄 Response length: ${responseText.length} characters`);
let data;
try {
data = JSON.parse(responseText);
console.log('📋 Parsed JSON response:');
console.log(JSON.stringify(data, null, 2));
} catch (e) {
console.log('📋 Raw response (not JSON):');
console.log(responseText);
return;
}
for (const office of data) {
let officeRattachementsData = [];
// Now test the office rattachements
const officeRattachements = await fetch(`${BASE_URL}/api/v1/idnot/office/rattachements?idNot=${office.ou}`, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
console.log(`📊 Status: ${officeRattachements.status} ${officeRattachements.statusText}`);
const officeRattachementsText = await officeRattachements.text();
console.log(`📄 Response length: ${officeRattachementsText.length} characters`);
try {
officeRattachementsData = JSON.parse(officeRattachementsText);
console.log('📋 Parsed JSON response:');
console.log(JSON.stringify(officeRattachementsData, null, 2));
} catch (e) {
console.log('📋 Raw response (not JSON):');
console.log(officeRattachementsText);
return;
}
// Now try to create a new process with all the users that have `activite` set to `En exercice`
const usersToAdd = officeRattachementsData.result.filter(user => user.activite === 'En exercice');
console.log(`📋 Users to add: ${usersToAdd.length}`);
console.log(JSON.stringify(usersToAdd, null, 2));
// Probably the idnot number should be public so that caller can easily find the processId?
// Caller can now create the office process with the following data
const processData = {
name: 'New Process',
description: 'New Process Description',
timestamp: new Date().toISOString(),
office: office.ou,
};
const privateFields = Object.keys(processData);
privateFields.splice(privateFields.indexOf('office'), 1); // Make office public data
const roles = {
owner: {
members: usersToAdd.map(user => user.uid),
validation_rules: [
{
quorum: 0.1,
fields: [...privateFields, 'roles', 'office'],
min_sig_member: 1,
},
],
storages: ["https://dev3.4nkweb.com/storage"]
},
apophis: {
members: usersToAdd.map(user => user.uid),
validation_rules: [],
storages: []
}
};
}
} catch (error) {
console.log(`💥 Error: ${error.message}`);
}
}
// Main execution
async function main() {
const idNot = process.argv[2]; // Get IDNot from command line argument
console.log('🚀 Quick Rattachements Endpoint Test');
console.log('=' .repeat(60));
// Check if server is running
try {
const healthCheck = await fetch(`${BASE_URL}/api/v1/health`);
if (healthCheck.ok) {
console.log('✅ Server is running');
} else {
console.log(`⚠️ Server responded but health check failed with status: ${healthCheck.status}`);
}
} catch (error) {
console.log('❌ Server is not responding');
console.log('💡 Make sure to start your server first with: npm run dev');
return;
}
console.log('');
await testWithIdNot(idNot);
console.log('\n💡 Usage: node quick-test-rattachements.js [idNot]');
console.log(' Example: node quick-test-rattachements.js 12345');
console.log(' Example: node quick-test-rattachements.js (no parameter to test without idNot)');
console.log(' URL format: /api/v1/idnot/user/{idnot}/rattachements');
}
main().catch(console.error);