@@ -168,19 +168,10 @@ func (a *adapter) ListCustomCurrencies(ctx context.Context, params currencies.Li
168168 q = filter .ApplyToQuery (q , params .Code , customcurrency .FieldCode )
169169 }
170170
171- now := time .Now ()
171+ now := clock .Now ()
172172
173173 if params .CurrencyExpandOptions .CostBasis {
174- q = q .WithCostBasisHistory (func (cbq * entdb.CurrencyCostBasisQuery ) {
175- cbq .Where (
176- currencycostbasis .Namespace (params .Namespace ),
177- currencycostbasis .EffectiveFromLTE (now ),
178- currencycostbasis .Or (
179- currencycostbasis .EffectiveToIsNil (),
180- currencycostbasis .EffectiveToGT (now ),
181- ),
182- )
183- })
174+ q = WithCostBasis (q , now )
184175 }
185176
186177 order := entutils .GetOrdering (sortx .OrderDefault )
@@ -342,7 +333,7 @@ func (a *adapter) GetCurrency(ctx context.Context, params currencies.GetCurrency
342333 return entutils .TransactingRepo (ctx , a , func (ctx context.Context , tx * adapter ) (currencies.Currency , error ) {
343334 at := clock .Now ()
344335
345- qetQuery := tx .db .CustomCurrency .Query ().
336+ q := tx .db .CustomCurrency .Query ().
346337 Where (
347338 customcurrency .Namespace (params .Namespace ),
348339 customcurrency .ID (params .ID ),
@@ -352,7 +343,11 @@ func (a *adapter) GetCurrency(ctx context.Context, params currencies.GetCurrency
352343 ),
353344 )
354345
355- c , err := qetQuery .First (ctx )
346+ if params .CostBasis {
347+ q = WithCostBasis (q , at )
348+ }
349+
350+ c , err := q .First (ctx )
356351 if err != nil {
357352 if entdb .IsNotFound (err ) {
358353 return currencies.Currency {}, models .NewGenericNotFoundError (
@@ -368,42 +363,42 @@ func (a *adapter) GetCurrency(ctx context.Context, params currencies.GetCurrency
368363 return currencies.Currency {}, fmt .Errorf ("failed to map currency from database: %w" , err )
369364 }
370365
371- if params .CostBasis {
372- if c .DeletedAt != nil {
373- at = * c .DeletedAt
374- }
366+ return curr , nil
367+ })
368+ }
375369
376- costBasisQuery := tx .db .CurrencyCostBasis .Query ().
377- Where (
378- currencycostbasis .Namespace (params .Namespace ),
379- currencycostbasis .CurrencyID (params .ID ),
380- currencycostbasis .EffectiveFromLTE (at ),
381- currencycostbasis .Or (
382- currencycostbasis .EffectiveToIsNil (),
383- currencycostbasis .EffectiveToGT (at ),
370+ func WithCostBasis (q * entdb.CustomCurrencyQuery , at time.Time ) * entdb.CustomCurrencyQuery {
371+ return q .WithCostBasisHistory (func (query * entdb.CurrencyCostBasisQuery ) {
372+ query .Where (func (s * sql.Selector ) {
373+ ct := sql .Table (customcurrency .Table )
374+
375+ s .Join (ct ).On (ct .C (customcurrency .FieldID ), s .C (currencycostbasis .FieldCurrencyID ))
376+
377+ s .Where (
378+ sql .Or (
379+ sql .And (
380+ sql .NotNull (ct .C (customcurrency .FieldDeletedAt )),
381+ sql .ColumnsEQ (s .C (currencycostbasis .FieldDeletedAt ), ct .C (currencycostbasis .FieldDeletedAt )),
382+ sql .ColumnsLTE (s .C (currencycostbasis .FieldEffectiveFrom ), ct .C (customcurrency .FieldDeletedAt )),
383+ sql .Or (
384+ sql .IsNull (s .C (currencycostbasis .FieldEffectiveTo )),
385+ sql .ColumnsGT (s .C (currencycostbasis .FieldEffectiveTo ), ct .C (customcurrency .FieldDeletedAt )),
386+ ),
387+ ),
388+ sql .And (
389+ sql .IsNull (ct .C (customcurrency .FieldDeletedAt )),
390+ sql .Or (
391+ sql .IsNull (s .C (currencycostbasis .FieldDeletedAt )),
392+ sql .GT (s .C (currencycostbasis .FieldDeletedAt ), at ),
393+ ),
394+ sql .LTE (s .C (currencycostbasis .FieldEffectiveFrom ), at ),
395+ sql .Or (
396+ sql .IsNull (s .C (currencycostbasis .FieldEffectiveTo )),
397+ sql .GT (s .C (currencycostbasis .FieldEffectiveTo ), at ),
398+ ),
384399 ),
385- )
386-
387- cbs , err := costBasisQuery .All (ctx )
388- if err != nil {
389- if entdb .IsNotFound (err ) {
390- return currencies.Currency {}, models .NewGenericNotFoundError (
391- fmt .Errorf ("currency with id %s not found" , params .ID ),
392- )
393- }
394-
395- return currencies.Currency {}, fmt .Errorf ("failed to get currency: %w" , err )
396- }
397-
398- curr .CostBasis = lo .ToPtr (
399- lo .Map [* entdb.CurrencyCostBasis , currencies.CostBasis ](cbs ,
400- func (item * entdb.CurrencyCostBasis , _ int ) currencies.CostBasis {
401- return mapCostBasisFromDB (item )
402- },
403400 ),
404401 )
405- }
406-
407- return curr , nil
402+ })
408403 })
409404}
0 commit comments