72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
// API Endpoint: POST /api/god/campaigns/launch/[id]
|
|
import type { APIRoute } from 'astro';
|
|
import { pool } from '../../../../../lib/db/db';
|
|
import { batchQueue } from '../../../../../lib/queue/config';
|
|
|
|
export const POST: APIRoute = async ({ params, request }) => {
|
|
try {
|
|
const godToken = request.headers.get('X-God-Token');
|
|
if (!godToken || godToken !== import.meta.env.GOD_TOKEN) {
|
|
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
|
status: 401,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
const { id } = params;
|
|
if (!id) {
|
|
return new Response(JSON.stringify({ error: 'Campaign ID required' }), {
|
|
status: 400,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
// Fetch campaign
|
|
const result = await pool.query(
|
|
`SELECT id, name, blueprint_json FROM campaign_masters WHERE id = $1`,
|
|
[id]
|
|
);
|
|
|
|
if (result.rows.length === 0) {
|
|
return new Response(JSON.stringify({ error: 'Campaign not found' }), {
|
|
status: 404,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
const campaign = result.rows[0];
|
|
|
|
// Queue the job
|
|
await batchQueue.add('generate_campaign_content', {
|
|
campaignId: id,
|
|
campaignName: campaign.name
|
|
});
|
|
|
|
// Update status
|
|
await pool.query(
|
|
`UPDATE campaign_masters SET status = 'processing', updated_at = NOW() WHERE id = $1`,
|
|
[id]
|
|
);
|
|
|
|
return new Response(JSON.stringify({
|
|
success: true,
|
|
campaignId: id,
|
|
message: 'Campaign queued for generation',
|
|
status: 'processing'
|
|
}), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
|
|
} catch (error: any) {
|
|
console.error('Campaign launch error:', error);
|
|
return new Response(JSON.stringify({
|
|
error: 'Campaign launch failed',
|
|
details: error.message
|
|
}), {
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
};
|