@capillarytech/creatives-library 7.13.5 → 7.13.8

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.
Files changed (34) hide show
  1. package/package.json +2 -1
  2. package/routes.js +1 -0
  3. package/services/api.js +6 -1
  4. package/utils/cdnTransformation.js +416 -0
  5. package/utils/tests/__snapshots__/cdnTransformation.test.js.snap +298 -0
  6. package/utils/tests/cdnTransformation.mockdata.js +301 -0
  7. package/utils/tests/cdnTransformation.test.js +287 -0
  8. package/v2Components/FormBuilder/index.js +18 -12
  9. package/v2Components/FormBuilder/tests/index.test.js +41 -0
  10. package/v2Components/FormBuilder/tests/mockData.js +120 -0
  11. package/v2Components/NewCallTask/index.js +3 -1
  12. package/v2Components/NewCallTask/tests/index.test.js +36 -0
  13. package/v2Components/NewCallTask/tests/mockData.js +20 -0
  14. package/v2Containers/CallTask/index.js +4 -2
  15. package/v2Containers/Cap/actions.js +0 -1
  16. package/v2Containers/CreativesContainer/actions.js +1 -1
  17. package/v2Containers/CreativesContainer/index.js +6 -1
  18. package/v2Containers/CreativesContainer/tests/index.test.js +4 -0
  19. package/v2Containers/Email/index.js +4 -1
  20. package/v2Containers/Facebook/Advertisement/index.js +4 -4
  21. package/v2Containers/Line/Container/Image/index.js +5 -4
  22. package/v2Containers/Line/Container/ImageCarousel/index.js +6 -5
  23. package/v2Containers/Line/Container/ImageMap/index.js +2 -1
  24. package/v2Containers/MobilePush/Create/index.js +5 -7
  25. package/v2Containers/MobilePush/Edit/index.js +4 -3
  26. package/v2Containers/MobilePush/initialSchema.js +4 -0
  27. package/v2Containers/Rcs/index.js +2 -1
  28. package/v2Containers/Templates/actions.js +6 -0
  29. package/v2Containers/Templates/constants.js +4 -0
  30. package/v2Containers/Templates/sagas.js +25 -0
  31. package/v2Containers/TemplatesV2/index.js +5 -0
  32. package/v2Containers/Viber/index.js +11 -2
  33. package/v2Containers/WeChat/RichmediaTemplates/Create/index.js +3 -1
  34. package/v2Containers/Whatsapp/index.js +4 -2
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.8",
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,416 @@
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
+ import Bugsnag from '@bugsnag/js';
6
+
7
+ /* eslint-disable no-unused-expressions */
8
+ import { parse } from "node-html-parser";
9
+ import { EMAIL } from '../v2Containers/CreativesContainer/constants';
10
+
11
+ // example cdn url
12
+ // 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
13
+
14
+ /**
15
+ * regex tests presence of intouch_creative_assets in the url.
16
+ * We are not testing for extensions such as jpg/png because some channels like LINE - IMAGEMAP url doesn't contain extension.
17
+ */
18
+ const SPECIFIED = "specified";
19
+ const FORMAT_TYPES = { AUTO: "auto" };
20
+ const QUALITY_TYPE = {
21
+ DECREASE: "decrease",
22
+ PRESERVE: "preserve",
23
+ };
24
+ const CREATIVES_CDN_BASE_URL_KEY = 'CREATIVES_CDN_BASE_URL';
25
+ const CREATIVES_S3_BUCKET_PATH_KEY = 'CREATIVES_S3_BUCKET_PATH';
26
+ const CREATIVES_CDN_QUALITY_CONFIG_KEY = 'CREATIVES_CDN_QUALITY_CONFIG';
27
+ const CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY = 'CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX';
28
+
29
+ const HTTPS_STR = "https://"
30
+ const ALLOWED_EXTENSIONS_STR = ".*(?:jpg|png|jpeg)$"
31
+ const ANY_STR = ".*"
32
+
33
+ /**
34
+ * CHANNEL_CONFIGS maintains the transformation details for each channel (and optionally subchannel ex. FACEBOOK/LINE)
35
+ * In case of SUBCHANNELS, they are nested inside CHANNEL.
36
+ */
37
+ const CHANNEL_CONFIGS = {
38
+ EMAIL: {
39
+ transformations: {
40
+ width: SPECIFIED,
41
+ height: SPECIFIED,
42
+ format: FORMAT_TYPES.AUTO,
43
+ quality: QUALITY_TYPE.DECREASE,
44
+ },
45
+ },
46
+ RCS: {
47
+ transformations: {
48
+ width: 1440,
49
+ height: 720,
50
+ quality: QUALITY_TYPE.DECREASE,
51
+ },
52
+ },
53
+ VIBER: {
54
+ transformations: {
55
+ width: 300,
56
+ height: 400,
57
+ quality: QUALITY_TYPE.DECREASE,
58
+ },
59
+ },
60
+ WHATSAPP: {
61
+ transformations: {
62
+ quality: QUALITY_TYPE.DECREASE, //TODO whatsapp itself might reduce quality. To be checked
63
+ },
64
+ },
65
+ MOBILE_PUSH: {
66
+ transformations: {
67
+ quality: QUALITY_TYPE.DECREASE,
68
+ },
69
+ },
70
+ FACEBOOK: {
71
+ IMAGE: {
72
+ transformations: {
73
+ width: 1080,
74
+ height: 1080,
75
+ quality: QUALITY_TYPE.DECREASE,
76
+ },
77
+ }
78
+ },
79
+ LINE: {
80
+ IMAGE: {
81
+ transformations: {
82
+ width: 300,
83
+ height: 400,
84
+ quality: QUALITY_TYPE.DECREASE,
85
+ },
86
+ },
87
+ CARD: {
88
+ transformations: {
89
+ width: 1024,
90
+ height: 1024,
91
+ quality: QUALITY_TYPE.DECREASE,
92
+ },
93
+ },
94
+ RICH_MESSAGE: {
95
+ //Rich Message in Line has Square and Custom templates.
96
+ //Width (1040px) is same in both whereas height varies. Hence including width only.
97
+ transformations: {
98
+ width: 1040,
99
+ quality: QUALITY_TYPE.DECREASE,
100
+ },
101
+ }
102
+ },
103
+ WECHAT: {
104
+ transformations: {
105
+ quality: QUALITY_TYPE.DECREASE,
106
+ }
107
+ }
108
+ };
109
+
110
+ /**
111
+ *
112
+ * @param {string} url : required
113
+ * @param {number} height : optional | required when height needs to be manually specified for channels such as email.
114
+ * @param {number} width : optional | required when width needs to be manually specified for channels such as email.
115
+ * @param {string} channelName : required
116
+ * @param {string} channelSubType : optional | required when channel has multiple subtypes
117
+ *
118
+ * @returns
119
+ * getCdnUrl : utility function to replace and return s3/cdn url with cdn url.
120
+ * Step 1. Basic validations:
121
+ * 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.
122
+ * b) If cdn local storage items are emtpy then received url in param is returned.
123
+ * c) If url doesn't match with regex.
124
+ * d) Channel/Subchannel not present in CHANNEL_CONFIGS
125
+ * e) No quality and default quality mentioned in CHANNEL_CONFIGS for given channel.
126
+ * f) If channels quality is non numeric(maybe as a result of manual modification) then quality=100 is returned.
127
+ *
128
+ * Step 2. The CHANNEL_CONFIGS determine the applicableTransformations.
129
+ * Step 3. If applicable transformations are present `CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX` is appended to cdn url and transformation properties are added.
130
+ * Step 4. The original s3 file name is appened at the end of the url to get complete cdn url and value is returned.
131
+ *
132
+ * Error handling:
133
+ * In case of any unknown exception, url received in param is returned.
134
+ */
135
+ export const getCdnUrl = ({
136
+ url,
137
+ height,
138
+ width,
139
+ channelName: receivedChannelName,
140
+ channelSubType: receivedChannelSubType,
141
+ }) => {
142
+ try{
143
+ const channelName = receivedChannelName?.toUpperCase();
144
+ const channelSubType = receivedChannelSubType?.toUpperCase();
145
+
146
+ const CREATIVES_CDN_BASE_URL = getLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
147
+ const CREATIVES_CDN_QUALITY_CONFIG = getLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY, true);
148
+ const CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX = getLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
149
+ let CREATIVES_S3_BUCKET_PATH = getLocalStorageItem(CREATIVES_S3_BUCKET_PATH_KEY);
150
+
151
+ //remove all leading and trailing forward slash with empty string, if leading/trailing forward slash is present in node env by mistake then url will not be correct hence removing them.
152
+ CREATIVES_S3_BUCKET_PATH = CREATIVES_S3_BUCKET_PATH?.replace(/^\/|\/$/g, '');
153
+
154
+ //Basic validations on data stored in local storage.
155
+ if (
156
+ typeof CREATIVES_CDN_QUALITY_CONFIG !== "object" ||
157
+ Array.isArray(CREATIVES_CDN_QUALITY_CONFIG) ||
158
+ CREATIVES_CDN_QUALITY_CONFIG === null ||
159
+ typeof CREATIVES_CDN_BASE_URL !== "string" ||
160
+ typeof CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX !== "string" ||
161
+ typeof CREATIVES_S3_BUCKET_PATH !== 'string'
162
+ )
163
+ return url;
164
+
165
+ const QUALITY = CREATIVES_CDN_QUALITY_CONFIG?.[channelName] || CREATIVES_CDN_QUALITY_CONFIG?.DEFAULT;
166
+
167
+ const regexWithExtension = new RegExp(HTTPS_STR + ANY_STR + CREATIVES_S3_BUCKET_PATH + ANY_STR + ALLOWED_EXTENSIONS_STR)
168
+ const regexWithoutExtension = new RegExp(HTTPS_STR + ANY_STR + CREATIVES_S3_BUCKET_PATH + ANY_STR);
169
+
170
+ let skipTransformations = true;
171
+
172
+ if (
173
+ !regexWithoutExtension.test(url) ||
174
+ !CHANNEL_CONFIGS?.[channelName] ||
175
+ (channelSubType && !CHANNEL_CONFIGS?.[channelName]?.[channelSubType]) ||
176
+ !CREATIVES_CDN_BASE_URL ||
177
+ !CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX ||
178
+ !QUALITY
179
+ )
180
+ return url;
181
+
182
+ //Skip transformations if extensions are not proper. Else consider transformations if available.
183
+ if(regexWithExtension.test(url)){
184
+ skipTransformations = false;
185
+ }
186
+
187
+ const [, assetKey] = url.split(CREATIVES_S3_BUCKET_PATH);
188
+
189
+ let newUrl = `${CREATIVES_CDN_BASE_URL}`;
190
+
191
+ let applicableTransformations;
192
+
193
+ if (channelSubType) {
194
+ applicableTransformations =
195
+ CHANNEL_CONFIGS?.[channelName]?.[channelSubType]?.transformations;
196
+ } else {
197
+ applicableTransformations = CHANNEL_CONFIGS?.[channelName]?.transformations;
198
+ }
199
+
200
+ if (!isEmpty(applicableTransformations) && !skipTransformations) {
201
+
202
+ newUrl += `/${CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX}/`;
203
+
204
+ // Object.keys(applicableTransformations)?.forEach((transformationParam) => {
205
+ forOwn(applicableTransformations, (transformationParamVal, transformationParam)=>{
206
+ try {
207
+ switch (transformationParam) {
208
+ case "height":
209
+ if (transformationParamVal === SPECIFIED && height) {
210
+ newUrl += `${transformationParam}=${height},`;
211
+ } else if(transformationParamVal !== SPECIFIED && isNumber(transformationParamVal)) {
212
+ newUrl += `${transformationParam}=${transformationParamVal},`;
213
+ }
214
+ break;
215
+ case "width":
216
+ if (transformationParamVal === SPECIFIED && width) {
217
+ newUrl += `${transformationParam}=${width},`;
218
+ } else if(transformationParamVal !== SPECIFIED && isNumber(transformationParamVal)) {
219
+ newUrl += `${transformationParam}=${transformationParamVal},`;
220
+ }
221
+ break;
222
+ case "format":
223
+ if(transformationParamVal){
224
+ newUrl += `${transformationParam}=${transformationParamVal},`;
225
+ }
226
+ break;
227
+ case "quality":
228
+ if (transformationParamVal === QUALITY_TYPE?.DECREASE && QUALITY && typeof QUALITY === "number") {
229
+ newUrl += `${transformationParam}=${QUALITY},`;
230
+ } else {
231
+ newUrl += `${transformationParam}=100,`;
232
+ }
233
+ break;
234
+ default:
235
+ break;
236
+ }
237
+ } catch (e) {
238
+ Bugsnag.leaveBreadcrumb("some error occured while applying transformation")
239
+ Bugsnag.notify(e, (event) => {
240
+ event.severity = "error";
241
+ });
242
+ }
243
+ })
244
+ }
245
+ newUrl += `/${CREATIVES_S3_BUCKET_PATH}${assetKey}`;
246
+ return newUrl;
247
+
248
+ }catch(e){
249
+ Bugsnag.leaveBreadcrumb("Some exception occurred in getCdnUrl");
250
+ Bugsnag.notify(e, (event) => {
251
+ event.severity = "error";
252
+ });
253
+ return url;
254
+ }
255
+ };
256
+
257
+ /**
258
+ *
259
+ * @param {string} htmlContents accepts htmlContents - strigified.
260
+ * @returns htmlContents with updated urls in image tag
261
+ */
262
+ export const updateImagesInHtml = (htmlContents) => {
263
+ const copiedHtmlContents = htmlContents;
264
+ try {
265
+ const root = parse(htmlContents);
266
+ root?.querySelectorAll("img")?.forEach((element) => {
267
+ const imageSrc = element.getAttribute("src");
268
+ const height = element.getAttribute("height");
269
+ const width = element.getAttribute("width");
270
+ const newUrl = getCdnUrl({
271
+ url: imageSrc,
272
+ height,
273
+ width,
274
+ channelName: EMAIL,
275
+ });
276
+ element.setAttribute("src", newUrl);
277
+ });
278
+ return root.toString();
279
+ } catch (e) {
280
+ Bugsnag.leaveBreadcrumb("An error occured while updating images in html")
281
+ Bugsnag.notify(e, (event) => {
282
+ event.severity = "error";
283
+ });
284
+ return copiedHtmlContents; //return received input directly in case an exception is thrown
285
+ }
286
+ };
287
+
288
+ /**
289
+ *
290
+ * @param {*} contents
291
+ * @returns
292
+ * Transforms the email template's html by images replacing the s3 url / cdn url with cdn url.
293
+ */
294
+ export const transformEmailTemplates = (contents) => {
295
+ const contentsCopy = cloneDeep(contents);
296
+ try {
297
+ const base = contentsCopy?.versions?.base;
298
+ const languages = base?.selectedLanguages;
299
+ languages?.forEach((lang) => {
300
+ const htmlContents = base?.[lang]?.["template-content"];
301
+ const newHtmlContents = updateImagesInHtml(htmlContents);
302
+ base[lang]["template-content"] = newHtmlContents;
303
+ });
304
+ return contentsCopy;
305
+ } catch (e) {
306
+ Bugsnag.leaveBreadcrumb(
307
+ "Some error has occured while transforming email templates"
308
+ );
309
+ Bugsnag.notify(e, (event) => {
310
+ event.severity = "error";
311
+ });
312
+ return contents; //return received input directly in case an exception is thrown
313
+ }
314
+ };
315
+
316
+
317
+ /**
318
+ *
319
+ * @param {*} key
320
+ * @param {*} parse
321
+ * @returns Localstorage item with mentioned key. Parses the value if second param `parse` is true.
322
+ */
323
+ export function getLocalStorageItem(key, parse = false){
324
+ const val = localStorage.getItem(key);
325
+ if(parse){
326
+ return JSON.parse(val);
327
+ }
328
+ return val;
329
+ }
330
+
331
+ /**
332
+ * Removes specified key from local storage.
333
+ */
334
+ export function removeLocalStorageItem(key) {
335
+ localStorage.removeItem(key);
336
+ return;
337
+ }
338
+
339
+ /**
340
+ * Util function. Removes all cdn related local storage items.
341
+ */
342
+ export function removeAllCdnLocalStorageItems() {
343
+ try{
344
+ removeLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
345
+ removeLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY);
346
+ removeLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
347
+ removeLocalStorageItem(CREATIVES_S3_BUCKET_PATH_KEY);
348
+ }catch(e){
349
+ Bugsnag.leaveBreadcrumb("some error occured while deleting all keys for cdn");
350
+ Bugsnag.notify(e, (event) => {
351
+ event.severity = "error";
352
+ });
353
+ }
354
+ }
355
+
356
+ /**
357
+ *
358
+ * @param {*} configsResponse
359
+ * This util function saves the getCdnConfigs response into the local storage.
360
+ * The following response items are mapped into the following keys in local storage:
361
+ * hostname -> CREATIVES_CDN_BASE_URL
362
+ * qualityCfg ->CREATIVES_CDN_QUALITY_CONFIG
363
+ * transformationUrlSuffix -> CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX
364
+ *
365
+ * 1. If configsReponse is empty. All above mentioned keys are deleted from localstorage.
366
+ * 2. If any one of the above keys is missing. The respective keys are deleted from localstorage.
367
+ * 3. Else it is saved into the localstorage.
368
+ */
369
+ export function saveCdnConfigs(configsResponse) {
370
+ try {
371
+ //If no response from api, then remove localStorage items if they were set previously
372
+ if (isEmpty(configsResponse)) {
373
+ removeAllCdnLocalStorageItems();
374
+ return;
375
+ }
376
+
377
+ if (configsResponse?.hostname) {
378
+ localStorage.setItem(
379
+ CREATIVES_CDN_BASE_URL_KEY,
380
+ configsResponse?.hostname
381
+ );
382
+ } else {
383
+ removeLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
384
+ }
385
+
386
+ if (configsResponse?.bucketPath) {
387
+ localStorage.setItem(
388
+ CREATIVES_S3_BUCKET_PATH_KEY,
389
+ configsResponse?.bucketPath
390
+ );
391
+ } else {
392
+ removeLocalStorageItem(CREATIVES_S3_BUCKET_PATH_KEY);
393
+ }
394
+
395
+ if (!isEmpty(configsResponse?.qualityCfg)) {
396
+ const qualityString = JSON.stringify(configsResponse?.qualityCfg);
397
+ localStorage.setItem(CREATIVES_CDN_QUALITY_CONFIG_KEY, qualityString);
398
+ } else {
399
+ removeLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY);
400
+ }
401
+
402
+ if (configsResponse?.transformationUrlSuffix) {
403
+ localStorage.setItem(
404
+ CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY,
405
+ configsResponse?.transformationUrlSuffix
406
+ );
407
+ } else {
408
+ removeLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
409
+ }
410
+ } catch (e) {
411
+ Bugsnag.leaveBreadcrumb("some error while setting localstorage cdn configs");
412
+ Bugsnag.notify(e, (event) => {
413
+ event.severity = "error";
414
+ });
415
+ }
416
+ }