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);
|
||||
|
||||
@@ -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 rejected
|
||||
const result = db.prepare(`
|
||||
UPDATE purchases
|
||||
SET approvalStatus = 'rejected'
|
||||
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, 'rejected');
|
||||
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 rejected successfully'
|
||||
message: 'Purchase rejected successfully',
|
||||
purchase,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Error rejecting purchase:', error);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { purchaseDb } from '@/lib/db';
|
||||
import { Purchase } from '@/lib/types';
|
||||
import { notifyPurchaseApprovers } from '@/lib/telegram';
|
||||
|
||||
// GET - Get all purchases
|
||||
export async function GET(request: NextRequest) {
|
||||
@@ -54,6 +55,10 @@ export async function POST(request: NextRequest) {
|
||||
};
|
||||
|
||||
const created = purchaseDb.create(purchase);
|
||||
// Telegram delivery must not make a successfully recorded purchase fail.
|
||||
void notifyPurchaseApprovers(created).catch((error) => {
|
||||
console.error('Failed to notify Telegram approvers:', error);
|
||||
});
|
||||
return NextResponse.json(created, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating purchase:', error);
|
||||
|
||||
Reference in New Issue
Block a user