Skip to content

Commit fc01e09

Browse files
committed
refactor: sideloading costbasis for currencies
1 parent 5083276 commit fc01e09

1 file changed

Lines changed: 35 additions & 45 deletions

File tree

openmeter/currencies/adapter/currencies.go

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
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+
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,37 @@ 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-
}
375-
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),
384-
),
385-
)
366+
return curr, nil
367+
})
368+
}
386369

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-
}
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)
394374

395-
return currencies.Currency{}, fmt.Errorf("failed to get currency: %w", err)
396-
}
375+
s.Join(ct).On(ct.C(customcurrency.FieldID), s.C(currencycostbasis.FieldCurrencyID))
397376

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-
},
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+
),
383+
sql.And(
384+
sql.IsNull(ct.C(customcurrency.FieldDeletedAt)),
385+
sql.Or(
386+
sql.IsNull(s.C(currencycostbasis.FieldDeletedAt)),
387+
sql.GT(s.C(currencycostbasis.FieldDeletedAt), at),
388+
),
389+
sql.LTE(s.C(currencycostbasis.FieldEffectiveFrom), at),
390+
sql.Or(
391+
sql.IsNull(s.C(currencycostbasis.FieldEffectiveTo)),
392+
sql.GT(s.C(currencycostbasis.FieldEffectiveTo), at),
393+
),
394+
),
403395
),
404396
)
405-
}
406-
407-
return curr, nil
397+
})
408398
})
409399
}

0 commit comments

Comments
 (0)