def increment_template_usage_cache(service_id, template_id, created_at): key = cache_key_for_service_template_usage_per_day(service_id, convert_utc_to_aet(created_at)) redis_store.increment_hash_value(key, template_id) # set key to expire in eight days - we don't know if we've just created the key or not, so must assume that we # have and reset the expiry. Eight days is longer than any notification is in the notifications table, so we'll # always capture the full week's numbers redis_store.expire(key, current_app.config['EXPIRE_CACHE_EIGHT_DAYS'])
def persist_notification( *, template_id, template_version, recipient, service, personalisation, notification_type, api_key_id, key_type, created_at=None, job_id=None, job_row_number=None, reference=None, client_reference=None, notification_id=None, simulated=False, created_by_id=None, status=NOTIFICATION_CREATED, reply_to_text=None, status_callback_url=None, status_callback_bearer_token=None, ): notification_created_at = created_at or datetime.utcnow() if not notification_id: notification_id = uuid.uuid4() notification = Notification( id=notification_id, template_id=template_id, template_version=template_version, to=recipient, service_id=service.id, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key_id, key_type=key_type, created_at=notification_created_at, job_id=job_id, job_row_number=job_row_number, client_reference=client_reference, reference=reference, created_by_id=created_by_id, status=status, reply_to_text=reply_to_text, status_callback_url=status_callback_url, status_callback_bearer_token=status_callback_bearer_token, ) if notification_type == SMS_TYPE: formatted_recipient = validate_and_format_phone_number_and_allow_international( recipient) recipient_info = get_international_phone_info(formatted_recipient) notification.normalised_to = formatted_recipient notification.international = recipient_info.international notification.phone_prefix = recipient_info.country_prefix notification.rate_multiplier = recipient_info.billable_units # We can't use a sender name/ID if the text is sending to an # international number. At the time of writing, this is because Telstra # won't send to an international number unless sending from the number # associated with the subscription. Additionally, Twilio can send from a # sender name/ID, however, it requires configuration and it depends on # the countries in play. if notification.international: notification.reply_to_text = None elif notification_type == EMAIL_TYPE: notification.normalised_to = format_email_address(notification.to) # if simulated create a Notification model to return but do not persist the Notification to the dB if not simulated: dao_create_notification(notification) if key_type != KEY_TYPE_TEST: if redis_store.get(redis.daily_limit_cache_key(service.id)): redis_store.incr(redis.daily_limit_cache_key(service.id)) if redis_store.get_all_from_hash( cache_key_for_service_template_counter(service.id)): redis_store.increment_hash_value( cache_key_for_service_template_counter(service.id), template_id) increment_template_usage_cache(service.id, template_id, notification_created_at) current_app.logger.info("{} {} created at {}".format( notification_type, notification_id, notification_created_at)) return notification
def persist_notification(*, template_id, template_version, recipient, service, personalisation, notification_type, api_key_id, key_type, created_at=None, job_id=None, job_row_number=None, reference=None, client_reference=None, notification_id=None, simulated=False, created_by_id=None, status=NOTIFICATION_CREATED, reply_to_text=None): notification_created_at = created_at or datetime.utcnow() if not notification_id: notification_id = uuid.uuid4() notification = Notification( id=notification_id, template_id=template_id, template_version=template_version, to=recipient, service_id=service.id, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key_id, key_type=key_type, created_at=notification_created_at, job_id=job_id, job_row_number=job_row_number, client_reference=client_reference, reference=reference, created_by_id=created_by_id, status=status, reply_to_text=reply_to_text, ) if notification_type == SMS_TYPE: formatted_recipient = validate_and_format_phone_number( recipient, international=True) recipient_info = get_international_phone_info(formatted_recipient) notification.normalised_to = formatted_recipient notification.international = recipient_info.international notification.phone_prefix = recipient_info.country_prefix notification.rate_multiplier = recipient_info.billable_units elif notification_type == EMAIL_TYPE: notification.normalised_to = format_email_address(notification.to) # if simulated create a Notification model to return but do not persist the Notification to the dB if not simulated: dao_create_notification(notification) if key_type != KEY_TYPE_TEST: if redis_store.get(redis.daily_limit_cache_key(service.id)): redis_store.incr(redis.daily_limit_cache_key(service.id)) if redis_store.get_all_from_hash( cache_key_for_service_template_counter(service.id)): redis_store.increment_hash_value( cache_key_for_service_template_counter(service.id), template_id) increment_template_usage_cache(service.id, template_id, notification_created_at) current_app.logger.info("{} {} created at {}".format( notification_type, notification_id, notification_created_at)) return notification