Sfoglia il codice sorgente

ProductVariant

- impl RequiresShipping
Alexey Kim 2 anni fa
parent
commit
a80c4c404d
3 ha cambiato i file con 34 aggiunte e 23 eliminazioni
  1. 17 11
      db/clickhouse.go
  2. 4 4
      db/product.go
  3. 13 8
      relation/product_variant.go

+ 17 - 11
db/clickhouse.go

@@ -84,7 +84,9 @@ func (db *clickhouse) ProductCollections(ln model.LanguageCode, id string) ([]*g
 				var o []relation.Collection
 
 				rows, err := db.session.SelectBySql("SELECT "+
-					ln.SqlFieldSelection("title")+", "+ln.SqlFieldSelection("description")+
+					ln.SqlFieldSelection("title", "")+
+					", "+
+					ln.SqlFieldSelection("description", "")+
 					", `id`, `handle`, `thumbnail`, "+
 					"`created_at`, `updated_at`, `deleted_at` "+
 					"FROM `product_collection` "+
@@ -202,18 +204,22 @@ func (db *clickhouse) ProductVariants(ctx *middleware.GShopifyContext, id string
 				var o []relation.ProductVariant
 				rows, err := db.session.
 					Select(
-						"id", "product_id", "options",
-						fmt.Sprintf("price['%s'] as price", defaultCurrency),
-						fmt.Sprintf("unit_price['%s'] as unit_price", defaultCurrency),
-						fmt.Sprintf("compare_at_price['%s'] as compare_at_price", defaultCurrency),
-						"barcode", "sku", "hs_code", "origin_country",
-						"allow_backorder", "manage_inventory", "unit_price_measurement",
-						"weight", "wight_unit", "created_at", "updated_at", "deleted_at",
-						ctx.Language.SqlFieldSelection("title"),
+						"t.id as id", "t.product_id as product_id", "t.options as options",
+						fmt.Sprintf("t.price['%s'] as price", defaultCurrency),
+						fmt.Sprintf("t.unit_price['%s'] as unit_price", defaultCurrency),
+						fmt.Sprintf("t.compare_at_price['%s'] as compare_at_price", defaultCurrency),
+						"t.barcode as barcode", "t.sku as sku", "t.hs_code as hs_code", "t.origin_country as origin_country",
+						"t.allow_backorder as allow_backorder", "t.manage_inventory as manage_inventory", "t.unit_price_measurement as unit_price_measurement",
+						"t.weight as weight", "t.wight_unit as weight_unit",
+						"t.created_at as created_at", "t.updated_at as updated_at", "t.deleted_at as deleted_at",
+						"shopping_profile.type as type",
+						ctx.Language.SqlFieldSelection("title", "t"),
 					).
-					From(key.Table()).
+					From(fmt.Sprintf("%s as t", key.Table())).
 					Where(key.Clause(), key.Args()...).
-					OrderBy("created_at").
+					LeftJoin("product", "product.id = t.product_id").
+					LeftJoin("shopping_profile", "product.profile_id = shopping_profile.id").
+					OrderBy("t.created_at").
 					Load(&o)
 				if rows < 1 || err != nil {
 					return nil

+ 4 - 4
db/product.go

@@ -21,9 +21,9 @@ var (
 			"hs_code", "mid_code", "weight", "length", "height", "width", "origin_country", "vendor", "material",
 			"created_at", "updated_at", "deleted_at", "tags", "status",
 		},
-			ln.SqlFieldSelection("title"),
-			ln.SqlFieldSelection("subtitle"),
-			ln.SqlFieldSelection("description"),
+			ln.SqlFieldSelection("title", ""),
+			ln.SqlFieldSelection("subtitle", ""),
+			ln.SqlFieldSelection("description", ""),
 		)
 	}
 	productCollectionKey = func(clause string, args ...any) *cache.SqlKey {
@@ -39,7 +39,7 @@ var (
 			"id", "product_id",
 			"created_at", "updated_at", "deleted_at",
 		},
-			ln.SqlFieldSelection("name"),
+			ln.SqlFieldSelection("name", ""),
 			ln.SqlArraySelection("values"),
 		)
 	}

+ 13 - 8
relation/product_variant.go

@@ -33,21 +33,26 @@ type ProductVariant struct {
 		Field4 int32
 	} `db:"unit_price_measurement"`
 	Weight     dbr.NullFloat64 `db:"weight"`
-	WeightUnit string          `db:"wight_unit"`
+	WeightUnit string          `db:"weight_unit"`
 	CreatedAt  time.Time       `db:"created_at"`
 	UpdatedAt  time.Time       `db:"updated_at"`
 	DeletedAt  *time.Time      `db:"deleted_at"`
+
+	ShoppingProfileType dbr.NullString `db:"type"`
 }
 
 func (v *ProductVariant) As() *generated.ProductVariant {
 	variant := generated.ProductVariant{
 		AvailableForSale:    false,
 		CurrentlyNotInStock: false,
-		RequiresShipping:    false,
 		QuantityAvailable:   nil,
+		Image:               nil,
+		SelectedOptions:     nil,
 
-		ID:    model.NewId(model.GidVariant, v.Id),
-		Title: v.Title,
+		ID:               model.NewId(model.GidVariant, v.Id),
+		Title:            v.Title,
+		Product:          &generated.Product{ID: model.NewId(model.GidProduct, v.ProductId)},
+		RequiresShipping: true,
 
 		Price: &generated.MoneyV2{
 			Amount:       scalar.NewDecimal(v.Price),
@@ -62,10 +67,6 @@ func (v *ProductVariant) As() *generated.ProductVariant {
 			CurrencyCode: generated.CurrencyCodeUsd,
 		},
 
-		Product:         &generated.Product{ID: model.NewId(model.GidProduct, v.ProductId)},
-		Image:           nil,
-		SelectedOptions: nil,
-
 		UnitPriceMeasurement: &generated.UnitPriceMeasurement{
 			QuantityValue:  v.UnitPriceMeasurement.Field2,
 			ReferenceValue: int(v.UnitPriceMeasurement.Field4),
@@ -73,6 +74,10 @@ func (v *ProductVariant) As() *generated.ProductVariant {
 		WeightUnit: generated.WeightUnit(v.WeightUnit),
 	}
 
+	if enum := model.ShoppingProfileType(v.ShoppingProfileType.String); enum.IsValid() {
+		variant.RequiresShipping = model.ShoppingProfileTypeDefault == enum
+	}
+
 	if enum := generated.UnitPriceMeasurementMeasuredType(v.UnitPriceMeasurement.Field0); enum.IsValid() {
 		variant.UnitPriceMeasurement.MeasuredType = &enum
 	}