Files
apskel-pos-backend/internal/contract/category_contract.go
T

117 lines
4.5 KiB
Go
Raw Normal View History

2025-07-18 20:10:29 +07:00
package contract
import (
2026-10-02 23:30:03 +07:00
"bytes"
"encoding/json"
2025-07-18 20:10:29 +07:00
"time"
"github.com/google/uuid"
)
type CreateCategoryRequest struct {
2026-10-02 23:30:03 +07:00
Name string `json:"name" validate:"required,min=1,max=255"`
Description *string `json:"description,omitempty"`
BusinessType *string `json:"business_type,omitempty"`
OutletID *uuid.UUID `json:"outlet_id,omitempty"`
ParentID *uuid.UUID `json:"parent_id,omitempty"`
Order *int `json:"order,omitempty"`
OwnerFeePercent *float64 `json:"owner_fee_percent,omitempty"`
2026-10-04 21:54:13 +07:00
IsTeam *bool `json:"is_team,omitempty"`
2026-10-02 23:30:03 +07:00
Metadata map[string]interface{} `json:"metadata,omitempty"`
2025-07-18 20:10:29 +07:00
}
type UpdateCategoryRequest struct {
2026-10-02 23:30:03 +07:00
Name *string `json:"name,omitempty" validate:"omitempty,min=1,max=255"`
Description *string `json:"description,omitempty"`
BusinessType *string `json:"business_type,omitempty"`
OutletID *uuid.UUID `json:"outlet_id,omitempty"`
ParentID *uuid.UUID `json:"parent_id,omitempty"`
Order *int `json:"order,omitempty"`
OwnerFeePercent *float64 `json:"owner_fee_percent,omitempty"`
2026-10-04 21:54:13 +07:00
IsTeam *bool `json:"is_team,omitempty"`
2026-10-02 23:30:03 +07:00
Metadata map[string]interface{} `json:"metadata,omitempty"`
// Set when the field is sent as null (or "" for parent_id), which asks for the
// value to be removed. A field that is left out stays unchanged.
ClearParentID bool `json:"-"`
ClearOwnerFeePercent bool `json:"-"`
}
// UnmarshalJSON tells an explicit null apart from a field that was left out, so a
// category can be detached from its parent and an owner fee override can be removed.
func (r *UpdateCategoryRequest) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
isEmpty := func(key string, allowEmptyString bool) bool {
value, ok := raw[key]
if !ok {
return false
}
value = bytes.TrimSpace(value)
return bytes.Equal(value, []byte("null")) || (allowEmptyString && bytes.Equal(value, []byte(`""`)))
}
clearParentID := isEmpty("parent_id", true)
clearOwnerFeePercent := isEmpty("owner_fee_percent", false)
if clearParentID {
// An empty string is not a valid UUID, so keep it away from the decoder
delete(raw, "parent_id")
cleaned, err := json.Marshal(raw)
if err != nil {
return err
}
data = cleaned
}
type plain UpdateCategoryRequest
var decoded plain
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}
*r = UpdateCategoryRequest(decoded)
r.ClearParentID = clearParentID
r.ClearOwnerFeePercent = clearOwnerFeePercent
return nil
2025-07-18 20:10:29 +07:00
}
type ListCategoriesRequest struct {
OrganizationID *uuid.UUID `json:"organization_id,omitempty"`
2026-05-21 21:27:57 +07:00
OutletID *uuid.UUID `json:"outlet_id,omitempty"`
2026-08-05 19:28:38 +07:00
ParentID *uuid.UUID `json:"parent_id,omitempty"`
2026-08-06 21:21:41 +07:00
Type string `json:"type,omitempty" validate:"omitempty,oneof=parent child"`
2025-07-18 20:10:29 +07:00
BusinessType string `json:"business_type,omitempty"`
Search string `json:"search,omitempty"`
Page int `json:"page" validate:"required,min=1"`
Limit int `json:"limit" validate:"required,min=1,max=100"`
}
// Category Response DTOs
type CategoryResponse struct {
2026-10-02 23:30:03 +07:00
ID uuid.UUID `json:"id"`
OrganizationID uuid.UUID `json:"organization_id"`
OutletID *uuid.UUID `json:"outlet_id"`
ParentID *uuid.UUID `json:"parent_id,omitempty"`
ParentName *string `json:"parent_name,omitempty"`
Name string `json:"name"`
Description *string `json:"description"`
BusinessType string `json:"business_type"`
Order int `json:"order"`
OwnerFeePercent *float64 `json:"owner_fee_percent"`
2026-10-04 21:54:13 +07:00
IsTeam bool `json:"is_team"`
2026-10-02 23:30:03 +07:00
Metadata map[string]interface{} `json:"metadata"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
2025-07-18 20:10:29 +07:00
}
type ListCategoriesResponse struct {
Categories []CategoryResponse `json:"categories"`
TotalCount int `json:"total_count"`
Page int `json:"page"`
Limit int `json:"limit"`
TotalPages int `json:"total_pages"`
}