PageSourceSearch

https://www.mothercare.ae/scripts/hellomember/api.js

js mothercare.ae collected 2026-09-24 11:57:26 UTC 7,417 bytes, 298 lines download raw bytes

1/* eslint-disable import/no-cycle */
2import { getConfigValue } from '../configs.js';
3import { getSignInToken, performCommerceRestMutation, performCommerceRestQuery } from '../commerce.js';
4import { getCustomer } from '../customer/api.js';
5
6async function createUrl(endpoint) {
7  const baseUrl = await getConfigValue('commerce-rest-endpoint');
8
9  const storeViewCode = await getConfigValue('commerce-store-view-code');
10
11  const url = `${baseUrl}/${storeViewCode}/V1/${endpoint}`;
12
13  return url;
14}
15
16export async function getAPCCustomerData(programCode = 'hello_member', sendQueryParams = true) {
17  const token = getSignInToken();
18
19  if (!token) {
20    return null;
21  }
22
23  const customer = await getCustomer(true);
24
25  if (!customer) {
26    return null;
27  }
28
29  const url = await createUrl(`customers/apcCustomerData/${customer.id}${sendQueryParams ? `?customerId=${customer.id}&programCode=${programCode}` : ''}`);
30
31  const {
32    data, success, message, status,
33  } = await performCommerceRestQuery(url, !!token);
34
35  if (!success) {
36    console.error('Error fetching APC customer data', status, message);
37  }
38
39  return data;
40}
41
42function isExpired(expiryTime) {
43  const now = new Date();
44  return now.getTime() > expiryTime;
45}
46
47export async function getAPCTierProgressData(useCache = false, programCode = 'hello_member') {
48  const token = getSignInToken();
49
50  if (!token) {
51    return null;
52  }
53  const lang = document.documentElement.lang || 'en';
54  const cachedData = sessionStorage.getItem(`apcTierProgressData:${lang}`);
55
56  if (useCache && cachedData) {
57    const parsedData = JSON.parse(cachedData);
58
59    if (isExpired(parsedData.expiryTime)) {
60      sessionStorage.removeItem(`apcTierProgressData:${lang}`);
61    } else if (parsedData.data) {
62      return parsedData.data;
63    }
64  }
65
66  const customer = await getCustomer(true);
67
68  if (!customer) {
69    return null;
70  }
71
72  const url = await createUrl(`customers/apcTierProgressData/customerId/${customer.id}?customerId=${customer.id}&programCode=${programCode}`);
73
74  const {
75    data, success, message, status,
76  } = await performCommerceRestQuery(url, !!token);
77
78  if (!success) {
79    console.error('Error fetching APC customer data', status, message);
80  }
81
82  sessionStorage.setItem(`apcTierProgressData:${lang}`, JSON.stringify({
83    data,
84    expiryTime: Date.now() + 1000 * 60 * 10, // TTL 10 minutes
85  }));
86  return data;
87}
88
89export async function getHelloMemberCoupons(programCode = 'hello_member') {
90  const token = getSignInToken();
91
92  if (!token) {
93    return null;
94  }
95
96  const customer = await getCustomer(true);
97
98  if (!customer) {
99    return null;
100  }
101
102  const url = await createUrl(`hello-member/customers/coupons?customerId=${customer.id}&programCode=${programCode}`);
103
104  const {
105    data, success, message, status,
106  } = await performCommerceRestQuery(url, !!token);
107
108  if (!success) {
109    console.error('Error fetching Hello Member Coupons data', status, message);
110  }
111
112  console.log('getHelloMemberCoupons', data);
113
114  return data;
115}
116
117export async function getHelloMemberOffers(programCode = 'hello_member') {
118  const token = getSignInToken();
119
120  if (!token) {
121    return null;
122  }
123
124  const customer = await getCustomer(true);
125
126  if (!customer) {
127    return null;
128  }
129
130  const url = await createUrl(`hello-member/customers/offers?customerId=${customer.id}&programCode=${programCode}`);
131
132  const {
133    data, success, message, status,
134  } = await performCommerceRestQuery(url, !!token);
135
136  if (!success) {
137    console.error('Error fetching Hello Member Offers data', status, message);
138  }
139
140  console.log('getHelloMemberOffers', data);
141
142  return data;
143}
144
145export async function getHelloMemberCouponDetails(couponId, programCode = 'hello_member') {
146  const token = getSignInToken();
147
148  if (!token) {
149    return null;
150  }
151
152  const customer = await getCustomer(true);
153
154  if (!customer) {
155    return null;
156  }
157
158  const url = await createUrl(`hello-member/customers/coupons/id/${couponId}?customerId=${customer.id}&programCode=${programCode}&code=${couponId}`);
159
160  const {
161    data, success, message, status,
162  } = await performCommerceRestQuery(url, !!token);
163
164  if (!success) {
165    console.error('Error fetching Hello Member Coupon Details data', status, message);
166    return { success: false, message };
167  }
168
169  console.log('getHelloMemberCouponDetails', data);
170
171  return { success: true, data };
172}
173
174export async function getHelloMemberOfferDetails(offerCode, programCode = 'hello_member') {
175  const token = getSignInToken();
176
177  if (!token) {
178    return null;
179  }
180
181  const customer = await getCustomer(true);
182
183  if (!customer) {
184    return null;
185  }
186
187  const url = await createUrl(`hello-member/customers/offers/code/${offerCode}?customerId=${customer.id}&programCode=${programCode}&code=${offerCode}`);
188
189  const {
190    data, success, message, status,
191  } = await performCommerceRestQuery(url, !!token);
192
193  if (!success) {
194    console.error('Error fetching Hello Member Offer Details data', status, message);
195    return { success: false, message };
196  }
197
198  console.log('getHelloMemberOfferDetails', customer);
199
200  return { success: true, data };
201}
202
203export async function applyOfferToCart(offerCode, offerType, programCode = 'hello_member') {
204  const token = getSignInToken();
205
206  if (!token) {
207    return null;
208  }
209
210  const customer = await getCustomer(true);
211
212  if (!customer) {
213    return null;
214  }
215
216  const variables = {
217    customerId: customer.id,
218    programCode,
219    offerCode,
220    offerType,
221  };
222
223  const url = await createUrl(`hello-member/carts/mine/memberOffers?customerId=${customer.id}&programCode=${programCode}&code=${offerCode}`);
224
225  const {
226    data, success, message, status,
227  } = await performCommerceRestMutation(url, variables, !!token, 'POST');
228
229  if (!success) {
230    console.error('Error applying Hello Member Offer to cart', status, message);
231    return { success: false, message };
232  }
233
234  console.log('applyOfferToCart', data);
235  return { success: true, data, message };
236}
237
238export async function applyVoucherToCart(couponCode, programCode = 'hello_member') {
239  const token = getSignInToken();
240
241  if (!token) {
242    return null;
243  }
244
245  const customer = await getCustomer(true);
246
247  if (!customer) {
248    return null;
249  }
250
251  const variables = {
252    customerId: customer.id,
253    programCode,
254    voucherCodes: [couponCode],
255  };
256
257  const url = await createUrl('hello-member/carts/mine/bonusVouchers');
258
259  const {
260    data, success, message, status,
261  } = await performCommerceRestMutation(url, variables, !!token, 'POST');
262
263  if (!success) {
264    console.error('Error applying Hello Member Voucher to cart', status, message);
265    return { success: false, message };
266  }
267
268  console.log('applyVoucherToCart', data);
269  return { success: true, data, message };
270}
271
272export async function getAPCTransactions(programCode = 'hello_member', firstResult = 1, pageSize = 10) {
273  const token = getSignInToken();
274
275  if (!token) {
276    return null;
277  }
278
279  const customer = await getCustomer(true);
280
281  if (!customer) {
282    return null;
283  }
284
285  const url = await createUrl(`customers/apcTransactions?customerId=${customer.id}&programCode=${programCode}&firstResult=${firstResult}&pageSize=${pageSize}`);
286
287  const {
288    data, success, message, status,
289  } = await performCommerceRestQuery(url, !!token);
290
291  if (!success) {
292    console.error('Error fetching APC Transactions data', status, message);
293  }
294
295  console.log('getAPCTransactions', data);
296
297  return data;
298}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.