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

Method findMatchingInternalTransaction

reconciliation.go:958–1012  ·  view source on GitHub ↗

findMatchingInternalTransaction attempts to find an internal transaction that matches the given external transaction. It processes the transactions in batches and applies the matching rules. Parameters: - ctx: The context controlling the operation. - externalTxn: The external transaction to match. -

(ctx context.Context, externalTxn *model.Transaction, matchingRules []model.MatchingRule, matchChan chan model.Match, unMatchChan chan string)

Source from the content-addressed store, hash-verified

956// Returns:
957// - error: If any error occurs during processing.
958func (s *LedgerForge) findMatchingInternalTransaction(ctx context.Context, externalTxn *model.Transaction, matchingRules []model.MatchingRule, matchChan chan model.Match, unMatchChan chan string) error {
959 conf, err := config.Fetch()
960 if err != nil {
961 return err
962 }
963
964 matchFound := false
965
966 minAmount, maxAmount, minDate, maxDate, currency := s.calculateMatchingBounds(externalTxn, matchingRules)
967
968 _, err = s.ProcessTransactionInBatches(
969 ctx,
970 externalTxn.TransactionID,
971 big.NewInt(int64(externalTxn.Amount)),
972 conf.Transaction.MaxWorkers,
973 false, // Stream mode
974 func(ctx context.Context, id string, limit int, offset int64) ([]*model.Transaction, error) {
975 return s.datasource.GetTransactionsByCriteria(ctx, minAmount, maxAmount, currency, minDate, maxDate, limit, offset)
976 },
977
978 func(ctx context.Context, jobs <-chan *model.Transaction, results chan<- BatchJobResult, wg *sync.WaitGroup, amount *big.Int) {
979 defer wg.Done()
980 for internalTxn := range jobs {
981 select {
982 case <-ctx.Done():
983 return
984 default:
985 if s.matchesRules(externalTxn, *internalTxn, matchingRules) {
986 matchChan <- model.Match{
987 ExternalTransactionID: externalTxn.TransactionID,
988 InternalTransactionID: internalTxn.TransactionID,
989 Amount: externalTxn.Amount,
990 Date: externalTxn.CreatedAt,
991 }
992 matchFound = true
993 return
994 }
995 }
996 }
997 },
998 )
999 if err != nil && err != context.Canceled {
1000 return err
1001 }
1002
1003 if !matchFound {
1004 select {
1005 case unMatchChan <- externalTxn.TransactionID:
1006 default:
1007 return fmt.Errorf("failed to send unmatched transaction ID to channel")
1008 }
1009 }
1010
1011 return nil
1012}
1013
1014// getExternalTransactionsPaginated retrieves paginated external transactions from the data source.
1015// Parameters:

Callers 1

Calls 5

matchesRulesMethod · 0.95
FetchFunction · 0.92

Tested by

no test coverage detected