@capillarytech/creatives-library 7.13.5 → 7.13.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@capillarytech/creatives-library",
3
3
  "author": "meharaj",
4
- "version": "7.13.05",
4
+ "version": "7.13.7",
5
5
  "description": "Capillary creatives ui",
6
6
  "main": "./index.js",
7
7
  "module": "./index.es.js",
@@ -24,6 +24,7 @@
24
24
  "jest-date-mock": "^1.0.8",
25
25
  "jquery": "^3.3.1",
26
26
  "load-script": "^1.0.0",
27
+ "node-html-parser": "^5.4.2-0",
27
28
  "normalizr": "^3.2.3",
28
29
  "papaparse": "^5.3.1",
29
30
  "pre-push": "^0.1.2",
package/routes.js CHANGED
@@ -13,6 +13,7 @@ import * as emailSagas from 'containers/Email/sagas';
13
13
  import * as templateSagas from 'containers/Templates/sagas';
14
14
  import * as lineSagas from 'containers/Line/Create/sagas';
15
15
  import * as languageSaga from './v2Containers/LanguageProvider/sagas';
16
+
16
17
  import pathConfig from './config/path';
17
18
 
18
19
  import {updateCharCount} from './utils/smsCharCountV2';
package/services/api.js CHANGED
@@ -484,4 +484,9 @@ export const createWhatsappTemplate = ({ payload }) => {
484
484
  export const getSenderDetails = (channel, orgUnitId) => {
485
485
  const url = `${CAMPAIGNS_API_ORG_ENDPOINT}/meta/domainProperties?channel=${channel}`;
486
486
  return request(url, getAPICallObject('GET', null, false, true, orgUnitId));
487
- };
487
+ };
488
+
489
+ export const getCdnTransformationConfig = () => {
490
+ const url = `${API_ENDPOINT}/common/getCdnTransformationConfig`;
491
+ return request(url, getAPICallObject('GET'));
492
+ }
@@ -0,0 +1,369 @@
1
+ import cloneDeep from "lodash/cloneDeep";
2
+ import isEmpty from "lodash/isEmpty";
3
+ import forOwn from "lodash/forOwn";
4
+ import isNumber from "lodash/isNumber";
5
+ /* eslint-disable no-unused-expressions */
6
+ import { parse } from "node-html-parser";
7
+
8
+ // example cdn url
9
+ // https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=100,height=100,quality=75,format=auto/intouch_creative_assets/005710a7-6412-44fe-a19f-e72456b1.jpg
10
+
11
+ /**
12
+ * regex tests presence of intouch_creative_assets in the url.
13
+ * We are not testing for extensions such as jpg/png because some channels like LINE - IMAGEMAP url doesn't contain extension.
14
+ */
15
+ const regex = /https:\/\/.*intouch_creative_assets.*$/;
16
+ const INTOUCH_CREATIVE_ASSETS = "intouch_creative_assets";
17
+ const SPECIFIED = "specified";
18
+ const FORMAT_TYPES = { AUTO: "auto" };
19
+ const QUALITY_TYPE = {
20
+ DECREASE: "decrease",
21
+ PRESERVE: "preserve",
22
+ };
23
+ const CREATIVES_CDN_BASE_URL_KEY = 'CREATIVES_CDN_BASE_URL';
24
+ const CREATIVES_CDN_QUALITY_CONFIG_KEY = 'CREATIVES_CDN_QUALITY_CONFIG';
25
+ const CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY = 'CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX';
26
+
27
+
28
+ /**
29
+ * CHANNEL_CONFIGS maintains the transformation details for each channel (and optionally subchannel ex. FACEBOOK/LINE)
30
+ * In case of SUBCHANNELS, they are nested inside CHANNEL.
31
+ */
32
+ const CHANNEL_CONFIGS = {
33
+ EMAIL: {
34
+ transformations: {
35
+ width: SPECIFIED,
36
+ height: SPECIFIED,
37
+ format: FORMAT_TYPES.AUTO,
38
+ quality: QUALITY_TYPE.DECREASE,
39
+ },
40
+ },
41
+ RCS: {
42
+ transformations: {
43
+ width: 1440,
44
+ height: 720,
45
+ quality: QUALITY_TYPE.DECREASE,
46
+ },
47
+ },
48
+ VIBER: {
49
+ transformations: {
50
+ width: 300,
51
+ height: 400,
52
+ quality: QUALITY_TYPE.DECREASE,
53
+ },
54
+ },
55
+ WHATSAPP: {
56
+ transformations: {
57
+ quality: QUALITY_TYPE.DECREASE, //TODO whatsapp itself might reduce quality. To be checked
58
+ },
59
+ },
60
+ MOBILE_PUSH: {
61
+ transformations: {
62
+ quality: QUALITY_TYPE.DECREASE,
63
+ },
64
+ },
65
+ FACEBOOK: {
66
+ IMAGE: {
67
+ transformations: {
68
+ width: 1080,
69
+ height: 1080,
70
+ quality: QUALITY_TYPE.DECREASE,
71
+ },
72
+ }
73
+ },
74
+ LINE: {
75
+ IMAGE: {
76
+ transformations: {
77
+ width: 300,
78
+ height: 400,
79
+ quality: QUALITY_TYPE.DECREASE,
80
+ },
81
+ },
82
+ CARD: {
83
+ transformations: {
84
+ width: 1024,
85
+ height: 1024,
86
+ quality: QUALITY_TYPE.DECREASE,
87
+ },
88
+ },
89
+ RICH_MESSAGE: {
90
+ //Rich Message in Line has Square and Custom templates.
91
+ //Width (1040px) is same in both whereas height varies. Hence including width only.
92
+ transformations: {
93
+ width: 1040,
94
+ quality: QUALITY_TYPE.DECREASE,
95
+ },
96
+ }
97
+ },
98
+ WECHAT: {
99
+ transformations: {
100
+ quality: QUALITY_TYPE.DECREASE,
101
+ }
102
+ }
103
+ };
104
+
105
+ /**
106
+ *
107
+ * @param {string} url : required
108
+ * @param {number} height : optional | required when height needs to be manually specified for channels such as email.
109
+ * @param {number} width : optional | required when width needs to be manually specified for channels such as email.
110
+ * @param {string} channelName : required
111
+ * @param {string} channelSubType : optional | required when channel has multiple subtypes
112
+ *
113
+ * @returns
114
+ * getCdnUrl : utility function to replace and return s3/cdn url with cdn url.
115
+ * Step 1. Basic validations:
116
+ * a) If data type of cdn items stored in local storage is incorrect(maybe as a result of manual modification), the received url in param is returned.
117
+ * b) If cdn local storage items are emtpy then received url in param is returned.
118
+ * c) If url doesn't match with regex.
119
+ * d) Channel/Subchannel not present in CHANNEL_CONFIGS
120
+ * e) No quality and default quality mentioned in CHANNEL_CONFIGS for given channel.
121
+ * f) If channels quality is non numeric(maybe as a result of manual modification) then quality=100 is returned.
122
+ *
123
+ * Step 2. The CHANNEL_CONFIGS determine the applicableTransformations.
124
+ * Step 3. If applicable transformations are present `CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX` is appended to cdn url and transformation properties are added.
125
+ * Step 4. The original s3 file name is appened at the end of the url to get complete cdn url and value is returned.
126
+ *
127
+ * Error handling:
128
+ * In case of any unknown exception, url received in param is returned.
129
+ */
130
+ export const getCdnUrl = ({
131
+ url,
132
+ height,
133
+ width,
134
+ channelName: receivedChannelName,
135
+ channelSubType: receivedChannelSubType,
136
+ }) => {
137
+ try{
138
+ const channelName = receivedChannelName?.toUpperCase();
139
+ const channelSubType = receivedChannelSubType?.toUpperCase();
140
+
141
+ const CREATIVES_CDN_BASE_URL = getLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
142
+ const CREATIVES_CDN_QUALITY_CONFIG = getLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY, true);
143
+ const CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX = getLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
144
+
145
+ //Basic validations on data stored in local storage.
146
+ if (
147
+ typeof CREATIVES_CDN_QUALITY_CONFIG !== "object" ||
148
+ Array.isArray(CREATIVES_CDN_QUALITY_CONFIG) ||
149
+ CREATIVES_CDN_QUALITY_CONFIG === null ||
150
+ typeof CREATIVES_CDN_BASE_URL !== "string" ||
151
+ typeof CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX !== "string"
152
+ )
153
+ return url;
154
+
155
+ const QUALITY = CREATIVES_CDN_QUALITY_CONFIG?.[channelName] || CREATIVES_CDN_QUALITY_CONFIG?.DEFAULT;
156
+
157
+ if (
158
+ !regex.test(url) ||
159
+ !CHANNEL_CONFIGS?.[channelName] ||
160
+ (channelSubType && !CHANNEL_CONFIGS?.[channelName]?.[channelSubType]) ||
161
+ !CREATIVES_CDN_BASE_URL ||
162
+ !CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX ||
163
+ !QUALITY
164
+ )
165
+ return url;
166
+
167
+ const [, assetKey] = url.split(INTOUCH_CREATIVE_ASSETS);
168
+
169
+ let newUrl = `${CREATIVES_CDN_BASE_URL}`;
170
+
171
+ let applicableTransformations;
172
+
173
+ if (channelSubType) {
174
+ applicableTransformations =
175
+ CHANNEL_CONFIGS?.[channelName]?.[channelSubType]?.transformations;
176
+ } else {
177
+ applicableTransformations = CHANNEL_CONFIGS?.[channelName]?.transformations;
178
+ }
179
+
180
+ if (!isEmpty(applicableTransformations)) {
181
+
182
+ newUrl += `/${CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX}/`;
183
+
184
+ // Object.keys(applicableTransformations)?.forEach((transformationParam) => {
185
+ forOwn(applicableTransformations, (transformationParamVal, transformationParam)=>{
186
+ try {
187
+ switch (transformationParam) {
188
+ case "height":
189
+ if (transformationParamVal === SPECIFIED && height) {
190
+ newUrl += `${transformationParam}=${height},`;
191
+ } else if(transformationParamVal !== SPECIFIED && isNumber(transformationParamVal)) {
192
+ newUrl += `${transformationParam}=${transformationParamVal},`;
193
+ }
194
+ break;
195
+ case "width":
196
+ if (transformationParamVal === SPECIFIED && width) {
197
+ newUrl += `${transformationParam}=${width},`;
198
+ } else if(transformationParamVal !== SPECIFIED && isNumber(transformationParamVal)) {
199
+ newUrl += `${transformationParam}=${transformationParamVal},`;
200
+ }
201
+ break;
202
+ case "format":
203
+ if(transformationParamVal){
204
+ newUrl += `${transformationParam}=${transformationParamVal},`;
205
+ }
206
+ break;
207
+ case "quality":
208
+ if (transformationParamVal === QUALITY_TYPE?.DECREASE && QUALITY && typeof QUALITY === "number") {
209
+ newUrl += `${transformationParam}=${QUALITY},`;
210
+ } else {
211
+ newUrl += `${transformationParam}=100,`;
212
+ }
213
+ break;
214
+ default:
215
+ break;
216
+ }
217
+ } catch (e) {
218
+ console.log("some error occured while applying transformation",e);
219
+ }
220
+ })
221
+ }
222
+ newUrl += `/${INTOUCH_CREATIVE_ASSETS}${assetKey}`;
223
+ return newUrl;
224
+
225
+ }catch(e){
226
+ console.log('Some exception occurred in getCdnUrl',e);
227
+ return url;
228
+ }
229
+ };
230
+
231
+ /**
232
+ *
233
+ * @param {string} htmlContents accepts htmlContents - strigified.
234
+ * @returns htmlContents with updated urls in image tag
235
+ */
236
+ export const updateImagesInHtml = (htmlContents) => {
237
+ const copiedHtmlContents = htmlContents;
238
+ try {
239
+ const root = parse(htmlContents);
240
+ root.querySelectorAll("img").forEach((element) => {
241
+ const imageSrc = element.getAttribute("src");
242
+ const isAsset = regex.test(imageSrc);
243
+ if (isAsset) {
244
+ const height = element.getAttribute("height");
245
+ const width = element.getAttribute("width");
246
+ const newUrl = getCdnUrl({
247
+ url: imageSrc,
248
+ height,
249
+ width,
250
+ channelName: "EMAIL",
251
+ });
252
+ element.setAttribute("src", newUrl);
253
+ }
254
+ });
255
+ return root.toString();
256
+ } catch (e) {
257
+ console.log("An error occured while updating images in html", e);
258
+ return copiedHtmlContents; //return received input directly in case an exception is thrown
259
+ }
260
+ };
261
+
262
+ /**
263
+ *
264
+ * @param {*} contents
265
+ * @returns
266
+ * Transforms the email template's html by images replacing the s3 url / cdn url with cdn url.
267
+ */
268
+ export const transformEmailTemplates = (contents) => {
269
+ const contentsCopy = cloneDeep(contents);
270
+ try {
271
+ const base = contentsCopy?.versions?.base;
272
+ const languages = base?.selectedLanguages;
273
+ languages?.forEach((lang) => {
274
+ const htmlContents = base?.[lang]?.["template-content"];
275
+ const newHtmlContents = updateImagesInHtml(htmlContents);
276
+ base[lang]["template-content"] = newHtmlContents;
277
+ });
278
+ return contentsCopy;
279
+ } catch (e) {
280
+ console.log("Some error has occured while transforming email templates", e);
281
+ return contents; //return received input directly in case an exception is thrown
282
+ }
283
+ };
284
+
285
+
286
+ /**
287
+ *
288
+ * @param {*} key
289
+ * @param {*} parse
290
+ * @returns Localstorage item with mentioned key. Parses the value if second param `parse` is true.
291
+ */
292
+ export function getLocalStorageItem(key, parse = false){
293
+ const val = localStorage.getItem(key);
294
+ if(parse){
295
+ return JSON.parse(val);
296
+ }
297
+ return val;
298
+ }
299
+
300
+ /**
301
+ * Removes specified key from local storage.
302
+ */
303
+ export function removeLocalStorageItem(key) {
304
+ localStorage.removeItem(key);
305
+ return;
306
+ }
307
+
308
+ /**
309
+ * Util function. Removes all cdn related local storage items.
310
+ */
311
+ export function removeAllCdnLocalStorageItems() {
312
+ try{
313
+ removeLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
314
+ removeLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY);
315
+ removeLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
316
+ }catch(e){
317
+ console.log('some error occured while deleting all keys for cdn', e);
318
+ }
319
+ }
320
+
321
+ /**
322
+ *
323
+ * @param {*} configsResponse
324
+ * This util function saves the getCdnConfigs response into the local storage.
325
+ * The following response items are mapped into the following keys in local storage:
326
+ * hostname -> CREATIVES_CDN_BASE_URL
327
+ * qualityCfg ->CREATIVES_CDN_QUALITY_CONFIG
328
+ * transformationUrlSuffix -> CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX
329
+ *
330
+ * 1. If configsReponse is empty. All above mentioned keys are deleted from localstorage.
331
+ * 2. If any one of the above keys is missing. The respective keys are deleted from localstorage.
332
+ * 3. Else it is saved into the localstorage.
333
+ */
334
+ export function saveCdnConfigs(configsResponse) {
335
+ try {
336
+ //If no response from api, then remove localStorage items if they were set previously
337
+ if (isEmpty(configsResponse)) {
338
+ removeAllCdnLocalStorageItems();
339
+ return;
340
+ }
341
+
342
+ if (configsResponse?.hostname) {
343
+ localStorage.setItem(
344
+ CREATIVES_CDN_BASE_URL_KEY,
345
+ configsResponse?.hostname
346
+ );
347
+ } else {
348
+ removeLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
349
+ }
350
+
351
+ if (!isEmpty(configsResponse?.qualityCfg)) {
352
+ const qualityString = JSON.stringify(configsResponse?.qualityCfg);
353
+ localStorage.setItem(CREATIVES_CDN_QUALITY_CONFIG_KEY, qualityString);
354
+ } else {
355
+ removeLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY);
356
+ }
357
+
358
+ if (configsResponse?.transformationUrlSuffix) {
359
+ localStorage.setItem(
360
+ CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY,
361
+ configsResponse?.transformationUrlSuffix
362
+ );
363
+ } else {
364
+ removeLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
365
+ }
366
+ } catch (e) {
367
+ console.log("some error while setting localstorage cdn configs");
368
+ }
369
+ }