diff --git a/internal/database/migrations/20251118044026_update_marketing_product_and_marketing_delivery.down.sql b/internal/database/migrations/20251118044026_update_marketing_product_and_marketing_delivery.down.sql new file mode 100644 index 00000000..4da2ec3e --- /dev/null +++ b/internal/database/migrations/20251118044026_update_marketing_product_and_marketing_delivery.down.sql @@ -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; \ No newline at end of file diff --git a/internal/database/migrations/20251118044026_update_marketing_product_and_marketing_delivery.up.sql b/internal/database/migrations/20251118044026_update_marketing_product_and_marketing_delivery.up.sql new file mode 100644 index 00000000..65750f8d --- /dev/null +++ b/internal/database/migrations/20251118044026_update_marketing_product_and_marketing_delivery.up.sql @@ -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 $$; \ No newline at end of file diff --git a/internal/entities/marketing_delivery_product.go b/internal/entities/marketing_delivery_product.go index 253c00b2..47f6e89d 100644 --- a/internal/entities/marketing_delivery_product.go +++ b/internal/entities/marketing_delivery_product.go @@ -2,23 +2,18 @@ package entities import ( "time" - - "gorm.io/gorm" ) type MarketingDeliveryProduct struct { - Id uint `gorm:"primaryKey;autoIncrement"` - MarketingProductId uint `gorm:"uniqueIndex;not null"` - Qty float64 `gorm:"type:numeric(15,3)"` - UnitPrice float64 `gorm:"type:numeric(15,3)"` - TotalWeight float64 `gorm:"type:numeric(15,3)"` - AvgWeight float64 `gorm:"type:numeric(15,3)"` - TotalPrice float64 `gorm:"type:numeric(15,3)"` - DeliveryDate *time.Time `gorm:"type:timestamptz"` - VehicleNumber string `gorm:"type:varchar(50)"` - CreatedAt time.Time `gorm:"autoCreateTime"` - UpdatedAt time.Time `gorm:"autoUpdateTime"` - DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + Id uint `gorm:"primaryKey;autoIncrement"` + MarketingProductId uint `gorm:"uniqueIndex;not null"` + Qty float64 `gorm:"type:numeric(15,3)"` + UnitPrice float64 `gorm:"type:numeric(15,3)"` + TotalWeight float64 `gorm:"type:numeric(15,3)"` + AvgWeight float64 `gorm:"type:numeric(15,3)"` + TotalPrice float64 `gorm:"type:numeric(15,3)"` + DeliveryDate *time.Time `gorm:"type:timestamptz"` + VehicleNumber string `gorm:"type:varchar(50)"` MarketingProduct MarketingProduct `gorm:"foreignKey:MarketingProductId;references:Id"` } diff --git a/internal/entities/marketing_product.go b/internal/entities/marketing_product.go index 66524bc6..f2294f10 100644 --- a/internal/entities/marketing_product.go +++ b/internal/entities/marketing_product.go @@ -1,23 +1,14 @@ package entities -import ( - "time" - - "gorm.io/gorm" -) - type MarketingProduct struct { - Id uint `gorm:"primaryKey;autoIncrement"` - MarketingId uint `gorm:"not null"` - ProductWarehouseId uint `gorm:"not null"` - Qty 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"` - TotalWeight 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:"-"` + Id uint `gorm:"primaryKey;autoIncrement"` + MarketingId uint `gorm:"not null"` + ProductWarehouseId uint `gorm:"not null"` + Qty 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"` + TotalWeight float64 `gorm:"type:numeric(15,3);not null"` + TotalPrice float64 `gorm:"type:numeric(15,3);not null"` Marketing Marketing `gorm:"foreignKey:MarketingId;references:Id"` ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"` diff --git a/internal/modules/marketing/sales-orders/services/sales-orders.service.go b/internal/modules/marketing/sales-orders/services/sales-orders.service.go index df25a70e..c1cd8f5f 100644 --- a/internal/modules/marketing/sales-orders/services/sales-orders.service.go +++ b/internal/modules/marketing/sales-orders/services/sales-orders.service.go @@ -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") } - // Ensure delivery product exists; if not, create default if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { mdp := &entity.MarketingDeliveryProduct{ @@ -321,21 +320,21 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u } } } - - if latestApproval != nil && latestApproval.StepNumber == 2 { + if latestApproval != nil { actorID := uint(1) // todo: ambil dari auth context - resetNote := "" action := entity.ApprovalActionUpdated _, err := approvalSvcTx.CreateApproval( c.Context(), utils.ApprovalWorkflowMarketing, id, - utils.MarketingStepPengajuan, + approvalutils.ApprovalStep(latestApproval.StepNumber), &action, actorID, - &resetNote) + 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 { for _, product := range marketing.Products { 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 { 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 { - return db.Where("marketing_id = ?", id) + return db.Where("marketing_id = ?", id).Unscoped() }); err != nil && err != gorm.ErrRecordNotFound { return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products") }