add cancel

This commit is contained in:
2025-07-18 00:14:45 -03:00
parent 6acc4bde4d
commit 61e7ac0f4b
7 changed files with 107 additions and 44 deletions

View File

@@ -31,7 +31,7 @@ def create_user(
name: str,
email: str,
cpf: str | None,
) -> dict:
) -> int:
url = urlparse(KONVIVA_API_URL)._replace(
path='/action/api/integrarUsuario',
query='sendMail=false',
@@ -58,9 +58,10 @@ def create_user(
},
)
r.raise_for_status()
data = r.json()
# Because Konviva does not return the proper HTTP status code
if err := glom(r.json(), 'errors', default=None):
if err := glom(data, 'errors', default=None):
err = err[0] if isinstance(err, list) else err
if err == 'Login já existente':
@@ -68,7 +69,7 @@ def create_user(
else:
raise KonvivaError(err)
return r.json().get('IDUsuario')
return int(data.get('IDUsuario'))
def update_user(id: str, **kwargs) -> dict:
@@ -86,12 +87,13 @@ def update_user(id: str, **kwargs) -> dict:
},
)
r.raise_for_status()
data = r.json()
if err := glom(r.json(), 'errors', default=None):
if err := glom(data, 'errors', default=None):
err = err[0] if isinstance(err, list) else err
raise KonvivaError(err)
return r.json()
return data
def get_users_by_email(email: str) -> list[dict]:
@@ -104,29 +106,48 @@ def get_users_by_email(email: str) -> list[dict]:
headers=headers,
)
r.raise_for_status()
data = r.json()
if glom(r.json(), '0.errors', default=None):
if glom(data, '0.errors', default=None):
return []
return r.json()
return data
def enroll(user_id: str, class_id: str) -> str:
def _post_enrollment(json: dict) -> dict:
url = urlparse(KONVIVA_API_URL)._replace(path='/action/api/integrarMatricula')
r = requests.post(
url=url.geturl(),
headers=headers,
json={
'IDUsuario': str(user_id),
'IDTurma': str(class_id),
'StatusMatricula': 'MATRICULADO',
},
json=json,
)
r.raise_for_status()
data = r.json()
if err := glom(r.json(), 'errors', default=None):
if err := glom(data, 'errors', default=None):
err = err[0] if isinstance(err, list) else err
raise KonvivaError(err)
return r.json().get('IDMatricula')
return data
def enroll(user_id: str, class_id: str) -> int:
r = _post_enrollment(
{
'IDUsuario': int(user_id),
'IDTurma': int(class_id),
'StatusMatricula': 'MATRICULADO',
}
)
return int(r.get('IDMatricula')) # type: ignore
def cancel_enrollment(id: str) -> dict:
return _post_enrollment(
{
'IDMatricula': str(id),
'StatusMatricula': 'CANCELADO',
}
)