This commit is contained in:
odzugkoev
2026-03-01 17:10:03 -05:00
parent 7e0887c62d
commit 85cf732a61
19 changed files with 2284 additions and 32 deletions

View File

@@ -3,6 +3,7 @@ const fetch = require('node-fetch');
class APIClient {
constructor(baseUrl = process.env.API_URL || 'https://api.thinklink.ai') {
this.baseUrl = baseUrl;
this._refreshInFlight = null;
}
async request(endpoint, options = {}) {
@@ -82,12 +83,117 @@ class APIClient {
return this.request('/food-order/modify', { body });
}
async refreshToken(token) {
return this.request('/user/refresh-token', {
body: { token }
});
}
async getAbandonedCalls(token, botId, options = {}) {
const body = {
token,
botId: parseInt(botId, 10) || 0,
limit: options.limit || 50,
offset: options.offset || 0
};
if (options.stage) body.stage = options.stage;
if (options.priority) body.priority = options.priority;
if (options.startDate) body.startDate = options.startDate;
if (options.endDate) body.endDate = options.endDate;
return this.request('/abandoned-calls/list', { body });
}
async getAbandonedCallbackQueue(token, botId, limit = 20, offset = 0) {
return this.request('/abandoned-calls/callback-queue', {
body: { token, botId: parseInt(botId, 10) || 0, limit, offset }
});
}
async updateAbandonedCallback(token, abandonedCallId, action, notes = '') {
return this.request('/abandoned-calls/update-callback', {
body: { token, abandonedCallId: parseInt(abandonedCallId, 10), action, notes }
});
}
async getAbandonedCallMetrics(token, botId, startDate = 0, endDate = 0) {
return this.request('/abandoned-calls/metrics', {
body: { token, botId: parseInt(botId, 10) || 0, startDate, endDate }
});
}
isTokenExpired(expirationDate) {
if (!expirationDate) return true;
const expiry = new Date(expirationDate);
if (Number.isNaN(expiry.getTime())) return true;
const now = new Date();
return now >= expiry;
}
isTokenNearExpiry(expirationDate, thresholdDays = 7) {
if (!expirationDate) return true;
const expiry = new Date(expirationDate);
if (Number.isNaN(expiry.getTime())) return true;
const now = new Date();
const msRemaining = expiry.getTime() - now.getTime();
const thresholdMs = thresholdDays * 24 * 60 * 60 * 1000;
return msRemaining < thresholdMs;
}
async ensureValidToken() {
const config = require('./config');
const token = config.get('authToken');
const expiry = config.get('tokenExpiry');
if (!token || !expiry) return null;
if (!this.isTokenNearExpiry(expiry, 7)) return token;
if (this._refreshInFlight) {
return this._refreshInFlight;
}
this._refreshInFlight = (async () => {
// Re-read in case another caller refreshed between checks.
const currentToken = config.get('authToken');
const currentExpiry = config.get('tokenExpiry');
if (!currentToken || !currentExpiry) return null;
if (!this.isTokenNearExpiry(currentExpiry, 7)) return currentToken;
console.log('Token near expiry or expired, attempting refresh...');
try {
const result = await this.refreshToken(currentToken);
if (result.error) {
// If another concurrent refresh already updated config, don't treat this as fatal.
const latestToken = config.get('authToken');
const latestExpiry = config.get('tokenExpiry');
if (latestToken && latestToken !== currentToken && !this.isTokenExpired(latestExpiry)) {
return latestToken;
}
console.error('Token refresh failed:', result.message);
return null;
}
config.set('previousAuthToken', currentToken);
config.set('authToken', result.token);
config.set('tokenExpiry', result.expirationDate);
console.log('Token refreshed successfully, new expiry:', result.expirationDate);
return result.token;
} catch (error) {
console.error('Token refresh error:', error.message);
return null;
}
})();
try {
return await this._refreshInFlight;
} finally {
this._refreshInFlight = null;
}
}
}
module.exports = new APIClient();