Create infra scripts
This commit is contained in:
138
uv_app/infrastructure/create_paslauga_kaina.py
Normal file
138
uv_app/infrastructure/create_paslauga_kaina.py
Normal file
@@ -0,0 +1,138 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from uv_app.core.mssql import connect_to_mssql
|
||||
|
||||
BASE_URL = "https://api.manorivile.lt/client/v2"
|
||||
TIMEOUT = 30
|
||||
|
||||
DOTENV_PATH = Path(__file__).resolve().parents[2] / ".env"
|
||||
load_dotenv(DOTENV_PATH, override=True)
|
||||
|
||||
SERVICE_CODE = "KAINA"
|
||||
SERVICE_NAME = "Kaina"
|
||||
SERVICE_UOM = "VNT"
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
api_key = os.getenv("RIVILE_API_KEY", "").strip()
|
||||
if not api_key:
|
||||
raise RuntimeError("Missing RIVILE_API_KEY environment variable.")
|
||||
return api_key
|
||||
|
||||
|
||||
def _post(api_key: str, payload: dict) -> dict:
|
||||
headers = {
|
||||
"ApiKey": api_key,
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
BASE_URL,
|
||||
json=payload,
|
||||
headers=headers,
|
||||
timeout=TIMEOUT,
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise RuntimeError(f"Rivile HTTP {response.status_code}: {response.text}")
|
||||
|
||||
data = response.json()
|
||||
if "errorMessage" in data:
|
||||
raise RuntimeError(f"Rivile API error: {data}")
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def _service_exists(api_key: str, code: str) -> bool:
|
||||
payload = {
|
||||
"method": "GET_N17_LIST",
|
||||
"params": {
|
||||
"list": "H",
|
||||
"fil": f"n17_kodas_ps='{code}'",
|
||||
"limit": 1,
|
||||
},
|
||||
}
|
||||
data = _post(api_key, payload)
|
||||
return bool(data.get("list") or data.get("N17"))
|
||||
|
||||
|
||||
def _choose_service_ds() -> Optional[str]:
|
||||
conn = connect_to_mssql()
|
||||
if conn is None:
|
||||
raise RuntimeError("Failed to connect to MSSQL.")
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT N17_KODAS_DS, COUNT(*) AS cnt
|
||||
FROM dbo.N17_PROD
|
||||
WHERE N17_TIPAS = 2
|
||||
AND N17_KODAS_DS IS NOT NULL
|
||||
AND LTRIM(RTRIM(N17_KODAS_DS)) <> ''
|
||||
GROUP BY N17_KODAS_DS
|
||||
ORDER BY cnt DESC, N17_KODAS_DS
|
||||
"""
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
return str(rows[0][0]).strip()
|
||||
|
||||
|
||||
def create_service_kaina() -> dict:
|
||||
api_key = _get_api_key()
|
||||
|
||||
service_ds = _choose_service_ds()
|
||||
if not service_ds:
|
||||
return {"status": "cancelled", "reason": "No N17_KODAS_DS selected"}
|
||||
|
||||
n17 = {
|
||||
"N17_KODAS_PS": SERVICE_CODE,
|
||||
"N17_TIPAS": "2",
|
||||
"N17_PAV": SERVICE_NAME,
|
||||
"N17_KODAS_US": SERVICE_UOM,
|
||||
"N17_KODAS_DS": service_ds,
|
||||
}
|
||||
|
||||
if _service_exists(api_key, SERVICE_CODE):
|
||||
payload = {
|
||||
"method": "EDIT_N17",
|
||||
"params": {
|
||||
"oper": "U",
|
||||
"user": api_key.split(".", 1)[0],
|
||||
"fld": "N17_KODAS_PS",
|
||||
"val": SERVICE_CODE,
|
||||
},
|
||||
"data": {"N17": n17},
|
||||
}
|
||||
data = _post(api_key, payload)
|
||||
return {"status": "updated", "code": SERVICE_CODE, "response": data}
|
||||
|
||||
payload = {
|
||||
"method": "EDIT_N17",
|
||||
"params": {"oper": "I"},
|
||||
"data": {"N17": n17},
|
||||
}
|
||||
data = _post(api_key, payload)
|
||||
return {"status": "created", "code": SERVICE_CODE, "response": data}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
result = create_service_kaina()
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user