def update_message_sent_status(message, status): message_id = message.get('message_id') if not message_id: return mode = message.get('mode') if not mode: return # Don't track this for twilio as those are kept track of separately. Make # use of this for email, and, as a side effect of that for slack if mode in ('sms', 'call'): return session = db.Session() try: session.execute( '''INSERT INTO `generic_message_sent_status` (`message_id`, `status`) VALUES (:message_id, :status) ON DUPLICATE KEY UPDATE `status` = :status''', { 'message_id': message_id, 'status': status }) session.commit() except (DataError, IntegrityError): logger.exception('Failed setting message sent status for message %s', message) session.close()
def initialize_twilio_message_status(self, sid, message_id): session = db.Session() try: session.execute('''INSERT INTO `twilio_delivery_status` (`twilio_sid`, `message_id`) VALUES (:sid, :mid)''', {'sid': sid, 'mid': message_id}) session.commit() except IntegrityError: logger.exception('Failed initializing twilio delivery status row. sid: %s, mid: %s', sid, message_id) session.close()
def set_target_contact_by_priority(message): session = db.Session() result = session.execute( ''' SELECT `target_contact`.`destination` AS dest, `mode`.`name` AS mode_name, `mode`.`id` AS mode_id FROM `mode` JOIN `target` ON `target`.`name` = :target JOIN `application` ON `application`.`name` = :application -- left join because the "drop" mode isn't going to have a target_contact entry LEFT JOIN `target_contact` ON `target_contact`.`mode_id` = `mode`.`id` AND `target_contact`.`target_id` = `target`.`id` WHERE mode.id = IFNULL(( SELECT `target_application_mode`.`mode_id` FROM `target_application_mode` WHERE `target_application_mode`.`target_id` = target.id AND `target_application_mode`.`application_id` = `application`.`id` AND `target_application_mode`.`priority_id` = :priority_id ), IFNULL( -- 2. Lookup default setting for this app ( SELECT `default_application_mode`.`mode_id` FROM `default_application_mode` WHERE `default_application_mode`.`priority_id` = :priority_id AND `default_application_mode`.`application_id` = `application`.`id` ), IFNULL( -- 3. lookup default user setting ( SELECT `target_mode`.`mode_id` FROM `target_mode` WHERE `target_mode`.`target_id` = target.id AND `target_mode`.`priority_id` = :priority_id ), ( -- 4. lookup default iris setting SELECT `mode_id` FROM `priority` WHERE `id` = :priority_id ) ) ) ) -- Make sure this mode is allowed for this application. Eg important apps can't drop. AND EXISTS (SELECT 1 FROM `application_mode` WHERE `application_mode`.`mode_id` = `mode`.`id` AND `application_mode`.`application_id` = `application`.`id`) ''', message) try: [(destination, mode, mode_id)] = result except ValueError: raise finally: session.close() if not destination and mode != 'drop': logger.error( 'Did not find destination for message %s and mode is not drop', message) return False message['destination'] = destination message['mode'] = mode message['mode_id'] = mode_id return True