feat: add Telegram album purchase bot

This commit is contained in:
2026-08-21 18:07:50 +03:30
parent 9478aa319f
commit 1e7b752532
21 changed files with 1399 additions and 459 deletions
+20 -12
View File
@@ -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);