mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 00:41:35 -05:00
Capture the count of sent notifications for a job
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
from flask import current_app
|
||||
from app import db
|
||||
from app.models import Notification
|
||||
from app.models import Notification, Job
|
||||
from sqlalchemy import desc
|
||||
|
||||
|
||||
def dao_create_notification(notification):
|
||||
if notification.job_id:
|
||||
db.session.query(Job).update({Job.notifications_sent: Job.notifications_sent + 1})
|
||||
db.session.add(notification)
|
||||
db.session.commit()
|
||||
|
||||
@@ -19,12 +21,12 @@ def get_notification_for_job(service_id, job_id, notification_id):
|
||||
|
||||
|
||||
def get_notifications_for_job(service_id, job_id, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id, job_id=job_id)\
|
||||
.order_by(desc(Notification.created_at))\
|
||||
query = Notification.query.filter_by(service_id=service_id, job_id=job_id) \
|
||||
.order_by(desc(Notification.created_at)) \
|
||||
.paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
)
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
)
|
||||
return query
|
||||
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ class Job(db.Model):
|
||||
onupdate=datetime.datetime.now)
|
||||
status = db.Column(db.Enum(*JOB_STATUS_TYPES, name='job_status_types'), nullable=False, default='pending')
|
||||
notification_count = db.Column(db.Integer, nullable=False)
|
||||
notifications_sent = db.Column(db.Integer, nullable=False)
|
||||
notifications_sent = db.Column(db.Integer, nullable=False, default=0)
|
||||
processing_started = db.Column(
|
||||
db.DateTime,
|
||||
index=False,
|
||||
|
||||
26
migrations/versions/0034_job_sent_count.py
Normal file
26
migrations/versions/0034_job_sent_count.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 0034_job_sent_count
|
||||
Revises: 0033_correct_permission_enums
|
||||
Create Date: 2016-03-04 13:44:32.217058
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0034_job_sent_count'
|
||||
down_revision = '0033_correct_permission_enums'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('jobs', sa.Column('notifications_sent', sa.Integer(), nullable=False))
|
||||
### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('jobs', 'notifications_sent')
|
||||
### end Alembic commands ###
|
||||
@@ -1,4 +1,4 @@
|
||||
from app.models import Notification
|
||||
from app.models import Notification, Job
|
||||
from datetime import datetime
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
@@ -9,12 +9,37 @@ from app.dao.notifications_dao import (
|
||||
)
|
||||
|
||||
|
||||
def test_save_notification(sample_template, sample_job):
|
||||
def test_save_notification_and_increment_job(sample_template, sample_job):
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
'job_id': sample_job.id,
|
||||
'service': sample_template.service,
|
||||
'template': sample_template,
|
||||
'created_at': datetime.utcnow()
|
||||
}
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification)
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
notification_from_db = Notification.query.all()[0]
|
||||
assert notification_from_db.id
|
||||
assert data['to'] == notification_from_db.to
|
||||
assert data['job_id'] == notification_from_db.job_id
|
||||
assert data['service'] == notification_from_db.service
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
assert 'sent' == notification_from_db.status
|
||||
assert Job.query.get(sample_job.id).notifications_sent == 1
|
||||
|
||||
|
||||
def test_save_notification_with_no_job(sample_template):
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
'job': sample_job,
|
||||
'service': sample_template.service,
|
||||
'template': sample_template,
|
||||
'created_at': datetime.utcnow()
|
||||
@@ -27,7 +52,6 @@ def test_save_notification(sample_template, sample_job):
|
||||
notification_from_db = Notification.query.all()[0]
|
||||
assert notification_from_db.id
|
||||
assert data['to'] == notification_from_db.to
|
||||
assert data['job'] == notification_from_db.job
|
||||
assert data['service'] == notification_from_db.service
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
|
||||
Reference in New Issue
Block a user