tango-app-api-task 3.7.27 → 3.7.29

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-task",
3
- "version": "3.7.27",
3
+ "version": "3.7.29",
4
4
  "description": "Task",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -7090,19 +7090,36 @@ export async function AITaskCreation( req, res ) {
7090
7090
  let inputBody = req.body;
7091
7091
 
7092
7092
  inputBody.taskDescription = '';
7093
+
7094
+ let checklistDetails = await checklistService.findOne( { checkListType: req.body.aiType, publish: true, client_id: req.body.clientId }, { aiConfig: 1 } );
7095
+
7096
+ if ( !checklistDetails?.aiConfig?.autoTaskConfig?.enabled ) {
7097
+ return res.sendError( 'No data found', 204 );
7098
+ }
7099
+ if ( !checklistDetails ) {
7100
+ return res.sendError( 'No data found', 204 );
7101
+ }
7093
7102
  // Step:1 Store Validation
7094
7103
  let storeData = await storeService.findOne( { storeName: inputBody.storeName, clientId: inputBody.clientId, status: 'active' }, { storeId: 1, storeName: 1, storeProfile: 1, spocDetails: 1 } );
7095
7104
  if ( !storeData ) {
7096
7105
  return res.sendError( 'Store Not Found', 500 );
7097
7106
  }
7098
-
7099
- if ( inputBody.taskAssigned == 'teams' ) {
7100
- let teamDetails = await teamsServices.findteams( { teamName: { $in: inputBody.userList }, clientId: inputBody.clientId }, { users: 1 } );
7107
+ let teamDetails;
7108
+ inputBody['userEmail'] = [];
7109
+ if ( checklistDetails?.aiConfig?.autoTaskConfig?.taskAssignedTo == 'teams' ) {
7110
+ teamDetails = await teamsServices.findteams( { teamName: { $in: checklistDetails?.aiConfig?.autoTaskConfig?.users?.map( ( ele ) => ele.name ) }, clientId: inputBody.clientId }, { users: 1 } );
7101
7111
  if ( teamDetails.length ) {
7102
- inputBody.userEmail = teamDetails.map( ( team ) => team.users.map( ( user ) => user.email ) );
7112
+ inputBody.userEmail.push( ...teamDetails.flatMap( ( team ) => team.users.map( ( user ) => user.email ) ) );
7103
7113
  }
7104
7114
  }
7105
7115
 
7116
+ if ( checklistDetails?.aiConfig?.autoTaskConfig?.taskAssignedTo == 'store' ) {
7117
+ inputBody.userEmail.push( storeData?.spocDetails?.[0]?.email );
7118
+ }
7119
+ if ( checklistDetails?.aiConfig?.autoTaskConfig?.taskAssignedTo == 'user' ) {
7120
+ inputBody.userEmail.push( ...checklistDetails?.aiConfig?.autoTaskConfig?.users.map( ( team ) => team.value ) );
7121
+ }
7122
+
7106
7123
  let finduser;
7107
7124
  // Step:2 User Validation
7108
7125
  if ( inputBody.userEmail.length ) {
@@ -7119,6 +7136,7 @@ export async function AITaskCreation( req, res ) {
7119
7136
  userId: '$_id',
7120
7137
  userName: 1,
7121
7138
  userEmail: '$email',
7139
+ fcmToken: 1,
7122
7140
  },
7123
7141
  },
7124
7142
  ];
@@ -7129,7 +7147,8 @@ export async function AITaskCreation( req, res ) {
7129
7147
  } else {
7130
7148
  // get Store Spoc
7131
7149
  if ( storeData.spocDetails[0].email && storeData.spocDetails[0].email !='' ) {
7132
- finduser =[ { userId: storeData.spocDetails[0]._id, userName: storeData.spocDetails[0].name, userEmail: storeData.spocDetails[0].email } ];
7150
+ let finduserData = await userService.findOne( { email: storeData.spocDetails[0].email } );
7151
+ finduser =[ { userId: finduserData?._id, userName: finduserData?.userName, userEmail: finduserData.email, fcmToken: finduserData.fcmToken } ];
7133
7152
  } else {
7134
7153
  return res.sendError( 'store spoc email invalid', 500 );
7135
7154
  }
@@ -7137,11 +7156,85 @@ export async function AITaskCreation( req, res ) {
7137
7156
 
7138
7157
  // return finduser;
7139
7158
  // console.log( 'finduser =>', finduser );
7140
- if ( !finduser.length ) {
7141
- return res.sendError( 'No user Found For this store', 500 );
7159
+ if ( [ 'user', 'teams' ].includes( checklistDetails?.aiConfig?.autoTaskConfig?.taskAssignedTo ) ) {
7160
+ let finduserList = finduser;
7161
+ finduser = [];
7162
+
7163
+ await Promise.all( finduserList.map( async ( user ) => {
7164
+ let stores = [];
7165
+ let userDetails = await userService.findOne( { email: user.userEmail } );
7166
+ if ( userDetails ) {
7167
+ if ( userDetails.userType === 'client' && userDetails.role !== 'superadmin' ) {
7168
+ let storeIds = new Set( userDetails.assignedStores.map( ( store ) => store.storeId ) );
7169
+
7170
+ // Fetch clusters and teams in parallel
7171
+ const [ clustersList, teamsList ] = await Promise.all( [
7172
+ clusterServices.findcluster( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userDetails.email } } } ),
7173
+ teamsServices.findteams( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userDetails.email } } } ),
7174
+ ] );
7175
+
7176
+ // Process clusters
7177
+ if ( clustersList.length > 0 ) {
7178
+ for ( let cluster of clustersList ) {
7179
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
7180
+ }
7181
+ }
7182
+
7183
+ // Process teams
7184
+ if ( teamsList.length > 0 ) {
7185
+ for ( let team of teamsList ) {
7186
+ for ( let user of team.users ) {
7187
+ let userInfo = await userService.findOne( { _id: user.userId } );
7188
+ if ( userInfo && userInfo.assignedStores?.length > 0 ) {
7189
+ userInfo.assignedStores.forEach( ( store ) => storeIds.add( store.storeId ) );
7190
+ }
7191
+
7192
+ // Fetch clusters for the user
7193
+ let userClustersList = await clusterServices.findcluster( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userInfo.email } } } );
7194
+ if ( userClustersList.length > 0 ) {
7195
+ for ( let cluster of userClustersList ) {
7196
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
7197
+ }
7198
+ }
7199
+ }
7200
+ }
7201
+ }
7202
+ let TeamMember = await teamsServices.findteams( { clientId: userDetails.clientId, users: { $elemMatch: { email: userDetails.email } } } );
7203
+ if ( TeamMember&&TeamMember.length>0 ) {
7204
+ for ( let team of TeamMember ) {
7205
+ let clusterList = await clusterServices.findcluster( { clientId: userDetails.clientId, teams: { $elemMatch: { name: team.teamName } } } );
7206
+ if ( clusterList.length > 0 ) {
7207
+ for ( let cluster of clusterList ) {
7208
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
7209
+ }
7210
+ }
7211
+ }
7212
+ }
7213
+ let TeamLeader = await teamsServices.findteams( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userDetails.email } } } );
7214
+ if ( TeamLeader&&TeamLeader.length>0 ) {
7215
+ for ( let team of TeamLeader ) {
7216
+ let clusterList = await clusterServices.findcluster( { clientId: userDetails.clientId, teams: { $elemMatch: { name: team.teamName } } } );
7217
+ if ( clusterList.length > 0 ) {
7218
+ for ( let cluster of clusterList ) {
7219
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
7220
+ }
7221
+ }
7222
+ }
7223
+ }
7224
+ stores = Array.from( storeIds );
7225
+ }
7226
+ if ( stores.includes( storeData.storeId ) ) {
7227
+ finduser.push( user );
7228
+ }
7229
+ }
7230
+ } ) );
7142
7231
  }
7143
7232
 
7233
+ if ( !finduser.length ) {
7234
+ return res.sendError( 'No data found', 204 );
7235
+ }
7144
7236
 
7237
+ inputBody['approver'] = checklistDetails?.aiConfig?.autoTaskConfig.approver?.map( ( ele ) => ele?.value );
7145
7238
  if ( inputBody.approver && !inputBody.approver.length ) {
7146
7239
  await Promise.all( finduser.map( async ( user ) => {
7147
7240
  let teamList = await findteams( { users: { $elemMatch: { email: user.userEmail } } } );
@@ -7159,27 +7252,25 @@ export async function AITaskCreation( req, res ) {
7159
7252
  // Send Push Notification
7160
7253
  // inputBody.approver = inputBody.approver.replace( /,$/, '' );
7161
7254
  let title = `New Task Alert ${inputBody.taskName}-${storeData.storeName}-${dayjs().format( 'YYYY-MM-DD' )}`;
7162
- let time = inputBody?.scheduleEndTime || '11:59 PM';
7163
- let date = inputBody?.scheduleDate || dayjs().format( 'YYYY-MM-DD' );
7255
+ let time = dayjs.utc().format( 'hh:mm A' );
7256
+ let date = dayjs.utc().format( 'YYYY-MM-DD' );
7164
7257
  let description = `A new task has been assigned to ${storeData.storeName}. Please complete it before the due date of ${date}.`;
7165
7258
  await Promise.all( finduser.map( async ( ele ) => {
7166
- let userDetails = userService.findOne( { _id: new mongoose.Types.ObjectId( ele.userId ) } );
7167
- if ( userDetails && userDetails.fcmToken ) {
7168
- const fcmToken = userDetails.fcmToken;
7259
+ if ( ele && ele.fcmToken ) {
7260
+ const fcmToken = ele.fcmToken;
7169
7261
  sendPushNotification( title, description, fcmToken );
7170
7262
  }
7171
7263
  } ) );
7172
7264
 
7173
- let scheduleEndData = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).format();
7174
-
7175
- if ( inputBody.taskValidity == 'min' ) {
7176
- scheduleEndData = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).add( inputBody.taskValidity.time, 'miuntes' ).format();
7265
+ inputBody['scheduleEndData'] = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).format();
7266
+ if ( checklistDetails?.aiConfig?.autoTaskConfig.taskValidity?.type == 'mins' ) {
7267
+ inputBody['scheduleEndData'] = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).add( checklistDetails?.aiConfig?.autoTaskConfig.taskValidity.time, 'minute' ).format();
7177
7268
  }
7178
- if ( inputBody.taskValidity == 'hour' ) {
7179
- scheduleEndData = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).add( inputBody.taskValidity.time, 'hour' ).format();
7269
+ if ( checklistDetails?.aiConfig?.autoTaskConfig.taskValidity?.type == 'hours' ) {
7270
+ inputBody['scheduleEndData'] = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).add( checklistDetails?.aiConfig?.autoTaskConfig.taskValidity.time, 'hour' ).format();
7180
7271
  }
7181
- if ( inputBody.taskValidity == 'day' ) {
7182
- scheduleEndData = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).add( inputBody.taskValidity.time, 'day' ).format();
7272
+ if ( checklistDetails?.aiConfig?.autoTaskConfig.taskValidity?.type == 'day' ) {
7273
+ inputBody['scheduleEndData'] = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).add( checklistDetails?.aiConfig?.autoTaskConfig.taskValidity, 'day' ).format();
7183
7274
  }
7184
7275
 
7185
7276
  const inputDateTime = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' );
@@ -7353,7 +7444,6 @@ export async function AITaskCreation( req, res ) {
7353
7444
  };
7354
7445
  let output = await getObject( inputData );
7355
7446
  // / Check Bucket
7356
- console.log( 'output =>', output );
7357
7447
  let image = {
7358
7448
  data: output.Body,
7359
7449
  name: imgpath,
@@ -7474,7 +7564,8 @@ async function insertAItaskProcessedTaskCreation( checkTask, inputBody, date, ti
7474
7564
  oldData.date_iso = new Date( dayjs( date, 'YYYY-MM-DD' ).format( 'YYYY-MM-DD' ) );
7475
7565
  oldData.date_string = dayjs( date ).format( 'YYYY-MM-DD' );
7476
7566
  oldData.scheduleStartTime_iso = date;
7477
- oldData.scheduleEndTime_iso = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).format();
7567
+ oldData.scheduleEndTime_iso = dayjs.utc( inputBody.scheduleEndData, 'YYYY-MM-DD hh:mm A' ).format();
7568
+ oldData.scheduleEndTime = dayjs.utc( inputBody.scheduleEndData, 'YYYY-MM-DD hh:mm A' ).format( 'hh:mm A' );
7478
7569
  oldData.store_id = storeDetails.storeId;
7479
7570
  oldData.storeName = storeDetails.storeName;
7480
7571
  oldData.country = storeDetails.storeProfile.country;