Copy const axios = require('axios');
const config = {
orgId: process.env.LEXAMICA_ORG_ID,
publicKey: process.env.LEXAMICA_PUBLIC_KEY,
privateKey: process.env.LEXAMICA_PRIVATE_KEY,
mappingId: process.env.LEXAMICA_CASE_MAPPING_ID,
baseUrl: 'https://integration.lexamica.com'
};
// ============================================
// IMPORT CASES
// ============================================
async function importExistingCase(caseData, partnerFirmId) {
const response = await axios.post(
`${config.baseUrl}/organization/${config.orgId}/inbound-webhooks/case/${config.mappingId}/create-existing`,
{ ...caseData, partnerFirmId },
{
headers: { 'Content-Type': 'application/json' },
params: { Key: config.publicKey }
}
);
return response.data;
}
// ============================================
// POLL FOR UPDATES
// ============================================
const EVENT_TYPES = [
'Case Update Made',
'Case Stage Changed',
'Case Settlement'
];
class ExistingCasePoller {
constructor() {
this.lastPollTime = null;
}
async poll() {
const now = new Date();
const from = this.lastPollTime || new Date(now.getTime() - 24 * 60 * 60 * 1000);
console.log(`Polling from ${from.toISOString()}`);
try {
for (const eventType of EVENT_TYPES) {
const events = await this.fetchEvents(eventType, from, now);
if (events.length > 0) {
console.log(`Found ${events.length} ${eventType} event(s)`);
await this.processEvents(eventType, events);
}
}
this.lastPollTime = now;
} catch (error) {
console.error('Poll failed:', error.message);
}
}
async fetchEvents(eventType, from, to) {
const params = new URLSearchParams({
event: eventType,
from: from.toISOString(),
to: to.toISOString(),
limit: '100'
});
const response = await axios.get(
`${config.baseUrl}/organization/${config.orgId}/stored-events?${params}`,
{ headers: { Authorization: `Bearer ${config.privateKey}` } }
);
return response.data.events || [];
}
async processEvents(eventType, events) {
for (const event of events) {
await this.handleEvent(eventType, event.payload);
}
}
async handleEvent(eventType, payload) {
switch (eventType) {
case 'Case Update Made':
console.log(`Update on ${payload.case_id}: ${payload.title}`);
await this.recordUpdate(payload);
break;
case 'Case Stage Changed':
console.log(`Stage changed for ${payload.lexamica_case_id}`);
await this.updateStage(payload);
break;
case 'Case Settlement':
console.log(`Settlement on ${payload.lexamica_case_id}`);
await this.recordSettlement(payload);
break;
}
}
// Implement these for your system
async recordUpdate(payload) {
// Save status update to your database
}
async updateStage(payload) {
// Update case stage in your system
}
async recordSettlement(payload) {
// Record settlement info
}
}
// ============================================
// RUN
// ============================================
const poller = new ExistingCasePoller();
const POLL_INTERVAL = 15 * 60 * 1000; // 15 minutes
async function run() {
console.log('Polling for updates...');
await poller.poll();
console.log('Done. Next poll in 15 minutes.');
}
run();
setInterval(run, POLL_INTERVAL);