fixes
This commit is contained in:
parent
04160a2fd2
commit
6ebd2527cd
@ -52,8 +52,8 @@ const createApiClient = () => {
|
|||||||
console.log('[wrenClient] Creating API client with key:', config.wrenApiKey ? '********' + config.wrenApiKey.slice(-4) : 'MISSING');
|
console.log('[wrenClient] Creating API client with key:', config.wrenApiKey ? '********' + config.wrenApiKey.slice(-4) : 'MISSING');
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
// Updated base URL to match the sample code
|
// Base URL to Wren API
|
||||||
baseURL: 'https://www.wren.co/api',
|
baseURL: 'https://www.wren.co',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${config.wrenApiKey}`,
|
'Authorization': `Bearer ${config.wrenApiKey}`,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@ -130,7 +130,8 @@ export async function getPortfolios(): Promise<Portfolio[]> {
|
|||||||
console.log('[wrenClient] Getting portfolios with token:', config.wrenApiKey ? '********' + config.wrenApiKey.slice(-4) : 'MISSING');
|
console.log('[wrenClient] Getting portfolios with token:', config.wrenApiKey ? '********' + config.wrenApiKey.slice(-4) : 'MISSING');
|
||||||
|
|
||||||
const api = createApiClient();
|
const api = createApiClient();
|
||||||
const response = await api.get('/portfolios');
|
// Updated endpoint to match documentation
|
||||||
|
const response = await api.get('/api/portfolios');
|
||||||
|
|
||||||
if (!response.data?.portfolios?.length) {
|
if (!response.data?.portfolios?.length) {
|
||||||
console.warn('[wrenClient] No portfolios returned from API, using fallback');
|
console.warn('[wrenClient] No portfolios returned from API, using fallback');
|
||||||
@ -177,41 +178,70 @@ export async function createOffsetOrder(
|
|||||||
console.log(`[wrenClient] Creating offset order: portfolio=${portfolioId}, tons=${tons}`);
|
console.log(`[wrenClient] Creating offset order: portfolio=${portfolioId}, tons=${tons}`);
|
||||||
|
|
||||||
const api = createApiClient();
|
const api = createApiClient();
|
||||||
// Updated to use the correct endpoint and parameter names
|
// Updated to use the correct endpoint and match exact API format from documentation
|
||||||
const response = await api.post('/offset-orders', {
|
const response = await api.post('/api/offset-orders', {
|
||||||
|
// Using exactly the format shown in API docs
|
||||||
portfolioId,
|
portfolioId,
|
||||||
tons,
|
tons,
|
||||||
dryRun,
|
dryRun
|
||||||
currency: 'USD'
|
// Currency is not shown as a parameter in the API docs
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add detailed response logging
|
||||||
|
console.log('[wrenClient] Offset order response:',
|
||||||
|
response.status,
|
||||||
|
response.data ? 'has data' : 'no data');
|
||||||
|
|
||||||
|
if (response.status === 400) {
|
||||||
|
console.error('[wrenClient] Bad request details:', response.data);
|
||||||
|
throw new Error(`Failed to create offset order: ${JSON.stringify(response.data)}`);
|
||||||
|
}
|
||||||
|
|
||||||
const order = response.data;
|
const order = response.data;
|
||||||
|
if (!order) {
|
||||||
|
throw new Error('Empty response received from offset order API');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log to help diagnose issues
|
||||||
|
console.log('[wrenClient] Order data keys:', Object.keys(order).join(', '));
|
||||||
|
if (order.portfolio) {
|
||||||
|
console.log('[wrenClient] Portfolio data keys:', Object.keys(order.portfolio).join(', '));
|
||||||
|
}
|
||||||
|
|
||||||
// Updated to handle costPerTon vs pricePerTon
|
// Updated to handle costPerTon vs pricePerTon
|
||||||
let pricePerTon = 200;
|
let pricePerTon = 200;
|
||||||
if (order.portfolio?.pricePerTon !== undefined && order.portfolio.pricePerTon !== null) {
|
if (order.portfolio?.pricePerTon !== undefined) {
|
||||||
pricePerTon = typeof order.portfolio.pricePerTon === 'number' ? order.portfolio.pricePerTon : parseFloat(order.portfolio.pricePerTon) || 200;
|
pricePerTon = typeof order.portfolio.pricePerTon === 'number' ? order.portfolio.pricePerTon : parseFloat(order.portfolio.pricePerTon) || 200;
|
||||||
} else if (order.portfolio?.costPerTon !== undefined && order.portfolio.costPerTon !== null) {
|
} else if (order.portfolio?.costPerTon !== undefined) {
|
||||||
pricePerTon = typeof order.portfolio.costPerTon === 'number' ? order.portfolio.costPerTon : parseFloat(order.portfolio.costPerTon) || 200;
|
pricePerTon = typeof order.portfolio.costPerTon === 'number' ? order.portfolio.costPerTon : parseFloat(order.portfolio.costPerTon) || 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjusted to use camelCase as per API response
|
// Create a safe method to extract properties with fallbacks
|
||||||
|
const getSafeProp = (obj: any, prop: string, fallback: any) => {
|
||||||
|
if (!obj) return fallback;
|
||||||
|
return obj[prop] !== undefined ? obj[prop] : fallback;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Use safe accessor to avoid undefined errors
|
||||||
|
const portfolio = order.portfolio || {};
|
||||||
|
|
||||||
|
// Adjusted to use camelCase as per API docs response format
|
||||||
return {
|
return {
|
||||||
id: order.id,
|
id: getSafeProp(order, 'id', ''),
|
||||||
amountCharged: order.amountCharged,
|
amountCharged: getSafeProp(order, 'amountCharged', 0),
|
||||||
currency: order.currency || 'USD',
|
currency: getSafeProp(order, 'currency', 'USD'),
|
||||||
tons: order.tons,
|
tons: getSafeProp(order, 'tons', 0),
|
||||||
portfolio: {
|
portfolio: {
|
||||||
id: order.portfolio.id,
|
id: getSafeProp(portfolio, 'id', 1),
|
||||||
name: order.portfolio.name,
|
name: getSafeProp(portfolio, 'name', 'Default Portfolio'),
|
||||||
description: order.portfolio.description || '',
|
description: getSafeProp(portfolio, 'description', ''),
|
||||||
projects: order.portfolio.projects || [],
|
projects: getSafeProp(portfolio, 'projects', []),
|
||||||
pricePerTon,
|
pricePerTon,
|
||||||
currency: order.currency || 'USD'
|
currency: getSafeProp(order, 'currency', 'USD')
|
||||||
},
|
},
|
||||||
status: order.status,
|
status: getSafeProp(order, 'status', ''),
|
||||||
createdAt: order.createdAt || order.created_at,
|
createdAt: getSafeProp(order, 'createdAt', new Date().toISOString()),
|
||||||
dryRun: order.dryRun
|
dryRun: getSafeProp(order, 'dryRun', false)
|
||||||
};
|
};
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
logError(error);
|
logError(error);
|
||||||
@ -221,15 +251,24 @@ export async function createOffsetOrder(
|
|||||||
|
|
||||||
console.error('[wrenClient] Axios error details:', {
|
console.error('[wrenClient] Axios error details:', {
|
||||||
status: axiosError.response?.status,
|
status: axiosError.response?.status,
|
||||||
|
statusText: axiosError.response?.statusText,
|
||||||
data: axiosError.response?.data,
|
data: axiosError.response?.data,
|
||||||
config: {
|
config: {
|
||||||
url: axiosError.config?.url,
|
url: axiosError.config?.url,
|
||||||
method: axiosError.config?.method,
|
method: axiosError.config?.method,
|
||||||
headers: axiosError.config?.headers ? 'Headers present' : 'No headers',
|
headers: axiosError.config?.headers ? 'Headers present' : 'No headers',
|
||||||
baseURL: axiosError.config?.baseURL
|
baseURL: axiosError.config?.baseURL,
|
||||||
|
data: axiosError.config?.data
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (axiosError.response?.status === 400) {
|
||||||
|
// Provide more specific error for 400 Bad Request
|
||||||
|
const responseData = axiosError.response.data as any;
|
||||||
|
const errorMessage = responseData?.message || responseData?.error || 'Invalid request format';
|
||||||
|
throw new Error(`Bad request: ${errorMessage}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (axiosError.code === 'ECONNABORTED') {
|
if (axiosError.code === 'ECONNABORTED') {
|
||||||
throw new Error('Request timed out. Please try again.');
|
throw new Error('Request timed out. Please try again.');
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user