import { NextRequest, NextResponse } from 'next/server';
import Vern from 'vern';
const vern = new Vern({
apiKey: process.env['VERN_API_KEY']
});
export async function POST(request: NextRequest) {
try {
const { sheetId } = await request.json();
// Step 1: Fetch data from external sales API
const response = await fetch('https://api.example.com/sales', {
headers: {
'Authorization': `Bearer ${process.env.SALES_API_TOKEN}`,
},
});
const salesData = await response.json();
// Step 2: Chain Vern operations using pipeline
const result = await vern.workbooks.pipeline(sheetId)
.import(salesData.records, {
remappings: [
{ from: 'price', to: 'Amount' },
{ from: 'date', to: 'Transaction Date' },
{ from: 'region', to: 'Sales Region' },
{ from: 'address', to: ['Street', 'City', 'Post Code'], delimiter: ','},
{ from: ['sales_id', 'sku'], to: 'ID', join: '-'},
],
autoremap: true // Use AI to remap the remaining columns
})
.transform([
'Convert international amounts to AUD',
'Update sales region to relevant city'
]) // Use natural language prompts to transform data
.autoclean() // Use AI to resolve remaining formatting issues
.export({
type: 'webhook',
url: 'https://{install}.{geo}.deputy.com/api/v2/metrics',
headers: {
'Authorization': `Bearer ${process.env.DEPUTY_INTERNAL_API_TOKEN}`,
'Content-Type': 'application/json',
},
})
.execute();
console.log('Successfully synced sales data to Deputy');
return NextResponse.json({ success: true, result });
} catch (error) {
console.error('Error in sales data pipeline:', error);
return NextResponse.json({ error: 'Pipeline failed' }, { status: 500 });
}
}