changes
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/app/db/drizzle';
|
||||
import { emails } from '@/app/db/schema';
|
||||
import { authenticate, getBody } from '@/app/lib/utils';
|
||||
import { authenticate } from '@/app/lib/utils';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { CopyObjectCommand, GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { getS3Client } from '@/app/lib/utils';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
@@ -26,67 +22,9 @@ export async function GET(req: NextRequest) {
|
||||
html: email.html,
|
||||
raw: email.raw,
|
||||
processed: email.processed ? 'true' : 'false',
|
||||
processedAt: email.processedAt?.toISOString() || null,
|
||||
processedBy: email.processedBy,
|
||||
queuedTo: email.queuedTo,
|
||||
status: email.status,
|
||||
});
|
||||
}
|
||||
|
||||
// PUT: Update processed in S3 and DB
|
||||
export async function PUT(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { bucket, key, processed } = await req.json();
|
||||
if (!bucket || !key) return NextResponse.json({ error: 'Missing params' }, { status: 400 });
|
||||
|
||||
const s3 = getS3Client();
|
||||
const head = await s3.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
|
||||
const newMeta = { ...head.Metadata, [process.env.PROCESSED_META_KEY!]: processed };
|
||||
await s3.send(new CopyObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
CopySource: `${bucket}/${key}`,
|
||||
Metadata: newMeta,
|
||||
MetadataDirective: 'REPLACE'
|
||||
}));
|
||||
|
||||
await db.update(emails).set({ processed: processed === 'true' }).where(eq(emails.s3Key, key));
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
||||
// POST: Resend, update in S3 and DB
|
||||
export async function POST(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { bucket, key } = await req.json();
|
||||
if (!bucket || !key) return NextResponse.json({ error: 'Missing params' }, { status: 400 });
|
||||
|
||||
const s3 = getS3Client();
|
||||
const { Body } = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
|
||||
const raw = await getBody(Body as Readable);
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT),
|
||||
secure: false,
|
||||
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
|
||||
tls: { rejectUnauthorized: false }
|
||||
});
|
||||
|
||||
try {
|
||||
await transporter.sendMail({ raw });
|
||||
// Update S3 Metadata
|
||||
const head = await s3.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
|
||||
const newMeta = { ...head.Metadata, [process.env.PROCESSED_META_KEY!]: process.env.PROCESSED_META_VALUE! };
|
||||
await s3.send(new CopyObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
CopySource: `${bucket}/${key}`,
|
||||
Metadata: newMeta,
|
||||
MetadataDirective: 'REPLACE'
|
||||
}));
|
||||
// Update DB
|
||||
await db.update(emails).set({ processed: true }).where(eq(emails.s3Key, key));
|
||||
return NextResponse.json({ message: 'Resent successfully' });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: (error as Error).message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user