fix get detail marketing delivery

This commit is contained in:
giovanni
2026-04-08 13:41:17 +07:00
parent ee7fa71139
commit 80f190b69b
@@ -31,6 +31,8 @@ type MarketingDeliveryProductRepositoryImpl struct {
*commonRepo.BaseRepositoryImpl[entity.MarketingDeliveryProduct] *commonRepo.BaseRepositoryImpl[entity.MarketingDeliveryProduct]
} }
const marketingDeliveryProductSelectWithNullAttributed = "marketing_delivery_products.*, NULL AS attributed_project_flock_kandang_id"
func NewMarketingDeliveryProductRepository(db *gorm.DB) MarketingDeliveryProductRepository { func NewMarketingDeliveryProductRepository(db *gorm.DB) MarketingDeliveryProductRepository {
return &MarketingDeliveryProductRepositoryImpl{ return &MarketingDeliveryProductRepositoryImpl{
BaseRepositoryImpl: commonRepo.NewBaseRepository[entity.MarketingDeliveryProduct](db), BaseRepositoryImpl: commonRepo.NewBaseRepository[entity.MarketingDeliveryProduct](db),
@@ -43,9 +45,9 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetDeliveryProductsByProjectFlo
attributionQuery := commonRepo.MarketingDeliveryAttributionRowsQuery(r.DB().WithContext(ctx)) attributionQuery := commonRepo.MarketingDeliveryAttributionRowsQuery(r.DB().WithContext(ctx))
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
Select("DISTINCT "+marketingDeliveryProductSelectWithNullAttributed).
Joins("JOIN (?) AS mda ON mda.marketing_delivery_product_id = marketing_delivery_products.id", attributionQuery). Joins("JOIN (?) AS mda ON mda.marketing_delivery_product_id = marketing_delivery_products.id", attributionQuery).
Where("mda.project_flock_id = ?", projectFlockID). Where("mda.project_flock_id = ?", projectFlockID)
Distinct("marketing_delivery_products.*")
if callback != nil { if callback != nil {
db = callback(db) db = callback(db)
@@ -110,6 +112,7 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetByMarketingId(ctx context.Co
// JOIN untuk filter by marketing_id yang ada di related table // JOIN untuk filter by marketing_id yang ada di related table
db := r.DB().WithContext(ctx). db := r.DB().WithContext(ctx).
Select(marketingDeliveryProductSelectWithNullAttributed).
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id"). Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
Where("marketing_products.marketing_id = ?", marketingId) Where("marketing_products.marketing_id = ?", marketingId)
@@ -124,6 +127,8 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetByMarketingProductID(ctx con
var deliveryProduct entity.MarketingDeliveryProduct var deliveryProduct entity.MarketingDeliveryProduct
if err := r.DB().WithContext(ctx). if err := r.DB().WithContext(ctx).
Model(&entity.MarketingDeliveryProduct{}).
Select(marketingDeliveryProductSelectWithNullAttributed).
Where("marketing_product_id = ?", marketingProductID). Where("marketing_product_id = ?", marketingProductID).
First(&deliveryProduct).Error; err != nil { First(&deliveryProduct).Error; err != nil {
return nil, err return nil, err
@@ -132,6 +137,27 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetByMarketingProductID(ctx con
return &deliveryProduct, nil return &deliveryProduct, nil
} }
func (r *MarketingDeliveryProductRepositoryImpl) GetByID(
ctx context.Context,
id uint,
modifier func(*gorm.DB) *gorm.DB,
) (*entity.MarketingDeliveryProduct, error) {
var deliveryProduct entity.MarketingDeliveryProduct
q := r.DB().WithContext(ctx).
Model(&entity.MarketingDeliveryProduct{}).
Select(marketingDeliveryProductSelectWithNullAttributed)
if modifier != nil {
q = modifier(q)
}
if err := q.First(&deliveryProduct, id).Error; err != nil {
return nil, err
}
return &deliveryProduct, nil
}
func (r *MarketingDeliveryProductRepositoryImpl) GetAttributionRowsByDeliveryProductIDs(ctx context.Context, deliveryProductIDs []uint) ([]commonRepo.MarketingDeliveryAttributionRow, error) { func (r *MarketingDeliveryProductRepositoryImpl) GetAttributionRowsByDeliveryProductIDs(ctx context.Context, deliveryProductIDs []uint) ([]commonRepo.MarketingDeliveryAttributionRow, error) {
if len(deliveryProductIDs) == 0 { if len(deliveryProductIDs) == 0 {
return []commonRepo.MarketingDeliveryAttributionRow{}, nil return []commonRepo.MarketingDeliveryAttributionRow{}, nil
@@ -211,6 +237,7 @@ func (r *MarketingDeliveryProductRepositoryImpl) fetchClosingDeliveryProducts(
} }
query := r.closingDeliveryProductsQuery(ctx). query := r.closingDeliveryProductsQuery(ctx).
Select(marketingDeliveryProductSelectWithNullAttributed).
Where("marketing_delivery_products.id IN ?", deliveryIDs). Where("marketing_delivery_products.id IN ?", deliveryIDs).
Order("marketing_delivery_products.delivery_date DESC") Order("marketing_delivery_products.delivery_date DESC")