diff --git a/apply_polled_quote_template_jobmaterials.py b/apply_polled_quote_template_jobmaterials.py index 1e7dcfd..8248fb7 100755 --- a/apply_polled_quote_template_jobmaterials.py +++ b/apply_polled_quote_template_jobmaterials.py @@ -547,7 +547,38 @@ def main() -> int: } if not args.apply: + remote_existing_rows = list_remote_job_materials(session, job_uuid) + remote_existing_blocks_apply = bool(remote_existing_rows) + if remote_existing_blocks_apply: + remote_active_count = sum(1 for remote_row in remote_existing_rows if is_active_remote_job_material(remote_row)) + reason = ( + f"Remote ServiceM8 job already has {len(remote_existing_rows)} jobMaterial row(s) " + f"({remote_active_count} active); apply would be blocked before updates or creates" + ) + incident_id = record_remote_existing_incident( + conn, + form_response_uuid=form_response_uuid, + job_uuid=job_uuid, + apply_run_id=run_id, + desired_count=len(desired_rows), + remote_rows=remote_existing_rows, + action="dry_run_would_block", + reason=reason, + ) + result["remote_existing"] = { + "incident_id": incident_id, + "action": "would_block_remote_existing", + "remote_count": len(remote_existing_rows), + "remote_active_count": remote_active_count, + "reason": reason, + } + if job_update_payload: + job_update_action = ( + "would_update_work_done_description_if_remote_empty" + if remote_existing_blocks_apply + else "would_update_work_done_description" + ) record_apply_row( conn, run_id=run_id, @@ -556,13 +587,14 @@ def main() -> int: row_index=0, row=job_update_row, api_payload=job_update_record_payload, - action="would_update_work_done_description", + action=job_update_action, ) - result["job_update"] = {"action": "would_update_work_done_description", **job_update_record_payload} + result["job_update"] = {"action": job_update_action, **job_update_record_payload} else: result["job_update"] = {"action": "skipped", "reason": "Quote description is empty"} for idx, row in enumerate(desired_rows, start=1): api_payload = build_payload(job_uuid, row) + row_action = "would_create_if_remote_empty" if remote_existing_blocks_apply else "would_create" record_apply_row( conn, run_id=run_id, @@ -571,9 +603,9 @@ def main() -> int: row_index=idx, row=row, api_payload=api_payload, - action="would_create", + action=row_action, ) - result["rows"].append({"action": "would_create", "kind": row.get("kind"), "payload": api_payload}) + result["rows"].append({"action": row_action, "kind": row.get("kind"), "payload": api_payload}) finish_apply_run(conn, run_id, status="dry-run", created_count=0) conn.execute( "UPDATE quote_template_form_responses SET process_status = ? WHERE form_response_uuid = ?",