Next.js Discord

Discord Forum

Sent email link only works upon reloading?

Answered
BakaPresident posted this in #help-forum
Open in Discord
Avatar
I've sent out a link that looks like
http://localhost:3000/api/accept-quotation/66d6817200249621acb5

@Codebase When the email is sent out to the user, it initially doesn't work, but if u press "Enter to refresh it" on the link in chrome again, it does work. However clicking on the refresh button in the chrome doesn't work

This is some extract from the code
import { NextResponse } from 'next/server';
import { Resend } from 'resend';
import { QuotationEmail } from '@/emails/QuotationEmail';

const resend = new Resend(process.env.RESEND_API_KEY);

export async function POST(request: Request) {
    const { busRequestId, quotationPrice, email } = await request.json();

    const acceptUrl = `${process.env.NEXT_PUBLIC_BASE_URL}/api/accept-quotation/${busRequestId} `;
    const rejectUrl = `${process.env.NEXT_PUBLIC_BASE_URL}/api/reject-quotation/${busRequestId} `;

    try {
        await resend.emails.send({
            from: 'ShiftBus <noreply@shiftbus.com>',
            to: email,
            subject: 'Your Quotation is Ready',
            react: QuotationEmail({ quotationPrice, acceptUrl, rejectUrl }),
        });

        return NextResponse.json({ success: true });
    } catch (error) {
        return NextResponse.json({ error: 'Failed to send email' }, { status: 500 });
    }
}
Answered by BakaPresident
Fixed. It was appwrite permission
View full answer

3 Replies

Avatar
The code to get the api for accept/reject would be
import { NextResponse } from 'next/server';
import { updateBusRequestStatus } from '@/lib/services/bookingService';

export async function GET(request: Request, { params }: { params: { id: string } }) {
    const busRequestId = params.id.trim()


    console.log(busRequestId);

    try {
        console.log("Updating bus request status to accepted");
        let results = await updateBusRequestStatus(busRequestId, 'accepted');
        console.log("Results: ", results);


        return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/quotation-accepted`);
    } catch (error) {
        console.log("Error caught: ", error);



        if (error instanceof Error && error.message === 'Quotation has already been processed') {
            return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/quotation-already-processed`);
        }
        return NextResponse.json({ error: 'Failed to accept quotation' }, { status: 500 });
    }
}
I've tried console logging the
console.log(busRequestId); on the accept. It seems like it's retrieving the correct busRequestId but for some reason it's still not working on the first load without refreshing
Avatar
Fixed. It was appwrite permission
Answer