feat: add Telegram album purchase bot
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDatabase } from '@/lib/db';
|
||||
import { purchaseDb } from '@/lib/db';
|
||||
import { notifyCustomerOfDecision } from '@/lib/telegram';
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
@@ -16,25 +17,32 @@ export async function PATCH(
|
||||
);
|
||||
}
|
||||
|
||||
const db = getDatabase();
|
||||
|
||||
// Update purchase status to approved
|
||||
const result = db.prepare(`
|
||||
UPDATE purchases
|
||||
SET approvalStatus = 'approved'
|
||||
WHERE id = ?
|
||||
`).run(purchaseId);
|
||||
|
||||
if (result.changes === 0) {
|
||||
const existing = purchaseDb.getById(purchaseId);
|
||||
if (!existing) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Purchase not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
if (existing.approvalStatus !== 'pending') {
|
||||
return NextResponse.json(
|
||||
{ error: `Purchase is already ${existing.approvalStatus}` },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
const purchase = purchaseDb.setStatus(purchaseId, 'approved');
|
||||
if (!purchase) {
|
||||
return NextResponse.json({ error: 'Purchase was reviewed concurrently' }, { status: 409 });
|
||||
}
|
||||
void notifyCustomerOfDecision(purchase).catch((error) => {
|
||||
console.error('Failed to notify Telegram customer:', error);
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Purchase approved successfully'
|
||||
message: 'Purchase approved successfully',
|
||||
purchase,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error approving purchase:', error);
|
||||
|
||||
Reference in New Issue
Block a user