mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Refactored to put logic into API
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from app.dao.inbound_numbers_dao import (
|
||||
dao_get_inbound_numbers,
|
||||
dao_get_inbound_number,
|
||||
dao_get_inbound_number_for_service,
|
||||
dao_get_available_inbound_numbers,
|
||||
dao_set_inbound_number_to_service,
|
||||
dao_set_inbound_number_active_flag
|
||||
)
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import InboundNumber
|
||||
from app.schema_validation import validate
|
||||
|
||||
inbound_number_blueprint = Blueprint('inbound_number', __name__)
|
||||
register_errors(inbound_number_blueprint)
|
||||
@@ -20,42 +17,34 @@ register_errors(inbound_number_blueprint)
|
||||
def get_inbound_numbers():
|
||||
inbound_numbers = [i.serialize() for i in dao_get_inbound_numbers()]
|
||||
|
||||
return jsonify(data=inbound_numbers if inbound_numbers else None)
|
||||
|
||||
|
||||
@inbound_number_blueprint.route('/available', methods=['GET'])
|
||||
def get_next_available_inbound_numbers():
|
||||
inbound_numbers = [i.serialize() for i in dao_get_available_inbound_numbers()]
|
||||
|
||||
return jsonify(data=inbound_numbers[0] if len(inbound_numbers) else [])
|
||||
return jsonify(data=inbound_numbers if inbound_numbers else [])
|
||||
|
||||
|
||||
@inbound_number_blueprint.route('/service/<uuid:service_id>', methods=['GET'])
|
||||
def get_inbound_number_for_service(service_id):
|
||||
inbound_number = dao_get_inbound_number_for_service(service_id)
|
||||
|
||||
return jsonify(data=inbound_number.serialize() if inbound_number else None)
|
||||
return jsonify(data=inbound_number.serialize() if inbound_number else {})
|
||||
|
||||
|
||||
@inbound_number_blueprint.route('/<uuid:inbound_number_id>/service/<uuid:service_id>', methods=['POST'])
|
||||
def post_set_inbound_number_for_service(inbound_number_id, service_id):
|
||||
@inbound_number_blueprint.route('/service/<uuid:service_id>', methods=['POST'])
|
||||
def post_allocate_inbound_number(service_id):
|
||||
inbound_number = dao_get_inbound_number_for_service(service_id)
|
||||
|
||||
if inbound_number:
|
||||
raise InvalidRequest('Service already has an inbound number', status_code=400)
|
||||
if not inbound_number.active:
|
||||
dao_set_inbound_number_active_flag(inbound_number.id, active=True)
|
||||
return '', 204
|
||||
else:
|
||||
return '', 200
|
||||
|
||||
inbound_number = dao_get_inbound_number(inbound_number_id)
|
||||
if inbound_number.service_id:
|
||||
raise InvalidRequest('Inbound number already assigned', status_code=400)
|
||||
available_numbers = dao_get_available_inbound_numbers()
|
||||
|
||||
dao_set_inbound_number_to_service(service_id, inbound_number)
|
||||
|
||||
return '', 204
|
||||
|
||||
|
||||
@inbound_number_blueprint.route('/<uuid:inbound_number_id>/on', methods=['POST'])
|
||||
def post_set_inbound_number_on(inbound_number_id):
|
||||
dao_set_inbound_number_active_flag(inbound_number_id, active=True)
|
||||
return '', 204
|
||||
if len(available_numbers) > 0:
|
||||
dao_set_inbound_number_to_service(service_id, available_numbers[0])
|
||||
return '', 204
|
||||
else:
|
||||
raise InvalidRequest('No available inbound numbers', status_code=400)
|
||||
|
||||
|
||||
@inbound_number_blueprint.route('/<uuid:inbound_number_id>/off', methods=['POST'])
|
||||
|
||||
Reference in New Issue
Block a user