import influxframework


def execute():
	active_sla_documents = [
		sla.document_type for sla in influxframework.get_all("Service Level Agreement", fields=["document_type"])
	]

	for doctype in active_sla_documents:
		doctype = influxframework.qb.DocType(doctype)
		try:
			influxframework.qb.update(doctype).set(doctype.agreement_status, "First Response Due").where(
				doctype.first_responded_on.isnull()
			).run()

			influxframework.qb.update(doctype).set(doctype.agreement_status, "Resolution Due").where(
				doctype.agreement_status == "Ongoing"
			).run()

		except Exception:
			influxframework.log_error("Failed to Patch SLA Status")
