2
0

product.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package db
  2. import (
  3. "fmt"
  4. "github.com/gshopify/service-wrapper/model"
  5. "gshopper.com/gshopify/products/cache"
  6. "gshopper.com/gshopify/products/graphql/generated"
  7. "time"
  8. )
  9. const defaultCurrency = "USD"
  10. var (
  11. productKey = func(ln model.LanguageCode, currency generated.CurrencyCode, clause string, args ...any) *cache.SqlKey {
  12. return cache.NewSQLKey(
  13. "product",
  14. time.Minute,
  15. []string{
  16. "t.id as id",
  17. "anyLast(handle) as handle", "anyLast(type) as type", "anyLast(scope) as scope", "anyLast(tags) as tags", "anyLast(vendor) as vendor",
  18. "anyLast(gift) as gift", "anyLast(collections) as collections", "anyLast(images) as images",
  19. "anyLast(product_variant.inventory_management) as inventory_management",
  20. "anyLast(product_variant.inventory_policy) as inventory_policy",
  21. "anyLast(product_variant.options) as options",
  22. "anyLast(inventory_item.requires_shipping) as requires_shipping",
  23. "anyLast(inventory_item.tracked) as tracked",
  24. "anyLast(inventory_level.available) as available",
  25. "if(tracked, if(available > 0, true, if(inventory_policy == 'continue', true, false)), true) as for_sale",
  26. "anyLast(created_at) as created_at", "anyLast(updated_at) as updated_at", "anyLast(published_at) as published_at", "anyLast(deleted_at) as deleted_at",
  27. ln.SqlFieldSelection("title", "", "anyLast"),
  28. ln.SqlFieldSelection("description", "", "anyLast"),
  29. fmt.Sprintf("min(product_variant.price['%s']) as price_min", currency),
  30. fmt.Sprintf("max(product_variant.price['%s']) as price_max", currency),
  31. fmt.Sprintf("min(product_variant.compare_at_price['%s']) as compare_at_price_min", currency),
  32. fmt.Sprintf("max(product_variant.compare_at_price['%s']) as compare_at_price_max", currency),
  33. },
  34. clause, args...)
  35. }
  36. productCollectionKey = func(ln model.LanguageCode, clause string, args ...any) *cache.SqlKey {
  37. return cache.NewSQLKey(
  38. "product_collection",
  39. 3*time.Minute,
  40. []string{
  41. "id", "handle", "scope", "sort_order", "image", "template_suffix",
  42. "created_at", "updated_at", "published_at", "deleted_at",
  43. ln.SqlFieldSelection("title", "", ""),
  44. ln.SqlFieldSelection("description", "", ""),
  45. },
  46. clause,
  47. args...,
  48. )
  49. }
  50. productOptionKey = func(ln model.LanguageCode, clause string, args ...any) *cache.SqlKey {
  51. return cache.NewSQLKey(
  52. "product_option",
  53. time.Minute,
  54. []string{
  55. "id", "position", "created_at", "updated_at", "published_at", "deleted_at",
  56. ln.SqlFieldSelection("name", "", ""),
  57. ln.SqlArraySelection("values"),
  58. "values[tupleElement(opt, 2)] as value",
  59. },
  60. clause, args...)
  61. }
  62. productVariantKey = func(ln model.LanguageCode, currency generated.CurrencyCode, clause string, args ...any) *cache.SqlKey {
  63. return cache.NewSQLKey(
  64. "product_variant",
  65. time.Minute,
  66. []string{
  67. "anyLast(t.id) as id",
  68. "anyLast(t.product_id) as product_id",
  69. "anyLast(t.inventory_item_id) as inventory_item_id",
  70. "anyLast(t.position) as position",
  71. "anyLast(t.image) as image",
  72. "anyLast(t.inventory_management) as inventory_management",
  73. "anyLast(t.inventory_policy) as inventory_policy",
  74. "anyLast(t.options) as options",
  75. "anyLast(t.grams) as grams",
  76. "anyLast(t.weight) as weight",
  77. "anyLast(t.weight_unit) as weight_unit",
  78. "anyLast(t.created_at) as created_at",
  79. "anyLast(t.updated_at) as updated_at",
  80. "anyLast(t.published_at) as published_at",
  81. "anyLast(t.deleted_at) as deleted_at",
  82. "sum(inventory_level.available) as available",
  83. "anyLast(inventory_item.sku) as sku",
  84. "anyLast(inventory_item.barcode) as barcode",
  85. "anyLast(inventory_item.requires_shipping) as requires_shipping",
  86. "anyLast(inventory_item.tracked) as tracked",
  87. "if(tracked, if(available > 0, true, if(inventory_policy == 'continue', true, false)), true) as for_sale",
  88. ln.SqlFieldSelection("title", "product", "anyLast"),
  89. fmt.Sprintf("any(t.price['%s']) as price", currency),
  90. fmt.Sprintf("any(t.compare_at_price['%s']) as compare_at_price", currency),
  91. },
  92. clause, args...)
  93. }
  94. productTagKey = func(clause string) *cache.SqlKey {
  95. return cache.NewSQLKey("product_tags", time.Minute, []string{"id, tag, products, count"}, clause)
  96. }
  97. productTypeKey = func(clause string) *cache.SqlKey {
  98. return cache.NewSQLKey("product_types", time.Minute, []string{"id, type, products, count"}, clause)
  99. }
  100. )