Refactor: extract outlet ID filtering to helper method
This commit is contained in:
parent
c573b23d76
commit
015292e830
@ -29,6 +29,13 @@ func NewAnalyticsRepositoryImpl(db *gorm.DB) *AnalyticsRepositoryImpl {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AnalyticsRepositoryImpl) resolveOutletID(query *gorm.DB, outletID *uuid.UUID, column string) *gorm.DB {
|
||||
if outletID != nil {
|
||||
return query.Where(column+" = ?", *outletID)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (r *AnalyticsRepositoryImpl) GetPaymentMethodAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time) ([]*entities.PaymentMethodAnalytics, error) {
|
||||
var results []*entities.PaymentMethodAnalytics
|
||||
|
||||
@ -50,9 +57,7 @@ func (r *AnalyticsRepositoryImpl) GetPaymentMethodAnalytics(ctx context.Context,
|
||||
Where("p.status = ?", entities.PaymentTransactionStatusCompleted).
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
||||
|
||||
if outletID != nil {
|
||||
query = query.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
query = r.resolveOutletID(query, outletID, "o.outlet_id")
|
||||
|
||||
err := query.
|
||||
Group("pm.id, pm.name, pm.type").
|
||||
@ -180,9 +185,7 @@ func (r *AnalyticsRepositoryImpl) GetProductAnalytics(ctx context.Context, organ
|
||||
Where("oi.status != ?", entities.OrderItemStatusCancelled).
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
||||
|
||||
if outletID != nil {
|
||||
query = query.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
query = r.resolveOutletID(query, outletID, "o.outlet_id")
|
||||
|
||||
err := query.
|
||||
Group("p.id, p.name, p.cost, c.id, c.name, c.order, mahpp.hpp_per_unit").
|
||||
@ -235,9 +238,7 @@ func (r *AnalyticsRepositoryImpl) GetProductAnalyticsPerCategory(ctx context.Con
|
||||
Where("oi.status != ?", entities.OrderItemStatusCancelled).
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
||||
|
||||
if outletID != nil {
|
||||
query = query.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
query = r.resolveOutletID(query, outletID, "o.outlet_id")
|
||||
|
||||
err := query.
|
||||
Group("c.id, c.name").
|
||||
@ -267,9 +268,7 @@ func (r *AnalyticsRepositoryImpl) GetDashboardOverview(ctx context.Context, orga
|
||||
Where("o.organization_id = ?", organizationID).
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
||||
|
||||
if outletID != nil {
|
||||
query = query.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
query = r.resolveOutletID(query, outletID, "o.outlet_id")
|
||||
|
||||
err := query.Scan(&result).Error
|
||||
if err != nil {
|
||||
@ -320,9 +319,7 @@ func (r *AnalyticsRepositoryImpl) GetProfitLossAnalytics(ctx context.Context, or
|
||||
Where("o.is_void = false AND o.is_refund = false").
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
||||
|
||||
if outletID != nil {
|
||||
summaryQuery = summaryQuery.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
summaryQuery = r.resolveOutletID(summaryQuery, outletID, "o.outlet_id")
|
||||
|
||||
err := summaryQuery.Scan(&summary).Error
|
||||
if err != nil {
|
||||
@ -374,9 +371,7 @@ func (r *AnalyticsRepositoryImpl) GetProfitLossAnalytics(ctx context.Context, or
|
||||
Group(timeFormat).
|
||||
Order(timeFormat)
|
||||
|
||||
if outletID != nil {
|
||||
dataQuery = dataQuery.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
dataQuery = r.resolveOutletID(dataQuery, outletID, "o.outlet_id")
|
||||
|
||||
err = dataQuery.Scan(&data).Error
|
||||
if err != nil {
|
||||
@ -419,9 +414,7 @@ func (r *AnalyticsRepositoryImpl) GetProfitLossAnalytics(ctx context.Context, or
|
||||
Order("p.name ASC").
|
||||
Limit(1000)
|
||||
|
||||
if outletID != nil {
|
||||
productQuery = productQuery.Where("o.outlet_id = ?", *outletID)
|
||||
}
|
||||
productQuery = r.resolveOutletID(productQuery, outletID, "o.outlet_id")
|
||||
|
||||
err = productQuery.Scan(&productData).Error
|
||||
if err != nil {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user