119 lines
5.3 KiB
Go
119 lines
5.3 KiB
Go
package platform
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ResourceMode describes the operations a platform-admin resource supports.
|
|
type ResourceMode string
|
|
|
|
const (
|
|
Writable ResourceMode = "writable"
|
|
ReadOnly ResourceMode = "readonly"
|
|
AppendOnly ResourceMode = "append_only"
|
|
)
|
|
|
|
// ResourceContract is the expected cross-layer representation of one resource.
|
|
type ResourceContract struct {
|
|
Domain string
|
|
Name string
|
|
PageKind string
|
|
Mode ResourceMode
|
|
}
|
|
|
|
// ResourceDefinition describes a route resource and the fields it may change.
|
|
type ResourceDefinition struct {
|
|
Domain string
|
|
Name string
|
|
Model func() any
|
|
Mode ResourceMode
|
|
CreateFields []string
|
|
UpdateFields []string
|
|
}
|
|
|
|
// Allows reports whether a resource mode supports an HTTP method.
|
|
func (d ResourceDefinition) Allows(method string) bool {
|
|
switch d.Mode {
|
|
case ReadOnly:
|
|
return method == http.MethodGet
|
|
case AppendOnly:
|
|
return method == http.MethodGet || method == http.MethodPost
|
|
case Writable:
|
|
return method == http.MethodGet || method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch || method == http.MethodDelete
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func archiveValues() gin.H {
|
|
return gin.H{"status": "archived"}
|
|
}
|
|
|
|
func filterFields(values map[string]any, allowedFields []string) gin.H {
|
|
allowed := make(map[string]struct{}, len(allowedFields))
|
|
for _, field := range allowedFields {
|
|
allowed[field] = struct{}{}
|
|
}
|
|
|
|
filtered := make(gin.H, len(allowed))
|
|
for field, value := range values {
|
|
if _, ok := allowed[field]; ok {
|
|
filtered[field] = value
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// ExpectedResources returns the complete platform-admin resource catalogue.
|
|
func ExpectedResources() []ResourceContract {
|
|
return []ResourceContract{
|
|
{Domain: "gas", Name: "gas_basic", Mode: Writable, PageKind: "list"},
|
|
{Domain: "gas", Name: "gas_account", Mode: Writable, PageKind: "list"},
|
|
{Domain: "delivery", Name: "delivery_basic", Mode: Writable, PageKind: "list"},
|
|
{Domain: "delivery", Name: "delivery_account", Mode: Writable, PageKind: "list"},
|
|
{Domain: "staff", Name: "staff_account", Mode: Writable, PageKind: "list"},
|
|
{Domain: "staff", Name: "staff_credential", Mode: Writable, PageKind: "list"},
|
|
{Domain: "user", Name: "user_account", Mode: Writable, PageKind: "list"},
|
|
{Domain: "user", Name: "user_address", Mode: Writable, PageKind: "list"},
|
|
{Domain: "user", Name: "user_service_relation", Mode: Writable, PageKind: "list"},
|
|
{Domain: "device", Name: "dev_smart_cylinder_valve", Mode: Writable, PageKind: "list"},
|
|
{Domain: "device", Name: "dev_device_binding", Mode: Writable, PageKind: "list"},
|
|
{Domain: "safety", Name: "saf_rule", Mode: Writable, PageKind: "list"},
|
|
{Domain: "safety", Name: "saf_event", Mode: Writable, PageKind: "list"},
|
|
{Domain: "safety", Name: "saf_inspection", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_category", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_product", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_product_attribute", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_product_image", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_cart", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_order", Mode: Writable, PageKind: "list"},
|
|
{Domain: "ec", Name: "ec_review", Mode: Writable, PageKind: "list"},
|
|
{Domain: "delivery", Name: "delivery_task", Mode: Writable, PageKind: "list"},
|
|
{Domain: "delivery", Name: "delivery_track", Mode: Writable, PageKind: "list"},
|
|
{Domain: "delivery", Name: "delivery_track_point", Mode: Writable, PageKind: "list"},
|
|
{Domain: "finance", Name: "fin_payment", Mode: Writable, PageKind: "list"},
|
|
{Domain: "finance", Name: "fin_settlement", Mode: Writable, PageKind: "list"},
|
|
{Domain: "finance", Name: "fin_reconciliation", Mode: Writable, PageKind: "list"},
|
|
{Domain: "content", Name: "cnt_content", Mode: Writable, PageKind: "list"},
|
|
{Domain: "notification", Name: "ntf_template", Mode: Writable, PageKind: "list"},
|
|
{Domain: "customer_service", Name: "cs_ticket", Mode: Writable, PageKind: "list"},
|
|
{Domain: "platform", Name: "platfrom_account", Mode: Writable, PageKind: "list"},
|
|
{Domain: "platform", Name: "platform_role", Mode: Writable, PageKind: "list"},
|
|
{Domain: "platform", Name: "platform_menu", Mode: Writable, PageKind: "tree"},
|
|
{Domain: "device", Name: "dev_telemetry", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "wallet", Name: "wallet", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "wallet", Name: "wallet_ledger", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "wallet", Name: "wallet_recharge", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "wallet", Name: "wallet_withdrawal", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "report", Name: "report", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "report", Name: "report_item", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "report", Name: "report_metric_snapshot", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "audit", Name: "aud_operation_log", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "audit", Name: "aud_export_log", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "audit", Name: "aud_approval", Mode: ReadOnly, PageKind: "list"},
|
|
{Domain: "safety", Name: "saf_event_disposal", Mode: AppendOnly, PageKind: "list"},
|
|
}
|
|
}
|