| 1 |
/** |
| 2 |
* Centralized REST calls for Email Automation (Yatra Pro). |
| 3 |
*/ |
| 4 |
|
| 5 |
import { apiClient } from "../lib/api-client"; |
| 6 |
import { API_ENDPOINTS } from "../lib/api-endpoints"; |
| 7 |
import { unwrapApiPayload } from "../lib/unwrap-api-payload"; |
| 8 |
|
| 9 |
export type EmailTemplateVariableMap = Record< |
| 10 |
string, |
| 11 |
{ key: string; description?: string }[] |
| 12 |
>; |
| 13 |
|
| 14 |
export type EmailPreviewPayload = { |
| 15 |
subject: string; |
| 16 |
body: string; |
| 17 |
}; |
| 18 |
|
| 19 |
export async function fetchEmailTemplates(): Promise<unknown[]> { |
| 20 |
const raw = await apiClient.get(API_ENDPOINTS.EMAIL_TEMPLATES); |
| 21 |
const unwrapped = unwrapApiPayload<unknown>(raw); |
| 22 |
return Array.isArray(unwrapped) ? unwrapped : []; |
| 23 |
} |
| 24 |
|
| 25 |
export async function fetchEmailTemplate( |
| 26 |
id: string | number, |
| 27 |
): Promise<Record<string, unknown> | null> { |
| 28 |
return unwrapApiPayload<Record<string, unknown> | null>( |
| 29 |
await apiClient.get(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id)), |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Fetch the merge-tag catalog for the Template Editor "Available |
| 35 |
* Variables" sidebar. |
| 36 |
* |
| 37 |
* Pass `eventKey` to get only the variables that apply to that |
| 38 |
* automation event (e.g. `booking.created` returns the booking + |
| 39 |
* customer + payment tags but skips enquiry / consent). Omit it |
| 40 |
* (or pass an empty string) to get the full union of all tags — |
| 41 |
* matches the pre-3.0.5 behavior for callers that haven't been |
| 42 |
* updated. |
| 43 |
*/ |
| 44 |
export async function fetchEmailTemplateVariables( |
| 45 |
eventKey?: string, |
| 46 |
): Promise<EmailTemplateVariableMap> { |
| 47 |
const url = |
| 48 |
eventKey && eventKey.trim() !== "" |
| 49 |
? `${API_ENDPOINTS.EMAIL_TEMPLATE_VARIABLES}?event_key=${encodeURIComponent(eventKey.trim())}` |
| 50 |
: API_ENDPOINTS.EMAIL_TEMPLATE_VARIABLES; |
| 51 |
return unwrapApiPayload<EmailTemplateVariableMap>(await apiClient.get(url)); |
| 52 |
} |
| 53 |
|
| 54 |
export async function createEmailTemplate(data: unknown): Promise<unknown> { |
| 55 |
return apiClient.post(API_ENDPOINTS.EMAIL_TEMPLATES, data); |
| 56 |
} |
| 57 |
|
| 58 |
export async function updateEmailTemplate( |
| 59 |
id: string | number, |
| 60 |
data: unknown, |
| 61 |
): Promise<unknown> { |
| 62 |
return apiClient.put(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id), data); |
| 63 |
} |
| 64 |
|
| 65 |
export async function setEmailTemplateActive( |
| 66 |
id: number, |
| 67 |
is_active: boolean, |
| 68 |
): Promise<unknown> { |
| 69 |
return apiClient.put(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id), { is_active }); |
| 70 |
} |
| 71 |
|
| 72 |
export async function deleteEmailTemplate(id: number): Promise<unknown> { |
| 73 |
return apiClient.delete(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id)); |
| 74 |
} |
| 75 |
|
| 76 |
export async function duplicateEmailTemplate(id: number): Promise<unknown> { |
| 77 |
return apiClient.post(API_ENDPOINTS.EMAIL_TEMPLATE_DUPLICATE(id)); |
| 78 |
} |
| 79 |
|
| 80 |
export async function previewEmailTemplate( |
| 81 |
id: string | number, |
| 82 |
): Promise<EmailPreviewPayload> { |
| 83 |
const response = await apiClient.post( |
| 84 |
API_ENDPOINTS.EMAIL_TEMPLATE_PREVIEW(id), |
| 85 |
{}, |
| 86 |
); |
| 87 |
return unwrapApiPayload<EmailPreviewPayload>(response); |
| 88 |
} |
| 89 |
|
| 90 |
export async function sendEmailTemplateTest( |
| 91 |
id: string | number, |
| 92 |
email: string, |
| 93 |
): Promise<unknown> { |
| 94 |
return apiClient.post(API_ENDPOINTS.EMAIL_TEMPLATE_TEST(id), { email }); |
| 95 |
} |
| 96 |
|
| 97 |
export async function fetchEmailSequences(): Promise<unknown[]> { |
| 98 |
const raw = await apiClient.get(API_ENDPOINTS.EMAIL_SEQUENCES); |
| 99 |
const unwrapped = unwrapApiPayload<unknown>(raw); |
| 100 |
return Array.isArray(unwrapped) ? unwrapped : []; |
| 101 |
} |
| 102 |
|
| 103 |
export async function fetchEmailSequence( |
| 104 |
id: string | number, |
| 105 |
): Promise<unknown> { |
| 106 |
return unwrapApiPayload( |
| 107 |
await apiClient.get(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id)), |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
export async function createEmailSequence(data: unknown): Promise<unknown> { |
| 112 |
return apiClient.post(API_ENDPOINTS.EMAIL_SEQUENCES, data); |
| 113 |
} |
| 114 |
|
| 115 |
export async function updateEmailSequence( |
| 116 |
id: string | number, |
| 117 |
data: unknown, |
| 118 |
): Promise<unknown> { |
| 119 |
return apiClient.put(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id), data); |
| 120 |
} |
| 121 |
|
| 122 |
export async function deleteEmailSequence(id: number): Promise<unknown> { |
| 123 |
return apiClient.delete(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id)); |
| 124 |
} |
| 125 |
|
| 126 |
export async function updateEmailSequenceStatus( |
| 127 |
id: number, |
| 128 |
status: string, |
| 129 |
): Promise<unknown> { |
| 130 |
return apiClient.put(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id), { status }); |
| 131 |
} |
| 132 |
|
| 133 |
export type EmailLogsPageResult = { |
| 134 |
items: unknown[]; |
| 135 |
total: number; |
| 136 |
per_page: number; |
| 137 |
current_page: number; |
| 138 |
}; |
| 139 |
|
| 140 |
export async function fetchEmailLogs(params?: { |
| 141 |
page?: number; |
| 142 |
per_page?: number; |
| 143 |
template_key?: string; |
| 144 |
recipient_email?: string; |
| 145 |
status?: string; |
| 146 |
}): Promise<EmailLogsPageResult> { |
| 147 |
const raw = await apiClient.get(API_ENDPOINTS.EMAIL_LOGS, { params }); |
| 148 |
if (raw !== null && typeof raw === "object") { |
| 149 |
const r = raw as Record<string, unknown>; |
| 150 |
if (r.success === true && "data" in r) { |
| 151 |
const meta = (r.meta as Record<string, unknown>) || {}; |
| 152 |
const items = Array.isArray(r.data) ? r.data : []; |
| 153 |
return { |
| 154 |
items, |
| 155 |
total: Number(meta.total) || items.length, |
| 156 |
per_page: Number(meta.per_page) || params?.per_page || 20, |
| 157 |
current_page: Number(meta.current_page) || params?.page || 1, |
| 158 |
}; |
| 159 |
} |
| 160 |
} |
| 161 |
const fallback = unwrapApiPayload<unknown>(raw); |
| 162 |
const items = Array.isArray(fallback) ? fallback : []; |
| 163 |
return { |
| 164 |
items, |
| 165 |
total: items.length, |
| 166 |
per_page: params?.per_page || 20, |
| 167 |
current_page: params?.page || 1, |
| 168 |
}; |
| 169 |
} |
| 170 |
|