diff --git a/app/models.py b/app/models.py index 7e064345a..a9e63109a 100644 --- a/app/models.py +++ b/app/models.py @@ -120,7 +120,7 @@ class User(db.Model): organisations = db.relationship( 'Organisation', secondary='user_to_organisation', - backref=db.backref('user_to_organisation', lazy='dynamic')) + backref=db.backref('users', lazy='dynamic')) @property def password(self): diff --git a/app/user/rest.py b/app/user/rest.py index c29ec8342..207d19bfe 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -395,6 +395,45 @@ def update_password(user_id): return jsonify(data=user.serialize()), 200 +@user_blueprint.route('//organisations-and-services', methods=['GET']) +def get_organisations_and_services_for_user(user_id): + user = get_user_by_id(user_id=user_id) + + data = { + 'organisations': [ + { + 'name': org.name, + 'id': org.id, + 'services': [ + { + 'id': service.id, + 'name': service.name + } + for service in org.services + if service.active and user in service.users + ] + } + for org in user.organisations + ], + 'services_without_organisations': [ + { + 'id': service.id, + 'name': service.name + } for service in user.services + if ( + service.active and + # include services that either aren't in an organisation, or are in an organisation, + # but not one that the user can see. + ( + not service.organisation or + user not in service.organisation.users + ) + ) + ] + } + return jsonify(data) + + def _create_reset_password_url(email): data = json.dumps({'email': email, 'created_at': str(datetime.utcnow())}) url = '/new-password/'