Fix{BE-222] fixing approval status when updated and delete timestamz on children of marketing table

This commit is contained in:
aguhh18
2025-11-18 12:44:19 +07:00
parent 320f5e65c6
commit a57ef82ebb
5 changed files with 63 additions and 40 deletions
@@ -0,0 +1,11 @@
-- Add back timestamp columns to marketing_products table
ALTER TABLE marketing_products
ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW(),
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT NOW(),
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
-- Add back timestamp columns to marketing_delivery_products table
ALTER TABLE marketing_delivery_products
ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW(),
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT NOW(),
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
@@ -0,0 +1,27 @@
-- Drop timestamp columns from marketing_products table if it exists
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_products' AND column_name = 'created_at') THEN
ALTER TABLE marketing_products DROP COLUMN created_at;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_products' AND column_name = 'updated_at') THEN
ALTER TABLE marketing_products DROP COLUMN updated_at;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_products' AND column_name = 'deleted_at') THEN
ALTER TABLE marketing_products DROP COLUMN deleted_at;
END IF;
END $$;
-- Drop timestamp columns from marketing_delivery_products table if it exists
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_delivery_products' AND column_name = 'created_at') THEN
ALTER TABLE marketing_delivery_products DROP COLUMN created_at;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_delivery_products' AND column_name = 'updated_at') THEN
ALTER TABLE marketing_delivery_products DROP COLUMN updated_at;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_delivery_products' AND column_name = 'deleted_at') THEN
ALTER TABLE marketing_delivery_products DROP COLUMN deleted_at;
END IF;
END $$;
@@ -2,23 +2,18 @@ package entities
import ( import (
"time" "time"
"gorm.io/gorm"
) )
type MarketingDeliveryProduct struct { type MarketingDeliveryProduct struct {
Id uint `gorm:"primaryKey;autoIncrement"` Id uint `gorm:"primaryKey;autoIncrement"`
MarketingProductId uint `gorm:"uniqueIndex;not null"` MarketingProductId uint `gorm:"uniqueIndex;not null"`
Qty float64 `gorm:"type:numeric(15,3)"` Qty float64 `gorm:"type:numeric(15,3)"`
UnitPrice float64 `gorm:"type:numeric(15,3)"` UnitPrice float64 `gorm:"type:numeric(15,3)"`
TotalWeight float64 `gorm:"type:numeric(15,3)"` TotalWeight float64 `gorm:"type:numeric(15,3)"`
AvgWeight float64 `gorm:"type:numeric(15,3)"` AvgWeight float64 `gorm:"type:numeric(15,3)"`
TotalPrice float64 `gorm:"type:numeric(15,3)"` TotalPrice float64 `gorm:"type:numeric(15,3)"`
DeliveryDate *time.Time `gorm:"type:timestamptz"` DeliveryDate *time.Time `gorm:"type:timestamptz"`
VehicleNumber string `gorm:"type:varchar(50)"` VehicleNumber string `gorm:"type:varchar(50)"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
MarketingProduct MarketingProduct `gorm:"foreignKey:MarketingProductId;references:Id"` MarketingProduct MarketingProduct `gorm:"foreignKey:MarketingProductId;references:Id"`
} }
+8 -17
View File
@@ -1,23 +1,14 @@
package entities package entities
import (
"time"
"gorm.io/gorm"
)
type MarketingProduct struct { type MarketingProduct struct {
Id uint `gorm:"primaryKey;autoIncrement"` Id uint `gorm:"primaryKey;autoIncrement"`
MarketingId uint `gorm:"not null"` MarketingId uint `gorm:"not null"`
ProductWarehouseId uint `gorm:"not null"` ProductWarehouseId uint `gorm:"not null"`
Qty float64 `gorm:"type:numeric(15,3);not null"` Qty float64 `gorm:"type:numeric(15,3);not null"`
UnitPrice float64 `gorm:"type:numeric(15,3);not null"` UnitPrice float64 `gorm:"type:numeric(15,3);not null"`
AvgWeight float64 `gorm:"type:numeric(15,3);not null"` AvgWeight float64 `gorm:"type:numeric(15,3);not null"`
TotalWeight float64 `gorm:"type:numeric(15,3);not null"` TotalWeight float64 `gorm:"type:numeric(15,3);not null"`
TotalPrice float64 `gorm:"type:numeric(15,3);not null"` TotalPrice float64 `gorm:"type:numeric(15,3);not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Marketing Marketing `gorm:"foreignKey:MarketingId;references:Id"` Marketing Marketing `gorm:"foreignKey:MarketingId;references:Id"`
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"` ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
@@ -270,7 +270,6 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update marketing product") return fiber.NewError(fiber.StatusInternalServerError, "Failed to update marketing product")
} }
// Ensure delivery product exists; if not, create default
if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil { if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
mdp := &entity.MarketingDeliveryProduct{ mdp := &entity.MarketingDeliveryProduct{
@@ -321,21 +320,21 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
} }
} }
} }
if latestApproval != nil {
if latestApproval != nil && latestApproval.StepNumber == 2 {
actorID := uint(1) // todo: ambil dari auth context actorID := uint(1) // todo: ambil dari auth context
resetNote := ""
action := entity.ApprovalActionUpdated action := entity.ApprovalActionUpdated
_, err := approvalSvcTx.CreateApproval( _, err := approvalSvcTx.CreateApproval(
c.Context(), c.Context(),
utils.ApprovalWorkflowMarketing, utils.ApprovalWorkflowMarketing,
id, id,
utils.MarketingStepPengajuan, approvalutils.ApprovalStep(latestApproval.StepNumber),
&action, &action,
actorID, actorID,
&resetNote) nil)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to reset approval status") if !errors.Is(err, gorm.ErrDuplicatedKey) {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create update approval")
}
} }
} }
@@ -371,7 +370,7 @@ func (s salesOrdersService) DeleteOne(c *fiber.Ctx, id uint) error {
if len(marketing.Products) > 0 { if len(marketing.Products) > 0 {
for _, product := range marketing.Products { for _, product := range marketing.Products {
if err := marketingDeliveryProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB { if err := marketingDeliveryProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
return db.Where("marketing_product_id = ?", product.Id) return db.Where("marketing_product_id = ?", product.Id).Unscoped()
}); err != nil && err != gorm.ErrRecordNotFound { }); err != nil && err != gorm.ErrRecordNotFound {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products") return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products")
} }
@@ -379,7 +378,7 @@ func (s salesOrdersService) DeleteOne(c *fiber.Ctx, id uint) error {
} }
if err := marketingProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB { if err := marketingProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
return db.Where("marketing_id = ?", id) return db.Where("marketing_id = ?", id).Unscoped()
}); err != nil && err != gorm.ErrRecordNotFound { }); err != nil && err != gorm.ErrRecordNotFound {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products") return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products")
} }