sio-2425/delivery2/server/routes/org.py

35 lines
1.3 KiB
Python

import json
from flask import Blueprint, request, jsonify
from services import OrganizationService
from utils.comms_encryption import decrypt_request_with_iv, encrypt_response_with_iv
org_bp = Blueprint("org", __name__)
@org_bp.route("/create", methods=["POST"])
def org_create():
if request.headers.get("Content-Type") != "application/octet-stream":
return jsonify({"error": "Invalid request"}), 400
data = json.loads(decrypt_request_with_iv(request.data))
if "name" not in data or "username" not in data or "full_name" not in data or "email" not in data or "public_key" not in data:
return jsonify({"error": "Missing required fields"}), 400
existing_org = OrganizationService.get_organization_by_name(data["name"])
if existing_org:
return jsonify({"error": "Organization already exists"}), 400
org = OrganizationService.create_organization(
name=data["name"],
username=data["username"],
full_name=data["full_name"],
email=data["email"],
public_key=data["public_key"]
)
return encrypt_response_with_iv(json.dumps(org.to_dict()), 201)
@org_bp.route("/list", methods=["GET"])
def org_list():
return encrypt_response_with_iv(json.dumps([org.to_dict() for org in OrganizationService.list_organizations()]), 200)