Copy const axios = require('axios');
const config = {
orgId: process.env.LEXAMICA_ORG_ID,
privateKey: process.env.LEXAMICA_PRIVATE_KEY,
baseUrl: 'https://integration.lexamica.com'
};
// Events to poll for
const EVENT_TYPES = [
'Case Relay Matched',
'Case Relay Rejected',
'Case Relay Missed',
'Case Invitation Accepted',
'Case Invitation Declined',
'Case Update Made',
'Case Settlement'
];
class OriginatorPoller {
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()} to ${now.toISOString()}`);
try {
// Poll each event type
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;
console.log('Poll complete');
} catch (error) {
console.error('Polling 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 Relay Matched':
await this.handleRelayMatched(payload);
break;
case 'Case Relay Rejected':
await this.handleRelayRejected(payload);
break;
case 'Case Relay Missed':
await this.handleRelayMissed(payload);
break;
case 'Case Invitation Accepted':
await this.handleInvitationAccepted(payload);
break;
case 'Case Invitation Declined':
await this.handleInvitationDeclined(payload);
break;
case 'Case Update Made':
await this.handleUpdateMade(payload);
break;
case 'Case Settlement':
await this.handleSettlement(payload);
break;
}
}
// Event handlers
async handleRelayMatched(payload) {
console.log(`Matched: ${payload.lexamica_case_id}`);
await this.updateCase(payload.lexamica_case_id, { status: 'matched' });
}
async handleRelayRejected(payload) {
console.log(`Rejected: ${payload.lexamica_case_id}`);
await this.updateCase(payload.lexamica_case_id, { status: 'no_match' });
await this.alertStaff(`No partners for case ${payload.lexamica_case_id}`);
}
async handleRelayMissed(payload) {
console.log(`Missed: ${payload.lexamica_case_id}`);
await this.updateCase(payload.lexamica_case_id, { status: 'all_declined' });
}
async handleInvitationAccepted(payload) {
console.log(`Accepted: ${payload.case_id} by ${payload.handler_firm_id}`);
await this.updateCase(payload.case_id, {
status: 'accepted',
handlerFirmId: payload.handler_firm_id,
acceptedAt: payload.accepted_at
});
}
async handleInvitationDeclined(payload) {
console.log(`Declined: ${payload.case_id} - ${payload.decline_reason}`);
await this.addActivity(payload.case_id, {
type: 'declined',
reason: payload.decline_reason
});
}
async handleUpdateMade(payload) {
console.log(`Update: ${payload.case_id}`);
await this.addActivity(payload.case_id, {
type: 'status_update',
title: payload.title,
content: payload.content
});
}
async handleSettlement(payload) {
console.log(`Settlement: ${payload.lexamica_case_id}`);
await this.updateCase(payload.lexamica_case_id, { status: 'settling' });
}
// Placeholder methods - implement for your system
async updateCase(caseId, data) {
console.log('Update:', caseId, data);
}
async addActivity(caseId, activity) {
console.log('Activity:', caseId, activity);
}
async alertStaff(message) {
console.log('Alert:', message);
}
}
// Run poller
const poller = new OriginatorPoller();
const POLL_INTERVAL = 5 * 60 * 1000; // 5 minutes
async function run() {
await poller.poll();
}
run();
setInterval(run, POLL_INTERVAL);
console.log('Originator poller started');