All files / services email.service.ts

94.59% Statements 35/37
90% Branches 18/20
100% Functions 7/7
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                                                3x             22x 22x 22x 22x 22x   22x     20x       3x 2x             3x 2x           4x       2x             3x       2x                     20x 20x 20x 20x 20x 20x 20x   20x     19x       4x 3x        
import { apiClient as api } from '@/lib/api';
 
export interface EmailTemplate {
  id: string;
  account_id: string;
  template_type: string;
  locale: string;
  subject: string;
  html_body: string;
  text_body: string;
  enabled: boolean;
  is_builtin: boolean;
  created_at: string;
  updated_at: string;
}
 
export interface EmailTemplateUpdate {
  subject?: string;
  html_body?: string;
  text_body?: string;
  enabled?: boolean;
}
 
export interface EmailTemplateTestRequest {
  recipient_email: string;
  variables?: Record<string, string>;
  provider?: string;
}
 
export interface EmailTemplateRenderRequest {
  template_type: string;
  variables: Record<string, string>;
  locale?: string;
  account_id?: string;
  subject?: string;
  html_body?: string;
  text_body?: string;
}
 
export interface EmailTemplateRenderResponse {
  subject: string;
  html_body: string;
  text_body: string;
}
 
export interface EmailLog {
  id: string;
  account_id: string;
  template_type: string;
  recipient_email: string;
  provider: string;
  status: string;
  error_message: string | null;
  variables: Record<string, string> | null;
  sent_at: string;
}
 
export interface EmailLogListResponse {
  logs: EmailLog[];
  total: number;
  page: number;
  page_size: number;
}
 
export const emailService = {
  listEmailTemplates: async (params?: {
    template_type?: string;
    locale?: string;
    account_id?: string;
    enabled?: boolean;
  }): Promise<EmailTemplate[]> => {
    const queryParams = new URLSearchParams();
    if (params?.template_type) queryParams.append('template_type', params.template_type);
    if (params?.locale) queryParams.append('locale', params.locale);
    Iif (params?.account_id) queryParams.append('account_id', params.account_id);
    if (params?.enabled !== undefined) queryParams.append('enabled', params.enabled.toString());
 
    const response = await api.get<EmailTemplate[]>(
      `/admin/email/templates?${queryParams.toString()}`
    );
    return response.data;
  },
 
  getEmailTemplate: async (id: string): Promise<EmailTemplate> => {
    const response = await api.get<EmailTemplate>(`/admin/email/templates/${id}`);
    return response.data;
  },
 
  updateEmailTemplate: async (
    id: string,
    data: EmailTemplateUpdate
  ): Promise<EmailTemplate> => {
    const response = await api.put<EmailTemplate>(`/admin/email/templates/${id}`, data);
    return response.data;
  },
 
  renderEmailTemplate: async (
    data: EmailTemplateRenderRequest
  ): Promise<EmailTemplateRenderResponse> => {
    const response = await api.post<EmailTemplateRenderResponse>(
      '/admin/email/templates/render',
      data
    );
    return response.data;
  },
 
  sendTestEmail: async (
    id: string,
    data: EmailTemplateTestRequest
  ): Promise<{ status: string; message: string }> => {
    const response = await api.post<{ status: string; message: string }>(
      `/admin/email/templates/${id}/test`,
      data
    );
    return response.data;
  },
 
  listEmailLogs: async (params?: {
    status_filter?: string;
    template_type?: string;
    start_date?: string;
    end_date?: string;
    page?: number;
    page_size?: number;
  }): Promise<EmailLogListResponse> => {
    const queryParams = new URLSearchParams();
    if (params?.status_filter) queryParams.append('status_filter', params.status_filter);
    Iif (params?.template_type) queryParams.append('template_type', params.template_type);
    if (params?.start_date) queryParams.append('start_date', params.start_date);
    if (params?.end_date) queryParams.append('end_date', params.end_date);
    if (params?.page) queryParams.append('page', params.page.toString());
    if (params?.page_size) queryParams.append('page_size', params.page_size.toString());
 
    const response = await api.get<EmailLogListResponse>(
      `/admin/email/logs?${queryParams.toString()}`
    );
    return response.data;
  },
 
  getEmailLog: async (id: string): Promise<EmailLog> => {
    const response = await api.get<EmailLog>(`/admin/email/logs/${id}`);
    return response.data;
  },
};