forked from mito-systems/sol-mem-gen
Add method to initialize DB
This commit is contained in:
parent
77a59ac520
commit
da2d490814
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,3 +6,5 @@ node_modules
|
|||||||
.env.development
|
.env.development
|
||||||
.env.production
|
.env.production
|
||||||
.env.test
|
.env.test
|
||||||
|
|
||||||
|
database.sqlite
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server'
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
import { fal } from "@fal-ai/client"
|
import { fal } from "@fal-ai/client"
|
||||||
import { FLUX_MODELS } from '../../../services/fluxService'
|
import { FLUX_MODELS } from '../../../services/fluxService'
|
||||||
|
import { initializeDataSource } from '../../../data-source'
|
||||||
|
|
||||||
import { verifyPayment, isSignatureUsed, markSignatureAsUsed } from '../../../utils/verifyPayment'
|
import { verifyPayment, isSignatureUsed, markSignatureAsUsed } from '../../../utils/verifyPayment'
|
||||||
|
|
||||||
@ -19,6 +20,8 @@ const IMAGE_HEIGHT: number = 1024
|
|||||||
|
|
||||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||||
try {
|
try {
|
||||||
|
await initializeDataSource();
|
||||||
|
|
||||||
const { prompt, modelId, transactionSignature } = await req.json()
|
const { prompt, modelId, transactionSignature } = await req.json()
|
||||||
|
|
||||||
if (!prompt || !modelId) {
|
if (!prompt || !modelId) {
|
||||||
@ -35,13 +38,6 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await isSignatureUsed(transactionSignature)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: 'Transaction signature has already been used' },
|
|
||||||
{ status: 400 }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const model = FLUX_MODELS.find((model) => model.modelId === modelId)
|
const model = FLUX_MODELS.find((model) => model.modelId === modelId)
|
||||||
if (!model) {
|
if (!model) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@ -54,7 +50,10 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
|
|||||||
const isPaymentVerified = await verifyPayment(transactionSignature, expectedAmount)
|
const isPaymentVerified = await verifyPayment(transactionSignature, expectedAmount)
|
||||||
|
|
||||||
if (!isPaymentVerified) {
|
if (!isPaymentVerified) {
|
||||||
throw new Error('Payment verification failed');
|
return NextResponse.json(
|
||||||
|
{ error: 'Payment verification failed or transaction signature has already been used' },
|
||||||
|
{ status: 400 }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
await markSignatureAsUsed(transactionSignature)
|
await markSignatureAsUsed(transactionSignature)
|
||||||
|
@ -11,10 +11,14 @@ export const AppDataSource = new DataSource({
|
|||||||
subscribers: [],
|
subscribers: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
AppDataSource.initialize()
|
export async function initializeDataSource() {
|
||||||
.then(() => {
|
try {
|
||||||
console.log('Data Source has been initialized!');
|
if (!AppDataSource.isInitialized){
|
||||||
})
|
await AppDataSource.initialize();
|
||||||
.catch((err) => {
|
console.log('Data Source has been initialized!');
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
console.error('Error during Data Source initialization:', err);
|
console.error('Error during Data Source initialization:', err);
|
||||||
});
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -19,7 +19,10 @@ const connection = new Connection(
|
|||||||
export async function isSignatureUsed(transactionSignature: string): Promise<boolean> {
|
export async function isSignatureUsed(transactionSignature: string): Promise<boolean> {
|
||||||
const paymentRepository = AppDataSource.getRepository(Payment);
|
const paymentRepository = AppDataSource.getRepository(Payment);
|
||||||
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
||||||
return !!payment;
|
if (payment) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markSignatureAsUsed(transactionSignature: string): Promise<void> {
|
export async function markSignatureAsUsed(transactionSignature: string): Promise<void> {
|
||||||
@ -39,7 +42,7 @@ export async function verifyPayment(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const transaction = await connection.getParsedTransaction(transactionSignature, 'finalized');
|
const transaction = await connection.getParsedTransaction(transactionSignature, 'confirmed');
|
||||||
if (!transaction) {
|
if (!transaction) {
|
||||||
throw new Error('Transaction not found');
|
throw new Error('Transaction not found');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user