func PostEmailWebhook(w http.ResponseWriter, r *http.Request, matcher *matching.Matcher, mondoApiClient *mondo.MondoApiClient) { defer r.Body.Close() var request = &email.Email{} err := json.NewDecoder(r.Body).Decode(request) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) log.Printf("json parse error: %s\n", err.Error()) return } vendorMatchKey := getVendorMatchKey(request.Subject) if vendorMatchKey == "" { log.Printf("ignored unrecognised vendor") return } bodyHtml, err := request.HTML() if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) log.Printf("html decode error: %s\n", err.Error()) return } transaction := &matching.Email{ MessageId: request.Id, Subject: request.Subject, BodyHtml: bodyHtml, BodyHtmlBase64Url: base64.URLEncoding.EncodeToString([]byte(bodyHtml)), VendorMatchKey: vendorMatchKey, } match := matcher.MatchEmail(transaction) if match == nil { return } err = uploadAttachment(mondoApiClient, match.Transaction.Id, match.Email.BodyHtmlBase64Url) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) log.Printf("upload error: %s\n", err.Error()) return } }
func PostMondoWebhook(w http.ResponseWriter, r *http.Request, matcher *matching.Matcher, mondoApiClient *mondo.MondoApiClient) { defer r.Body.Close() var request = &mondo.WebhookRequest{} err := json.NewDecoder(r.Body).Decode(request) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) log.Printf("json parse error: %s\n", err.Error()) return } if request.Data.Amount > 0 { log.Printf("ignored credit transaction") return } vendorMatchKey := getVendorMatchKey(request.Data.Description) if vendorMatchKey == "" { log.Printf("ignored unrecognised vendor") return } transaction := &matching.Transaction{ Amount: formatAmount(request.Data.Amount), Created: request.Data.Created, Currency: request.Data.Currency, Description: request.Data.Description, Id: request.Data.Id, VendorMatchKey: vendorMatchKey, } match := matcher.MatchTransaction(transaction) if match == nil { return } log.Printf("got match!!") err = uploadAttachment(mondoApiClient, match.Transaction.Id, match.Email.BodyHtmlBase64Url) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) log.Printf("upload error: %s\n", err.Error()) return } }