summaryrefslogtreecommitdiff
path: root/src/app/(home)/actions.ts
blob: fdb16cabf0bb8a7e3a8f99231bdbe0f13f1cc978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'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.');
    }
  });