Prepares a dynamic SQL query based on the fields to be included. This query fetches balance details, including optional joins for identity and ledger.
(queryBuilder strings.Builder, include []string)
| 58 | // Prepares a dynamic SQL query based on the fields to be included. |
| 59 | // This query fetches balance details, including optional joins for identity and ledger. |
| 60 | func prepareQueries(queryBuilder strings.Builder, include []string) string { |
| 61 | var selectFields []string |
| 62 | |
| 63 | // Default fields for balances |
| 64 | selectFields = append(selectFields, |
| 65 | "b.balance_id", "b.balance", "b.credit_balance", "b.debit_balance", |
| 66 | "b.currency", "b.currency_multiplier", "b.ledger_id", |
| 67 | "COALESCE(b.identity_id, '') as identity_id", "b.created_at", "b.meta_data", "b.inflight_balance", "b.inflight_credit_balance", "b.inflight_debit_balance", "b.version", "b.indicator", "b.track_fund_lineage", "COALESCE(b.allocation_strategy, 'FIFO') as allocation_strategy") |
| 68 | |
| 69 | // Conditionally include identity fields |
| 70 | if contains(include, "identity") { |
| 71 | selectFields = append(selectFields, |
| 72 | "i.identity_id", "i.first_name", "i_name", "i.category", "i.last_name", "i.other_names", |
| 73 | "i.gender", "i.dob", "i.email_address", "i.phone_number", |
| 74 | "i.nationality", "i.street", "i.country", "i.state", |
| 75 | "i.post_code", "i.city", "i.created_at") |
| 76 | } |
| 77 | |
| 78 | // Conditionally include ledger fields |
| 79 | if contains(include, "ledger") { |
| 80 | selectFields = append(selectFields, |
| 81 | "l.ledger_id", "l.name", "l.created_at") |
| 82 | } |
| 83 | |
| 84 | // Construct the SQL query |
| 85 | queryBuilder.WriteString("SELECT ") |
| 86 | queryBuilder.WriteString(strings.Join(selectFields, ", ")) |
| 87 | queryBuilder.WriteString(` |
| 88 | FROM ( |
| 89 | SELECT * FROM ledgerforge.balances WHERE balance_id = $1 |
| 90 | ) AS b |
| 91 | `) |
| 92 | |
| 93 | // Add optional joins for identity and ledger |
| 94 | if contains(include, "identity") { |
| 95 | queryBuilder.WriteString(` |
| 96 | LEFT JOIN ledgerforge.identity i ON b.identity_id = i.identity_id |
| 97 | `) |
| 98 | } |
| 99 | if contains(include, "ledger") { |
| 100 | queryBuilder.WriteString(` |
| 101 | LEFT JOIN ledgerforge.ledgers l ON b.ledger_id = l.ledger_id |
| 102 | `) |
| 103 | } |
| 104 | |
| 105 | return queryBuilder.String() |
| 106 | } |
| 107 | |
| 108 | // Scans a SQL row result and maps it into a Balance object, including optional identity and ledger data. |
| 109 | // Converts string representations of big.Int fields into actual big.Int objects. |
no test coverage detected