UpdateMatchingRule updates a specific matching rule in the database. Parameters: - ctx: Context for managing the request and tracing. - rule: The matching rule to be updated, including the RuleID, name, description, and criteria. Returns: - An error wrapped in an APIError if the operation fails, or
(ctx context.Context, rule *model.MatchingRule)
| 493 | // Returns: |
| 494 | // - An error wrapped in an APIError if the operation fails, or nil if the update is successful. |
| 495 | func (d Datasource) UpdateMatchingRule(ctx context.Context, rule *model.MatchingRule) error { |
| 496 | ctx, span := otel.Tracer("reconciliation.database").Start(ctx, "Updating matching rule") |
| 497 | defer span.End() |
| 498 | |
| 499 | // Marshal the Criteria field into JSON |
| 500 | criteriaJSON, err := json.Marshal(rule.Criteria) |
| 501 | if err != nil { |
| 502 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to marshal matching rule criteria", err) |
| 503 | } |
| 504 | |
| 505 | // Execute the SQL update statement |
| 506 | result, err := d.Conn.ExecContext(ctx, ` |
| 507 | UPDATE ledgerforge.matching_rules |
| 508 | SET name = $2, description = $3, criteria = $4 |
| 509 | WHERE rule_id = $1 |
| 510 | `, rule.RuleID, rule.Name, rule.Description, criteriaJSON) |
| 511 | if err != nil { |
| 512 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to update matching rule", err) |
| 513 | } |
| 514 | |
| 515 | // Check how many rows were affected |
| 516 | rowsAffected, err := result.RowsAffected() |
| 517 | if err != nil { |
| 518 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to get rows affected", err) |
| 519 | } |
| 520 | |
| 521 | // If no rows were affected, return a NotFound error |
| 522 | if rowsAffected == 0 { |
| 523 | return apierror.NewAPIError(apierror.ErrNotFound, fmt.Sprintf("Matching rule with ID '%s' not found", rule.RuleID), nil) |
| 524 | } |
| 525 | |
| 526 | return nil |
| 527 | } |
| 528 | |
| 529 | // DeleteMatchingRule deletes a specific matching rule from the database. |
| 530 | // Parameters: |