@capillarytech/creatives-library 7.12.114 → 7.12.116

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.12.114",
4
+ "version": "7.12.116",
5
5
  "description": "Capillary creatives ui",
6
6
  "main": "./index.js",
7
7
  "module": "./index.es.js",
@@ -2,8 +2,11 @@ import cloneDeep from "lodash/cloneDeep";
2
2
  import isEmpty from "lodash/isEmpty";
3
3
  import forOwn from "lodash/forOwn";
4
4
  import isNumber from "lodash/isNumber";
5
+ import Bugsnag from '@bugsnag/js';
6
+
5
7
  /* eslint-disable no-unused-expressions */
6
8
  import { parse } from "node-html-parser";
9
+ import { EMAIL } from '../v2Containers/CreativesContainer/constants';
7
10
 
8
11
  // example cdn url
9
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
@@ -12,8 +15,6 @@ import { parse } from "node-html-parser";
12
15
  * regex tests presence of intouch_creative_assets in the url.
13
16
  * We are not testing for extensions such as jpg/png because some channels like LINE - IMAGEMAP url doesn't contain extension.
14
17
  */
15
- const regex = /https:\/\/.*intouch_creative_assets.*$/;
16
- const INTOUCH_CREATIVE_ASSETS = "intouch_creative_assets";
17
18
  const SPECIFIED = "specified";
18
19
  const FORMAT_TYPES = { AUTO: "auto" };
19
20
  const QUALITY_TYPE = {
@@ -21,9 +22,13 @@ const QUALITY_TYPE = {
21
22
  PRESERVE: "preserve",
22
23
  };
23
24
  const CREATIVES_CDN_BASE_URL_KEY = 'CREATIVES_CDN_BASE_URL';
25
+ const CREATIVES_S3_BUCKET_PATH_KEY = 'CREATIVES_S3_BUCKET_PATH';
24
26
  const CREATIVES_CDN_QUALITY_CONFIG_KEY = 'CREATIVES_CDN_QUALITY_CONFIG';
25
27
  const CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY = 'CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX';
26
28
 
29
+ const HTTPS_STR = "https://"
30
+ const ALLOWED_EXTENSIONS_STR = ".*(?:jpg|png|jpeg)$"
31
+ const ANY_STR = ".*"
27
32
 
28
33
  /**
29
34
  * CHANNEL_CONFIGS maintains the transformation details for each channel (and optionally subchannel ex. FACEBOOK/LINE)
@@ -141,21 +146,31 @@ export const getCdnUrl = ({
141
146
  const CREATIVES_CDN_BASE_URL = getLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
142
147
  const CREATIVES_CDN_QUALITY_CONFIG = getLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY, true);
143
148
  const CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX = getLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
144
-
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
+
145
154
  //Basic validations on data stored in local storage.
146
155
  if (
147
156
  typeof CREATIVES_CDN_QUALITY_CONFIG !== "object" ||
148
157
  Array.isArray(CREATIVES_CDN_QUALITY_CONFIG) ||
149
158
  CREATIVES_CDN_QUALITY_CONFIG === null ||
150
159
  typeof CREATIVES_CDN_BASE_URL !== "string" ||
151
- typeof CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX !== "string"
160
+ typeof CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX !== "string" ||
161
+ typeof CREATIVES_S3_BUCKET_PATH !== 'string'
152
162
  )
153
163
  return url;
154
164
 
155
165
  const QUALITY = CREATIVES_CDN_QUALITY_CONFIG?.[channelName] || CREATIVES_CDN_QUALITY_CONFIG?.DEFAULT;
156
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
+
157
172
  if (
158
- !regex.test(url) ||
173
+ !regexWithoutExtension.test(url) ||
159
174
  !CHANNEL_CONFIGS?.[channelName] ||
160
175
  (channelSubType && !CHANNEL_CONFIGS?.[channelName]?.[channelSubType]) ||
161
176
  !CREATIVES_CDN_BASE_URL ||
@@ -163,8 +178,13 @@ export const getCdnUrl = ({
163
178
  !QUALITY
164
179
  )
165
180
  return url;
166
-
167
- const [, assetKey] = url.split(INTOUCH_CREATIVE_ASSETS);
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);
168
188
 
169
189
  let newUrl = `${CREATIVES_CDN_BASE_URL}`;
170
190
 
@@ -177,7 +197,7 @@ export const getCdnUrl = ({
177
197
  applicableTransformations = CHANNEL_CONFIGS?.[channelName]?.transformations;
178
198
  }
179
199
 
180
- if (!isEmpty(applicableTransformations)) {
200
+ if (!isEmpty(applicableTransformations) && !skipTransformations) {
181
201
 
182
202
  newUrl += `/${CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX}/`;
183
203
 
@@ -215,15 +235,21 @@ export const getCdnUrl = ({
215
235
  break;
216
236
  }
217
237
  } catch (e) {
218
- console.log("some error occured while applying transformation",e);
238
+ Bugsnag.leaveBreadcrumb("some error occured while applying transformation")
239
+ Bugsnag.notify(e, (event) => {
240
+ event.severity = "error";
241
+ });
219
242
  }
220
243
  })
221
244
  }
222
- newUrl += `/${INTOUCH_CREATIVE_ASSETS}${assetKey}`;
245
+ newUrl += `/${CREATIVES_S3_BUCKET_PATH}${assetKey}`;
223
246
  return newUrl;
224
247
 
225
248
  }catch(e){
226
- console.log('Some exception occurred in getCdnUrl',e);
249
+ Bugsnag.leaveBreadcrumb("Some exception occurred in getCdnUrl");
250
+ Bugsnag.notify(e, (event) => {
251
+ event.severity = "error";
252
+ });
227
253
  return url;
228
254
  }
229
255
  };
@@ -237,24 +263,24 @@ export const updateImagesInHtml = (htmlContents) => {
237
263
  const copiedHtmlContents = htmlContents;
238
264
  try {
239
265
  const root = parse(htmlContents);
240
- root.querySelectorAll("img").forEach((element) => {
266
+ root?.querySelectorAll("img")?.forEach((element) => {
241
267
  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
- }
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);
254
277
  });
255
278
  return root.toString();
256
279
  } catch (e) {
257
- console.log("An error occured while updating images in html", e);
280
+ Bugsnag.leaveBreadcrumb("An error occured while updating images in html")
281
+ Bugsnag.notify(e, (event) => {
282
+ event.severity = "error";
283
+ });
258
284
  return copiedHtmlContents; //return received input directly in case an exception is thrown
259
285
  }
260
286
  };
@@ -277,7 +303,12 @@ export const transformEmailTemplates = (contents) => {
277
303
  });
278
304
  return contentsCopy;
279
305
  } catch (e) {
280
- console.log("Some error has occured while transforming email templates", e);
306
+ Bugsnag.leaveBreadcrumb(
307
+ "Some error has occured while transforming email templates"
308
+ );
309
+ Bugsnag.notify(e, (event) => {
310
+ event.severity = "error";
311
+ });
281
312
  return contents; //return received input directly in case an exception is thrown
282
313
  }
283
314
  };
@@ -313,8 +344,12 @@ export function removeAllCdnLocalStorageItems() {
313
344
  removeLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
314
345
  removeLocalStorageItem(CREATIVES_CDN_QUALITY_CONFIG_KEY);
315
346
  removeLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
347
+ removeLocalStorageItem(CREATIVES_S3_BUCKET_PATH_KEY);
316
348
  }catch(e){
317
- console.log('some error occured while deleting all keys for cdn', e);
349
+ Bugsnag.leaveBreadcrumb("some error occured while deleting all keys for cdn");
350
+ Bugsnag.notify(e, (event) => {
351
+ event.severity = "error";
352
+ });
318
353
  }
319
354
  }
320
355
 
@@ -348,6 +383,15 @@ export function saveCdnConfigs(configsResponse) {
348
383
  removeLocalStorageItem(CREATIVES_CDN_BASE_URL_KEY);
349
384
  }
350
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
+
351
395
  if (!isEmpty(configsResponse?.qualityCfg)) {
352
396
  const qualityString = JSON.stringify(configsResponse?.qualityCfg);
353
397
  localStorage.setItem(CREATIVES_CDN_QUALITY_CONFIG_KEY, qualityString);
@@ -364,6 +408,9 @@ export function saveCdnConfigs(configsResponse) {
364
408
  removeLocalStorageItem(CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX_KEY);
365
409
  }
366
410
  } catch (e) {
367
- console.log("some error while setting localstorage cdn configs");
411
+ Bugsnag.leaveBreadcrumb("some error while setting localstorage cdn configs");
412
+ Bugsnag.notify(e, (event) => {
413
+ event.severity = "error";
414
+ });
368
415
  }
369
416
  }
@@ -5,6 +5,7 @@ beforeEach(() => {
5
5
  var localStorageMock = (function () {
6
6
  var store = {
7
7
  CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX: "cdn-cgi/image",
8
+ CREATIVES_S3_BUCKET_PATH: "intouch_creative_assets",
8
9
  CREATIVES_CDN_BASE_URL: "https://storage.crm.n.content-cdn.io",
9
10
  CREATIVES_CDN_QUALITY_CONFIG:
10
11
  '{"EMAIL":75,"RCS":75,"VIBER":75,"WHATSAPP":75,"MOBILE_PUSH":75,"FACEBOOK":75,"LINE":75,"DEFAULT":75}',
@@ -32,6 +33,9 @@ afterEach(() => {
32
33
  "CREATIVES_CDN_QUALITY_CONFIG",
33
34
  '{"EMAIL":75,"RCS":75,"VIBER":75,"WHATSAPP":75,"MOBILE_PUSH":75,"FACEBOOK":75,"LINE":75,"DEFAULT":75}'
34
35
  );
36
+ localStorage.setItem(
37
+ "CREATIVES_S3_BUCKET_PATH", "intouch_creative_assets"
38
+ );
35
39
  });
36
40
 
37
41
  describe("cdnTransformationTests", () => {
@@ -83,6 +87,44 @@ describe("cdnTransformationTests", () => {
83
87
  expect(GENERATED_CDN_URL).toEqual(EXPECTED_CDN_URL);
84
88
  });
85
89
 
90
+ it("Should replace s3 url with cdn url for email when bucket name is other than intouch_creatives_assets in case of prod.", () => {
91
+ localStorage.setItem("CREATIVES_S3_BUCKET_PATH", "fileservice.in/intouch_creative_assets");
92
+ const TEST_S3_URL = `https://crm-nightly-new-fileservice.s3.amazonaws.com/fileservice.in/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.jpg`;
93
+ const GENERATED_CDN_URL = cdnUtils.getCdnUrl({
94
+ url: TEST_S3_URL,
95
+ channelName: "EMAIL",
96
+ height: 100,
97
+ width: 200,
98
+ });
99
+ const EXPECTED_CDN_URL = `https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=200,height=100,format=auto,quality=75,/fileservice.in/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.jpg`;
100
+ expect(GENERATED_CDN_URL).toEqual(EXPECTED_CDN_URL);
101
+ });
102
+
103
+ it("Should replace s3 url with cdn url for email when bucket name is other than intouch_creatives_assets in case of prod. It should remove leading/trailing forward slashes from bucket path", () => {
104
+ localStorage.setItem("CREATIVES_S3_BUCKET_PATH", "/fileservice.in/intouch_creative_assets/");
105
+ const TEST_S3_URL = `https://crm-nightly-new-fileservice.s3.amazonaws.com/fileservice.in/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.jpg`;
106
+ const GENERATED_CDN_URL = cdnUtils.getCdnUrl({
107
+ url: TEST_S3_URL,
108
+ channelName: "EMAIL",
109
+ height: 100,
110
+ width: 200,
111
+ });
112
+ const EXPECTED_CDN_URL = `https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=200,height=100,format=auto,quality=75,/fileservice.in/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.jpg`;
113
+ expect(GENERATED_CDN_URL).toEqual(EXPECTED_CDN_URL);
114
+ });
115
+
116
+ it("Should replace s3 url with cdn url for email without transformation for formats other than jpg/png/jpeg", () => {
117
+ const TEST_S3_URL = `https://crm-nightly-new-fileservice.s3.amazonaws.com/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.avif`; //this is random url
118
+ const GENERATED_CDN_URL = cdnUtils.getCdnUrl({
119
+ url: TEST_S3_URL,
120
+ channelName: "EMAIL",
121
+ height: 100,
122
+ width: 200,
123
+ });
124
+ const EXPECTED_CDN_URL = `https://storage.crm.n.content-cdn.io/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.avif`;
125
+ expect(GENERATED_CDN_URL).toEqual(EXPECTED_CDN_URL);
126
+ });
127
+
86
128
  it("should replace cdn url with new cdn url for email incase h/w etc changed", () => {
87
129
  const TEST_S3_URL = `https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=200,height=100,format=auto,quality=75,/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8.jpg`;
88
130
  const GENERATED_CDN_URL = cdnUtils.getCdnUrl({
@@ -95,14 +137,14 @@ describe("cdnTransformationTests", () => {
95
137
  expect(GENERATED_CDN_URL).toEqual(EXPECTED_CDN_URL);
96
138
  });
97
139
 
98
- it("should replace cdn url with new cdn url incase channelName is LINE and channelSubType is RICH_MESSAGE ", () => {
140
+ it("should replace cdn url with new cdn url incase channelName is LINE and channelSubType is RICH_MESSAGE without transformation", () => {
99
141
  const TEST_S3_URL = `https://crm-nightly-new-fileservice.s3.amazonaws.com/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8`;
100
142
  const GENERATED_CDN_URL = cdnUtils.getCdnUrl({
101
143
  url: TEST_S3_URL,
102
144
  channelName: "LINE",
103
145
  channelSubType: "RICH_MESSAGE",
104
146
  });
105
- const EXPECTED_CDN_URL = `https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=1040,quality=75,/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8`;
147
+ const EXPECTED_CDN_URL = `https://storage.crm.n.content-cdn.io/intouch_creative_assets/2c9233ed-0959-4aff-b749-9171b5c8`;
106
148
  expect(GENERATED_CDN_URL).toEqual(EXPECTED_CDN_URL);
107
149
  });
108
150
 
@@ -1,18 +0,0 @@
1
- /**
2
- * This is not an integration test rather it's created to run the integration test script for now.
3
- * The tests case and file name will be re-written as per the requirement in future.
4
- */
5
-
6
- describe('Create Templates ', () => {
7
- it('Should pass the test case for truthy values', async () => {
8
- expect(true).toBeTruthy();
9
- });
10
-
11
- it('Should pass the test case for falsy values', async () => {
12
- expect(false).toBeFalsy();
13
- });
14
-
15
- it('Should pass the test case for falsy values', async () => {
16
- expect({}).toEqual({});
17
- });
18
- });