Commit 389af47c authored by David Foucher's avatar David Foucher

Add remuneration api endpoint

parent 4101b858
Pipeline #1821 passed with stage
in 1 minute and 14 seconds
......@@ -12,6 +12,7 @@ from openapi_spec_validator import validate_spec
from trefle.config import FINANCEMENTS
from trefle.config import REMUNERATIONS
from trefle import VERSION
pytestmark = pytest.mark.asyncio
......@@ -55,6 +56,33 @@ async def test_simulate_endpoint(client):
async def test_remuneration_endpoint(client):
resp = await client.get("/schema")
spec = create_spec(json.loads(resp.body))
resp = await
"beneficiaire.age": 20,
"formation.region": 27,
"formation.codes_financeur": [2],
assert resp.status == HTTPStatus.OK
assert "remunerations" in json.loads(resp.body)
remunerations = json.loads(resp.body)["remunerations"]
assert remunerations
assert "remuneration" in remunerations[0]
assert "Version" in resp.headers
validator = ResponseValidator(spec)
request = MockRequest("", "post", "/remuneration")
response = MockResponse(resp.body, resp.status.value)
result = validator.validate(request, response)
async def test_simulate_endpoint_without_formation_prix_horaire(client):
resp = await client.get("/schema")
......@@ -37,3 +37,10 @@ def get_financements(tags=None):
for tag in tags or []:
financements = [f for f in financements if tag in f["tags"]]
return financements
def get_remunerations(tags=None):
remunerations = [config.Remuneration(f) for f in config.REMUNERATIONS]
for tag in tags or []:
remunerations = [f for f in remunerations if tag in f["tags"]]
return remunerations
......@@ -49,6 +49,77 @@ async def simulate_(request, response):
log_simulate(context, errors=error)
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, error)
eligible = request.query.bool("eligible", None)
if eligible is not None:
financements = [f for f in financements if f["eligible"] == eligible]
financements = sorted(
financements, key=lambda value: value["eligible"], reverse=True
explain = request.query.bool("explain", False)
for financement in financements:
financement["explain"] = (
[s.json for s in financement["explain"]] if explain else None
body = {"financements": financements}
if request.query.bool("context", False):
body["context"] = {
k: v for k, v in context.items()
if k in SCHEMA and "label" in SCHEMA[k]}
if request.query.bool("scenario", False):
body["scenario"] = make_scenario(context, financements)
response.json = body
log_simulate(context, financements=financements)
@app.route("/remuneration", methods=["POST"])
async def remuneration_(request, response):
data = request.json
remunerations = get_remunerations(tags=request.query.list("tags", []))
context = Context(data.copy())
for remuneration in remunerations:
copy = context.copy()
routine.check_remuneration(context, remuneration)
# FIXME (limits of the single-store-all context object)
# Clean keys not meant to be exposed
for key in list(data.keys()):
if key.startswith("remuneration"):
del data[key]
except DataError as err:
error = {err.key: err.error}
log_simulate(context, errors=error)
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, error)
# TODO: explain only for financement see check_remuneration
# explain = request.query.bool("explain", False)
# for remuneration in remunerations:
# remuneration["explain"] = (
# [s.json for s in remunerations["explain"]] if explain else None