refactor: improve news and email handling, style, folder structure (#16)
This commit is contained in:
@@ -1,44 +1,61 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from '../../../prisma/prisma';
|
||||
import { ApiResponse } from '../../../utils/apiResponse';
|
||||
import { ConfirmationSchema, ResponseSchema } from '../../../utils/schemas';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import {
|
||||
BAD_REQUEST,
|
||||
INTERNAL_SERVER_ERROR,
|
||||
STATUS_BAD_REQUEST,
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
STATUS_OK
|
||||
} from '@utils/statusCodes';
|
||||
import { ConfirmationSchema, ResponseType } from '@utils/validationSchemas';
|
||||
import { Resend } from 'resend';
|
||||
|
||||
export const dynamic = 'force-dynamic'; // defaults to force-static
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const validation = ConfirmationSchema.safeParse(body);
|
||||
if (!validation.success || !validation.data.code) {
|
||||
return ApiResponse(400, 'Bad request');
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
code: validation.data.code
|
||||
try {
|
||||
if (!process.env.RESEND_KEY || !process.env.RESEND_AUDIENCE) {
|
||||
throw new Error('RESEND_AUDIENCE is not set');
|
||||
}
|
||||
const body = await request.json();
|
||||
const validation = ConfirmationSchema.safeParse(body);
|
||||
if (!validation.success || !validation.data.code) {
|
||||
return ApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
}
|
||||
});
|
||||
|
||||
if (user) {
|
||||
await prisma.user.update({
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
code: validation.data.code
|
||||
},
|
||||
data: {
|
||||
confirmed: true
|
||||
}
|
||||
});
|
||||
|
||||
const message: z.infer<typeof ResponseSchema> = {
|
||||
success: true,
|
||||
message: `Thank you for confirming the subscription, ${user.email}!`
|
||||
};
|
||||
if (user) {
|
||||
const resend = new Resend(process.env.RESEND_KEY);
|
||||
|
||||
return ApiResponse(200, JSON.stringify(message));
|
||||
await resend.contacts.update({
|
||||
id: user.resendId,
|
||||
audienceId: process.env.RESEND_AUDIENCE,
|
||||
unsubscribed: false
|
||||
});
|
||||
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
code: validation.data.code
|
||||
},
|
||||
data: {
|
||||
confirmed: true
|
||||
}
|
||||
});
|
||||
|
||||
const message: ResponseType = {
|
||||
success: true,
|
||||
message: `Thank you for confirming the subscription, ${user.email}!`
|
||||
};
|
||||
|
||||
return ApiResponse(STATUS_OK, message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
const message: z.infer<typeof ResponseSchema> = {
|
||||
success: false,
|
||||
message: `It was not possible to confirm the subscription.`
|
||||
};
|
||||
|
||||
return ApiResponse(200, JSON.stringify(message));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user