This commit is contained in:
Omar Najjar 2025-04-13 03:58:58 +10:00
parent 1fa9b3ddc3
commit 3a3e6b4363

View file

@ -1014,10 +1014,24 @@ async function getProposals(request, env) {
);
const offset = (page - 1) * limit;
// Add to your SQL query
// Define the base query
let query = `
SELECT
p.id, p.text, p.timestamp, p.trending, p.meme_url,
u.name as author_name, u.id as author_id,
(SELECT COUNT(*) FROM votes WHERE proposal_id = p.id AND vote_type = 'upvote') as upvotes,
(SELECT COUNT(*) FROM votes WHERE proposal_id = p.id AND vote_type = 'downvote') as downvotes,
(SELECT COUNT(*) FROM votes WHERE proposal_id = p.id AND vote_type = 'upvote' AND is_petition = 1) as petition_signatures,
(SELECT COUNT(DISTINCT pd.user_id) FROM petition_details pd WHERE pd.proposal_id = p.id AND pd.verified = 1) as verified_petitioners
FROM proposals p
JOIN users u ON p.author_id = u.id
ORDER BY p.timestamp DESC
`;
// Add pagination
query += ` LIMIT ? OFFSET ?`;
// Add to your bind parameters
// Execute the query
const proposals = await env.DB.prepare(query).bind(limit, offset).all();
// Add pagination info to response
@ -1031,7 +1045,10 @@ async function getProposals(request, env) {
}
});
} catch (error) {
// ... error handling ...
return createResponse({
error: 'Failed to retrieve proposals',
details: logError(error, { action: 'get_proposals' })
}, 500);
}
}
@ -1198,28 +1215,16 @@ async function createProposal(request, env) {
const data = await request.json();
const { text, authorId, trending = false } = data;
console.log(`Creating proposal with data:`, {
text: text?.substring(0, 50) + '...',
authorId,
trending
});
console.log(`Creating proposal: ${JSON.stringify(data)}`);
if (!text || !authorId) {
console.error('Missing required fields:', { text: !!text, authorId: !!authorId });
return createResponse({
error: 'Missing required fields',
received: {
text: text ? '[present]' : '[missing]',
authorId: authorId ? '[present]' : '[missing]'
}
}, 400);
return createResponse({ error: 'Missing required fields', received: { text, authorId } }, 400);
}
// Check if user exists first
try {
const userCheck = await env.DB.prepare(`SELECT id, name FROM users WHERE id = ?`).bind(authorId).first();
if (!userCheck) {
console.error('Author does not exist:', authorId);
return createResponse({
error: 'Author does not exist',
authorId,
@ -1227,15 +1232,15 @@ async function createProposal(request, env) {
}, 400);
}
const id = 'proposal_' + Date.now();
const timestamp = Date.now();
// Create a temporary proposal object with author name for image generation
const tempProposal = {
id: 'proposal_' + Date.now(),
text: text,
author_name: userCheck.name
};
console.log('Inserting proposal into database:', {
id,
authorId,
timestamp,
trending
});
const id = tempProposal.id;
const timestamp = Date.now();
// Insert the proposal first
const query = `
@ -1245,31 +1250,16 @@ async function createProposal(request, env) {
await env.DB.prepare(query).bind(id, authorId, text, timestamp, trending ? 1 : 0).run();
console.log('Proposal inserted successfully');
// Generate and store the share image
const shareImageUrl = await generateAndStoreShareImage(tempProposal, env);
// Try to generate share image, but don't fail if it doesn't work
let shareImageUrl = null;
try {
// Create a temporary proposal object with author name for image generation
const tempProposal = {
id: id,
text: text,
author_name: userCheck.name
};
shareImageUrl = await generateAndStoreShareImage(tempProposal, env);
if (shareImageUrl) {
console.log('Updating proposal with share image URL:', shareImageUrl);
await env.DB.prepare(`
UPDATE proposals
SET share_image_url = ?
WHERE id = ?
`).bind(shareImageUrl, id).run();
}
} catch (imageError) {
console.error('Failed to generate share image, continuing without it:', imageError);
// Continue without the share image - it's not critical
// Update the proposal with the share image URL
if (shareImageUrl) {
await env.DB.prepare(`
UPDATE proposals
SET share_image_url = ?
WHERE id = ?
`).bind(shareImageUrl, id).run();
}
console.log(`Successfully created proposal with ID: ${id}`);
@ -1283,7 +1273,6 @@ async function createProposal(request, env) {
shareImageUrl
});
} catch (dbError) {
console.error('Database error during proposal creation:', dbError);
return createResponse({
error: 'Failed to create proposal in database',
details: logError(dbError, {
@ -1293,7 +1282,6 @@ async function createProposal(request, env) {
}, 500);
}
} catch (error) {
console.error('Error in createProposal:', error);
return createResponse({
error: 'Failed to process proposal creation',
details: logError(error, { action: 'create_proposal_outer' })