tango-app-api-trax 3.4.0-flag-4 → 3.4.0-flag-5

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/app.js ADDED
@@ -0,0 +1,55 @@
1
+ import express from 'express';
2
+ import { traxRouter, mobileRouter, traxFlagRouter, traxDashboardRouter, galleryRouter, internalTraxRouter, downloadRouter, activityLogRouter } from './index.js';
3
+ import responseMiddleware from './config/response/response.js';
4
+ import errorMiddleware from './config/response/response.js';
5
+ import dotenv from 'dotenv';
6
+ import { logger } from 'tango-app-api-middleware';
7
+ import { connectdb } from './config/database/database.js';
8
+ import cors from 'cors';
9
+ import pkg from 'body-parser';
10
+ import fileupload from 'express-fileupload';
11
+ import admin from 'firebase-admin';
12
+ const serviceAccountPath = JSON.parse( process.env.FIREBASE );
13
+ admin.initializeApp( {
14
+ credential: admin.credential.cert( serviceAccountPath ),
15
+ } );
16
+
17
+ const env=dotenv.config();
18
+
19
+ const app = express();
20
+ const PORT = process.env.PORT || 3000;
21
+
22
+ const { json, urlencoded } =pkg;
23
+
24
+
25
+ if ( env.error ) {
26
+ logger.error( '.env not found' );
27
+ process.exit( 1 );
28
+ }
29
+
30
+ app.use( cors() );
31
+ app.use( responseMiddleware );
32
+ app.use( errorMiddleware );
33
+ app.use( json( { limit: '500mb' } ) );
34
+ app.use( fileupload() );
35
+ app.use(
36
+ urlencoded( {
37
+ extended: true,
38
+ } ),
39
+ );
40
+
41
+ app.use( '/trax', traxRouter );
42
+ app.use( '/trax/flag', traxFlagRouter );
43
+ app.use( '/trax/mobileAPI', mobileRouter );
44
+ app.use( '/trax/dashboard', traxDashboardRouter );
45
+ app.use( '/gallery', galleryRouter );
46
+ app.use( '/trax/internalAPI', internalTraxRouter );
47
+ app.use( '/trax/downloads', downloadRouter );
48
+ app.use( '/activityLog', activityLogRouter );
49
+
50
+
51
+ app.listen( PORT, () => {
52
+ console.log( `server is running on port= ${PORT} ` );
53
+ logger.info( `server is running on port= ${PORT} ` );
54
+ connectdb();
55
+ } );
package/index.js CHANGED
@@ -8,5 +8,5 @@ import { downloadRouter } from './src/routes/download.router.js';
8
8
  import { mobileRouter } from './src/routes/mobileTrax.routes.js';
9
9
  import { internalTraxRouter } from './src/routes/internalTraxApi.router.js';
10
10
  import { locusOrderRouter } from './src/routes/locus.router.js';
11
-
12
- export { traxDashboardRouter, traxFlagRouter, traxRouter, galleryRouter, downloadRouter, mobileRouter, internalTraxRouter, locusOrderRouter };
11
+ import { activityLogRouter } from './src/routes/activityLog.router.js';
12
+ export { traxDashboardRouter, traxFlagRouter, traxRouter, galleryRouter, downloadRouter, mobileRouter, internalTraxRouter, locusOrderRouter, activityLogRouter };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "tango-app-api-trax",
3
- "version": "3.4.0-flag-4",
3
+ "version": "3.4.0-flag-5",
4
4
  "description": "Trax",
5
- "main": "index.js",
5
+ "main": "app.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "start": "nodemon --exec \"eslint --fix . && node index.js\""
8
+ "start": "nodemon --exec \"eslint --fix . && node app.js\""
9
9
  },
10
10
  "engines": {
11
11
  "node": ">=18.10.0"
@@ -0,0 +1,353 @@
1
+ // import mongoose from 'mongoose';
2
+ import { insertOpenSearchData, updateOpenSearchData, getOpenSearchData } from 'tango-app-api-middleware';
3
+ import * as checklistService from '../services/checklist.service.js';
4
+
5
+ export const insertLog = async ( req, res ) => {
6
+ try {
7
+ console.log( 'insertLog' );
8
+ // let currentDate = new Date();
9
+ let inserttraxlogs = {
10
+ 'client_id': '11',
11
+ 'createAt': new Date(),
12
+ 'sourceCheckList_id': '668e3e1807d5312fa49b0046',
13
+ 'checkListName': 'Daily Store Checklistnafi33',
14
+ 'fromCheckListName': '',
15
+ 'type': 'checklist',
16
+ 'action': 'updated',
17
+ 'storeName': 'LKST007',
18
+ 'store_id': '',
19
+ 'createdByEmail': 'lkst007bc1@lenskart.co.in',
20
+ 'createdBy': 'lkst007bc1',
21
+ 'coverage': 'store',
22
+ 'logDetails': {
23
+ 'schedule': {
24
+ 'previous': {
25
+ 'schedule': 'repeats',
26
+ // "repeats":"daily",
27
+ 'time': '10:00 AM to 11:00 PM',
28
+ // "occurs":[],
29
+ 'scheduleRepeatedDay': [ '01' ],
30
+ 'scheduleRepeatedType': 'daily',
31
+ 'scheduleStartTime_iso': '2025-02-10T23:30:00.000+00:00',
32
+ 'scheduleEndTime_iso': '2025-02-10T23:30:00.000+00:00',
33
+ 'configStartDate': '2025-03-11T00:00:00.000+00:00',
34
+ 'configEndDate': '2025-03-12T00:00:00.000+00:00',
35
+ 'scheduleWeekDays': [ 'Monday', 'Tuesday' ],
36
+ 'scheduleRepeatedMonthWeek': '2 Tuesday',
37
+ 'specificDate': [ '10', '11' ],
38
+
39
+ },
40
+ 'new': {
41
+ 'schedule': 'onetime',
42
+ // "repeats":"weekly",
43
+ 'time': '10:00 AM to 11:00 PM',
44
+ // "occurs":["Mondy","Tuesday"],
45
+ 'scheduleRepeatedDay': [ '01' ],
46
+ 'scheduleRepeatedType': 'daily',
47
+ 'configStartDate': '2025-03-11T00:00:00.000+00:00',
48
+ 'configEndDate': '2025-03-12T00:00:00.000+00:00',
49
+ 'scheduleStartTime_iso': '2025-02-10T23:30:00.000+00:00',
50
+ 'scheduleEndTime_iso': '2025-02-10T23:30:00.000+00:00',
51
+ 'scheduleWeekDays': [ 'Monday', 'Tuesday' ],
52
+ 'scheduleRepeatedMonthWeek': '2 Tuesday',
53
+ 'specificDate': [ '10', '11' ],
54
+ },
55
+ },
56
+ 'response': {
57
+ 'previous': [ 'Geo Facing', 'Allow Multiple response' ],
58
+ 'new': [ 'Geo Facing 2', 'Allow Multiple response 2' ],
59
+ },
60
+ 'owner': {
61
+ 'previous': [ 'Ajinkya Malshe' ],
62
+ 'new': [ 'Ajinkya Malshe' ],
63
+ },
64
+ 'approver': {
65
+ 'previous': [ 'raju', 'rajuapprover' ],
66
+ 'new': [ 'raju 1', 'rajuapprover 2' ],
67
+ },
68
+ 'storeAdded': [ 'LKST011', 'LKST022', 'LKST3444', 'LKST455', 'LKST566', 'LKST666', 'LKST766', 'LKST866', 'LkST966' ],
69
+ 'storeRemoved': [ 'LKST1166', 'LKST1177', 'LKST1188', 'LKST1199', 'LKST1155', 'LKST1144', 'LKST1133', 'LKST1122', 'LkST1111' ],
70
+ 'userAdded': [ 'LKST011@yopmail.com', 'LKST022@yopmail.com', 'LKST3444@yopmail.com', 'LKST455@yopmail.com', 'LKST566@yopmail.com', 'LKST666@yopmail.com', 'LKST766@yopmail.com', 'LKST866@yopmail.com', 'LkST966@yopmail.com' ],
71
+ 'userRemoved': [ 'LKST1166@yopmail.com', 'LKST1177@yopmail.com', 'LKST1188@yopmail.com', 'LKST1199@yopmail.com', 'LKST1155@yopmail.com', 'LKST1144@yopmail.com', 'LKST1133@yopmail.com', 'LKST1122@yopmail.com', 'LkST1111@yopmail.com' ],
72
+ },
73
+ };
74
+ let insertOS = await insertOpenSearchData( 'test-traxlogs', inserttraxlogs );
75
+ console.log( 'insertOS', insertOS );
76
+ return res.sendSuccess( 'insert successfully' );
77
+ // if ( insertOS && insertOS.body.result == 'created' ) {
78
+ // emailersIds.push( insertOS.body._id );
79
+ // }
80
+ } catch ( e ) {
81
+ // logger.error( 'insertLog =>', e );
82
+ console.log( 'insertLog =>', e );
83
+ return res.sendError( e, 500 );
84
+ }
85
+ };
86
+ export const updateLog = async ( req, res ) => {
87
+ try {
88
+ console.log( 'update' );
89
+ const document = {
90
+ doc: {
91
+ storeName: 'LKST008',
92
+ // fromDate: '2025-01-13',
93
+ // toDate: '2025-01-19',
94
+ // status: 'completed',
95
+ // storeIds: [ '347-1' ],
96
+ },
97
+ };
98
+ let updateResult = await updateOpenSearchData( 'test-traxlogs', '5DwBYZUBcRcpuflp5oek', document );
99
+ console.log( 'updateResult', updateResult );
100
+ if ( updateResult?.statusCode == 200 && updateResult?.body?.result == 'updated' ) {
101
+ return res.sendSuccess( 'Email Send Successfully' );
102
+ } else {
103
+ return res.sendSuccess( 'Email Send Successfully' );
104
+ // return res.sendError( { error: error }, 500 );
105
+ }
106
+ } catch ( e ) {
107
+ logger.error( 'updateLog =>', e );
108
+ return res.sendError( e, 500 );
109
+ }
110
+ };
111
+ export const listLogOld = async ( req, res ) => {
112
+ try {
113
+ console.log( 'list' );
114
+ const mustConditions = [];
115
+ // mustConditions.push( { match: { _id: '5DwBYZUBcRcpuflp5oek' } } );
116
+ let query = {
117
+ from: 1,
118
+ size: 2000,
119
+ query: {
120
+ bool: {
121
+ must: mustConditions,
122
+ },
123
+ },
124
+ sort: [
125
+ {
126
+ createAt: {
127
+ order: 'desc',
128
+ },
129
+ },
130
+ ],
131
+ };
132
+ let result = await getOpenSearchData( 'test-traxlogs', query );
133
+ console.log( 'result', result );
134
+ if ( !result || !result.body.hits.hits.length ) {
135
+ return res.sendError( 'no data found', 204 );
136
+ }
137
+ return res.sendSuccess( { count: result.body.hits.total.value, result: result.body.hits.hits } );
138
+ } catch ( e ) {
139
+ console.log( 'listLog', e );
140
+ // logger.error( 'listLog =>', e );
141
+ return res.sendError( e, 500 );
142
+ }
143
+ };
144
+ export const listLog = async ( req, res ) => {
145
+ try {
146
+ let requestBody = req.body;
147
+ let from = ( requestBody.offset ) * requestBody.limit;
148
+
149
+ // let start = new Date( requestBody.fromDate );
150
+ // let userTimezoneOffset = start.getTimezoneOffset() * 60000;
151
+ // start = new Date( start.getTime() - userTimezoneOffset );
152
+ // start.setUTCHours( 0, 0, 0, 0 );
153
+ // let end = new Date( requestBody.toDate );
154
+ // end = new Date( end.getTime() - userTimezoneOffset );
155
+ // end.setUTCHours( 23, 59, 59, 59 );
156
+
157
+ // console.log( 'list' );
158
+ const mustConditions = [];
159
+
160
+ if ( requestBody.clientId ) {
161
+ mustConditions.push( { term: { client_id: requestBody.clientId } } );
162
+ }
163
+
164
+ if ( requestBody.type ) {
165
+ mustConditions.push( { term: { type: requestBody.type } } );
166
+ }
167
+
168
+ if ( requestBody.action ) {
169
+ mustConditions.push( { terms: { action: requestBody.action } } );
170
+ }
171
+
172
+ if ( req.user.userType == 'client' && req.user.role == 'user' ) {
173
+ return res.sendError( 'no data found', 204 );
174
+ }
175
+
176
+ if ( req.user.userType == 'client' && req.user.role == 'admin' ) {
177
+ const sourceCheckListResult = await checklistService.aggregate( [
178
+ { $match: { 'owner.value': req.user.email } },
179
+ { $group: { _id: null, checklistIds: { $push: { $toString: '$_id' } } } },
180
+ { $project: { _id: 0, checklistIds: 1 } },
181
+ ] );
182
+ const sourceCheckListIds = sourceCheckListResult.length > 0 ? sourceCheckListResult[0].checklistIds : [];
183
+ console.log( 'sourceCheckListIds', sourceCheckListIds );
184
+ // mustConditions.push( { terms: { $or: [ { store_id: { $in: requestBody.storeId } }, { store_id: { $eq: '' }, userEmail: { $in: requestBody.userEmailes } }, { sourceCheckList_id: { $in: sourceCheckListIds } } ] } } );
185
+ mustConditions.push( {
186
+ 'bool': {
187
+ 'should': [
188
+ { 'terms': { 'store_id': requestBody.storeId } },
189
+ {
190
+ 'bool': {
191
+ 'must': [
192
+ { 'term': { 'store_id': '' } },
193
+ { 'terms': { 'userEmail': requestBody.userEmailes } },
194
+ ],
195
+ },
196
+ },
197
+ { 'terms': { 'sourceCheckList_id': sourceCheckListIds.map( String ) } },
198
+ ],
199
+ 'minimum_should_match': 1,
200
+ },
201
+ } );
202
+ }
203
+
204
+ if ( requestBody.searchValue ) {
205
+ mustConditions.push( { multi_match: {
206
+ 'query': requestBody.searchValue,
207
+ 'fields': [ 'checkListName', 'storeName', 'userEmail' ],
208
+ 'type': 'phrase_prefix',
209
+ },
210
+ } );
211
+ }
212
+ // console.log( 'Generated Query:', JSON.stringify( mustConditions, null, 2 ) );
213
+
214
+ // console.log( 'mustConditions =>', mustConditions );
215
+ // if ( requestBody.dateRange ) {
216
+ // mustConditions.push( {
217
+ // range: {
218
+ // createdAt: {
219
+ // gte: start, // Start date
220
+ // lte: end, // End date
221
+ // },
222
+ // },
223
+ // } );
224
+ // }
225
+ let sortCreatedAt = 'desc';
226
+ if ( requestBody.sortColumnName && requestBody.sortColumnName != '' ) {
227
+ if ( requestBody.sortBy == '1' ) {
228
+ sortCreatedAt = 'asc';
229
+ }
230
+ }
231
+ let query = {
232
+ from: parseInt( from ),
233
+ size: parseInt( requestBody.limit ),
234
+ query: {
235
+ bool: {
236
+ must: mustConditions,
237
+ },
238
+ },
239
+ sort: [
240
+ {
241
+ createAt: {
242
+ order: sortCreatedAt,
243
+ },
244
+ },
245
+ ],
246
+ };
247
+ let result = await getOpenSearchData( 'test-traxlogs', query );
248
+ // console.log( 'result', result );
249
+ if ( !result || !result.body.hits.hits.length ) {
250
+ return res.sendError( 'no data found', 204 );
251
+ }
252
+ return res.sendSuccess( { count: result.body.hits.total.value, result: result.body.hits.hits } );
253
+ } catch ( e ) {
254
+ console.log( 'listLog', e );
255
+ return res.sendError( e, 500 );
256
+ }
257
+ };
258
+
259
+ export const listLogNotwork = async ( req, res ) => {
260
+ try {
261
+ let requestBody = req.body;
262
+ let from = ( requestBody.offset ) * requestBody.limit;
263
+
264
+ // let start = new Date( requestBody.fromDate );
265
+ // let userTimezoneOffset = start.getTimezoneOffset() * 60000;
266
+ // start = new Date( start.getTime() - userTimezoneOffset );
267
+ // start.setUTCHours( 0, 0, 0, 0 );
268
+ // let end = new Date( requestBody.toDate );
269
+ // end = new Date( end.getTime() - userTimezoneOffset );
270
+ // end.setUTCHours( 23, 59, 59, 59 );
271
+
272
+ // console.log( 'list' );
273
+ const mustConditions = [];
274
+ // if user role is admin fetch checklist owners
275
+
276
+
277
+ if ( requestBody.clientId ) {
278
+ mustConditions.push( { term: { client_id: requestBody.clientId } } );
279
+ }
280
+
281
+ // if ( req.user.userType == 'client' && req.user.role == 'admin' ) {
282
+ // const sourceCheckListIds = await checklistService.aggregate( [
283
+ // { $match: { 'owner.value': req.user.email } },
284
+ // { $group: { _id: null, checklistIds: { $push: { $toString: '$_id' } } } },
285
+ // { $project: { _id: 0, checklistIds: 1 } },
286
+ // ] );
287
+
288
+ // mustConditions.push( { terms: { $or: [ { store_id: { $in: requestBody.storeId } }, { store_id: { $eq: '' }, userEmail: { $in: requestBody.userEmailes } }, { sourceCheckList_id: { $in: sourceCheckListIds } } ] } } );
289
+ // }
290
+
291
+ if ( requestBody.type ) {
292
+ mustConditions.push( { term: { type: requestBody.type } } );
293
+ }
294
+
295
+ if ( requestBody.action ) {
296
+ mustConditions.push( { terms: { action: { $in: requestBody.action } } } );
297
+ }
298
+
299
+ if ( requestBody.searchValue ) {
300
+ mustConditions.push( { multi_match: {
301
+ 'query': requestBody.searchValue,
302
+ 'fields': [ 'checkListName', 'storeName', 'userEmail' ],
303
+ 'type': 'phrase_prefix',
304
+ },
305
+ } );
306
+ }
307
+ // console.log( 'Generated Query:', JSON.stringify( mustConditions, null, 2 ) );
308
+
309
+ // console.log( 'mustConditions =>', mustConditions );
310
+ // if ( requestBody.dateRange ) {
311
+ // mustConditions.push( {
312
+ // range: {
313
+ // createdAt: {
314
+ // gte: start, // Start date
315
+ // lte: end, // End date
316
+ // },
317
+ // },
318
+ // } );
319
+ // }
320
+ let sortCreatedAt = 'desc';
321
+ if ( requestBody.sortColumnName && requestBody.sortColumnName != '' ) {
322
+ if ( requestBody.sortBy == '1' ) {
323
+ sortCreatedAt = 'asc';
324
+ }
325
+ }
326
+ let query = {
327
+ from: parseInt( from ),
328
+ size: parseInt( requestBody.limit ),
329
+ query: {
330
+ bool: {
331
+ must: mustConditions,
332
+ },
333
+ },
334
+ sort: [
335
+ {
336
+ createAt: {
337
+ order: sortCreatedAt,
338
+ },
339
+ },
340
+ ],
341
+ };
342
+ let result = await getOpenSearchData( 'test-traxlogs', query );
343
+ console.log( 'result', result );
344
+ if ( !result || !result.body.hits.hits.length ) {
345
+ return res.sendError( 'no data found', 204 );
346
+ }
347
+ return res.sendSuccess( { count: result.body.hits.total.value, result: result.body.hits.hits } );
348
+ } catch ( e ) {
349
+ console.log( 'listLog', e );
350
+ logger.error( 'listLog =>', e );
351
+ return res.sendError( e, 500 );
352
+ }
353
+ };
@@ -1,4 +1,4 @@
1
- import { logger, signedUrl, fileUpload, getOtp, sendEmailWithSES, getUuid } from 'tango-app-api-middleware';
1
+ import { logger, signedUrl, fileUpload, getOtp, sendEmailWithSES, getUuid, insertOpenSearchData } from 'tango-app-api-middleware';
2
2
  import * as processedchecklist from '../services/processedchecklist.services.js';
3
3
  import * as processedtask from '../services/processedTaskList.service.js';
4
4
  import * as PCLconfig from '../services/processedchecklistconfig.services.js';
@@ -27,6 +27,7 @@ dayjs.extend( timeZone );
27
27
  import isSameOrBefore from 'dayjs/plugin/isSameOrBefore.js';
28
28
  dayjs.extend( isSameOrBefore );
29
29
 
30
+
30
31
  export async function storeList( req, res ) {
31
32
  try {
32
33
  if ( !req.query?.date ) {
@@ -262,6 +263,27 @@ export async function startChecklist( req, res ) {
262
263
  getchecklist[index].questionAnswers[secIndex].questions = questions;
263
264
  }
264
265
  }
266
+ let openSearch = JSON.parse( process.env.openSearch );
267
+ console.log( 'openSearch', openSearch );
268
+ let inserttraxlogs = {
269
+ client_id: req.user.clientId,
270
+ createAt: new Date(),
271
+ sourceCheckList_id: getBeforeChecklist[0]?.sourceCheckList_id || '',
272
+ checkListName: getBeforeChecklist[0]?.checkListName || '',
273
+ fromCheckListName: '',
274
+ type: 'checkList',
275
+ action: 'start',
276
+ storeName: getBeforeChecklist[0]?.storeName || '',
277
+ store_id: getBeforeChecklist[0]?.store_id || '',
278
+ createdByEmail: getBeforeChecklist[0]?.userEmail || '',
279
+ createdBy: getBeforeChecklist[0]?.userName || '',
280
+ coverage: getBeforeChecklist[0]?.coverage || '',
281
+ logDetails: {},
282
+ };
283
+ // let insertOS = await insertOpenSearchData( 'test-traxlogs', inserttraxlogs );
284
+ console.log( 'inserttraxlogs', inserttraxlogs );
285
+ let insertOS = await insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
286
+ console.log( 'insertOS', insertOS );
265
287
  return res.sendSuccess( getchecklist );
266
288
  } else {
267
289
  return res.sendError( 'something went wrong please try again', 500 );
@@ -433,6 +455,27 @@ export async function startTask( req, res ) {
433
455
  };
434
456
  await checklistLogs.create( logData );
435
457
 
458
+ let openSearch = JSON.parse( process.env.openSearch );
459
+ console.log( 'openSearch', openSearch );
460
+ let inserttraxlogs = {
461
+ client_id: req.user.clientId,
462
+ createAt: new Date(),
463
+ sourceCheckList_id: task.sourceCheckList_id || '',
464
+ checkListName: task.checkListName || '',
465
+ fromCheckListName: '',
466
+ type: 'task',
467
+ action: 'start',
468
+ storeName: task.storeName || '',
469
+ store_id: task.store_id || '',
470
+ createdByEmail: task.userEmail || '',
471
+ createdBy: task.userName || '',
472
+ coverage: task.coverage || '',
473
+ logDetails: {},
474
+ };
475
+ // let insertOS = await insertOpenSearchData( 'test-traxlogs', inserttraxlogs );
476
+ console.log( 'inserttraxlogs', inserttraxlogs );
477
+ let insertOS = await insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
478
+ console.log( 'insertOS', insertOS );
436
479
  return res.sendSuccess( getUpdatedTask );
437
480
  } catch ( error ) {
438
481
  logger.error( { function: 'startTask', error, body: req.body } );
@@ -1805,7 +1848,7 @@ export async function submitChecklist( req, res ) {
1805
1848
  userName: getchecklist[0].userName || '',
1806
1849
  coverage: getchecklist[0].coverage || '',
1807
1850
  };
1808
- console.log( 'logInsertData=>', logInsertData );
1851
+ // console.log( 'logInsertData=>', logInsertData );s
1809
1852
  await checklistLogs.create( logInsertData );
1810
1853
  // let time = dayjs().format( 'HH:mm:ss' );
1811
1854
  // let [ hours, minutes ] = time.split( ':' ).map( Number );
@@ -1840,8 +1883,32 @@ export async function submitChecklist( req, res ) {
1840
1883
  // await detectionService.create( detectionData );
1841
1884
  if ( requestData.submittype == 'submit' ) {
1842
1885
  updateOpenSearch( req.user, requestData );
1886
+
1887
+ let openSearch = JSON.parse( process.env.openSearch );
1888
+ console.log( 'openSearch', openSearch );
1889
+ let inserttraxlogs = {
1890
+ client_id: req.user.clientId,
1891
+ createAt: new Date(),
1892
+ sourceCheckList_id: getchecklist[0].sourceCheckList_id,
1893
+ checkListName: getchecklist[0].checkListName,
1894
+ type: 'checkList',
1895
+ action: requestData?.submittype === 'draft' ? 'save' : 'submit',
1896
+ storeName: getchecklist[0].storeName,
1897
+ store_id: getchecklist[0].store_id,
1898
+ userName: getchecklist[0].userName,
1899
+ userEmail: getchecklist[0].userEmail,
1900
+ createdByEmail: getchecklist[0].userEmail,
1901
+ createdBy: getchecklist[0].userName,
1902
+ coverage: getchecklist[0].coverage,
1903
+ checkListType: getchecklist[0].checkListType,
1904
+ logDetails: {},
1905
+ };
1906
+ console.log( 'inserttraxlogs', inserttraxlogs );
1907
+ let insertOS = await insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
1908
+ console.log( 'insertOS', insertOS );
1843
1909
  }
1844
1910
 
1911
+
1845
1912
  return res.sendSuccess( 'Checklist Updated Successfully' );
1846
1913
  } else {
1847
1914
  return res.sendError( 'something went wrong please try again', 500 );
@@ -2006,6 +2073,29 @@ export async function submitTask( req, res ) {
2006
2073
 
2007
2074
  if ( submittype == 'submit' ) {
2008
2075
  updateOpenSearchTask( user, requestData );
2076
+
2077
+ let openSearch = JSON.parse( process.env.openSearch );
2078
+ console.log( 'openSearch', openSearch );
2079
+ let inserttraxlogs = {
2080
+ client_id: req.user.clientId,
2081
+ createAt: new Date(),
2082
+ sourceCheckList_id: checklist.sourceCheckList_id,
2083
+ checkListName: checklist.checkListName,
2084
+ type: 'task',
2085
+ action: requestData?.submittype === 'draft' ? 'save' : 'submit',
2086
+ storeName: checklist.storeName,
2087
+ store_id: checklist.store_id,
2088
+ userName: checklist.userName,
2089
+ userEmail: checklist.userEmail,
2090
+ createdByEmail: checklist.userEmail,
2091
+ createdBy: checklist.userName,
2092
+ coverage: checklist.coverage,
2093
+ checkListType: checklist.checkListType,
2094
+ logDetails: {},
2095
+ };
2096
+ console.log( 'inserttraxlogs', inserttraxlogs );
2097
+ let insertOS = await insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
2098
+ console.log( 'insertOS', insertOS );
2009
2099
  }
2010
2100
 
2011
2101
  return res.sendSuccess( 'Task Updated Successfully' );
@@ -1,4 +1,4 @@
1
- import { signedUrl, fileUpload, chunkArray, logger, sendPushNotification } from 'tango-app-api-middleware';
1
+ import { signedUrl, fileUpload, chunkArray, logger, sendPushNotification, insertOpenSearchData } from 'tango-app-api-middleware';
2
2
  import * as checklistService from '../services/checklist.service.js';
3
3
  import * as questionService from '../services/checklistQuestion.service.js';
4
4
  import * as assignedService from '../services/checklistAssign.service.js';
@@ -24,7 +24,6 @@ dayjs.extend( customParseFormat );
24
24
  import * as clusterServices from '../services/cluster.service.js';
25
25
  import * as teamsServices from '../services/teams.service.js';
26
26
 
27
-
28
27
  export const checklist = async ( req, res ) => {
29
28
  try {
30
29
  let limit = parseInt( req.query.limit ) || 10;
@@ -276,13 +275,29 @@ export const create = async ( req, res ) => {
276
275
  section: section.name,
277
276
  createdBy: req.user._id,
278
277
  createdByName: req.user.userName,
279
- client_id: req.body.clientId,
278
+ client_id: inputBody.clientId,
280
279
  checkListId: checkListId,
281
280
  question: section.questions,
282
281
  checkList: inputBody.checklistName,
283
282
  };
284
283
  await questionService.create( sectionList ).then( async ( data ) => {
285
284
  if ( i == inputBody.sections.length - 1 ) {
285
+ let logObj = {
286
+ client_id: inputBody.clientId,
287
+ createAt: new Date(),
288
+ sourceCheckList_id: checkListId,
289
+ checkListName: inputBody.checklistName,
290
+ fromCheckListName: '',
291
+ type: 'checklist',
292
+ action: 'created',
293
+ storeName: '',
294
+ store_id: '',
295
+ createdByEmail: req.user.email,
296
+ createdBy: req.user.userName,
297
+ coverage: 'store',
298
+ logDetails: {},
299
+ };
300
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, logObj );
286
301
  return res.sendSuccess( { checklistId: checkListId, msg: 'CheckList Created Successfully' } );
287
302
  }
288
303
  } ).catch( ( e ) => {
@@ -715,6 +730,8 @@ export const update = async ( req, res ) => {
715
730
  return res.sendError( 'no data found', 204 );
716
731
  }
717
732
 
733
+ let getExistQuestions = await questionService.find( { checkListId: req.params.checklistId, client_id: req.body.clientId } );
734
+
718
735
  inputBody.sections.forEach( async ( element ) => {
719
736
  if ( !element.questions.length && inputBody.submitType == 'configure' ) {
720
737
  return res.sendError( { message: 'Question is Required' }, 400 );
@@ -725,116 +742,211 @@ export const update = async ( req, res ) => {
725
742
  }
726
743
  } );
727
744
 
728
- checkListDetails.checkListName = inputBody.checklistName;
729
- checkListDetails.checkListDescription = inputBody.checklistDescription;
730
- checkListDetails.questionCount = questionCount;
745
+ let params = {
746
+ checkListName: inputBody.checklistName,
747
+ checkListDescription: inputBody.checklistDescription,
748
+ questionCount: questionCount,
749
+ };
731
750
 
732
- checkListDetails.save().then( async () => {
733
- let checkListId = req.params.checklistId;
734
- let logInsertData = {
735
- action: 'checklistUpdate',
736
- checklistId: checkListId,
737
- checkListName: inputBody.checklistName,
738
- createdBy: req.user._id,
739
- createdByName: req.user.userName,
740
- client_id: req.body.clientId,
741
- sections: inputBody?.sections,
742
- createdByEmail: req.user.email,
743
- };
744
- await checklistLogs.create( logInsertData );
745
- if ( inputBody.sections.length ) {
746
- let sectionId =[];
747
- for ( let i = 0; i < inputBody?.sections?.length; i++ ) {
748
- let section = inputBody.sections[i];
749
- section.questions.forEach( ( section ) => {
750
- if ( section.questionReferenceImage && section.questionReferenceImage !='' ) {
751
- let imgUrl = section.questionReferenceImage.split( '?' )[0];
751
+ checkListDetails.updateOne( { _id: req.params.checklistId }, params );
752
+ let checkListId = req.params.checklistId;
753
+ let logInsertData = {
754
+ action: 'checklistUpdate',
755
+ checklistId: checkListId,
756
+ checkListName: inputBody.checklistName,
757
+ createdBy: req.user._id,
758
+ createdByName: req.user.userName,
759
+ client_id: req.body.clientId,
760
+ sections: inputBody?.sections,
761
+ createdByEmail: req.user.email,
762
+ };
763
+ await checklistLogs.create( logInsertData );
764
+ let sectionList =[];
765
+ if ( inputBody.sections.length ) {
766
+ for ( let i = 0; i < inputBody?.sections?.length; i++ ) {
767
+ let section = inputBody.sections[i];
768
+ section.questions.forEach( ( section ) => {
769
+ if ( section.questionReferenceImage && section.questionReferenceImage !='' ) {
770
+ let imgUrl = section.questionReferenceImage.split( '?' )[0];
771
+ let url = imgUrl.split( '/' );
772
+ url.splice( 0, 3 );
773
+ section.questionReferenceImage = url.join( '/' );
774
+ }
775
+ section.answers.forEach( ( answer ) => {
776
+ if ( answer.referenceImage != '' ) {
777
+ let imgUrl = answer.referenceImage.split( '?' )[0];
752
778
  let url = imgUrl.split( '/' );
753
779
  url.splice( 0, 3 );
754
- section.questionReferenceImage = url.join( '/' );
780
+ answer.referenceImage = url.join( '/' );
755
781
  }
756
- // if (['image', 'descriptiveImage'].includes(section.answerType)) {
757
- section.answers.forEach( ( answer ) => {
758
- if ( answer.referenceImage != '' ) {
759
- let imgUrl = answer.referenceImage.split( '?' )[0];
760
- let url = imgUrl.split( '/' );
761
- url.splice( 0, 3 );
762
- answer.referenceImage = url.join( '/' );
763
- }
764
- } );
765
- // }
766
782
  } );
783
+ } );
767
784
 
768
- for ( let [ index, question ] of section.questions.entries() ) {
769
- await processNested( index, question );
770
- }
785
+ for ( let [ index, question ] of section.questions.entries() ) {
786
+ await processNested( index, question );
787
+ }
771
788
 
772
- async function processNested( qIdx, question, nestedIndex=-1 ) {
773
- if ( question?.answers?.length ) {
774
- for ( let [ index, answer ] of question?.answers?.entries() ) {
775
- if ( !section.questions[qIdx].answers[index]?.nestedQuestion && nestedIndex == -1 ) {
776
- section.questions[qIdx].answers[index].nestedQuestion = [];
777
- }
778
- if ( answer.showLinked ) {
779
- if ( nestedIndex != -1 ) {
780
- if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
781
- section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
782
- }
783
- } else {
784
- if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
785
- section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
786
- }
789
+ async function processNested( qIdx, question, nestedIndex=-1 ) {
790
+ if ( question?.answers?.length ) {
791
+ for ( let [ index, answer ] of question?.answers?.entries() ) {
792
+ if ( !section.questions[qIdx].answers[index]?.nestedQuestion && nestedIndex == -1 ) {
793
+ section.questions[qIdx].answers[index].nestedQuestion = [];
794
+ }
795
+ if ( answer.showLinked ) {
796
+ if ( nestedIndex != -1 ) {
797
+ if ( !section.questions[qIdx].answers[nestedIndex].nestedQuestion.includes( answer.linkedQuestion ) ) {
798
+ section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
787
799
  }
788
- let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
789
- if ( nestedLinkedQuestion ) {
790
- let findNestedAnswers = nestedLinkedQuestion.answers.find( ( item ) => item.showLinked );
791
- if ( findNestedAnswers ) {
792
- if ( nestedIndex != -1 ) {
793
- await processNested( qIdx, nestedLinkedQuestion, nestedIndex );
794
- } else {
795
- await processNested( qIdx, nestedLinkedQuestion, index );
796
- }
800
+ } else {
801
+ if ( !section.questions[qIdx].answers[index].nestedQuestion.includes( answer.linkedQuestion ) ) {
802
+ section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
803
+ }
804
+ }
805
+ let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
806
+ if ( nestedLinkedQuestion ) {
807
+ let findNestedAnswers = nestedLinkedQuestion.answers.find( ( item ) => item.showLinked );
808
+ if ( findNestedAnswers ) {
809
+ if ( nestedIndex != -1 ) {
810
+ await processNested( qIdx, nestedLinkedQuestion, nestedIndex );
811
+ } else {
812
+ await processNested( qIdx, nestedLinkedQuestion, index );
797
813
  }
798
814
  }
799
815
  }
800
816
  }
801
817
  }
802
818
  }
819
+ }
803
820
 
804
- let sectionList = {
805
- section: section.name,
806
- sectionOldName: section.oldName,
807
- createdBy: req.user._id,
808
- createdByName: req.user.userName,
809
- client_id: req.body.clientId,
810
- checkListId: checkListId,
811
- question: section.questions,
812
- checkList: inputBody.checklistName,
813
- };
814
- await questionService.create( sectionList ).then( async ( data ) => {
815
- sectionId.push( data._id );
816
- if ( i == inputBody.sections.length - 1 ) {
817
- await questionService.deleteMany( { checkListId: checkListId, client_id: req.body.clientId, _id: { $nin: sectionId } } );
818
- await assignedService.updateMany( { checkListId: checkListId }, { checkListName: inputBody.checklistName } );
819
- return res.sendSuccess( { checklistId: checkListId, msg: 'CheckList Created Successfully' } );
820
- }
821
- } ).catch( ( e ) => {
822
- return res.sendError( e, 500 );
823
- } );
821
+ sectionList.push( {
822
+ section: section.name,
823
+ sectionOldName: section.oldName,
824
+ createdBy: req.user._id,
825
+ createdByName: req.user.userName,
826
+ client_id: req.body.clientId,
827
+ checkListId: checkListId,
828
+ question: section.questions,
829
+ checkList: inputBody.checklistName,
830
+ } );
831
+ }
832
+ }
833
+ await questionService.deleteMany( { checkListId: checkListId, client_id: req.body.clientId } );
834
+ if ( sectionList.length ) {
835
+ await questionService.insertMany( sectionList );
836
+ }
837
+
838
+ let questionList = {
839
+ questionAdd: [],
840
+ questionEdit: [],
841
+ questionDelete: [],
842
+ };
843
+ let updateSection = [];
844
+
845
+ function findDifferences( obj1, obj2 ) {
846
+ return Object.keys( obj1 ).reduce( ( diff, key ) => {
847
+ if ( !isEqual( obj1[key], obj2[key] ) ) {
848
+ diff[key] = { previous: obj1[key], new: obj2[key] };
824
849
  }
825
- } else {
826
- if ( inputBody.submitType == 'save' ) {
827
- await questionService.deleteMany( { checkListId: checkListId, client_id: req.body.clientId } );
828
- return res.sendSuccess( { message: 'CheckList Updated Successfully' } );
850
+ return diff;
851
+ }, {} );
852
+ }
853
+
854
+ getExistQuestions.forEach( ( ele ) => {
855
+ let sectionDetails = inputBody.sections.find( ( sec ) => sec.oldName === ele.section );
856
+
857
+ if ( !sectionDetails ) {
858
+ questionList.questionDelete.push( { sectionName: ele.section, questions: ele.question } );
859
+ return;
860
+ }
861
+
862
+ if ( ele.section !== sectionDetails.name ) {
863
+ updateSection.push( { previous: ele.section, new: sectionDetails.name } );
864
+ }
865
+
866
+ ele.question.forEach( ( qn ) => {
867
+ let question = sectionDetails.questions.find( ( qns ) => qns.oldQname === qn.qname );
868
+
869
+ if ( question ) {
870
+ qn.answers.forEach( ( ans ) => {
871
+ delete ans.answeroptionNumber;
872
+ } );
873
+ question.answers.forEach( ( ans ) => {
874
+ delete ans.answeroptionNumber;
875
+ } );
876
+ let compare = findDifferences( qn, question );
877
+ if ( Object.keys( compare ).length && ( compare?.answerType || compare?.answers || compare?.linkType ) ) {
878
+ questionList.questionEdit.push( { sectionName: ele.section, questions: [ { previous: qn }, { new: question } ] } );
879
+ }
880
+ } else {
881
+ let sectionIndex = questionList.questionDelete.findIndex( ( sec ) => sec.sectionName === ele.section );
882
+
883
+ if ( sectionIndex !== -1 ) {
884
+ questionList.questionDelete[sectionIndex].questions.push( qn );
885
+ } else {
886
+ questionList.questionDelete.push( { sectionName: ele.section, questions: [ qn ] } );
887
+ }
829
888
  }
889
+ } );
890
+ } );
891
+
892
+ inputBody.sections.forEach( ( ele ) => {
893
+ let sectionDetails = getExistQuestions.find( ( sec ) => sec.section === ele.oldName );
894
+
895
+ if ( !sectionDetails ) {
896
+ questionList.questionAdd.push( { sectionName: ele.name, questions: ele.questions } );
897
+ return;
830
898
  }
831
- } ).catch( ( e ) => {
832
- logger.error( 'update =>', e );
833
- return res.sendError( e, 500 );
899
+
900
+ ele.questions.forEach( ( qn ) => {
901
+ let question = sectionDetails.question.find( ( qns ) => qns.qname === qn.oldQname );
902
+
903
+ if ( !question ) {
904
+ let sectionIndex = questionList.questionAdd.findIndex( ( sec ) => sec.sectionName === ele.name );
905
+
906
+ if ( sectionIndex !== -1 ) {
907
+ questionList.questionAdd[sectionIndex].questions.push( qn );
908
+ } else {
909
+ questionList.questionAdd.push( { sectionName: ele.name, questions: [ qn ] } );
910
+ }
911
+ }
912
+ } );
834
913
  } );
914
+
915
+ if ( updateSection.length || questionList.questionAdd.length || questionList.questionEdit.length || questionList.questionDelete.length || inputBody.checklistName.toLowerCase() != checkListDetails.checkListName.toLowerCase() || inputBody.checklistDescription.toLowerCase() != checkListDetails.checkListDescription.toLowerCase() ) {
916
+ let insertLogData = {
917
+ client_id: req.body.clientId,
918
+ createAt: new Date(),
919
+ sourceCheckList_id: req.params.checklistId,
920
+ checkListName: inputBody.checklistName,
921
+ fromCheckListName: '',
922
+ type: 'checklist',
923
+ action: 'edited',
924
+ storeName: '',
925
+ store_id: '',
926
+ createdByEmail: req.user.email,
927
+ createdBy: req.user.userName,
928
+ coverage: checkListDetails.coverage,
929
+ logDetails: {
930
+ ...( inputBody.checklistName.toLowerCase() == checkListDetails.checkListName.toLowerCase() ) ? { updatedChecklistName: {} } :{ updatedChecklistName: {
931
+ previous: checkListDetails.checkListName,
932
+ new: inputBody.checklistName,
933
+ } },
934
+ ...( inputBody.checklistDescription.toLowerCase() == checkListDetails.checkListDescription.toLowerCase() ) ? { updatedChecklistDescription: {} } :{ updatedChecklistDescription: {
935
+ previous: checkListDetails.checkListDescription,
936
+ new: inputBody.checklistDescription,
937
+ } },
938
+ updatedSectionName: updateSection,
939
+ questionAdd: questionList.questionAdd,
940
+ questionEdit: questionList.questionEdit,
941
+ questionDelete: questionList.questionDelete,
942
+ },
943
+ };
944
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, insertLogData );
945
+ }
946
+ return res.sendSuccess( { message: 'CheckList Updated Successfully', checklistId: req.params.checklistId } );
835
947
  } catch ( e ) {
836
- logger.error( 'update =>', e );
837
- return res.sendError( e, 500 );
948
+ logger.error( { functionName: 'questionUpdate', error: e } );
949
+ res.sendError( e, 500 );
838
950
  }
839
951
  };
840
952
 
@@ -1613,6 +1725,109 @@ export const updateConfigurev1 =async ( req, res ) => {
1613
1725
  futureDaysDataRemove( currentDate, req.params.checklistId, checklistDetails.checkListName, '333' );
1614
1726
  }
1615
1727
  }
1728
+ checklistDetails = checklistDetails.toObject();
1729
+ checklistDetails.configStartDate = checklistDetails.configStartDate ? dayjs.utc( checklistDetails?.configStartDate ).format( 'YYYY-MM-DD' ) : '';
1730
+ checklistDetails.configEndDate = checklistDetails.configEndDate ? dayjs.utc( checklistDetails?.configEndDate ).format( 'YYYY-MM-DD' ) : '';
1731
+ checklistDetails.scheduleDate = checklistDetails.scheduleDate ? dayjs.utc( checklistDetails?.scheduleDate ).format( 'YYYY-MM-DD' ) : '';
1732
+ checklistDetails.scheduleRepeatedDay = Array.isArray( checklistDetails.scheduleRepeatedDay ) ? checklistDetails.scheduleRepeatedDay[0] : checklistDetails.scheduleRepeatedDay;
1733
+ configDetails.scheduleRepeatedDay = Array.isArray( configDetails.scheduleRepeatedDay ) ? configDetails?.scheduleRepeatedDay[0] : configDetails?.scheduleRepeatedDay;
1734
+ configDetails.scheduleDate = configDetails.scheduleDate ? dayjs( configDetails?.scheduleDate ).format( 'YYYY-MM-DD' ) : '';
1735
+ let removedKeys = [ 'publish', 'publishDate', 'storeCount', 'sections', 'createdAt', 'updatedAt', 'scheduleStartTimeISO', 'scheduleEndTimeISO' ];
1736
+ removedKeys.forEach( ( item ) => {
1737
+ delete configDetails?.[item];
1738
+ delete checklistDetails?.[item];
1739
+ } );
1740
+ let differences = findObjectDifference( checklistDetails, configDetails );
1741
+ if ( Object.keys( differences ).length || req.body.added.length || req.body.removed.length ) {
1742
+ let insertData = {
1743
+ client_id: req.body.clientId,
1744
+ createAt: new Date(),
1745
+ sourceCheckList_id: configDetails._id,
1746
+ checkListName: configDetails.checkListName,
1747
+ fromCheckListName: '',
1748
+ type: 'checklist',
1749
+ action: 'updated',
1750
+ storeName: '',
1751
+ store_id: '',
1752
+ createdByEmail: req.user.email,
1753
+ createdBy: req.user.userName,
1754
+ coverage: configDetails.coverage,
1755
+ logDetails: {
1756
+ schedule: {
1757
+ previous: {
1758
+ ...( differences?.scheduleDate && ( differences?.scheduleDate.previous != '' && differences?.scheduleDate.previous != null ) ) ? { scheduleDate: dayjs( checklistDetails?.scheduleDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1759
+ schedule: differences?.schedule ? checklistDetails.schedule : '',
1760
+ time: ( ( differences?.scheduleEndTime || differences?.scheduleStartTime ) && ( checklistDetails.scheduleStartTime != '' || checklistDetails.scheduleEndTime != '' ) ) ? checklistDetails.scheduleStartTime + ' to ' + checklistDetails.scheduleEndTime : '',
1761
+ scheduleRepeatedDay: differences?.scheduleRepeatedDay ? checklistDetails?.scheduleRepeatedDay : '',
1762
+ scheduleRepeatedType: differences?.scheduleRepeatedType ? checklistDetails?.scheduleRepeatedType : '',
1763
+ ...( differences?.configStartDate && checklistDetails?.configStartDate ) ? { configStartDate: dayjs( checklistDetails?.configStartDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1764
+ ...( differences?.configEndDate && checklistDetails?.configEndDate ) ? { configEndDate: dayjs( checklistDetails?.configEndDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1765
+ scheduleWeekDays: differences?.scheduleWeekDays ? checklistDetails?.scheduleWeekDays : [],
1766
+ scheduleRepeatedMonthWeek: differences?.scheduleRepeatedMonthWeek ? checklistDetails?.scheduleRepeatedMonthWeek : '',
1767
+ specificDate: differences?.specificDate ? checklistDetails?.specificDate : [],
1768
+
1769
+ },
1770
+ new: {
1771
+ ...( differences?.scheduleDate && ( differences?.scheduleDate.new != '' && differences?.scheduleDate.new != null ) ) ? { scheduleDate: dayjs( configDetails?.scheduleDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1772
+ schedule: differences?.schedule ? configDetails.schedule : '',
1773
+ time: ( ( differences?.scheduleEndTime || differences?.scheduleStartTime ) && ( configDetails.scheduleStartTime != '' || configDetails.scheduleEndTime != '' ) ) ? configDetails.scheduleStartTime + ' to ' + configDetails.scheduleEndTime : '',
1774
+ scheduleRepeatedDay: differences?.scheduleRepeatedDay ? configDetails?.scheduleRepeatedDay : '',
1775
+ scheduleRepeatedType: differences?.scheduleRepeatedType ? configDetails?.scheduleRepeatedType : '',
1776
+ ...( differences?.configStartDate && configDetails?.configStartDate ) ? { configStartDate: dayjs( configDetails?.configStartDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1777
+ ...( differences?.configEndDate && configDetails?.configEndDate ) ? { configEndDate: dayjs( configDetails?.configEndDate, 'YYYY-MM-DD' ).format( 'DD MMM, YYYY' ) } :{},
1778
+ scheduleWeekDays: differences?.scheduleWeekDays ? configDetails?.scheduleWeekDays : [],
1779
+ scheduleRepeatedMonthWeek: differences?.scheduleRepeatedMonthWeek ? configDetails?.scheduleRepeatedMonthWeek : '',
1780
+ specificDate: differences?.specificDate ? configDetails?.specificDate : [],
1781
+ },
1782
+ },
1783
+ response: {
1784
+ previous: [
1785
+ ...( differences?.allowedStoreLocation && checklistDetails.allowedStoreLocation ? [ 'Geo fencing' ] : [] ),
1786
+ ...( differences?.allowedMultiSubmit && checklistDetails.allowedMultiSubmit? [ 'Allow multiple responses' ] : [] ),
1787
+ ...( differences?.allowedOverTime && checklistDetails.allowedOverTime ? [ 'Fill response after configure time.' ] : [] ),
1788
+ ...( differences?.allowOnce && checklistDetails.allowOnce ? [ 'Only one time' ] : [] ),
1789
+ ],
1790
+ new: [
1791
+ ...( differences?.allowedStoreLocation && configDetails.allowedStoreLocation ? [ 'Geo fencing' ] : [] ),
1792
+ ...( differences?.allowedMultiSubmit && configDetails.allowedMultiSubmit? [ 'Allow multiple responses' ] : [] ),
1793
+ ...( differences?.allowedOverTime && configDetails.allowedOverTime ? [ 'Fill response after configure time' ] : [] ),
1794
+ ...( differences?.allowOnce && configDetails.allowOnce ? [ 'Only one time' ] : [] ),
1795
+ ],
1796
+ },
1797
+ ...( differences?.approver ) ? { approver:
1798
+ { previous: differences?.approver?.previous.map( ( item ) => item.name ).toString(),
1799
+ new: differences?.approver?.new.map( ( item ) => item.name ).toString() },
1800
+ } : { approver: {} },
1801
+ ...( differences?.owner ) ? { owner:
1802
+ { previous: differences?.owner?.previous.map( ( item ) => item.name ).toString(),
1803
+ new: differences?.owner?.new.map( ( item ) => item.name ).toString() },
1804
+ } : { owner: {} },
1805
+ ...( req.body.checkListDetails.coverage == 'store' ) ? { storeAdded: req.body.added } :{ storeAdded: [] },
1806
+ ...( req.body.checkListDetails.coverage == 'store' ) ? { storeRemoved: req.body.removed } :{ storeRemoved: [] },
1807
+ ...( req.body.checkListDetails.coverage == 'user' ) ? { userAdded: req.body.added } :{ userAdded: [] },
1808
+ ...( req.body.checkListDetails.coverage == 'user' ) ? { userRemoved: req.body.removed } :{ userRemoved: [] },
1809
+ },
1810
+ };
1811
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, insertData );
1812
+ }
1813
+
1814
+ let logObj = {
1815
+ client_id: inputBody.checkListDetails.client_id,
1816
+ createAt: new Date(),
1817
+ sourceCheckList_id: inputBody.checkListDetails._id,
1818
+ checkListName: inputBody.checkListDetails.checkListName,
1819
+ fromCheckListName: '',
1820
+ type: 'checklist',
1821
+ action: inputBody.checkListDetails.publish ? 'published' : 'unpublished',
1822
+ storeName: '',
1823
+ store_id: '',
1824
+ createdByEmail: req.user.email,
1825
+ createdBy: req.user.userName,
1826
+ coverage: inputBody.checkListDetails.coverage,
1827
+ logDetails: {},
1828
+ };
1829
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, logObj );
1830
+
1616
1831
  if ( response?.modifiedCount || response?.matchedCount || response?.upsertedCount ) {
1617
1832
  return res.sendSuccess( { id, message: 'Configured Updated Successfully' } );
1618
1833
  }
@@ -1625,6 +1840,32 @@ export const updateConfigurev1 =async ( req, res ) => {
1625
1840
  }
1626
1841
  };
1627
1842
 
1843
+ function findObjectDifference( oldObj, newObj ) {
1844
+ const isEqual = ( a, b ) => JSON.stringify( a ) === JSON.stringify( b );
1845
+
1846
+ const getArrayDiff = ( oldArr, newArr ) => {
1847
+ const removed = oldArr.filter( ( item ) => !newArr.some( ( newItem ) => isEqual( newItem, item ) ) );
1848
+ const added = newArr.filter( ( item ) => !oldArr.some( ( oldItem ) => isEqual( oldItem, item ) ) );
1849
+ if ( removed.length || added.length ) {
1850
+ return { previous: oldArr, new: newArr };
1851
+ }
1852
+ return [];
1853
+ };
1854
+ const difference = Object.keys( oldObj ).reduce( ( diff, key ) => {
1855
+ oldObj[key] = oldObj[key] == null ? '' : oldObj[key];
1856
+ if ( !isEqual( oldObj[key], newObj[key] ) ) {
1857
+ if ( !Array.isArray( newObj[key] ) ) {
1858
+ diff[key] = { previous: oldObj[key], new: newObj[key] };
1859
+ } else {
1860
+ diff[key] = getArrayDiff( oldObj[key] || [], newObj[key] || [] );
1861
+ }
1862
+ }
1863
+ return diff;
1864
+ }, {} );
1865
+
1866
+ return difference;
1867
+ }
1868
+
1628
1869
  export const updatePublish = async ( req, res ) => {
1629
1870
  try {
1630
1871
  if ( typeof req?.body?.checklistId == 'undefined' && typeof req.body.type == 'undefined' ) {
@@ -1699,6 +1940,22 @@ export const updatePublish = async ( req, res ) => {
1699
1940
 
1700
1941
 
1701
1942
  await checklistService.updateOne( query, getCheckDetails );
1943
+ let logObj = {
1944
+ client_id: req.body.clientId,
1945
+ createAt: new Date(),
1946
+ sourceCheckList_id: req.body.checklistId,
1947
+ checkListName: getCheckDetails.checkListName,
1948
+ fromCheckListName: '',
1949
+ type: 'checklist',
1950
+ action: req.body.publish ? 'published' : 'unpublished',
1951
+ storeName: '',
1952
+ store_id: '',
1953
+ createdByEmail: req.user.email,
1954
+ createdBy: req.user.userName,
1955
+ coverage: getCheckDetails.coverage,
1956
+ logDetails: {},
1957
+ };
1958
+ await insertOpenSearchData( JSON.parse( process.env.OPENSEARCH ).traxActivityLog, logObj );
1702
1959
  if ( getCheckDetails.checkListType == 'custom' ) {
1703
1960
  let currentDate = dayjs.utc().format();
1704
1961
  let updatedscheduleEndTimeISO = dayjs.utc( getCheckDetails.scheduleEndTimeISO ).format( 'HH:mm:ss' );
@@ -3788,6 +4045,44 @@ export async function updateAssign( req, res ) {
3788
4045
  // req.body.assignedGroup = req.body.assignedGroup.map( ( ele ) => {
3789
4046
  // return { id: ele };
3790
4047
  // } );
4048
+ let getAssignedDetails = await assignedService.find( { checkListId: req.body.checkListId } );
4049
+ if ( getAssignedDetails.length ) {
4050
+ if ( req.body.coverage == 'store' ) {
4051
+ getAssignedDetails = [ ...new Set( getAssignedDetails.map( ( ele ) => ele.storeName ) ) ];
4052
+ } else {
4053
+ getAssignedDetails = [ ...new Set( getAssignedDetails.map( ( ele ) => ele.userName ) ) ];
4054
+ }
4055
+ }
4056
+
4057
+ let added = req.body.assignedList.filter( ( item ) => {
4058
+ let storeUsername;
4059
+ if ( req.body.coverage == 'store' ) {
4060
+ storeUsername = item.storeName;
4061
+ } else {
4062
+ storeUsername = item.userName;
4063
+ }
4064
+ if ( !getAssignedDetails.includes( storeUsername ) ) {
4065
+ return item;
4066
+ }
4067
+ } ).map( ( ele ) => {
4068
+ if ( req.body.coverage == 'store' ) {
4069
+ return ele.storeName;
4070
+ } else {
4071
+ return ele.userName;
4072
+ }
4073
+ } );
4074
+ let removed = getAssignedDetails.filter( ( item ) => {
4075
+ let list;
4076
+ if ( req.body.coverage == 'store' ) {
4077
+ list = req.body.assignedList.map( ( item ) => item.storeName );
4078
+ } else {
4079
+ list = req.body.assignedList.map( ( item ) => item.userName ); ;
4080
+ }
4081
+ if ( !list.includes( item ) ) {
4082
+ return item;
4083
+ }
4084
+ } );
4085
+
3791
4086
  await assignedService.deleteMany( { checkListId: req.body.checkListId } );
3792
4087
  let assignedUserList = [];
3793
4088
  let userEmailList = req.body.assignedList.map( ( ele ) => ele.userEmail.toLowerCase() );
@@ -3851,7 +4146,7 @@ export async function updateAssign( req, res ) {
3851
4146
  }
3852
4147
  } ) );
3853
4148
  await assignedService.insertMany( assignedUserList );
3854
- return res.sendSuccess( 'Assign details updated successfully' );
4149
+ return res.sendSuccess( { messgage: 'Assign details updated successfully', added, removed } );
3855
4150
  } catch ( e ) {
3856
4151
  logger.error( { functionName: 'updateAssign', error: e } );
3857
4152
  return res.sendError( e, 500 );
@@ -0,0 +1,18 @@
1
+ import express from 'express';
2
+ import { isAllowedInternalAPIHandler, isAllowedSessionHandler } from 'tango-app-api-middleware';
3
+
4
+ export const activityLogRouter = express.Router();
5
+
6
+ import {
7
+ insertLog,
8
+ updateLog,
9
+ listLog,
10
+ } from '../controllers/activityLog.controller.js';
11
+
12
+ activityLogRouter
13
+ .post( '/insertLog', isAllowedInternalAPIHandler, insertLog )
14
+ .post( '/updateLog', isAllowedInternalAPIHandler, updateLog )
15
+ .post( '/listLog', isAllowedSessionHandler, listLog );
16
+
17
+
18
+ export default activityLogRouter;