Update Auto Generate SKU

This commit is contained in:
ahmadafriadi 2025-06-27 13:34:54 +07:00
parent f8798a53a4
commit 9b1f58d783
5 changed files with 62 additions and 2 deletions

View File

@ -11,7 +11,7 @@ func GenerateJWT(userID uint, role string) (string, error) {
claims := jwt.MapClaims{ claims := jwt.MapClaims{
"user_id": userID, "user_id": userID,
"role": role, "role": role,
"exp": time.Now().Add(time.Hour * 1).Unix(), // expired dalam 1 jam "exp": time.Now().Add(time.Hour * 24).Unix(), // expired dalam 1 jam
} }
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

View File

@ -4,6 +4,7 @@ import (
"BE-MiniERP/database" "BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models" "BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository" "BE-MiniERP/modules/inventory/repository"
"BE-MiniERP/modules/inventory/service"
"strconv" "strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -32,9 +33,23 @@ func (h *ProductHandler) Create(c *fiber.Ctx) error {
if err := c.BodyParser(&input); err != nil { if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"}) return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
} }
// Jika hanya terima ID, lakukan preload relasi
if err := h.Repo.PreloadRelations(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Failed to preload relations"})
}
// Generate SKU
sku, err := service.GenerateSKU(&input)
if err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Failed to generate SKU: " + err.Error()})
}
input.SKU = sku
if err := h.Repo.Create(&input); err != nil { if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create product"}) return c.Status(500).JSON(fiber.Map{"error": "Failed to create product"})
} }
return c.JSON(input) return c.JSON(input)
} }

View File

@ -4,7 +4,7 @@ import "time"
type Colour struct { type Colour struct {
ID uint `gorm:"primaryKey" json:"id"` ID uint `gorm:"primaryKey" json:"id"`
Code string `json:"code"` Code string `gorm:"not null;unique" json:"code"`
Name string `gorm:"not null" json:"name"` Name string `gorm:"not null" json:"name"`
Hex string `json:"hex"` // Hexadecimal color code Hex string `json:"hex"` // Hexadecimal color code
CreatedAt time.Time CreatedAt time.Time

View File

@ -3,6 +3,8 @@ package repository
import ( import (
"BE-MiniERP/modules/inventory/models" "BE-MiniERP/modules/inventory/models"
"fmt"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -36,3 +38,24 @@ func (r *ProductRepository) Update(id uint, product *models.Product) error {
func (r *ProductRepository) Delete(id uint) error { func (r *ProductRepository) Delete(id uint) error {
return r.DB.Delete(&models.Product{}, id).Error return r.DB.Delete(&models.Product{}, id).Error
} }
func (r *ProductRepository) PreloadRelations(product *models.Product) error {
// Collection
if err := r.DB.First(&product.Collection, product.CollectionID).Error; err != nil {
return fmt.Errorf("collection not found: %w", err)
}
// Category
if err := r.DB.First(&product.Category, product.CategoryID).Error; err != nil {
return fmt.Errorf("category not found: %w", err)
}
// Colour
if err := r.DB.First(&product.Colour, product.ColourID).Error; err != nil {
return fmt.Errorf("colour not found: %w", err)
}
// Size
if err := r.DB.First(&product.Size, product.SizeID).Error; err != nil {
return fmt.Errorf("size not found: %w", err)
}
return nil
}

View File

@ -0,0 +1,22 @@
package service
import (
"BE-MiniERP/modules/inventory/models"
"fmt"
)
func GenerateSKU(product *models.Product) (string, error) {
if product.Collection.Code == "" ||
product.Category.Code == "" ||
product.Colour.Code == "" ||
product.Size.Code == "" {
return "", fmt.Errorf("missing code from one or more components")
}
return fmt.Sprintf("%s-%s-%s-%s",
product.Collection.Code,
product.Category.Code,
product.Colour.Code,
product.Size.Code,
), nil
}