Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

3.0 KB
Raw Blame
package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"github.com/google/uuid"
	"github.com/sysadminsmedia/homebox/backend/internal/data/ent/schema/mixins"
)

// EntityTemplate holds the schema definition for the EntityTemplate entity.
type EntityTemplate struct {
	ent.Schema
}

func (EntityTemplate) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.BaseMixin{},
		mixins.DetailsMixin{},
		GroupMixin{ref: "entity_templates"},
	}
}

func (EntityTemplate) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("name"),
	}
}

// Fields of the EntityTemplate.
func (EntityTemplate) Fields() []ent.Field {
	return []ent.Field{
		// Notes for the template (instructions, hints, etc.)
		field.String("notes").
			MaxLen(1000).
			Optional(),

		// ------------------------------------
		// Default values for item fields
		field.Float("default_quantity").
			Default(1),
		field.Bool("default_insured").
			Default(false),

		// ------------------------------------
		// Default item name/description (templates for item identity)
		field.String("default_name").
			MaxLen(255).
			Optional().
			Comment("Default name template for items (can use placeholders)"),
		field.Text("default_description").
			MaxLen(1000).
			Optional().
			Comment("Default description for items created from this template"),

		// ------------------------------------
		// Default item identification
		field.String("default_manufacturer").
			MaxLen(255).
			Optional(),
		field.String("default_model_number").
			MaxLen(255).
			Optional().
			Comment("Default model number for items created from this template"),

		// ------------------------------------
		// Default warranty settings
		field.Bool("default_lifetime_warranty").
			Default(false),
		field.Text("default_warranty_details").
			MaxLen(1000).
			Optional(),

		// ------------------------------------
		// Template metadata
		field.Bool("include_warranty_fields").
			Default(false).
			Comment("Whether to include warranty fields in items created from this template"),
		field.Bool("include_purchase_fields").
			Default(false).
			Comment("Whether to include purchase fields in items created from this template"),
		field.Bool("include_sold_fields").
			Default(false).
			Comment("Whether to include sold fields in items created from this template"),

		// ------------------------------------
		// Default tags (stored as JSON array of UUIDs to allow reuse across templates)
		field.JSON("default_tag_ids", []uuid.UUID{}).
			Optional().
			Comment("Default tag IDs for items created from this template"),
	}
}

// Edges of the EntityTemplate.
func (EntityTemplate) Edges() []ent.Edge {
	owned := func(s string, t any) ent.Edge {
		return edge.To(s, t).
			Annotations(entsql.Annotation{
				OnDelete: entsql.Cascade,
			})
	}

	return []ent.Edge{
		owned("fields", TemplateField.Type),
		// Default location for items created from this template
		edge.To("location", Entity.Type).
			Unique(),
	}
}