RecordMatches batches the saving of match records associated with a specific reconciliation ID. It uses a database transaction to ensure atomicity and consistency of the batch insert operation. Parameters: - ctx: Context for managing request and tracing. - reconciliationID: The ID of the reconciliat
(ctx context.Context, reconciliationID string, matches []model.Match)
| 178 | // Returns: |
| 179 | // - An error if the operation fails, wrapped in an APIError for consistency. |
| 180 | func (d Datasource) RecordMatches(ctx context.Context, reconciliationID string, matches []model.Match) error { |
| 181 | ctx, span := otel.Tracer("reconciliation.database").Start(ctx, "Batch saving matches to db") |
| 182 | defer span.End() |
| 183 | |
| 184 | txn, err := d.Conn.BeginTx(ctx, nil) |
| 185 | if err != nil { |
| 186 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to start transaction", err) |
| 187 | } |
| 188 | defer func() { |
| 189 | if err := txn.Rollback(); err != nil && err != sql.ErrTxDone { |
| 190 | span.RecordError(fmt.Errorf("error rolling back transaction: %w", err)) |
| 191 | } |
| 192 | }() |
| 193 | |
| 194 | for _, match := range matches { |
| 195 | match.ReconciliationID = reconciliationID |
| 196 | err := d.recordMatchInTransaction(ctx, txn, &match) |
| 197 | if err != nil { |
| 198 | return err // The error is already wrapped in an APIError by recordMatchInTransaction |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if err := txn.Commit(); err != nil { |
| 203 | return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to commit transaction", err) |
| 204 | } |
| 205 | |
| 206 | return nil |
| 207 | } |
| 208 | |
| 209 | // RecordUnmatched batches the saving of unmatched external transactions related to a specific reconciliation. |
| 210 | // It uses a database transaction to ensure atomicity and consistency of the batch insert operation. |