""" Provides routes related to Consumption objects. """ from flask import jsonify from sqlalchemy.exc import SQLAlchemyError from piket_server.flask import app, db from piket_server.models import Consumption @app.route("/consumptions/", methods=["DELETE"]) def reverse_consumption(consumption_id: int): """ Reverse a consumption. """ consumption = Consumption.query.get_or_404(consumption_id) if consumption.reversed: return ( jsonify( { "error": "Consumption already reversed", "consumption": consumption.as_dict, } ), 409, ) try: consumption.reversed = True db.session.add(consumption) db.session.commit() except SQLAlchemyError: return jsonify({"error": "Database error."}), 500 return jsonify(consumption=consumption.as_dict), 200