@capillarytech/creatives-library 7.17.108-alpha.0 → 7.17.109

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.17.108-alpha.0",
4
+ "version": "7.17.109",
5
5
  "description": "Capillary creatives ui",
6
6
  "main": "./index.js",
7
7
  "module": "./index.es.js",
package/services/api.js CHANGED
@@ -84,7 +84,7 @@ function checkStatus(response) {
84
84
  statusCode: status,
85
85
  });
86
86
  }
87
- if ((status >= 200 && status < 300) || [500, 409, 413].includes(response.status)) {
87
+ if ((status >= 200 && status < 300) || [400, 409, 413, 500].includes(response.status)) {
88
88
  return response;
89
89
  }
90
90
  const isLoginPage = window.location.pathname.indexOf('/login') !== -1;
@@ -1,5 +1,7 @@
1
1
  import { take, cancel, call, put, takeLatest } from 'redux-saga/effects';
2
2
  import { LOCATION_CHANGE } from 'react-router-redux';
3
+ import isEmpty from 'lodash/isEmpty';
4
+ import get from 'lodash/get';
3
5
  import * as Api from '../../services/api';
4
6
  import {
5
7
  TEMPLATE_CREATE_REQUEST,
@@ -44,7 +46,19 @@ export function* sendForApprovalCreate({ payload, callback, gupshupMediaFile })
44
46
  try {
45
47
  const result = yield call(Api.createWhatsappTemplate, { payload, gupshupMediaFile });
46
48
  if (result?.status?.code && result.status.code >= 400) {
47
- errorMsg = result?.message || result.status.code;
49
+ const { message, status } = result || {};
50
+ const errorMessageObj = get(message?.error, 'errorMessage', {});
51
+ if (!isEmpty(errorMessageObj)) {
52
+ try {
53
+ // If the errorMessageObj is plain JS and JSON.parse fails, catch block will assign the error msg
54
+ const parsedErrorMsgObj = JSON.parse(errorMessageObj);
55
+ errorMsg = parsedErrorMsgObj?.error?.message;
56
+ } catch (error) {
57
+ errorMsg = message;
58
+ }
59
+ } else {
60
+ errorMsg = message || status.code;
61
+ }
48
62
  throw errorMsg;
49
63
  }
50
64
  yield put({
@@ -251,4 +251,75 @@ export const mockData = {
251
251
  description: 'sample',
252
252
  url: "https://capillarytech.com",
253
253
  },
254
+ sendForApprovalCreatePayload: {
255
+ name: 'dffdf',
256
+ versions: {
257
+ base: {
258
+ content: {
259
+ whatsapp: {
260
+ category: 'UTILITY',
261
+ languages: [
262
+ {
263
+ language: 'sq',
264
+ content: 'fdfd',
265
+ },
266
+ ],
267
+ buttonType: 'CTA',
268
+ buttons: [
269
+ {
270
+ index: 0,
271
+ type: 'PHONE_NUMBER',
272
+ text: 'tt',
273
+ phone_number: '+913333333333333',
274
+ },
275
+ ],
276
+ mediaType: 'TEXT',
277
+ varMapped: {},
278
+ templateEditor: false,
279
+ accountId: '123456789',
280
+ accessToken: '12345',
281
+ hostName: 'karixwhatsappbulk',
282
+ whatsappMedia: {
283
+ header: '',
284
+ headerVarMapped: {},
285
+ headerTemplate: false,
286
+ footer: '',
287
+ },
288
+ },
289
+ },
290
+ },
291
+ },
292
+ type: "WHATSAPP",
293
+ },
294
+ sendForApprovalCreateResult1: {
295
+ success: false,
296
+ status: {
297
+ isError: true,
298
+ code: 400,
299
+ },
300
+ message: {
301
+ name: "StatusCodeError",
302
+ statusCode: 500,
303
+ error: {
304
+ errorCode: "1004",
305
+ errorMessage: "{\"error\":{\"message\":\"(#192) Param components[1]['buttons'][0]['phone_number'] is not a valid phone number.\",\"type\":\"OAuthException\",\"code\":192,\"fbtrace_id\":\"12345\"}}",
306
+ },
307
+ },
308
+ },
309
+ sendForApprovalCreateResult2: {
310
+ success: false,
311
+ status: {
312
+ isError: true,
313
+ code: 400,
314
+ },
315
+ message: {
316
+ name: "StatusCodeError",
317
+ statusCode: 500,
318
+ message: "500 - Invalid params",
319
+ error: {
320
+ errorCode: "1004",
321
+ errorMessage: 'Invalid params',
322
+ },
323
+ },
324
+ },
254
325
  };
@@ -1,13 +1,16 @@
1
1
  import { expectSaga, testSaga } from "redux-saga-test-plan";
2
2
  import * as matchers from "redux-saga-test-plan/matchers";
3
3
  import { takeLatest } from "redux-saga/effects";
4
- import { getMetaTagsDetails, watchGetMetaTagsDetails } from "../sagas";
4
+ import { getMetaTagsDetails, watchGetMetaTagsDetails, sendForApprovalCreate } from "../sagas";
5
5
  import {
6
6
  URL_META_TAGS_REQUEST,
7
7
  URL_META_TAGS_SUCCESS,
8
8
  URL_META_TAGS_FAILURE,
9
+ TEMPLATE_CREATE_SUCCESS,
10
+ TEMPLATE_CREATE_FAILURE,
9
11
  } from "../constants";
10
12
  import * as Api from "../../../services/api";
13
+ import { mockData } from "./mockData";
11
14
 
12
15
  describe("Whatsapp saga", () => {
13
16
  const error = new Error("error");
@@ -26,11 +29,11 @@ describe("Whatsapp saga", () => {
26
29
  },
27
30
  ],
28
31
  ])
29
- .put({
30
- type: URL_META_TAGS_SUCCESS,
31
- result: {},
32
- })
33
- .run();
32
+ .put({
33
+ type: URL_META_TAGS_SUCCESS,
34
+ result: {},
35
+ })
36
+ .run();
34
37
  });
35
38
 
36
39
  it("handle success false response from getMetaTagsDetails", () => {
@@ -47,11 +50,11 @@ describe("Whatsapp saga", () => {
47
50
  },
48
51
  ],
49
52
  ])
50
- .put({
51
- type: URL_META_TAGS_FAILURE,
52
- error: 'error',
53
- })
54
- .run();
53
+ .put({
54
+ type: URL_META_TAGS_FAILURE,
55
+ error: 'error',
56
+ })
57
+ .run();
55
58
  });
56
59
 
57
60
  it("handle success false response from getMetaTagsDetails when callback not present", () => {
@@ -67,11 +70,11 @@ describe("Whatsapp saga", () => {
67
70
  },
68
71
  ],
69
72
  ])
70
- .put({
71
- type: URL_META_TAGS_FAILURE,
72
- error: 'error',
73
- })
74
- .run();
73
+ .put({
74
+ type: URL_META_TAGS_FAILURE,
75
+ error: 'error',
76
+ })
77
+ .run();
75
78
  });
76
79
 
77
80
  it("handles error thrown", () => {
@@ -87,4 +90,112 @@ describe("Whatsapp saga", () => {
87
90
  );
88
91
  });
89
92
  });
93
+
94
+ describe("whatsapp test saga for sendForApprovalCreate", () => {
95
+ // const callBack = jest.fn();
96
+ it("handle success response from sendForApprovalCreate", () => {
97
+ expectSaga(sendForApprovalCreate, {
98
+ payload: mockData.sendForApprovalCreatePayload,
99
+ callBack,
100
+ }).provide([
101
+ [
102
+ matchers.call.fn(Api.createWhatsappTemplate),
103
+ {
104
+ success: true,
105
+ result: { response: "Test data"},
106
+ },
107
+ ],
108
+ ])
109
+ .put({
110
+ type: TEMPLATE_CREATE_SUCCESS,
111
+ result: {},
112
+ })
113
+ .run();
114
+ });
115
+
116
+ it("handle error response with json string for sendForApprovalCreate", () => {
117
+ expectSaga(sendForApprovalCreate, {
118
+ payload: mockData.sendForApprovalCreatePayload,
119
+ callBack,
120
+ }).provide([
121
+ [
122
+ matchers.call.fn(Api.createWhatsappTemplate),
123
+ mockData.sendForApprovalCreateResult1,
124
+ ],
125
+ ])
126
+ .put({
127
+ type: TEMPLATE_CREATE_FAILURE,
128
+ error: "(#192) Param components[1]['buttons'][0]['phone_number'] is not a valid phone number.",
129
+ })
130
+ .run();
131
+ });
132
+
133
+ it("handle error response for sendForApprovalCreate even if json parsing fails", () => {
134
+ expectSaga(sendForApprovalCreate, {
135
+ payload: mockData.sendForApprovalCreatePayload,
136
+ callBack,
137
+ }).provide([
138
+ [
139
+ matchers.call.fn(Api.createWhatsappTemplate),
140
+ mockData.sendForApprovalCreateResult2,
141
+ ],
142
+ ])
143
+ .put({
144
+ type: TEMPLATE_CREATE_FAILURE,
145
+ error: 'Invalid params',
146
+ })
147
+ .run();
148
+ });
149
+
150
+ it("handle error response for sendForApprovalCreate", () => {
151
+ expectSaga(sendForApprovalCreate, {
152
+ payload: mockData.sendForApprovalCreatePayload,
153
+ callBack,
154
+ }).provide([
155
+ [
156
+ matchers.call.fn(Api.createWhatsappTemplate),
157
+ {
158
+ success: false,
159
+ status: {
160
+ isError: true,
161
+ code: 500,
162
+ },
163
+ message: 'error',
164
+ },
165
+ ],
166
+ ])
167
+ .put({
168
+ type: TEMPLATE_CREATE_FAILURE,
169
+ error: 'error',
170
+ })
171
+ .run();
172
+ });
173
+
174
+ it("handle error response for sendForApprovalCreate with status code if error message empty", () => {
175
+ expectSaga(sendForApprovalCreate, {
176
+ payload: mockData.sendForApprovalCreatePayload,
177
+ callBack,
178
+ }).provide([
179
+ [
180
+ matchers.call.fn(Api.createWhatsappTemplate),
181
+ {
182
+ success: false,
183
+ status: {
184
+ isError: true,
185
+ code: 500,
186
+ },
187
+ },
188
+ ],
189
+ ])
190
+ .put({
191
+ type: TEMPLATE_CREATE_FAILURE,
192
+ error: 500,
193
+ })
194
+ .run();
195
+ });
196
+
197
+ it("handles error thrown", () => {
198
+ testSaga(sendForApprovalCreate, { callBack }).next().throw(error).next().isDone();
199
+ });
200
+ });
90
201
  });