aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/mutators/send-email.ts
blob: 45b6fca6624f735f4914969429b0bebae1bc97c6 (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
import { fetchGraphQL, getGraphQLUrl } from 'src/utils/helpers';

type SentEmail = {
  clientMutationId: string;
  message: string;
  origin: string;
  replyTo: string;
  sent: boolean;
};

type SendEmailResponse = {
  sendEmail: SentEmail;
};

const sendMailMutation = `mutation SendEmail($body: String, $clientMutationId: String, $replyTo: String, $subject: String) {
  sendEmail(
    input: {body: $body, clientMutationId: $clientMutationId, replyTo: $replyTo, subject: $subject}
  ) {
    clientMutationId
    message
    origin
    replyTo
    sent
    to
  }
}`;

export type SendMailInput = {
  body: string;
  clientMutationId: string;
  replyTo: string;
  subject: string;
};

/**
 * Send an email using GraphQL API.
 *
 * @param {SendMailInput} data - The mail data.
 * @returns {Promise<SentEmail>} The mutation response.
 */
export const sendMail = async (data: SendMailInput): Promise<SentEmail> => {
  const response = await fetchGraphQL<SendEmailResponse>({
    query: sendMailMutation,
    url: getGraphQLUrl(),
    variables: { ...data },
  });

  return response.sendEmail;
};