All files / src/services emailTemplateService.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 5/5
100% Lines 11/11

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                                  1x                 3x 3x       2x     2x       1x 1x               1x       1x       1x           1x    
/**
 * Email Template Service (Story 10.2)
 *
 * HTTP client for Email Template Management API (event-management-service)
 * Features:
 * - JWT authentication via interceptors
 * - Generated types from OpenAPI spec (ADR-006)
 * - ORGANIZER-only operations (RBAC)
 */
 
import apiClient from '@/services/api/apiClient';
import type { components } from '@/types/generated/event-newsletter-api.types';
 
type EmailTemplateResponse = components['schemas']['EmailTemplateResponse'];
type CreateEmailTemplateRequest = components['schemas']['CreateEmailTemplateRequest'];
type UpdateEmailTemplateRequest = components['schemas']['UpdateEmailTemplateRequest'];
 
const BASE_PATH = '/email-templates';
 
export interface ListEmailTemplatesParams {
  category?: string;
  isLayout?: boolean;
}
 
class EmailTemplateService {
  async listTemplates(params?: ListEmailTemplatesParams): Promise<EmailTemplateResponse[]> {
    const response = await apiClient.get<EmailTemplateResponse[]>(BASE_PATH, { params });
    return response.data;
  }
 
  async getTemplate(templateKey: string, locale: string): Promise<EmailTemplateResponse> {
    const response = await apiClient.get<EmailTemplateResponse>(
      `${BASE_PATH}/${encodeURIComponent(templateKey)}/${encodeURIComponent(locale)}`
    );
    return response.data;
  }
 
  async createTemplate(req: CreateEmailTemplateRequest): Promise<EmailTemplateResponse> {
    const response = await apiClient.post<EmailTemplateResponse>(BASE_PATH, req);
    return response.data;
  }
 
  async updateTemplate(
    templateKey: string,
    locale: string,
    req: UpdateEmailTemplateRequest
  ): Promise<EmailTemplateResponse> {
    const response = await apiClient.put<EmailTemplateResponse>(
      `${BASE_PATH}/${encodeURIComponent(templateKey)}/${encodeURIComponent(locale)}`,
      req
    );
    return response.data;
  }
 
  async deleteTemplate(templateKey: string, locale: string): Promise<void> {
    await apiClient.delete(
      `${BASE_PATH}/${encodeURIComponent(templateKey)}/${encodeURIComponent(locale)}`
    );
  }
}
 
export const emailTemplateService = new EmailTemplateService();
export type { EmailTemplateResponse, CreateEmailTemplateRequest, UpdateEmailTemplateRequest };