PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.13
Yatra – Travel Booking & Tour Operator Software v3.0.13
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / resources / js / api / email-automation-api.ts

email-automation-api.ts in Yatra – Travel Booking & Tour Operator Software 3.0.13, at resources/js/api/email-automation-api.ts

188 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
55 * Live list of trigger events (key/name/description) from the server.
56 *
57 * Fetched instead of relying only on the one-time `window.yatraAdmin.emailEvents`
58 * localize snapshot: that snapshot reflects the module state at PHP render time,
59 * while the module-enabled flag is refreshed client-side (useModules), so the two
60 * can diverge — leaving the event dropdown empty when a template is created right
61 * after enabling the module (no full reload). This endpoint always reflects the
62 * current state.
63 */
64 export async function fetchEmailTemplateEvents(): Promise<
65 Array<{ key: string; name: string; description?: string }>
66 > {
67 return unwrapApiPayload<
68 Array<{ key: string; name: string; description?: string }>
69 >(await apiClient.get(API_ENDPOINTS.EMAIL_TEMPLATE_EVENTS));
70 }
71
72 export async function createEmailTemplate(data: unknown): Promise<unknown> {
73 return apiClient.post(API_ENDPOINTS.EMAIL_TEMPLATES, data);
74 }
75
76 export async function updateEmailTemplate(
77 id: string | number,
78 data: unknown,
79 ): Promise<unknown> {
80 return apiClient.put(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id), data);
81 }
82
83 export async function setEmailTemplateActive(
84 id: number,
85 is_active: boolean,
86 ): Promise<unknown> {
87 return apiClient.put(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id), { is_active });
88 }
89
90 export async function deleteEmailTemplate(id: number): Promise<unknown> {
91 return apiClient.delete(API_ENDPOINTS.EMAIL_TEMPLATE_GET(id));
92 }
93
94 export async function duplicateEmailTemplate(id: number): Promise<unknown> {
95 return apiClient.post(API_ENDPOINTS.EMAIL_TEMPLATE_DUPLICATE(id));
96 }
97
98 export async function previewEmailTemplate(
99 id: string | number,
100 ): Promise<EmailPreviewPayload> {
101 const response = await apiClient.post(
102 API_ENDPOINTS.EMAIL_TEMPLATE_PREVIEW(id),
103 {},
104 );
105 return unwrapApiPayload<EmailPreviewPayload>(response);
106 }
107
108 export async function sendEmailTemplateTest(
109 id: string | number,
110 email: string,
111 ): Promise<unknown> {
112 return apiClient.post(API_ENDPOINTS.EMAIL_TEMPLATE_TEST(id), { email });
113 }
114
115 export async function fetchEmailSequences(): Promise<unknown[]> {
116 const raw = await apiClient.get(API_ENDPOINTS.EMAIL_SEQUENCES);
117 const unwrapped = unwrapApiPayload<unknown>(raw);
118 return Array.isArray(unwrapped) ? unwrapped : [];
119 }
120
121 export async function fetchEmailSequence(
122 id: string | number,
123 ): Promise<unknown> {
124 return unwrapApiPayload(
125 await apiClient.get(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id)),
126 );
127 }
128
129 export async function createEmailSequence(data: unknown): Promise<unknown> {
130 return apiClient.post(API_ENDPOINTS.EMAIL_SEQUENCES, data);
131 }
132
133 export async function updateEmailSequence(
134 id: string | number,
135 data: unknown,
136 ): Promise<unknown> {
137 return apiClient.put(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id), data);
138 }
139
140 export async function deleteEmailSequence(id: number): Promise<unknown> {
141 return apiClient.delete(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id));
142 }
143
144 export async function updateEmailSequenceStatus(
145 id: number,
146 status: string,
147 ): Promise<unknown> {
148 return apiClient.put(API_ENDPOINTS.EMAIL_SEQUENCE_GET(id), { status });
149 }
150
151 export type EmailLogsPageResult = {
152 items: unknown[];
153 total: number;
154 per_page: number;
155 current_page: number;
156 };
157
158 export async function fetchEmailLogs(params?: {
159 page?: number;
160 per_page?: number;
161 template_key?: string;
162 recipient_email?: string;
163 status?: string;
164 }): Promise<EmailLogsPageResult> {
165 const raw = await apiClient.get(API_ENDPOINTS.EMAIL_LOGS, { params });
166 if (raw !== null && typeof raw === "object") {
167 const r = raw as Record<string, unknown>;
168 if (r.success === true && "data" in r) {
169 const meta = (r.meta as Record<string, unknown>) || {};
170 const items = Array.isArray(r.data) ? r.data : [];
171 return {
172 items,
173 total: Number(meta.total) || items.length,
174 per_page: Number(meta.per_page) || params?.per_page || 20,
175 current_page: Number(meta.current_page) || params?.page || 1,
176 };
177 }
178 }
179 const fallback = unwrapApiPayload<unknown>(raw);
180 const items = Array.isArray(fallback) ? fallback : [];
181 return {
182 items,
183 total: items.length,
184 per_page: params?.per_page || 20,
185 current_page: params?.page || 1,
186 };
187 }
188