From 02ae938c238c9d18448d17a8ec92c0edd8c17463 Mon Sep 17 00:00:00 2001 From: Bertrand Yuan Date: Tue, 16 Dec 2025 00:12:49 +0800 Subject: feat(back-end): introduce payload Payload is the next.js Headless CMS and App Framework, I would like to pick it up and modify it as it is MIT licensed. Many features in Payload is not applicable for our project. So, I modify it so that it is light and clear. --- src/app/(home)/actions.ts | 78 ----------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 src/app/(home)/actions.ts (limited to 'src/app/(home)/actions.ts') diff --git a/src/app/(home)/actions.ts b/src/app/(home)/actions.ts deleted file mode 100644 index fdb16ca..0000000 --- a/src/app/(home)/actions.ts +++ /dev/null @@ -1,78 +0,0 @@ -'use server'; - -import { getContact, sendWelcomeEmail, updateContact } from '@/lib/resend'; -import { ActionError, actionClient } from '@/lib/safe-action'; -import { getSortedByDatePosts } from '@/lib/source'; -import { NewsletterSchema } from '@/lib/validators'; -import { getSession } from '@/server/auth'; -import { Resend } from 'resend'; - -const resend = new Resend(process.env.RESEND_API_KEY as string); -const audienceId = process.env.RESEND_AUDIENCE_ID as string; - -const splitName = (name = '') => { - const [firstName, ...lastName] = name.split(' ').filter(Boolean); - return { - firstName: firstName, - lastName: lastName.join(' '), - }; -}; - -export const subscribeUser = actionClient - .schema(NewsletterSchema) - .action(async ({ parsedInput: { email } }) => { - const session = await getSession(); - const fullName = session?.user.name || ''; - const { firstName, lastName } = fullName - ? splitName(fullName) - : { firstName: '', lastName: '' }; - - try { - const contact = await getContact({ email, audienceId }); - - if (contact) { - await updateContact({ - email, - firstName, - lastName, - audienceId, - unsubscribed: false, - }); - - return { - success: true, - message: 'You are already subscribed to our newsletter!', - }; - } - - const { data, error } = await resend.contacts.create({ - email, - audienceId, - firstName, - lastName, - unsubscribed: false, - }); - - if (!data || error) { - throw new Error( - `Failed to create contact: ${error?.message || 'Unknown error'}`, - ); - } - - const posts = getSortedByDatePosts(); - await sendWelcomeEmail({ - posts, - to: email, - firstName: firstName || 'there', - }); - - return { - success: true, - message: 'You are now subscribed to our newsletter!', - }; - } catch (error) { - console.error('Failed to subscribe:', error); - if (error instanceof ActionError) throw error; - throw new ActionError('Oops, something went wrong while subscribing.'); - } - }); -- cgit v1.2.3