fix(invoiceintake): ToDos are assigned by the intake user
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 25m30s
ci / image (pull_request) Skipped

Frappe 15.112 lets a user without System Manager create a ToDo only when
it is assigned to or by that user (the owner clause came later), so the
first live run got 403 on its first ToDo. The service now asks ERPNext
for its own user once and sets it as assigned_by.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-10-20 10:34:02 +02:00
1 parent cee669d2bd
commit e96bd39e21
2 files changed
+35

No files matched your search

+26
View File
@@ -85,6 +85,7 @@ type Intake struct {
cfg Config
c *frappe
retries map[string]int // e-invoice attempts that failed with a server error
user string // the API key's ERPNext user (ToDo assigned_by)
}
// maxEInvoiceRetries: after that many server errors on the e-invoice import
@@ -291,11 +292,18 @@ func (in *Intake) todo(ctx context.Context, res Result, reason string) (Result,
if in.cfg.DryRun {
return res, nil
}
me, err := in.self(ctx)
if err != nil {
return res, err
}
// Frappe 15.112 lets a user without System Manager create a ToDo only if
// it is assigned to or by that user; the creator alone is not enough.
doc := map[string]any{
"description": "Rechnungseingang: " + reason,
"reference_type": "Communication",
"reference_name": res.Communication,
"priority": "Medium",
"assigned_by": me,
}
if in.cfg.AssignTo != "" {
doc["allocated_to"] = in.cfg.AssignTo
@@ -305,6 +313,24 @@ func (in *Intake) todo(ctx context.Context, res Result, reason string) (Result,
return res, err
}
// self is the ERPNext user the API key belongs to, asked once.
func (in *Intake) self(ctx context.Context) (string, error) {
if in.user != "" {
return in.user, nil
}
var out struct {
Message string `json:"message"`
}
if err := in.c.do(ctx, http.MethodGet, "/api/method/frappe.auth.get_logged_user", nil, &out); err != nil {
return "", fmt.Errorf("logged user: %w", err)
}
if out.Message == "" {
return "", errors.New("logged user: empty answer")
}
in.user = out.Message
return in.user, nil
}
// supplierFor finds the one supplier that sender belongs to: first by the
// exact address (Supplier e-mail, or a Contact linked to a Supplier), then by
// its domain unless that is a freemail or our own one. how says which, or why
+9
View File
@@ -101,6 +101,10 @@ func (f *fakeERP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
return
}
if r.URL.Path == "/api/method/frappe.auth.get_logged_user" {
_ = json.NewEncoder(w).Encode(map[string]any{"message": "bot@company.example"})
return
}
parts := strings.SplitN(strings.TrimPrefix(r.URL.Path, "/api/resource/"), "/", 2)
doctype, _ := url.PathUnescape(parts[0])
reply := func(status int, v any) {
@@ -129,6 +133,11 @@ func (f *fakeERP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var doc map[string]any
_ = json.NewDecoder(r.Body).Decode(&doc)
f.writes = append(f.writes, "POST "+doctype)
// Frappe 15.112: a ToDo must be assigned to or by the creating user
if doctype == "ToDo" && doc["assigned_by"] != "bot@company.example" && doc["allocated_to"] != "bot@company.example" {
reply(http.StatusForbidden, map[string]any{"exc_type": "PermissionError"})
return
}
if m, ok := doc[markerField]; ok && len(f.find(doctype, [][]any{{markerField, "=", m}})) > 0 {
reply(http.StatusConflict, map[string]any{"exc_type": "DuplicateEntryError"})
return