Closed look on webhook calling ofrm update scripts - no live updated to sevicem8 yet though. Create project-progress as well.
This commit is contained in:
@@ -7,6 +7,12 @@ from datetime import datetime, timezone
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import PlainTextResponse
|
||||
from servicem8_quote_template_parser import (
|
||||
QUOTE_TEMPLATE_FORM_UUID,
|
||||
STATE_DB_PATH,
|
||||
init_state_db as init_quote_state_db,
|
||||
parse_quote_template_form_response,
|
||||
)
|
||||
|
||||
DB_PATH = os.getenv("WEBHOOK_DB_PATH", "./servicem8_webhooks.db")
|
||||
APP_HOST = os.getenv("WEBHOOK_HOST", "0.0.0.0")
|
||||
@@ -75,6 +81,8 @@ def init_db():
|
||||
|
||||
conn.commit()
|
||||
|
||||
init_quote_state_db(STATE_DB_PATH)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
@@ -145,6 +153,37 @@ def store_simple_event(table_name, request: Request, client_host: str, received_
|
||||
conn.commit()
|
||||
|
||||
|
||||
def queue_quote_template_jobmaterials(payload, received_at: str):
|
||||
try:
|
||||
parsed = parse_quote_template_form_response(payload)
|
||||
except Exception as exc:
|
||||
logger.warning("Quote template parser failed: %s", exc)
|
||||
return
|
||||
|
||||
form_uuid = parsed.get("form_uuid", "")
|
||||
if form_uuid != QUOTE_TEMPLATE_FORM_UUID:
|
||||
return
|
||||
|
||||
queue_path = os.path.join(os.path.dirname(DB_PATH), "quote-template-jobmaterials-queue.jsonl")
|
||||
queue_record = {
|
||||
"queued_at": received_at,
|
||||
"form_uuid": form_uuid,
|
||||
"form_response_uuid": parsed.get("form_response_uuid", ""),
|
||||
"job_uuid": parsed.get("job_uuid", ""),
|
||||
"description": parsed.get("description", ""),
|
||||
"desired_job_materials": parsed.get("desired_job_materials", []),
|
||||
}
|
||||
with open(queue_path, "a", encoding="utf-8") as fh:
|
||||
fh.write(json.dumps(queue_record, ensure_ascii=False) + "\n")
|
||||
|
||||
logger.info(
|
||||
"Queued quote-template jobmaterials: form_response_uuid=%s job_uuid=%s rows=%s",
|
||||
queue_record["form_response_uuid"],
|
||||
queue_record["job_uuid"],
|
||||
len(queue_record["desired_job_materials"]),
|
||||
)
|
||||
|
||||
|
||||
@app.post("/webhooks/servicem8-job-updated")
|
||||
async def servicem8_event_webhook(request: Request):
|
||||
payload = await parse_request_payload(request)
|
||||
@@ -174,6 +213,7 @@ async def servicem8_form_response_webhook(request: Request):
|
||||
return challenge_response
|
||||
|
||||
store_simple_event("webhook_form_responses", request, client_host, received_at, headers, payload)
|
||||
queue_quote_template_jobmaterials(payload, received_at)
|
||||
|
||||
logger.info("Form response webhook received from %s and stored", client_host)
|
||||
return PlainTextResponse("OK", status_code=200)
|
||||
|
||||
Reference in New Issue
Block a user