MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / StartInstantReconciliation

Method StartInstantReconciliation

reconciliation.go:194–249  ·  view source on GitHub ↗

StartInstantReconciliation initiates a reconciliation process directly with provided transactions instead of loading them from an uploaded file. Parameters: - ctx: The context controlling the reconciliation process. - externalTransactions: The array of external transactions to reconcile. - strategy:

(ctx context.Context, externalTransactions []model.ExternalTransaction,
	strategy string, groupCriteria string, matchingRuleIDs []string, isDryRun bool,
)

Source from the content-addressed store, hash-verified

192// - string: The ID of the reconciliation process.
193// - error: If the reconciliation fails to start.
194func (s *LedgerForge) StartInstantReconciliation(ctx context.Context, externalTransactions []model.ExternalTransaction,
195 strategy string, groupCriteria string, matchingRuleIDs []string, isDryRun bool,
196) (string, error) {
197 // Generate a unique ID for the reconciliation
198 reconciliationID := model.GenerateUUIDWithSuffix("recon")
199
200 // Use a temporary ID for the transactions
201 tempID := model.GenerateUUIDWithSuffix("instant")
202
203 // Initialize a new reconciliation object with the provided parameters
204 reconciliation := model.Reconciliation{
205 ReconciliationID: reconciliationID,
206 UploadID: tempID,
207 Status: StatusStarted,
208 StartedAt: time.Now(),
209 IsDryRun: isDryRun,
210 }
211
212 // Record the reconciliation in the data source
213 if err := s.datasource.RecordReconciliation(ctx, &reconciliation); err != nil {
214 return "", err
215 }
216
217 // Store the provided transactions in the database with the temporary ID
218 for _, txn := range externalTransactions {
219 if err := s.storeExternalTransaction(ctx, tempID, txn); err != nil {
220 // Log error and update reconciliation status
221 logrus.Errorf("Error storing transaction: %v", err)
222 err := s.datasource.UpdateReconciliationStatus(ctx, reconciliationID, StatusFailed, 0, 0)
223 if err != nil {
224 logrus.Errorf("Error updating reconciliation status: %v", err)
225 return "", fmt.Errorf("failed to store external transaction: %w", err)
226 }
227 return "", fmt.Errorf("failed to store external transaction: %w", err)
228 }
229 }
230
231 // Detach the context to allow the reconciliation process to run in the background
232 detachedCtx := context.Background()
233 ctxWithTrace := trace.ContextWithSpan(detachedCtx, trace.SpanFromContext(ctx))
234
235 // Start the reconciliation process asynchronously
236 go func() {
237 err := s.processReconciliation(ctxWithTrace, reconciliation, strategy, groupCriteria, matchingRuleIDs)
238 if err != nil {
239 // If an error occurs during the reconciliation, log it and update the reconciliation status to "failed"
240 logrus.Errorf("Error in instant reconciliation process: %v", err)
241 err := s.datasource.UpdateReconciliationStatus(ctxWithTrace, reconciliationID, StatusFailed, 0, 0)
242 if err != nil {
243 logrus.Errorf("Error updating reconciliation status: %v", err)
244 }
245 }
246 }()
247
248 return reconciliationID, nil
249}
250
251// GetReconciliation retrieves a reconciliation by its ID.

Callers 1

InstantReconciliationMethod · 0.80

Calls 5

processReconciliationMethod · 0.95
GenerateUUIDWithSuffixFunction · 0.92
RecordReconciliationMethod · 0.65

Tested by

no test coverage detected