Update Auto Generate SKU
This commit is contained in:
parent
f8798a53a4
commit
9b1f58d783
@ -11,7 +11,7 @@ func GenerateJWT(userID uint, role string) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"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)
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"BE-MiniERP/database"
|
||||
"BE-MiniERP/modules/inventory/models"
|
||||
"BE-MiniERP/modules/inventory/repository"
|
||||
"BE-MiniERP/modules/inventory/service"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@ -32,9 +33,23 @@ func (h *ProductHandler) Create(c *fiber.Ctx) error {
|
||||
if err := c.BodyParser(&input); err != nil {
|
||||
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 {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to create product"})
|
||||
}
|
||||
|
||||
return c.JSON(input)
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import "time"
|
||||
|
||||
type Colour struct {
|
||||
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"`
|
||||
Hex string `json:"hex"` // Hexadecimal color code
|
||||
CreatedAt time.Time
|
||||
|
||||
@ -3,6 +3,8 @@ package repository
|
||||
import (
|
||||
"BE-MiniERP/modules/inventory/models"
|
||||
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -36,3 +38,24 @@ func (r *ProductRepository) Update(id uint, product *models.Product) error {
|
||||
func (r *ProductRepository) Delete(id uint) 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
|
||||
}
|
||||
|
||||
22
modules/inventory/service/inventory_logic.go
Normal file
22
modules/inventory/service/inventory_logic.go
Normal 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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user