summaryrefslogtreecommitdiff
path: root/src/app/(home)/actions.ts
diff options
context:
space:
mode:
authorBertrand Yuan <bert.yuan@outlook.com>2025-12-16 00:25:04 +0800
committerGitHub <noreply@github.com>2025-12-16 00:25:04 +0800
commit39c83fbb69ef06d2d56790d75abc254ba7e34394 (patch)
treedd006593448c3500bdcb414af3b4656f7a7683d4 /src/app/(home)/actions.ts
parent48b07bc308a35734a6a7a305c8fdccbfa47de7d8 (diff)
parent785371bb3eccca455e5ce5fccbe9b6e3752a03f6 (diff)
Merge pull request #1 from bertyuan/feat-introduce-payloadv1.0
Feat: introduce payload
Diffstat (limited to 'src/app/(home)/actions.ts')
-rw-r--r--src/app/(home)/actions.ts78
1 files changed, 0 insertions, 78 deletions
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.');
- }
- });