Create verification-service/index.js
Browse files
verification-service/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Minimal verification: subscribe to Redis events, perform a basic check (stub for ML/human)
|
| 2 |
+
const Redis = require('ioredis');
|
| 3 |
+
const fetch = require('node-fetch');
|
| 4 |
+
const { Pool } = require('pg');
|
| 5 |
+
|
| 6 |
+
const redis = new Redis(process.env.REDIS_URL || 'redis://localhost:6379');
|
| 7 |
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL || 'postgresql://localhost/integral' });
|
| 8 |
+
|
| 9 |
+
redis.subscribe('infra:faults:created', (err, count) => {
|
| 10 |
+
if (err) return console.error(err);
|
| 11 |
+
console.log('subscribed to faults created');
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
redis.on('message', async (channel, message) => {
|
| 15 |
+
if (channel !== 'infra:faults:created') return;
|
| 16 |
+
try {
|
| 17 |
+
const ev = JSON.parse(message);
|
| 18 |
+
console.log('verifying', ev.id);
|
| 19 |
+
|
| 20 |
+
// === SIMPLE AUTO-VERIFICATION STUB ===
|
| 21 |
+
// Real system: call ML model, analyze images, cross-check sensors, or queue human verification.
|
| 22 |
+
const fakeConfidence = Math.random(); // stub
|
| 23 |
+
const confirmed = fakeConfidence > 0.45; // threshold
|
| 24 |
+
|
| 25 |
+
const q = `UPDATE objects SET confirmed = $1 WHERE id = $2 RETURNING *`;
|
| 26 |
+
const r = await pool.query(q, [confirmed, ev.id]);
|
| 27 |
+
const obj = r.rows[0];
|
| 28 |
+
|
| 29 |
+
// Publish confirmation event
|
| 30 |
+
await redis.publish('infra:faults:confirmed', JSON.stringify({ id: obj.id, confirmed }));
|
| 31 |
+
|
| 32 |
+
// If confirmed, optionally auto-create a payout job (policy-driven)
|
| 33 |
+
if (confirmed) {
|
| 34 |
+
// simple rule: severity * baseAmount
|
| 35 |
+
const baseAmount = 5000; // 50.00 ZAR in minor units? (example uses cents)
|
| 36 |
+
const total = baseAmount * (obj.severity || 1);
|
| 37 |
+
const insert = `INSERT INTO payouts (fault_id, amount_minor_units, currency, payee_id, status)
|
| 38 |
+
VALUES ($1,$2,$3,$4,'created') RETURNING *`;
|
| 39 |
+
const payeeId = 'local-contractor-123'; // real system: choose via procurement/TMS
|
| 40 |
+
const pay = await pool.query(insert, [obj.id, total, 'ZAR', payeeId]);
|
| 41 |
+
await redis.publish('payouts:created', JSON.stringify({ payoutId: pay.rows[0].id }));
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
} catch (err) {
|
| 45 |
+
console.error('verify error', err);
|
| 46 |
+
}
|
| 47 |
+
});
|