tango-app-api-trax 3.7.97 → 3.7.98

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-trax",
3
- "version": "3.7.97",
3
+ "version": "3.7.98",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -2147,6 +2147,36 @@ export async function AiPushNotificationAlert( req, res ) {
2147
2147
  }
2148
2148
  }
2149
2149
 
2150
+ export async function liveAiPushNotificationAlert( req, res ) {
2151
+ try {
2152
+ // console.log( req.body );
2153
+ let findAichecklist = await PCLconfig.findOne( { checkListType: req.body.checkListType, client_id: req.body.clientId, date_string: req.body.Date }, { aiConfig: 1 } );
2154
+ if ( findAichecklist?.aiConfig?.alertConfig?.mobile.length ) {
2155
+ // console.log( findAichecklist.aiConfig.alerts.users );
2156
+ for ( let user of findAichecklist?.aiConfig?.alertConfig?.mobile ) {
2157
+ let findOneUser = await userService.findOne( { email: user.value }, { fcmToken: 1 } );
2158
+ // console.log( findOneUser );
2159
+ if ( findOneUser&&findOneUser.fcmToken&&findOneUser.fcmToken!='' ) {
2160
+ // console.log( findOneUser.fcmToken );
2161
+ try {
2162
+ await sendPushNotification( req.body.title, req.body.description, findOneUser.fcmToken );
2163
+ } catch ( e ) {
2164
+ logger.error( {
2165
+ message: 'push notification',
2166
+ error: e,
2167
+ details: data,
2168
+ } );
2169
+ }
2170
+ } ;
2171
+ }
2172
+ }
2173
+ return res.sendSuccess( 'Push notification send successfully' );
2174
+ } catch ( e ) {
2175
+ logger.error( { function: 'AiPushNotificationAlert', error: e } );
2176
+ return res.sendError( e, 500 );
2177
+ }
2178
+ }
2179
+
2150
2180
 
2151
2181
  export async function taskPushNotification( req, res ) {
2152
2182
  try {
@@ -4009,3 +4039,123 @@ async function LamdaServiceCall( url, data ) {
4009
4039
  return false;
4010
4040
  }
4011
4041
  }
4042
+
4043
+ export async function sendAIEmailList( req, res ) {
4044
+ try {
4045
+ let spocList;
4046
+ let result = [];
4047
+ let getChecklistDetails = await CLconfig.findOne( { client_id: req.body.clientId, publish: true, checkListName: req.body.checklistName }, { aiConfig: 1 } );
4048
+ if ( getChecklistDetails && getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.enabled && getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email?.type?.length ) {
4049
+ let userList = [ ...getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email?.users.map( ( ele ) => ele.value ) ];
4050
+ if ( getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email?.type.includes( 'store' ) ) {
4051
+ let checklistAssignList = await CLassign.find( { checkListId: getChecklistDetails._id } );
4052
+ let storeSpoc = await storeService.find( { storeName: { $in: checklistAssignList.map( ( ele ) => ele.storeName ) } }, { spocDetails: 1, storeName: 1, storeId: 1 } );
4053
+ if ( storeSpoc.length ) {
4054
+ spocList = storeSpoc.map( ( ele ) => {
4055
+ return { email: ele?.spocDetails?.[0]?.email, store: ele?.storeId };
4056
+ } );
4057
+ }
4058
+ }
4059
+ if ( getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email?.type.includes( 'teams' ) ) {
4060
+ let teamDetails = await teamsServices.findteams( { teamName: { $in: getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email?.teams } }, { spocDetails: 1 } );
4061
+ if ( teamDetails.length ) {
4062
+ userList.push( teamDetails.flatMap( ( team ) => team.users.map( ( user ) => user.email ) ) );
4063
+ }
4064
+ }
4065
+ if ( getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email?.type.includes( 'users' ) ) {
4066
+ userList.push( getChecklistDetails?.aiConfig?.alertConfig?.alertSendTo?.email.users.map( ( team ) => team.value ) );
4067
+ }
4068
+
4069
+
4070
+ await Promise.all( userList.map( async ( email ) => {
4071
+ let stores = [];
4072
+ let userDetails = await userService.findOne( { email: email } );
4073
+ if ( userDetails ) {
4074
+ if ( userDetails.userType === 'client' && userDetails.role !== 'superadmin' ) {
4075
+ let storeIds = new Set( userDetails.assignedStores.map( ( store ) => store.storeId ) );
4076
+
4077
+ // Fetch clusters and teams in parallel
4078
+ const [ clustersList, teamsList ] = await Promise.all( [
4079
+ clusterServices.findcluster( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userDetails.email } } } ),
4080
+ teamsServices.findteams( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userDetails.email } } } ),
4081
+ ] );
4082
+
4083
+ // Process clusters
4084
+ if ( clustersList.length > 0 ) {
4085
+ for ( let cluster of clustersList ) {
4086
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
4087
+ }
4088
+ }
4089
+
4090
+ // Process teams
4091
+ if ( teamsList.length > 0 ) {
4092
+ for ( let team of teamsList ) {
4093
+ for ( let user of team.users ) {
4094
+ let findUser = await userService.findOne( { _id: user.userId } );
4095
+ if ( findUser && findUser.assignedStores?.length > 0 ) {
4096
+ findUser.assignedStores.forEach( ( store ) => storeIds.add( store.storeId ) );
4097
+ }
4098
+
4099
+ // Fetch clusters for the user
4100
+ let userClustersList = await clusterServices.findcluster( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: findUser.email } } } );
4101
+ if ( userClustersList.length > 0 ) {
4102
+ for ( let cluster of userClustersList ) {
4103
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
4104
+ }
4105
+ }
4106
+ }
4107
+ }
4108
+ }
4109
+ let TeamMember = await teamsServices.findteams( { clientId: userDetails.clientId, users: { $elemMatch: { email: userDetails.email } } } );
4110
+ if ( TeamMember&&TeamMember.length>0 ) {
4111
+ for ( let team of TeamMember ) {
4112
+ let clusterList = await clusterServices.findcluster( { clientId: userDetails.clientId, teams: { $elemMatch: { name: team.teamName } } } );
4113
+ if ( clusterList.length > 0 ) {
4114
+ for ( let cluster of clusterList ) {
4115
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
4116
+ }
4117
+ }
4118
+ }
4119
+ }
4120
+ let TeamLeader = await teamsServices.findteams( { clientId: userDetails.clientId, Teamlead: { $elemMatch: { email: userDetails.email } } } );
4121
+ if ( TeamLeader&&TeamLeader.length>0 ) {
4122
+ for ( let team of TeamLeader ) {
4123
+ let clusterList = await clusterServices.findcluster( { clientId: userDetails.clientId, teams: { $elemMatch: { name: team.teamName } } } );
4124
+ if ( clusterList.length > 0 ) {
4125
+ for ( let cluster of clusterList ) {
4126
+ cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
4127
+ }
4128
+ }
4129
+ }
4130
+ }
4131
+ stores = Array.from( storeIds );
4132
+ }
4133
+ let data = {
4134
+ email: email,
4135
+ stores,
4136
+ role: userDetails.role,
4137
+ };
4138
+ result.push( data );
4139
+ }
4140
+ } ) );
4141
+
4142
+ spocList.forEach( ( ele ) => {
4143
+ let findIndex = result.findIndex( ( element ) => element.email == ele.email );
4144
+ if ( findIndex != -1 ) {
4145
+ result[findIndex].stores = new Set( ...result[findIndex].stores, ele.store );
4146
+ } else {
4147
+ result.push( {
4148
+ email: ele.email,
4149
+ stores: ele.store,
4150
+ role: 'admin',
4151
+ } );
4152
+ }
4153
+ } );
4154
+ }
4155
+
4156
+ return res.sendSuccess( result );
4157
+ } catch ( e ) {
4158
+ logger.error( { functionName: 'sendAIEmailList', error: e } );
4159
+ return res.sendError( e, 500 );
4160
+ }
4161
+ }
@@ -39,6 +39,8 @@ internalTraxRouter
39
39
  .post( '/runAIFlag', isAllowedInternalAPIHandler, internalController.runAIFlag )
40
40
  .post( '/downloadInsertPdf', isAllowedInternalAPIHandler, internalController.downloadInsertPdf )
41
41
  .get( '/checklistAutoMailList', isAllowedInternalAPIHandler, internalController.checklistAutoMailList )
42
+ .post( '/sendAIEmailList', isAllowedInternalAPIHandler, internalController.sendAIEmailList )
43
+ .post( '/liveAiPushNotificationAlert', isAllowedInternalAPIHandler, internalController.liveAiPushNotificationAlert )
42
44
  ;
43
45
 
44
46