tango-app-api-analysis-traffic 3.0.0-alpha.47 → 3.0.0-alpha.49

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,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-analysis-traffic",
3
- "version": "3.0.0-alpha.47",
3
+ "version": "3.0.0-alpha.49",
4
4
  "description": "Traffic Analysis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -156,7 +156,7 @@ export async function getNobData( req, res ) {
156
156
  return res.sendError( 'No data found', 204 );
157
157
  }
158
158
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( new Date( inputData.toDate ) ) );
159
-
159
+ let temp ={};
160
160
  let filter = [
161
161
  {
162
162
  'terms': {
@@ -170,28 +170,47 @@ export async function getNobData( req, res ) {
170
170
  ];
171
171
  if ( req.user.role !== 'superadmin' && req.user.userType !== 'tango' ) {
172
172
  filter.push(
173
- req.user.userType !== 'tango',
173
+ {
174
+ 'terms': {
175
+ 'storeId.keyword': inputData?.assignedStores,
176
+ },
177
+ },
174
178
  );
175
179
  }
180
+ temp = {
181
+ 'must': filter,
182
+ };
176
183
 
177
184
  if ( inputData.searchValue && inputData.searchValue!== '' ) {
178
185
  const searchValue = escapeSpecialChars( inputData.searchValue );
179
186
  logger.info( { searchvalue: inputData.searchValue, searchValue: searchValue } );
180
- filter.push(
187
+ temp = {
188
+ 'must': filter,
189
+ 'should': [
181
190
  {
182
191
  'wildcard': {
183
192
  'storeId.keyword': {
184
193
  'value': `${searchValue}*`,
185
194
  },
195
+ },
196
+ },
197
+ {
198
+ 'wildcard': {
186
199
  'storeCode.keyword': {
187
200
  'value': `${searchValue}*`,
188
201
  },
202
+ },
203
+ },
204
+ {
205
+ 'wildcard': {
189
206
  'storeName.keyword': {
190
207
  'value': `${searchValue}*`,
191
208
  },
192
209
  },
193
210
  },
194
- );
211
+ ],
212
+ 'minimum_should_match': 1,
213
+ };
195
214
  }
196
215
  const getClient = await findOne( { clientId: req.clientId }, { featureConfigs: 1 } );
197
216
  const openSearch = JSON.parse( process.env.OPENSEARCH );
@@ -201,21 +220,38 @@ export async function getNobData( req, res ) {
201
220
  'from': skip,
202
221
  'size': limit,
203
222
  'query': {
204
- 'bool': {
205
- 'must': filter,
206
- },
223
+ 'bool': temp,
207
224
  },
208
225
  };
209
226
 
210
227
  const getNobData=await getOpenSearchData( openSearch.nob, nobQuery );
211
228
  const nobData = getNobData?.body?.hits?.hits;
212
229
  if ( !nobData ||nobData?.length == 0 ) {
213
- return res.sendError( 'No Data Found', 204 );
230
+ const initialQuery={
231
+ 'size': 1,
232
+ 'query': {
233
+ 'bool': {
234
+ 'must': [
235
+ {
236
+ 'term': {
237
+ 'clientId.keyword': req.clientId,
238
+ },
239
+ },
240
+ ],
241
+ },
242
+ },
243
+ };
244
+ const getInitialData=await getOpenSearchData( openSearch.nob, initialQuery );
245
+ const initialData = getInitialData?.body?.hits?.hits;
246
+ logger.info( { initialData: initialData, clientId: req.clientId } );
247
+ if ( initialData && initialData.length > 0 ) {
248
+ return res.sendError( 'No Data Found', 204 );
249
+ } else {
250
+ return res.sendSuccess( { initialInsert: false } );
251
+ }
214
252
  }
215
253
 
216
254
  const footfallQuery={
217
- // 'from': skip,
218
- // 'size': limit,
219
255
  'query': {
220
256
  'bool': {
221
257
  'must': [
@@ -304,24 +340,24 @@ export async function getNobData( req, res ) {
304
340
  'Store Id': element.storeId,
305
341
  'Date': dayjs( element.nobDate ).format( 'D MMM,YYYY' ),
306
342
  'NoB\'s Count': element.nobCount,
307
- 'Conversion Rate': `${element.conversionRate} %`,
308
-
309
343
 
310
344
  } );
311
345
  switch ( getClient?.featureConfigs?.conversionCalculation ) {
312
346
  case 'footfall-count':
313
- exportData['Footfall'] = element.footfallCount;
347
+ exportData[index]['Footfall'] = element.footfallCount;
314
348
  break;
315
349
  case 'billable-entities':
316
- exportData['Potential Buyers'] = element.potentialBuyers;
350
+ exportData[index]['Potential Buyers'] = element.potentialBuyers;
317
351
  break;
318
352
  case 'engagers-count':
319
- exportData['Engagers Count'] = element.engagersCount;
353
+ exportData[index]['Engagers Count'] = element.engagersCount;
320
354
  break;
321
355
  default:
322
- exportData['Engagers Count'] = element.engagersCount;
356
+ exportData[index]['Engagers Count'] = element.engagersCount;
323
357
  }
358
+ element.conversionRate? exportData[index]['Conversion Rate'] = `${element.conversionRate} %` : exportData[index]['Conversion Rate'] =null;
324
359
  } );
360
+ logger.info( { exportData: exportData } );
325
361
  return exportData;
326
362
  } );
327
363
  const mappedArrays = await Promise.all( promises );
@@ -2,14 +2,14 @@ import express from 'express';
2
2
  import { accessVerification, getAssinedStore, isAllowedSessionHandler, validate } from 'tango-app-api-middleware';
3
3
  import { addBillsValid, getNobDataValid, storeListValid } from '../dtos/nob.dtos.js';
4
4
  import { addBills, getNobData, storeList } from '../controllers/nob.controllers.js';
5
- import { clientValidations } from '../validations/nob.validations.js';
5
+ import { clientValidations, roleVerification } from '../validations/nob.validations.js';
6
6
 
7
7
  const nobRouter=express.Router();
8
8
 
9
9
  // store list
10
10
  nobRouter.get( '/store-list', isAllowedSessionHandler, accessVerification( { userType: [ 'client', 'tango' ] } ), validate( storeListValid ), clientValidations, getAssinedStore, storeList );
11
11
 
12
- nobRouter.post( '/add-bills', isAllowedSessionHandler, accessVerification( { userType: [ 'client', 'tango' ] } ), validate( addBillsValid ), clientValidations, addBills );
12
+ nobRouter.post( '/add-bills', isAllowedSessionHandler, accessVerification( { userType: [ 'client', 'tango' ] } ), validate( addBillsValid ), roleVerification, clientValidations, addBills );
13
13
 
14
14
  nobRouter.post( '/get-nob-data', isAllowedSessionHandler, accessVerification( { userType: [ 'client', 'tango' ] } ), validate( getNobDataValid ), clientValidations, getAssinedStore, getNobData );
15
15
 
@@ -13,3 +13,16 @@ export async function clientValidations( req, res, next ) {
13
13
  return res.sendError( err, 500 );
14
14
  }
15
15
  }
16
+
17
+ export async function roleVerification( req, res, next ) {
18
+ try {
19
+ if ( [ 'admin', 'superadmin' ].includes( req?.user?.role ) ) {
20
+ return next();
21
+ } else {
22
+ return res.sendError( 'access forbidden', 403 );
23
+ }
24
+ } catch ( error ) {
25
+ logger.error( { error: error, message: req.body, function: 'nob-roleVerification' } );
26
+ return res.sendError( error, 500 );
27
+ }
28
+ }