Files
sharangandClaude Opus 5.5 c33ae65342
ci / shared (push) Successful in 13s
ci / test (push) Successful in 23m40s
ci / image (push) Successful in 49s
fix(erp): webhook templates and usage datetimes for the live ERPNext (#49)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-30 08:43:17 +00:00

136 lines
5.6 KiB
Go

package erpsetup
// What the registry expects in ERPNext. Keep in step with internal/erp
// (Customer.tenant_id, Lead fields, UsageRecord) and the webhook receiver
// (internal/server/erp.go, erp.WebhookEvent).
func customField(dt, fieldname, label, fieldtype, after string, unique bool) map[string]any {
f := map[string]any{
"doctype": "Custom Field",
"name": dt + "-" + fieldname,
"dt": dt,
"fieldname": fieldname,
"label": label,
"fieldtype": fieldtype,
"insert_after": after,
"no_copy": 1,
"description": "Set by the BreakPilot tenant-registry. Do not edit.",
}
if unique {
f["unique"] = 1
}
return f
}
func customFields(leadDoctype string) []map[string]any {
return []map[string]any{
customField("Customer", "tenant_id", "Tenant ID", "Data", "customer_name", true),
customField("Customer", "tenant_slug", "Tenant slug", "Data", "tenant_id", false),
customField(leadDoctype, "tenant_id", "Tenant ID", "Data", "company_name", false),
customField(leadDoctype, "requested_product", "Requested product", "Data", "tenant_id", false),
}
}
func usageRecordDoctype() map[string]any {
field := func(name, label, typ string, extra map[string]any) map[string]any {
f := map[string]any{"fieldname": name, "label": label, "fieldtype": typ, "in_list_view": 1}
for k, v := range extra {
f[k] = v
}
return f
}
return map[string]any{
"doctype": "DocType",
"name": "Usage Record",
"module": "Selling",
"custom": 1,
"autoname": "hash",
"track_changes": 0,
"description": "Metered usage pushed by the BreakPilot tenant-registry (ERP/02).",
"fields": []map[string]any{
field("customer", "Customer", "Link", map[string]any{"options": "Customer", "reqd": 1}),
field("tenant_id", "Tenant ID", "Data", map[string]any{"reqd": 1}),
field("product", "Product", "Data", map[string]any{"reqd": 1}),
field("metric", "Metric", "Data", map[string]any{"reqd": 1}),
field("qty", "Quantity", "Float", map[string]any{"reqd": 1}),
field("occurred_at", "Occurred at", "Datetime", map[string]any{"reqd": 1}),
field("idempotency_key", "Idempotency key", "Data", map[string]any{"reqd": 1, "unique": 1, "in_list_view": 0}),
},
"permissions": []map[string]any{
{"role": "System Manager", "read": 1, "write": 1, "create": 1, "delete": 1, "report": 1, "export": 1},
{"role": "Accounts Manager", "read": 1, "report": 1, "export": 1},
{"role": "Sales Manager", "read": 1, "report": 1, "export": 1},
// The registry's API user (platform-registry) holds Sales User and
// Accounts User, not System Manager: it only appends usage.
{"role": "Sales User", "read": 1, "create": 1},
},
}
}
type webhook struct {
Doctype string
DocEvent string
Condition string
Event string // the registry event the JSON carries
JSON string // Jinja template of the body
URL string
Secret string
}
func (w webhook) doc() map[string]any {
return map[string]any{
"doctype": "Webhook",
// Frappe names a Webhook by prompt: without a name the insert fails
// with "Please enter the document name".
"name": "breakpilot-" + w.Event,
"webhook_doctype": w.Doctype,
"webhook_docevent": w.DocEvent,
"condition": w.Condition,
"request_url": w.URL,
"request_method": "POST",
"request_structure": "JSON",
"webhook_json": w.JSON,
"enable_security": 1,
"webhook_secret": w.Secret,
"enabled": 1,
"webhook_headers": []map[string]any{{"key": "Content-Type", "value": "application/json"}},
}
}
// The body templates render erp.WebhookEvent. "modified" makes the receiver's
// fallback event id unique per document version (live Frappe sends no
// X-Frappe-Event-Id).
//
// Frappe renders them with {"doc": <the document as a dict>, "utils":
// frappe.utils} and nothing else: doc.items is the dict's items() method, not
// the order lines (hence doc["items"]), and there is no "frappe" (hence
// utils.date_diff). Both broke on the live ERPNext; the staging mock never
// renders Jinja.
func webhooks(url, secret string) []webhook {
ws := []webhook{
{
Doctype: "Sales Order", DocEvent: "on_submit", Event: "sales_order.submit",
JSON: `{"event":"sales_order.submit","doctype":"Sales Order","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.customer }}","items":[{% for i in doc["items"] %}{"item_code":"{{ i.item_code }}","qty":{{ i.qty }}}{% if not loop.last %},{% endif %}{% endfor %}]}`,
},
{
Doctype: "Sales Invoice", DocEvent: "on_update_after_submit", Event: "sales_invoice.overdue",
Condition: `doc.status == "Overdue"`,
JSON: `{"event":"sales_invoice.overdue","doctype":"Sales Invoice","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.customer }}","invoice":"{{ doc.name }}","days_overdue":{{ utils.date_diff(utils.nowdate(), doc.due_date) }}}`,
},
{
Doctype: "Payment Entry", DocEvent: "on_submit", Event: "payment_entry.submit",
Condition: `doc.party_type == "Customer"`,
JSON: `{"event":"payment_entry.submit","doctype":"Payment Entry","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.party }}","amount":{{ doc.paid_amount }}}`,
},
{
Doctype: "Subscription", DocEvent: "on_update", Event: "subscription.cancel",
Condition: `doc.status == "Cancelled"`,
JSON: `{"event":"subscription.cancel","doctype":"Subscription","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.party }}","subscription":"{{ doc.name }}"}`,
},
}
for i := range ws {
ws[i].URL, ws[i].Secret = url, secret
}
return ws
}