Esempio n. 1
0
class Connections(object, metaclass=Singleton):
    def __init__(self):
        ''' 
            Description:    Initialization internal method, called internally
            usage:          Called internally
            Return:         N/A
        '''
        self.env = Environment()
        self.util = Utility()
        self.mongo = MongoDB()
        self.dbutil = DBUtility()
        self.globaL = Global()
        self.activity = Activity()
        self.member = Member()

        self.myClass = self.__class__.__name__
        self.myModuleLogger = logging.getLogger('uConnect.' + str(__name__) +
                                                '.' + self.myClass)

    def __AddAConnection(self, argRequestDict):
        ''' This mehtod is for building a fresh new connection
            usage:          <__AddMember2MemberConnection(<argReqJsonDict>)
                            MainArg['MemberId','ConnectMemberId','Auth']
            Return:         Success/UnSuccess
        '''
        try:
            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug(
                'Argument [{arg}] received'.format(arg=argRequestDict))
            ''' declaring/initializing variables '''
            myRequestStatus = self.util.getCopy(
                self.globaL._Global__RequestStatus)
            isReqConnectionAdded = self.globaL._Global__False
            isInvConnectionAdded = self.globaL._Global__False
            isCleanUpDone = self.globaL._Global__False
            myArgKey = ['_id', 'Type', 'ConnectionId', 'ConnectionType']
            ''' validating arguments '''
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(
                    myArgValMessage)
            #fi
            ''' validating Requestor entity
            must be a valid id and its connection can not be same '''
            if myMainArgData['Type'] == self.globaL._Global__member:
                #validating
                if myMainArgData['_id'] == myMainArgData['ConnectionId']:
                    raise com.uconnect.core.error.MissingArgumentValues(
                        'Arg validation error, Member [{member}] and it\'s Connection[{connect}] can not be same'
                        .format(member=myMainArgData['_id'],
                                connect=myMainArgData['ConnectMemberId']))
                #fi
                myMemberValidationData = {
                    '_id': myMainArgData['ConnectionId'],
                    'ResponseMode': self.globaL._Global__InternalRequest
                }
                if (not self.member._Member__isAValidMember(
                        myMemberValidationData)):
                    raise com.uconnect.core.error.MissingArgumentValues(
                        'Arg validation error, Invalid MemberId [{member}]'.
                        format(member=myMainArgData['ConnectionId']))
                #fi
                myRequestorCollection = self.globaL._Global__memberColl
                # requestor is Member and it has been validated, preparing Invitee (revese)connection which will be a member
                # connection
                myInviteeCriteria = {'_id': myMainArgData['ConnectionId']}
                myInviteeConn = self.util.getConnTemplateCopy(
                    self.globaL._Global__member)
                myInviteeConn['Id'] = myMainArgData['_id']
                myInviteeConn['Status'] = self.util.getConnStatus4Action(
                    'New Connection', 'Invitee')
                myInviteeConnData = {'Connections': myInviteeConn}

                self.myModuleLogger.debug(
                    'Invitee [{invitee}] connection document [{doc}] prepared'.
                    format(invitee=myMainArgData['ConnectionId'],
                           doc=myInviteeConn))

            elif myMainArgData['Type'] == 'Agent':
                pass
            #fi

            #validating connection id
            if myMainArgData['ConnectionType'] == self.globaL._Global__member:
                # this is a member 2 member connection
                myMemberValidationData = {
                    '_id': myMainArgData['ConnectionId'],
                    'ResponseMode': self.globaL._Global__InternalRequest
                }
                if (not self.member._Member__isAValidMember(
                        myMemberValidationData)):
                    raise com.uconnect.core.error.MissingArgumentValues(\
                        'Arg validation error, Invalid Connect MemberId [{member}]'.format(member=myMainArgData['ConnectionId'] ))
                #fi
                myInviteeCollection = self.globaL._Global__memberColl
                # Preparing Requestor connection
                myRequestorCriteria = {'_id': myMainArgData['_id']}
                myRequestorConn = self.util.getConnTemplateCopy(
                    self.globaL._Global__member)
                myRequestorConn['Id'] = myMainArgData['ConnectionId']
                myRequestorConn['Status'] = self.util.getConnStatus4Action(
                    'New Connection', 'Requestor')
                myRequestorConnData = {'Connections': myRequestorConn}

                self.myModuleLogger.debug(
                    'Requestor [{requestor}] connection document [{doc}] prepared'
                    .format(requestor=myMainArgData['_id'],
                            doc=myRequestorConn))

            elif myMainArgData['ConnectionType'] == 'Agent':
                pass
            #fi

            #persisiting requestor connection data
            myReqConnectionResult = self.mongo.UpdateDoc(
                myRequestorCollection, myRequestorCriteria,
                myRequestorConnData, 'addToSet', False)
            if self.util.getUpdateStatus(
                    myReqConnectionResult) == self.globaL._Global__Success:
                # Requestor connection created successfully
                self.myModuleLogger.debug('Requestor connection [{conn}] created successfully, result [{result}]'.
                    format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + myMainArgData['ConnectionType']\
                            + ' ' + str(myMainArgData['ConnectionId']), result=myReqConnectionResult))

                isReqConnectionAdded = self.globaL._Global__True

                #building invitee connection (revese of requestor)
                myInvConnectionResult = self.mongo.UpdateDoc(
                    myInviteeCollection, myInviteeCriteria, myInviteeConnData,
                    'addToSet', False)
                if self.util.getUpdateStatus(
                        myInvConnectionResult) == self.globaL._Global__Success:
                    #invitee connection created successful
                    isInvConnectionAdded = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(
                        self.globaL._Global__Success)

                    # recording this successful activity; requestor
                    self.activity._Activity__logActivity(self.util.buildActivityArg( \
                        myMainArgData['_id'], myRequestorCollection,self.globaL._Global__Internal,\
                        'Requestor Connection [{conn}] created successfully'.format(conn=myMainArgData['Type'] + ' ' + \
                        str(myMainArgData['_id']) + ' --> ' + myMainArgData['ConnectionType'] + ' ' + \
                        str(myMainArgData['ConnectionId'])) ))
                    #recording this successful activity; invitee
                    self.activity._Activity__logActivity(self.util.buildActivityArg(\
                        myMainArgData['_id'],myInviteeCollection,self.globaL._Global__Internal,\
                        'Invitee Connection [{conn}] created successfully'.format(conn=myMainArgData['ConnectionType'] + ' ' + \
                        str(myMainArgData['ConnectionId']) + ' --> ' + myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])) ))
                else:
                    ''' invitee connection is not successful, remvoving requestor connection to maintain the data integrity '''
                    self.myModuleLogger.debug('Invitee connection [{conn}] creation unsuccessful, result [{result}]'.
                        format(conn = myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId'])  + ' -> ' + \
                                myMainArgData['Type'] + ' ' + str(myMainArgData['_id']), result=myInvConnectionResult))
                    ''' rolling back previous Requestor connection'''
                    self.mongo.UpdateDoc(myInviteeCollection,
                                         myRequestorCriteria,
                                         myRequestorConnData, 'pull', False)
                    isCleanUpDone = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(
                        self.globaL._Global__UnSuccess,
                        'Could not build Invitee [{inv}] while performing "Add Connection" task'
                        .format(inv=myMainArgData['ConnectionId']))
                #fi
            else:
                self.myModuleLogger.debug('Requestor connection [{conn}] creation unsuccesful, result [{result}]'.
                    format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + myMainArgData['ConnectionType']\
                            + ' ' + str(myMainArgData['ConnectionId']), result=myReqConnectionResult))
                myRequestStatus = self.util.getRequestStatus(
                    self.globaL._Global__UnSuccess,
                    'Could not build Requestor [{req}] while performing "Add Connection" task'
                    .format(req=myMainArgData['_id']))
            #fi

            return myRequestStatus

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            if isReqConnectionAdded and (not isInvConnectionAdded) and (
                    not isCleanUpDone):
                #print(isReqConnectionAdded,isInvConnectionAdded,isCleanUpDone)
                self.mongo.UpdateDoc(self.globaL._Global__memberColl,
                                     myRequestorCriteria, myRequestorConnData,
                                     'pull', False)
            #fi
            return myRequestStatus

    #__AddMember2MemberConnection Ends here

    def __acceptInvitation(self, argRequestDict):
        ''' This method is invoked by Invitee; update the status of connection status in each other (Requestor/Invitee)'s connection list 
            usage:          <__AcceptInvitation(<argReqJsonDict>)
                            MainArg['_id','Type','ConnectionId','ConnectionType','Auth']
            Return:         Success/UnSuccess
        '''
        try:
            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug(
                'Argument [{arg}] received'.format(arg=argRequestDict))
            ''' declaring/initializing variables '''
            myRequestStatus = self.util.getCopy(
                self.globaL._Global__RequestStatus)
            isReqConnectionAdded = self.globaL._Global__False
            isInvConnectionAdded = self.globaL._Global__False
            isCleanUpDone = self.globaL._Global__False

            if myMainArgData['Type'] == self.globaL._Global__member:
                myInviteeCollection = self.globaL._Global__memberColl
            elif myMainArgData['Type'] == self.globaL._Global__agent:
                myInviteeCollection = self.globaL._Global__agentColl
            #fi

            if myMainArgData['ConnectionType'] == self.globaL._Global__member:
                myRequestorCollection = self.globaL._Global__memberColl
            elif myMainArgData['ConnectionType'] == self.globaL._Global__agent:
                myRequestorCollection = self.globaL._Global__agentColl
            #fi
            ''' validating arguments '''
            myArgKey = ['_id', 'Type', 'ConnectionId', 'ConnectionType']
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(
                    myArgValMessage)
            #fi
            ''' Validate auth key for this request (we dont need auth val since it has been already performed by BPS process,
            we need to ensure that any call to this process is from BPS process '''
            ''' we need to check if this is a valid request; is this invitation pending for this id (id is invitee)'''
            myInviValCriteria = {'_id':myMainArgData['_id'],'Connections.Id':myMainArgData['ConnectionId'],\
                                'Connections.Type': myMainArgData['ConnectionType'],\
                                'Connections.Status':self.util.getConnStatus4Action('New Connection','Invitee')}
            #print('Invitee validation',myInviValCriteria)
            if self.mongo.findTotDocuments(myInviteeCollection,
                                           myInviValCriteria) == 0:
                raise com.uconnect.core.error.MissingArgumentValues(\
                    'Invalid Request, Invitee [{invitee}] does not have any connection pending from [{requestor}]'.\
                            format( invitee=myMainArgData['Type'] + str(myMainArgData['_id']),\
                                    requestor= myMainArgData['ConnectionType'] + str(myMainArgData['ConnectionId']) ))
            #fi
            # need to validate if this connectin is in wait state
            ''' Building Invitee connection data'''
            myInviteeCriteria = {
                '_id': myMainArgData['_id'],
                'Connections.Id': myMainArgData['ConnectionId']
            }
            #                     'Connections.Type': myMainArgData['ConnectionType'],\
            #                     'Connections.Status': self.util.getConnStatus4Action('New Connection','Invitee')}
            myInviteeConnData = {
                'Connections.$.Status':
                self.util.getConnStatus4Action('Accept Connection', 'Invitee')
            }
            ''' Building Requestor connection data '''
            myRequestorCriteria = {
                '_id': myMainArgData['ConnectionId'],
                'Connections.Id': myMainArgData['_id']
            }
            #                       'Connections.Type':myMainArgData['Type'],\
            #                       'Connections.Status': self.util.getConnStatus4Action('New Connection','Requestor')}
            myRequestorConnData = {
                'Connections.$.Status':
                self.util.getConnStatus4Action('Accept Connection',
                                               'Requestor')
            }
            ''' persisitng changes in database, Updating invitee's connection status '''
            myConnectionResult = self.mongo.UpdateDoc(myInviteeCollection,
                                                      myInviteeCriteria,
                                                      myInviteeConnData, 'set',
                                                      False)
            self.myModuleLogger.debug('Invitee connection status updated, [{conn}], result [{result}] '.
                format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + \
                              myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']),\
                              result=myConnectionResult))

            if self.util.getUpdateStatus(
                    myConnectionResult) == self.globaL._Global__Success:
                # Invitee connection update is successful, persisiting Requestor's connection status update
                isInvConnStatusUpdated = self.globaL._Global__True
                myConnectionResult = self.mongo.UpdateDoc(
                    myRequestorCollection, myRequestorCriteria,
                    myRequestorConnData, 'set', False)
                self.myModuleLogger.debug('Requestor connection status updated, [{conn}], result [{result}] '.
                format(conn = myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId'])  + ' -> ' + \
                              myMainArgData['Type'] + ' ' + str(myMainArgData['_id']),\
                              result=myConnectionResult))

                if self.util.getUpdateStatus(
                        myConnectionResult) == self.globaL._Global__Success:
                    ''' Requestor connection update is successful, undo Invitee connection change '''
                    isInvConnStatusUpdated = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(
                        self.globaL._Global__Success)
                    # recording this successful activity; Invitee
                    self.activity._Activity__logActivity(self.util.buildActivityArg( \
                        myMainArgData['_id'], myRequestorCollection,self.globaL._Global__Internal,\
                        'Invitee accepted connection [{conn}] '.format(conn=myMainArgData['Type'] + ' ' + str(myMainArgData['_id']) + \
                        ' --> ' + myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId'])) ))
                    #recording this successful activity; invitee
                    self.activity._Activity__logActivity(self.util.buildActivityArg(\
                        myMainArgData['_id'],myInviteeCollection,self.globaL._Global__Internal,\
                        'Requestor connection [{conn}] status updated'.format(conn=myMainArgData['ConnectionType'] + ' ' + \
                        str(myMainArgData['ConnectionId']) + ' --> ' + myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])) ))

                else:
                    ''' Requestor connection update is not successful, undo Invitee connection change '''
                    self.myModuleLogger.debug(
                        'Requestor [{req}] connection update unsuccessful, results [{result}]'
                        .format(req=myMainArgData['Connectiontype'] + ' ' +
                                str(myMainArgData['ConnectionId']),
                                result=myReqConnResults))

                    myInviteeConnData = {
                        'Connections.$.Status':
                        self.util.getConnStatus4Action('New Connection',
                                                       'Invitee')
                    }
                    self.mongo.UpdateDoc(myInviteeCollection,
                                         myInviteeCriteria, myInviteeConnData,
                                         'set', False)
                    isCleanUpDone = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                        'Requestor [{req}] connection status [{status}] update unsuccessful'.\
                        format(req=myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']), \
                               status=self.globaL._Global__Accepted_Req_ConnectionStatus))
                    self.myModuleLogger.debug('undo changes to Invitee\'s connection successful, result [{result}]'.\
                        format(result=myReqConnResults))
                #fi
            else:
                myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                    'Invitee [{inv}] connection status [{status}] update unsuccessful'. \
                    format(inv= myMainArgData['Type'] + ' ' + str(myMainArgData['_id']),
                            status=self.globaL._Global__Accepted_Inv_ConnectionStatus))
            #fi
            return myRequestStatus
        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            ''' we need to ensure if cleanup is required should there be an issue during failure of Invitee connection '''
            if isInvConnStatusUpdated and (not isReqConnStatusUpdated) and (
                    not isCleanUpDone):
                myInviteeConnData = {
                    'Connections.$.Status':
                    self.util.getConnStatus4Action('New Connection', 'Invitee')
                }
                self.mongo.UpdateDoc(myInviteeCollection, myInviteeCriteria,
                                     myInviteeConnData, 'set', False)
            #fi
            return myRequestStatus

    #__AcceptInvitation Ends here

    def __rejectInvitation(self, argRequestDict):
        ''' This method is invoked by Invitee; remove the connection from each other (Requestor/Invitee)'s connection list 
            usage:          <__rejectInvitation(<argReqJsonDict>)
                            MainArg['_id','Type','ConnectionId','ConnectionType','Auth']
            Return:         Success/UnSuccess
        '''
        try:
            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug(
                'Argument [{arg}] received'.format(arg=argRequestDict))
            ''' declaring/initializing variables '''
            myRequestStatus = self.util.getCopy(
                self.globaL._Global__RequestStatus)
            isInvConnStatusRemoved = self.globaL._Global__False
            isCleanUpDone = self.globaL._Global__False

            if myMainArgData['Type'] == self.globaL._Global__member:
                myInviteeCollection = self.globaL._Global__memberColl
            elif myMainArgData['Type'] == self.globaL._Global__agent:
                myInviteeCollection = self.globaL._Global__agentColl
            #fi

            if myMainArgData['ConnectionType'] == self.globaL._Global__member:
                myRequestorCollection = self.globaL._Global__memberColl
            elif myMainArgData['ConnectionType'] == self.globaL._Global__agent:
                myRequestorCollection = self.globaL._Global__agentColl
            #fi
            ''' validating arguments '''
            myArgKey = ['_id', 'Type', 'ConnectionId', 'ConnectionType']
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(
                    myArgValMessage)
            #fi
            ''' Validate auth key for this request (we dont need auth val since it has been already performed by BPS process,
            we need to ensure that any call to this process is from BPS process '''
            ''' we need to check if this is a valid request; is this invitation pending for this id (id is invitee)'''
            myInviValCriteria = {'_id':myMainArgData['_id'],'Connections.Id':myMainArgData['ConnectionId'],\
                                'Connections.Type': myMainArgData['ConnectionType'],\
                                'Connections.Status':self.util.getConnStatus4Action('New Connection','Invitee')}

            #print('Invitee validation',myInviValCriteria)
            if self.mongo.findTotDocuments(myInviteeCollection,
                                           myInviValCriteria) == 0:
                raise com.uconnect.core.error.MissingArgumentValues(\
                    'Invalid Request, Invitee [{invitee}] does not have pending connection from [{requestor}]'.\
                            format( invitee=myMainArgData['Type'] + str(myMainArgData['_id']),\
                                    requestor= myMainArgData['ConnectionType'] + str(myMainArgData['ConnectionId']) ))
            #fi
            ''' Building Invitee connection data to be removed'''
            myInviteeCriteria = {'_id':myMainArgData['_id'],'Connections.Id':myMainArgData['ConnectionId'], \
                                 'Connections.Type': myMainArgData['ConnectionType'],\
                                 'Connections.Status': self.util.getConnStatus4Action('New Connection','Invitee')}
            myInviteeConnData = {'Connections':{'Id': myMainArgData['ConnectionId'],'Type': myMainArgData['ConnectionType'],\
                                                'Status' : self.util.getConnStatus4Action('New Connection','Invitee')}}
            ''' Building Requestor connection data to be removed'''
            myRequestorCriteria = {'_id':myMainArgData['ConnectionId'], 'Connections.Id':myMainArgData['_id'],\
                                   'Connections.Type':myMainArgData['Type'],\
                                   'Connections.Status': self.util.getConnStatus4Action('New Connection','Requestor')}
            myRequestorConnData = {'Connections':\
                                        {'Id': myMainArgData['_id'],'Type': myMainArgData['Type'],\
                                         'Status' : self.util.getConnStatus4Action('New Connection','Requestor')}}
            ''' building backup data '''
            myInviteeConnBkupData = self.util.extr1stDocFromResultSets(\
                self.mongo.findDocument(myInviteeCollection,myInviteeCriteria,{'_id':1,'Connections':1},True))
            myRequestorConnBkupData = self.util.extr1stDocFromResultSets(\
                self.mongo.findDocument(myRequestorCollection,myRequestorCriteria,{'_id':1,'Connections':1},True))
            ''' persisitng changes in database, removing invitee's connection '''
            myConnectionResult = self.mongo.UpdateDoc(myInviteeCollection,
                                                      myInviteeCriteria,
                                                      myInviteeConnData,
                                                      'pull', False)

            self.myModuleLogger.debug('Invitee rejected request, connection [{conn}] removed, result [{result}] '.
                format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + \
                          myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']),\
                          result=myConnectionResult))

            if self.util.getUpdateStatus(
                    myConnectionResult) == self.globaL._Global__Success:

                # Invitee's connection removed.  removing Requestor's connection
                isInvConnStatusRemoved = self.globaL._Global__True
                myConnectionResult = self.mongo.UpdateDoc(
                    myRequestorCollection, myRequestorCriteria,
                    myRequestorConnData, 'pull', False)
                self.myModuleLogger.debug('Invitee rejected request, Requestor connection [{conn}] removed, result [{result}] '.
                    format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + \
                              myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']),\
                              result=myConnectionResult))

                if self.util.getUpdateStatus(
                        myConnectionResult) == self.globaL._Global__Success:

                    # Requestor connection update is successful, undo Invitee connection change
                    isInvConnStatusRemoved = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(
                        self.globaL._Global__Success)

                    # recording this successful activity; Invitee
                    self.activity._Activity__logActivity(self.util.buildActivityArg( \
                        myMainArgData['_id'], myRequestorCollection,self.globaL._Global__Internal,\
                        'Invitee rejected connection [{conn}], removing this connection '.format(conn=myMainArgData['Type'] + ' ' + str(myMainArgData['_id']) + \
                        ' --> ' + myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId'])) ))

                    #recording this successful activity; invitee
                    self.activity._Activity__logActivity(self.util.buildActivityArg(\
                        myMainArgData['_id'],myInviteeCollection,self.globaL._Global__Internal,\
                        'Invitee rejected connection [{conn}], removing this connection'.format(conn=myMainArgData['ConnectionType'] + ' ' + \
                        str(myMainArgData['ConnectionId']) + ' --> ' + myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])) ))
                else:
                    ''' Requestor connection update is not successful, undo Invitee connection change '''
                    self.myModuleLogger.debug(
                        'Requestor [{req}] connection update unsuccessful, results [{result}]'
                        .format(req=myMainArgData['Connectiontype'] + ' ' +
                                str(myMainArgData['ConnectionId']),
                                result=myReqConnResults))

                    self.mongo.UpdateDoc(myInviteeCollection, myInviteeCriteria, \
                        {'Connections':{myInviteeConnBkupData['Connections'][0]}}, 'addToSet',False)

                    isCleanUpDone = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                        'Requestor [{req}] connection restored'.format(req=myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']) ))

                    self.myModuleLogger.debug('undo changes to Invitee\'s connection successful, result [{result}]'.\
                        format(result=myReqConnResults))
                #fi
            else:
                myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                    'Invitee [{inv}] connection status [{status}] update unsuccessful'. \
                    format(inv= myMainArgData['Type'] + ' ' + str(myMainArgData['_id']),
                            status=self.globaL._Global__Accepted_Inv_ConnectionStatus ))
            #fi

            return myRequestStatus

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            ''' we need to ensure if cleanup is required should there be an issue during failure of Invitee connection '''
            if isInvConnStatusRemoved and (not isReqConnStatusUpdated) and (
                    not isCleanUpDone):
                myInviteeConnData = {
                    'Connections.$.Status':
                    self.util.getConnStatus4Action('New Connection', 'Invitee')
                }
                self.mongo.UpdateDoc(myInviteeCollection, myInviteeCriteria,
                                     myInviteeConnData, 'set', False)
            #fi
            return myRequestStatus

    #__rejectInvitation end here

    def __removeConnection(self, argRequestDict):
        ''' Removes the connection from each other (Requestor/Requestee)'s connection list 
            usage:          <__rejectInvitation(<argReqJsonDict>)
                            MainArg['_id','Type','ConnectionId','ConnectionType','Auth']
            Return:         Success/UnSuccess
            Requestor   --> who initiated the removal of a connection
            Requestee   --> whose connection to be removed from requestor
            Connection will be removed from Requestor/Requstee's conneciton list for each other 
        '''
        try:
            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug(
                'Argument [{arg}] received'.format(arg=argRequestDict))
            ''' declaring/initializing variables '''
            myRequestStatus = self.util.getCopy(
                self.globaL._Global__RequestStatus)
            isRequestorConnRemoved = self.globaL._Global__False
            isRequesteeConnRemoved = self.globaL._Global__False
            isCleanUpDone = self.globaL._Global__False

            if myMainArgData['Type'] == self.globaL._Global__member:
                myRequestorCollection = self.globaL._Global__memberColl
            elif myMainArgData['Type'] == self.globaL._Global__agent:
                myRequestorCollection = self.globaL._Global__agentColl
            #fi

            if myMainArgData['ConnectionType'] == self.globaL._Global__member:
                myRequesteeCollection = self.globaL._Global__memberColl
            elif myMainArgData['ConnectionType'] == self.globaL._Global__agent:
                myRequesteeCollection = self.globaL._Global__agentColl
            #fi
            ''' validating arguments '''
            myArgKey = ['_id', 'Type', 'ConnectionId', 'ConnectionType']
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(
                    myArgValMessage)
            #fi
            ''' Validate auth key for this request (we dont need auth val since it has been already performed by BPS process,
            we need to ensure that any call to this process is from BPS process '''
            ''' we need to check if this is a valid request; is this invitation pending for this id (id is invitee)'''
            myRequestorValCriteria = {'_id':myMainArgData['_id'],'Connections.Id':myMainArgData['ConnectionId'],\
                                'Connections.Type': myMainArgData['ConnectionType']}

            if self.mongo.findTotDocuments(myRequestorCollection,
                                           myRequestorValCriteria) == 0:
                raise com.uconnect.core.error.MissingArgumentValues(\
                    'Invalid Request, Requestor [{requestor}] does not have any connection from [{requestee}] to be removed'.\
                            format( requestor=myMainArgData['Type'] + str(myMainArgData['_id']),\
                                    requestee= myMainArgData['ConnectionType'] + str(myMainArgData['ConnectionId']) ))
            #fi
            ''' Building Invitee connection data to be removed'''
            myRequestorCriteria = {'_id':myMainArgData['_id'],'Connections.Id':myMainArgData['ConnectionId'], \
                                 'Connections.Type': myMainArgData['ConnectionType']}
            myRequestorConnData = {
                'Connections': {
                    'Id': myMainArgData['ConnectionId'],
                    'Type': myMainArgData['ConnectionType']
                }
            }
            ''' Building Requestor connection data to be removed'''
            myRequesteeCriteria = {'_id':myMainArgData['ConnectionId'], 'Connections.Id':myMainArgData['_id'],\
                                   'Connections.Type':myMainArgData['Type']}
            myRequesteeConnData = {
                'Connections': {
                    'Id': myMainArgData['_id'],
                    'Type': myMainArgData['Type']
                }
            }
            ''' building backup data '''
            myRequestorConnBkupData = self.util.extr1stDocFromResultSets(\
                self.mongo.findDocument(myRequestorCollection,myRequestorCriteria,{'_id':1,'Connections':1},True))
            myRequesteeConnBkupData = self.util.extr1stDocFromResultSets(\
                self.mongo.findDocument(myRequesteeCollection,myRequesteeCriteria,{'_id':1,'Connections':1},True))
            ''' persisitng changes in database, removing invitee's connection '''
            myConnectionResult = self.mongo.UpdateDoc(myRequestorCollection,
                                                      myRequestorCriteria,
                                                      myRequestorConnData,
                                                      'pull', False)

            self.myModuleLogger.debug('Requestor connection [{conn}] removed, result [{result}] '.
                format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + \
                          myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']),\
                          result=myConnectionResult))

            if self.util.getUpdateStatus(
                    myConnectionResult) == self.globaL._Global__Success:

                # Requestor's connection removed.  removing Requestor's connection
                isRequestorConnRemoved = self.globaL._Global__True
                myConnectionResult = self.mongo.UpdateDoc(
                    myRequesteeCollection, myRequesteeCriteria,
                    myRequesteeConnData, 'pull', False)

                self.myModuleLogger.debug('Requestee connection [{conn}] removed, result [{result}] '.
                    format(conn = myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])  + ' -> ' + \
                              myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']),\
                              result=myConnectionResult))

                if self.util.getUpdateStatus(
                        myConnectionResult) == self.globaL._Global__Success:
                    ''' Requestor connection update is successful, undo Requestor connection change '''
                    isRequesteeConnRemoved = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(
                        self.globaL._Global__Success)

                    # recording this successful activity; Requestor
                    self.activity._Activity__logActivity(self.util.buildActivityArg( \
                        myMainArgData['_id'], myRequestorCollection,self.globaL._Global__Internal,\
                        'Requestor [{req}] removed connection [{conn}]'.\
                            format(req=myMainArgData['Type'] + ' ' + str(myMainArgData['_id']),\
                                    conn=myMainArgData['Type'] + ' ' + str(myMainArgData['_id']) + ' --> ' + \
                                    myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId'])) ))

                    #recording this successful activity; Requestee
                    self.activity._Activity__logActivity(self.util.buildActivityArg(\
                        myMainArgData['_id'],myRequesteeCollection,self.globaL._Global__Internal,\
                        'Requestor [{req}] removed connection, removing connection [{conn}] from requestee'.\
                            format(req=myMainArgData['Type'] + ' ' + str(myMainArgData['_id']), \
                                conn=myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']) + ' --> ' + \
                                myMainArgData['Type'] + ' ' + str(myMainArgData['_id'])) ))
                else:
                    ''' Requestee connection update is not successful, undo Requestor connection change '''
                    self.myModuleLogger.debug(
                        'Requestee [{req}] connection update unsuccessful, results [{result}]'
                        .format(req=myMainArgData['Connectiontype'] + ' ' +
                                str(myMainArgData['ConnectionId']),
                                result=myReqConnResults))

                    self.mongo.UpdateDoc(myRequestorCollection, myRequestorCriteria, \
                        {'Connections':{myRequestorConnBkupData['Connections'][0]}}, 'addToSet',False)

                    isCleanUpDone = self.globaL._Global__True
                    myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                        'Requestor [{req}] connection restored'.format(req=myMainArgData['ConnectionType'] + ' ' + str(myMainArgData['ConnectionId']) ))
                    self.myModuleLogger.debug('undo changes to Requestor\'s connection successful, result [{result}]'.\
                        format(result=myReqConnResults))
                #fi
            else:
                myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                    'Requestor [{req}] connection status [{status}] update unsuccessful'. \
                    format(req= myMainArgData['Type'] + ' ' + str(myMainArgData['_id']),
                            status=self.globaL._Global__Accepted_Inv_ConnectionStatus ))
            #fi

            return myRequestStatus

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            ''' we need to ensure if cleanup is required should there be an issue during failure of Invitee connection '''
            if isRequestorConnRemoved and (not isRequesteeConnRemoved) and (
                    not isCleanUpDone):
                self.mongo.UpdateDoc(myRequestorCollection, myRequestorCriteria, \
                    {'Connections':{myRequestorConnBkupData['Connections'][0]}}, 'addToSet',False)
            #fi
            return myRequestStatus
        #__reoveConnection Ends here

    def __favoriteConnection(self, argRequestDict):
        ''' 
            Description:    Change favorite of a connection
            argRequestDict: Json/Dict; Following key name is expected in this dict/json object
            usage:          <__MarkConnectionFavorite(<argReqJsonDict>)
                            MainArg{'_id','Type','ConnectionId','ConnectionType','Favorite'}
            Return:         Json object
        '''
        try:
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug(
                'Argument [{arg}] received'.format(arg=argRequestDict))

            # validating arguments
            myArgKey = [
                '_id', 'Type', 'ConnectionId', 'ConnectionType', 'Favorite'
            ]
            myRequestStatus = self.util.getCopy(
                self.globaL._Global__RequestStatus)
            myArgValidationResults = self.util.valRequiredArg(
                myMainArgData, myArgKey)

            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(
                    myArgValMessage)
            #fi

            if myMainArgData['Favorite'] not in ["0", "1"]:
                raise com.uconnect.core.error.MissingArgumentValues(\
                    'Expecting favorite value in ["0","1"], got [{favorite}]'.format(favorite=myMainArgData['Favorite']))
            #fi

            if myMainArgData['Type'] == self.globaL._Global__member:
                myCollection = self.globaL._Global__memberColl
            elif myMainArgData['Type'] == self.globaL._Global__agent:
                myCollection = self.globaL._Global__agentColl
            #fi

            # validating this connection
            myValidConnectionArg = {'_id':myMainArgData['_id'], 'Connections.Id':myMainArgData['ConnectionId'], \
                                    'Connections.Type': myMainArgData['ConnectionType'],\
                                    'Connections.Status' :{'$in':['Accepted','Valid']}}
            if self.mongo.findTotDocuments(myCollection,
                                           myValidConnectionArg) == 0:
                raise com.uconnect.core.error.MissingArgumentValues('No connection found for this request [{request}]'.\
                    format( request=myMainArgData['Type'] + str(myMainArgData['_id']) + ': Connection(' + \
                        myMainArgData['ConnectionType'] + str(myMainArgData['ConnectionId']) + ')' ))
            #fi
            ''' Preparing document:    '''
            #myCriteria = {'_id':myMainArgData['_id'],'Connections.Id': myMainArgData['ConnectionId'], \
            #                'Connections.Type':myMainArgData['ConnectionType']}
            myCriteria = {
                '_id': myMainArgData['_id'],
                'Connections.Id': myMainArgData['ConnectionId']
            }
            myFavoriteData = {
                "Connections.$.Favorite": int(myMainArgData['Favorite'])
            }

            ##db.Member.update({'_id':313848,'LinkedBy.MemberId':313850},{ $set : {'LinkedBy.$.Favorite':1}})
            myMarkFavoriteStatus = self.mongo.UpdateDoc(
                myCollection, myCriteria, myFavoriteData, 'set', False)
            #print('Favorite Status:',myMarkFavoriteStatus)
            if self.util.getUpdateStatus(
                    myMarkFavoriteStatus) == self.globaL._Global__Success:
                myRequestStatus = self.util.getRequestStatus(
                    self.globaL._Global__Success)
            else:
                myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                    myMarkFavoriteStatus.get('Message'))
            #fi

            return myRequestStatus

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            return myRequestStatus
            #fi
        #__favoriteConnection Ends here

    def __blockConnection(self, argRequestDict):
        ''' 
            Description:    Change block of a connection
            argRequestDict: Json/Dict; Following key name is expected in this dict/json object
            usage:          <__blockConnection(<argReqJsonDict>)
                            MainArg{'_id','Type','ConnectionId','ConnectionType','Favorite'}
            Return:         Json object
        '''
        try:
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug(
                'Argument [{arg}] received'.format(arg=argRequestDict))

            # validating arguments
            myArgKey = [
                '_id', 'Type', 'ConnectionId', 'ConnectionType', 'Blocked'
            ]
            myRequestStatus = self.util.getCopy(
                self.globaL._Global__RequestStatus)

            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(
                    myArgValMessage)
            #fi

            if myMainArgData['Blocked'] not in ["0", "1"]:
                raise com.uconnect.core.error.MissingArgumentValues(\
                    'Expecting blocked value in ["0","1"], got [{block}]'.format(block=myMainArgData['Blocked']))
            #fi

            if myMainArgData['Type'] == self.globaL._Global__member:
                myCollection = self.globaL._Global__memberColl
            elif myMainArgData['Type'] == self.globaL._Global__agent:
                myCollection = self.globaL._Global__agentColl
            #fi

            # validating this connection
            myValidConnectionArg = {'_id':myMainArgData['_id'], 'Connections.Id':myMainArgData['ConnectionId'], \
                                    'Connections.Type': myMainArgData['ConnectionType'],\
                                    'Connections.Status' :{'$in':['Accepted','Valid']}}

            if self.mongo.findTotDocuments(myCollection,
                                           myValidConnectionArg) == 0:
                raise com.uconnect.core.error.MissingArgumentValues('No connection found for this request [{request}]'.\
                    format( request=myMainArgData['Type'] + str(myMainArgData['_id']) + ': Connection(' + \
                        myMainArgData['ConnectionType'] + str(myMainArgData['ConnectionId']) + ')' ))
            #fi
            ''' Preparing document:    '''
            myCriteria = {
                '_id': myMainArgData['_id'],
                'Connections.Id': myMainArgData['ConnectionId']
            }
            myFavoriteData = {
                "Connections.$.Blocked": int(myMainArgData['Blocked'])
            }

            ##db.Member.update({'_id':313848,'LinkedBy.MemberId':313850},{ $set : {'LinkedBy.$.Favorite':1}})
            myBlockedStatus = self.mongo.UpdateDoc(myCollection, myCriteria,
                                                   myFavoriteData, 'set',
                                                   False)
            #print('Blocked Status:',myBlockedStatus)
            if self.util.getUpdateStatus(
                    myBlockedStatus) == self.globaL._Global__Success:
                myRequestStatus = self.util.getRequestStatus(
                    self.globaL._Global__Success)
            else:
                myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,\
                    myBlockStatus.get('Message'))
            #fi

            return myRequestStatus

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            return myRequestStatus
Esempio n. 2
0
class MemberBPS(object):
    ''' 
    Member BPS class, called from factory method
    '''
    def __init__(self):
        ''' 
            Description:    Initialization internal method, called internally
            usage:          Called internally
            Return:         N/A
        '''        
        self.factory = Factory()
        self.util = Utility()
        self.mongo = MongoDB()
        self.globaL = Global()
        self.sec = Security()
        self.member = Member()
        self.activity = Activity()
        self.conn = Connections()

        self.myClass = self.__class__.__name__
        self.myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
    '''
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    MMM       MMM   EEEEEEEEEE   MMM       MMM   BBBBBBBBBBB    EEEEEEEEEE   RRRRRRRRRRRR
    MMMM     MMMM   EE           MMMM     MMMM   BB        BB   EE           RR        RR
    MM MM   MM MM   EE           MM MM   MM MM   BB        BB   EE           RR        RR
    MM  MM MM  MM   EEEEEEEEEE   MM  MM MM  MM   BBBBBBBBBBBB   EEEEEEEEEE   RRRRRRRRRRRR
    MM   MMM   MM   EE           MM   MMM   MM   BB        BB   EE           RR        RR
    MM         MM   EE           MM         MM   BB        BB   EE           RR         RR
    MM         MM   EEEEEEEEEE   MM         MM   BBBBBBBBBBB    EEEEEEEEEE   RR          RR
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    '''

    def ____createAMember(self,argRequestDict):
        ''' 
            Description:    Create a member
            argRequestDict: Json/Dict; Following key name is expected in this dict/json object
                            {'Request': 
                                {'Header':{ScreenId':'','ActionId':'',Page:},
                                {'MainArg': {<Member data 'Main','Address','Contact'>}}
                            }
                            We will add 'BusyHours', BusyDays' block from default value
            usage:          <createAMember(<argReqJsonDict>)
            Return:         Json object

            Collection:     Member: Insert a record in Member collection
        '''
        #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
        try:
            ## we need to check who called this function, must be from register
            #print(self.util.whoAmi())
            myMainArgData = self.util.getCopy(argRequestDict)
            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg = argRequestDict))

            myArgKey = ['Main','Address','Contact']

            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            ''' Preparing value to create a new member build initial data '''
            myMemberData = self.member._Member__buildInitMembderData({'Main':myMainArgData['Main'],'Address':myMainArgData['Address'],'Contact':myMainArgData['Contact']})
            myMemberId = myMemberData['_id'] 

            ''' Creating a member '''
            self.myModuleLogger.info('Creating new member, data [{doc}]'.format(doc=myMemberData))
            myMemberResult =  self.mongo.InsertOneDoc(self.globaL._Global__memberColl, myMemberData)
            self.myModuleLogger.info('Member [{id}] created, result[{result}]'.format(id=myMemberId, result=myMemberResult))

            ''' Building response data, we can not retrieve member information because we dont have Auth ket yet, will return member id created'''

            '''
            myRequestDict = self.util.builInternalRequestDict({'Data':{'_id':myMemberId}})
            myRequestDict = self.getAMemberDetail(myResponseDataDict)
            myResponse = self.util.buildResponseData(self.global._Global__InternalRequest,myMemberResult,'Insert',myResponseData)
            '''
            myResponse = myMemberResult['_id']
            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            raise

    def getAllInformation4Member(self,argRequestDict):
        # wip
        ''' 
            We need to combine all update of a Member
            Description:    Update Member's Main information (LastName,FirstName,NickName,Sex)
            argRequestDict: Json/Dict; Following key name is expected in this dict/json object
                            {'Request': 
                                {'Header':{ScreenId':'','ActionId':'',Page:},
                                {'MainArg': {'MemberId':'','Main':[{'Key':'Value'},...]
                            }
            usage:          <getAllInformation4Member(<argReqJsonDict>)
                            MainArg{'MemberId':'','Main':[{Key':'', 'Value':''}]}
            Return:         Json object
        '''

        try:
            
            ''' Initialization & Validation '''

            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi
            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg = argRequestDict))

            myArgKey = ['Auth','ResponseMode']
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            ''' validating Auth arg '''
            if (myMainArgData['Auth']['EntityType'] != self.globaL._Global__member):
                raise com.uconnect.core.error.MissingArgumentValues('Auth Arg validation error; entitytype key must be "Member"')
            #fi

            if not (self.sec._Security__isValidAuthentication(myMainArgData['Auth'])):
                #print(self.util.whoAmI())
                raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth Key [{auth}] for this request [{me}]'.
                    format(auth=myMainArgData['Auth'], me=self.myClass+'.'+self.util.whoAmI()))
            #fi

            ''' Preparing document:    '''
            myMemberId = myMainArgData['Auth']['EntityId']
            myCriteria = {'_id':myMemberId}

            # get Member Details
            myMemberDetails = self.getAMemberDetail({'ResponseMode':self.globaL._Global__InternalRequest,'Auth':myMainArgData['Auth']})
            # get All Connections (Member)
            myConnectionArgData = {'ConnectionType':'Member','ResponseMode': self.globaL._Global__InternalRequest,'Auth':myMainArgData['Auth']}
            myMemberConnections = self.getAMemberConnections(myResponseArgData)
            # Get All Group this member participates to (owner of a group is also member)
           
            # we need to combine all results in one result sets
            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            raise error

    def updateMemberDetail(self, argRequestDict):
        ''' 
            Description:    Update member information
                            Main      --> Member's Main information
                            Address   --> Member's Address information
                            Contact   --> Member's Contact information
            argRequestDict: Json/Dict; Following key name is expected in this dict/json object
                            {'Main','Address','Contact','Auth',ResponseMode'}
            usage:          <updateMemberDetail(<argRequestDict>)
            Return:         Json object

        '''
        try:
            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi

            myRequestStatus = self.util.getCopy(self.globaL._Global__RequestStatus)
            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg=myMainArgData))

            ''' validating arguments '''
            myArgKey = ['Auth']

            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            # will check either Main/Address/Contact/Settings information is passed for update
            #{'a','d'} <= set(a), need to fix this
            if not(any (block in myMainArgData.keys() for block in ['Main','Address','Contact','Settings'])):
                raise com.uconnect.core.error.MissingArgumentValues(\
                    'Mainarg validation error; main arg(s)[{arg}], expecting Main/Address/Contact/Settings'.\
                    format(arg=myMainArgData.keys()))
            
            ''' commenting below code, we will get the entityid from AUTH 
            will overwrite EntityType and EntityId if passed in Auth dictionary. 
            This is to ensure that Auth key must belong to this Member
            myMainArgData['Auth'] = self.sec._Security__updateAuthEntity(
                {'Auth':myMainArgData['Auth'],'EntityType':self.global._Global__member,'EntityId':myMainArgData['_id']})
            '''

            ''' validating auth args '''
            if (myMainArgData['Auth']['EntityType'] != self.globaL._Global__member):
                raise com.uconnect.core.error.MissingArgumentValues('Auth Arg validation error; entitytype key must be "Member"')
            #fi

            ''' Validate auth key for this request'''
            if not (self.sec._Security__isValidAuthentication(myMainArgData['Auth'])):
                #print(self.util.whoAmI())
                raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth Key [{auth}] for this request [{me}]'.
                    format(auth=myMainArgData['Auth'], me=self.myClass+'.'+self.util.whoAmI()))
            #fi

            #we need to find the block which is beieng changed in Member collection dcoument
            if 'Main' in myMainArgData and myMainArgData['Main']:
                self.myModuleLogger.debug('Got Main[{main}] information which changed'.format(main=myMainArgData['Main']))
                myUpdateResult = self.member._Member__updateMemberMain(myMainArgData)
                #print('MainUpdateResult',myUpdateResult,myMainArgData)
            #fi
            if 'Address' in myMainArgData and myMainArgData['Address']:
                #print('In Address')
                self.myModuleLogger.debug('Got Address[{address}] information which changed'.format(address=myMainArgData['Address']))
                myUpdateResult = self.member._Member__updateMemberAddress(myMainArgData)

            #fi
            if 'Contact' in myMainArgData and myMainArgData['Contact']:
                #print('In Contact')
                self.myModuleLogger.debug('Got Contact[{contact}] information which changed'.format(contact=myMainArgData['Contact']))
                myUpdateResult = self.member._Member__updateMemberContact(myMainArgData)

            #fi

            # rebuilding tag and recording this activity 
            self.myModuleLogger.info('Updating Tag for member [{member}]' .format(member=myMainArgData['Auth']['EntityId']))
            myTagUpdateResult = self.member._Member__updateMemberTag({'_id':myMainArgData['Auth']['EntityId']})

            #record this activity
            self.activity._Activity__logActivity(self.util.buildActivityArg(
                myMainArgData['Auth']['EntityId'],self.globaL._Global__member,self.globaL._Global__External,'Member [{member}] detail [{change}] changed'.
                    format(member=myMainArgData['Auth']['EntityId'], change=myMainArgData), myMainArgData['Auth']))

            myRequestStatus = self.util.getRequestStatus(self.globaL._Global__Success)

            ''' get all the information of a member '''
            myMemberDetailResults = self.getAMemberDetail({'ResponseMode':self.globaL._Global__InternalRequest,'Auth':myMainArgData['Auth']})
            #print(myMemberDetailResults)

            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Find', myMemberDetailResults)
            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Error')
            return myResponse

    def updateMemberSettings(self,argRequestDict):
        pass    

    def UpdateConnectionDetails(self, argRequestDict):
        ''' 
            Description:    UpdateConnectionDetails (Only Invitee can accept the connection)
                            Requestor   --> _id 
                            Invitee     --> [])
                            Action      --> Invite/Remove/Accept/Reject/Remove 
                                Requestor:  Invite/Remove
                                Invitee:    Accept/Reject/Remove
            argRequestDict: Json/Dict; Following key name is expected in this dict/json object
                            {'MainArg': {'_id':'','Connections':[{'MemberId':'','Actions':''}],'Auth':'','ResponseMode'}}
            usage:          <UpdateConnectionDetails(<argReqJsonDict>)
            Return:         Json object
        '''
        try:
            myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg=argRequestDict))
            
            myConnectionResult = self.globaL._Global__False
            myActivityDetails = ''
            myRequestStatus = self.util.getCopy(self.globaL._Global__RequestStatus)

            # validating arguments
            myArgKey = ['UpdateConnections','Auth','ResponseMode']
            myConnectionArgs = myMainArgData['UpdateConnections']

            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            # if connection is passed as dict, converting to list
            if not(self.util.isList(myConnectionArgs)):
                myConnectionArgs = [myConnectionArgs]

            # we might get more than one connection request
            for myConnections in myConnectionArgs:
                myArgKey = ['Id','Type','Action']
                if myConnections['Action'] not in self.globaL._Global__Connection_Action:
                    raise com.uconnect.core.error.MissingArgumentValues('Arg validation error, Action must be one of the value in[{action}]'.format(action=self.globaL._Global__Connection_Action))
                #fi
                myArgValidationResults = self.util.valRequiredArg(myConnections, myArgKey)
                myArgValidation = self.util.extractValFromTuple(myArgValidationResults,0)
                if not (myArgValidation):
                    raise com.uconnect.core.error.MissingArgumentValues(\
                        'Mainarg validation error; main arg(s)[{arg}], missing/empty key(s)[{key}]'.\
                        format(arg=myConnections.keys(), key=self.util.extractValFromTuple(myArgValidationResults,1)))
                #fi
                if (myConnections['Action'] == 'Favorite') and ('Favorite' not in myConnections):
                    raise com.uconnect.core.error.MissingArgumentValues(\
                        'Mainarg validation error; missing "Favorite" key')
                #fi
                if (myConnections['Action'] == 'Blocked') and ('Blocked' not in myConnections):
                    raise com.uconnect.core.error.MissingArgumentValues(\
                        'Mainarg validation error; missing "Blocked" key')
                #fi
            #end for loop

            ''' commenting out below code as its not needed, we will pick entity id from AUTH
            will overwrite EntityType and EntityId if passed in Auth dictionary. This is to ensure that Auth key must belong to this Member
            myMainArgData['Auth'] = self.sec._Security__updateAuthEntity(
                {'Auth':myMainArgData['Auth'],'EntityType':self.global._Global__member,'EntityId':myMainArgData['_id']})
            '''

            if (myMainArgData['Auth']['EntityType'] != self.globaL._Global__member):
                raise com.uconnect.core.error.MissingArgumentValues('Auth Arg validation error; entitytype key must be "Member"')
            #fi

            ''' Validate auth key for this request'''
            if not (self.sec._Security__isValidAuthentication(myMainArgData['Auth'])):
                raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth Key [{auth}] for this request [{me}]'.
                    format(auth=myMainArgData['Auth'], me=self.myClass+'.'+self.util.whoAmI()))
            #fi

            # lets perform action
            for myConnections in myConnectionArgs:
                myConnectionArgData = {'_id':myMainArgData['Auth']['EntityId'],'Type':self.globaL._Global__member,
                                    'ConnectionId': myConnections['Id'], 'ConnectionType': myConnections['Type']}
                # we need to populate favorite and block dict as well
                #print('processing connection request [{request}]'.format(request=myConnectionArgData))
                if myConnections['Action'] == self.globaL._Global__Connection_Action_Invite:
                    myConnectionResults = self.conn._Connections__AddAConnection(myConnectionArgData)
                    myActivityDetails = 'Member [{member}] added [{conn}] as a connection '.\
                        format(member=myMainArgData['Auth']['EntityId'], conn=myConnectionArgData['ConnectionType'] + ' ' + \
                            str(myConnectionArgData['ConnectionId']))
                elif myConnections['Action'] == self.globaL._Global__Connection_Action_Accepted:
                    myConnectionResults = self.conn._Connections__acceptInvitation(myConnectionArgData)
                    myActivityDetails = 'Member [{member}] accepted connection request from [{conn}]'.\
                        format(member=myMainArgData['Auth']['EntityId'], conn=myConnectionArgData['ConnectionType'] + ' ' + \
                            str(myConnectionArgData['ConnectionId']))
                elif myConnections['Action'] == self.globaL._Global__Connection_Action_Rejected:
                    myConnectionResults = self.conn._Connections__rejectInvitation(myConnectionArgData)
                    myActivityDetails = 'Member [{member}] rejected connection request from [{conn}]'.\
                        format(member=myMainArgData['Auth']['EntityId'], conn=myConnectionArgData['ConnectionType'] + ' ' + \
                            str(myConnectionArgData['ConnectionId']))
                elif  myConnections['Action'] == self.globaL._Global__Connection_Action_Removed:
                    myConnectionResults = self.conn._Connections__removeConnection(myConnectionArgData)
                    myActivityDetails = 'Member [{member}] removed connection from [{conn}]'.\
                        format(member=myMainArgData['Auth']['EntityId'], conn=myConnectionArgData['ConnectionType'] + ' ' + \
                            str(myConnectionArgData['ConnectionId']))
                elif  myConnections['Action'] == self.globaL._Global__Connection_Action_Favorite:
                    #print('Fav',myConnections)
                    myConnectionArgData.update({'Favorite':myConnections['Favorite']}) 
                    myConnectionResults = self.conn._Connections__favoriteConnection(myConnectionArgData)
                    myActivityDetails = 'Member [{member}] updated favorite attribute of connection [{conn}]'.\
                        format(member=myMainArgData['Auth']['EntityId'], conn=myConnectionArgData['ConnectionType'] + ' ' + \
                            str(myConnectionArgData['ConnectionId']))
                elif  myConnections['Action'] == self.globaL._Global__Connection_Action_Block:
                    myConnectionArgData.update({'Blocked':myConnections['Blocked']})
                    myConnectionResults = self.conn._Connections__blockConnection(myConnectionArgData)
                    myActivityDetails = 'Member [{member}] updated block attribute of connection [{conn}]'.\
                        format(member=myMainArgData['Auth']['EntityId'], conn=myConnectionArgData['ConnectionType'] + ' ' + \
                            str(myConnectionArgData['ConnectionId']))
                #fi
                #print('in BPS result',myConnectionResults)
                if myConnectionResults.get('Status') == self.globaL._Global__Success:
                    ''' recording activity '''
                    myActivityDetails = 'Success,' + myActivityDetails
                    myRequestStatus = self.util.getRequestStatus(self.globaL._Global__Success)
                else:
                    myActivityDetails = 'UnSuccess: ' + myActivityDetails
                    myRequestStatus = self.util.getRequestStatus(self.globaL._Global__UnSuccess,myConnectionResults.get('Message'))
                    #print('reqStatus',myRequestStatus)
                #fi
                self.myModuleLogger.info(myActivityDetails)
            # end for loop                

            ''' preparing response; get all connection member details for this member '''
            myResponseArgData = {'ConnectionType':'Member','ResponseMode': self.globaL._Global__InternalRequest,'Auth':myMainArgData['Auth']}
            myResponseData = self.getAMemberConnections(myResponseArgData)
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'],myRequestStatus,'Find',myResponseData)

            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Error')
            return myResponse
    # ends ExecConnectionAction

    def getAMemberDetail(self,argRequestDict):
        ''' 
            Description:    Find a member details
            argRequestDict:     Json/Dict; Following key name is expected in this dict/json object
                            {'MemberId','Auth','ResponseMode'}
            usage:          <getAMemberDetail(<argReqJsonDict>)
            Return:         Json object
        '''
        # we need to check which arguments are passed; valid argument is Phone/Email/LastName + FirstName

        #print (argRequestDict)
        ''' frollowing import is placed here; this is to avoid error while import module in each other '''

        try:
            
            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg=argRequestDict))

            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi

            myArgKey = ['Auth','ResponseMode']
            myRequestStatus = self.util.getCopy(self.globaL._Global__RequestStatus)

            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            if (myMainArgData['Auth']['EntityType'] != self.globaL._Global__member):
                raise com.uconnect.core.error.MissingArgumentValues('Auth Arg validation error; entitytype key must be "Member"')
            #fi

            ''' Validate auth key for this request'''
            if myMainArgData['ResponseMode']  == self.globaL._Global__InternalRequest:
                if not (self.sec._Security__isValAuthKeyInternal(myMainArgData['Auth'])):
                    raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth [{auth}] for this request [{me}]'.
                        format(auth=myMainArgData['Auth'], me=self.util.whoAmI()))
                #fi
            else:
                if not (self.sec._Security__isValidAuthentication(myMainArgData['Auth'])):
                    raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth [{auth}] for this request [{me}]'.
                        format(auth=myMainArgData['Auth'], me=self.util.whoAmI()))
                #fi
            #fi
            ''' preparing value needed to find member details'''
            myCriteria = {'_id':myMainArgData['Auth']['EntityId']}
            myFindOne = self.globaL._Global__True
            myProjection={"Main":1,"Address":1,"Contact":1,"Tag":1}

            self.myModuleLogger.info('Finding member [{member}] details'.format (member=myMainArgData['Auth']['EntityId']))
            myMemberData = self.mongo.findDocument(self.globaL._Global__memberColl, myCriteria,myProjection,myFindOne)
            
            # get the connection information
            myConnectionArgs = {'MemberId': myMainArgData['Auth']['EntityId'], 'ConnectionType':self.globaL._Global__member,\
                                 'ResponseMode':self.globaL._Global__InternalRequest}
            myMemberConnections = self.member._Member__getAMemberConnections(myConnectionArgs)
            # will update connection information to current result sets, there are only one document in 'Data'
            if 'Data' in myMemberConnections and 'Connections' in myMemberConnections['Data']:
                myMemberData['Data'][0].update({'Connections':myMemberConnections['Data']['Connections']})
            else:
                myMemberData['Data'][0].update({'Connections':[]})                
            #fi

            myRequestStatus = self.util.getRequestStatus(self.globaL._Global__Success)

            ''' Building response '''           
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'],myRequestStatus,'Find',myMemberData)
            
            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Error')
            return myResponse

    def getAMemberConnections(self,argRequestDict):
        ''' 
            Description:    Find a member's all connections details
            argRequestDict:     Json/Dict; Following key name is expected in this dict/json object
                            {'Request': 
                                {'Header':{ScreenId':'','ActionId':'',Page:},
                                {'MainArg': {}}
                            }
            usage:          <getsAMemberDetail(<argReqJsonDict>)
                    http://www.jsoneditoronline.org/?id=ae36cfdc68b1255530150d286d14bab8          
                    '''
        # we need to check which arguments are passed; valid argument is Phone/Email/LastName + FirstName

        #print (argRequestDict)
        # raise an user defined exception here
        try:

            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi

            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg=argRequestDict))
            
            myArgKey = ['ConnectionType','Auth','ResponseMode']
            myRequestStatus = self.util.getCopy(self.globaL._Global__RequestStatus)

            ''' validating arguments '''
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            if (myMainArgData['Auth']['EntityType'] != self.globaL._Global__member):
                raise com.uconnect.core.error.MissingArgumentValues('Auth Arg validation error; entitytype key must be "Member"')
            #fi

            ''' Validate auth key for this request'''
            if myMainArgData['ResponseMode'] == self.globaL._Global__InternalRequest:
                if not (self.sec._Security__isValAuthKeyInternal(myMainArgData['Auth'])):
                    raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth Key [{auth}] for this request [{me}]'.
                        format(auth=myMainArgData['Auth'], me=self.util.whoAmI()))
                #fi
            else:
                if not (self.sec._Security__isValidAuthentication(myMainArgData['Auth'])):
                    raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth Key [{auth}] for this request [{me}]'.
                        format(auth=myMainArgData['Auth'], me=self.util.whoAmI()))
                #fi
            #fi             

            ''' preparing value needed to find member connections'''
            self.myModuleLogger.info('Finding a members connection [{arg}]'.format(arg=myMainArgData))
            ''' we need threading for following request using threading of python '''

            myMemberId = myMainArgData['Auth']['EntityId']
            myConnectionType = myMainArgData['ConnectionType']
            
            ''' 
            commenting below, this has been moved to Memebr
            build aggregate pipeline 
            myAggregatePipeLine = self.member._Member__buildGetAllConnPipeline({'MemberId':myMemberId,'ConnectionType':myConnectionType})
            self.myModuleLogger.debug("Pipeline [{pipeline}] will be used to execute the aggregate function")

            #myAggregateDict = {"aggregate":self.memberColl,"pipeline":myAggregatePipeLine,"allowDiskUse":True}
            #myConnectionRawData = self.mongo.ExecCommand(self.memberColl, myAggregateDict)
            
            myAggregateDict = {"aggregate":self.global._Global__memberColl,"pipeline":myAggregatePipeLine,"allowDiskUse":True}
            myConnectionRawData = self.mongo.ExecCommand(myAggregateDict)

            if self.util.isAllArgumentsValid(myConnectionRawData):
                myMemberConnection = {"Data":self.member._Member__buildMyConnection({'ConnectionType':self.global._Global__member,'ConnectionRawData':myConnectionRawData})}
            else:
                myMemberConnection = {}
            #fi
            '''
            myMemberConnectionsArg = {'MemberId':myMemberId,'ConnectionType':myConnectionType,\
                                        'ResponseMode':self.globaL._Global__InternalRequest}
            myMemberConnection = self.member._Member__getAMemberConnections(myMemberConnectionsArg)

            myRequestStatus = self.util.getRequestStatus(self.globaL._Global__Success)
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Find', myMemberConnection)

            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Error')
            return myResponse

    def SearchMember(self,argRequestDict):
        ''' 
            Description:    Search memebr based of argument passed
            argRequestDict:     Json/Dict; Following key name is expected in this dict/json object
                            {'SearchCriteria','Page',Auth','ResponseMode'}
            usage:          <SearchMember(<argReqJsonDict>)
            Return:         Json object
        '''
        try:

            #myModuleLogger = logging.getLogger('uConnect.' +str(__name__) + '.' + self.myClass)
            if 'MainArg' in argRequestDict:
                myMainArgData = self.util.getCopy(argRequestDict)['MainArg']
            else:
                myMainArgData = self.util.getCopy(argRequestDict)
            #fi

            self.myModuleLogger.debug('Argument [{arg}] received'.format(arg=argRequestDict))            
            myArgKey = ['SearchCriteria','Page','Auth','ResponseMode']
            myRequestStatus = self.util.getCopy(self.globaL._Global__RequestStatus)

            ''' validating arguments '''
            myArgValidation, myMissingKeys, myArgValMessage = \
                    self.util.valRequiredArg(myMainArgData, myArgKey)
            if not (myArgValidation):
                raise com.uconnect.core.error.MissingArgumentValues(myArgValMessage)
            #fi

            if (myMainArgData['Auth']['EntityType'] != self.globaL._Global__member):
                raise com.uconnect.core.error.MissingArgumentValues('Auth Arg validation error; entitytype key must be "Member"')
            #fi

            ''' Validate auth key for this request'''
            if not (self.sec._Security__isValidAuthentication(myMainArgData['Auth'])):
                raise com.uconnect.core.error.InvalidAuthKey('Invalid Auth Key [{auth}] for this request [{me}]'.
                    format(auth=myMainArgData['Auth'], me=self.util.whoAmI()))
            #fi 
            
            # preparing documemt for search
            mySearchCriteria = myMainArgData['SearchCriteria'].upper()
            #print('Search',mySearchCriteria)
            myTextSearhDocArgDict = \
                {'Collection':'Member', 'Search':"\"mySearchCriteria\"",'Projection':{'_id':1,'Main':1}, 'Limit':10, 'Skip':"0"}
            mySearchResults = self.mongo.SearchText(myTextSearhDocArgDict)
            myRequestStatus = self.util.getRequestStatus(self.globaL._Global__Success)

            # building response
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Find', mySearchResults)

            return myResponse

        except Exception as err:
            myRequestStatus = self.util.extractLogError()
            myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Error')
            return myResponse