feat: implement GenerateCodeUom function for automatic UOM code generation and update UOM creation logic
Deploy Application / deploy (push) Successful in 21s Details

This commit is contained in:
Habib Fatkhul Rohman 2025-11-26 12:09:44 +07:00
parent 1d3d69a0fb
commit b2de10c414
3 changed files with 40 additions and 5 deletions

View File

@ -1,7 +1,11 @@
package entities
import (
"fmt"
"strings"
"github.com/google/uuid"
"gorm.io/gorm"
)
type MUomEntity struct {
@ -31,3 +35,27 @@ type MUomEntity struct {
func (MUomEntity) TableName() string {
return "m_uoms"
}
// GenerateCodeUom generates a new UOM code with sequence per client
func GenerateCodeUom(db *gorm.DB, clientId string) (string, error) {
prefix := "UOM"
var lastUom MUomEntity
err := db.
Where("client_id = ?", clientId).
Where("code LIKE ?", prefix+"-%").
Order("code DESC").
First(&lastUom).Error
seq := 1
if err == nil && lastUom.Code != "" {
parts := strings.Split(lastUom.Code, "-")
if len(parts) == 2 {
fmt.Sscanf(parts[1], "%d", &seq)
seq++
}
}
code := fmt.Sprintf("%s-%04d", prefix, seq)
return code, nil
}

View File

@ -29,7 +29,7 @@ type UomCreateRequest struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
Symbol string `json:"symbol"`
Code string `json:"code"`
// Code string `json:"code"`
StdPrecision int `json:"std_precision"`
IsActive bool `json:"is_active"`
ClientID string `json:"client_id" binding:"required"`

View File

@ -39,14 +39,21 @@ func (s *uomService) Create(ctx context.Context, req dtodomain.UomCreateRequest)
tx.Rollback()
}
}()
code, err := entities.GenerateCodeUom(s.db, req.ClientID)
if err != nil {
tx.Rollback()
return dtodomain.UomResponse{}, err
}
uom := entities.MUomEntity{
Name: req.Name,
Description: req.Description,
Symbol: req.Symbol,
Code: req.Code,
Code: code,
StdPrecision: req.StdPrecision,
IsActive: req.IsActive,
}
clientUUID, err := uuid.Parse(req.ClientID)
if err != nil {
tx.Rollback()