product.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(clause string, args ...any) *cache.SqlKey {
  12. return cache.NewSQLKey(
  13. "product",
  14. time.Minute,
  15. clause, args...)
  16. }
  17. productSelection = func(ln model.LanguageCode, currency generated.CurrencyCode) []string {
  18. return append([]string{
  19. "t.id as id",
  20. "any(handle) as handle", "any(type) as type", "any(scope) as scope", "any(tags) as tags", "any(vendor) as vendor",
  21. "any(gift) as gift", "any(collections) as collections", "any(images) as images",
  22. "any(product_variant.inventory_management) as inventory_management",
  23. "any(product_variant.inventory_policy) as inventory_policy",
  24. "any(product_variant.options) as options",
  25. "any(inventory_item.requires_shipping) as requires_shipping",
  26. "any(inventory_item.tracked) as tracked",
  27. "any(inventory_level.available) as available",
  28. "if(tracked, if(available > 0, true, if(inventory_policy == 'continue', true, false)), true) as for_sale",
  29. "any(created_at) as created_at", "any(updated_at) as updated_at", "any(published_at) as published_at", "any(deleted_at) as deleted_at",
  30. },
  31. ln.SqlFieldSelection("title", "", "any"),
  32. ln.SqlFieldSelection("description", "", "any"),
  33. fmt.Sprintf("min(product_variant.price['%s']) as price_min", currency),
  34. fmt.Sprintf("max(product_variant.price['%s']) as price_max", currency),
  35. fmt.Sprintf("min(product_variant.compare_at_price['%s']) as compare_at_price_min", currency),
  36. fmt.Sprintf("max(product_variant.compare_at_price['%s']) as compare_at_price_max", currency),
  37. )
  38. }
  39. productCollectionSelection = func(ln model.LanguageCode) []string {
  40. return append([]string{
  41. "id", "handle", "scope", "sort_order", "image", "template_suffix",
  42. "created_at", "updated_at", "published_at", "deleted_at",
  43. },
  44. ln.SqlFieldSelection("title", "", ""),
  45. ln.SqlFieldSelection("description", "", ""),
  46. )
  47. }
  48. productCollectionKey = func(clause string, args ...any) *cache.SqlKey {
  49. return cache.NewSQLKey(
  50. "product_collection",
  51. 3*time.Minute,
  52. clause,
  53. args...,
  54. )
  55. }
  56. productOptionSelection = func(ln model.LanguageCode) []string {
  57. return append([]string{"id", "position", "created_at", "updated_at", "published_at", "deleted_at"},
  58. ln.SqlFieldSelection("name", "", ""),
  59. ln.SqlArraySelection("values"),
  60. )
  61. }
  62. productOptionSelectedSelection = func(ln model.LanguageCode) []string {
  63. return append([]string{
  64. "id",
  65. "if(notEmpty(value_ln), value_ln, value_en) as value",
  66. "position", "created_at", "updated_at", "published_at", "deleted_at",
  67. },
  68. ln.SqlFieldSelection("name", "", ""),
  69. fmt.Sprintf("arrayElement(values, tupleElement(opt, 2))['%s'] as value_en", model.LanguageCodeEn),
  70. fmt.Sprintf("arrayElement(values, tupleElement(opt, 2))['%s'] as value_ln", ln),
  71. )
  72. }
  73. productOptionKey = func(clause string, args ...any) *cache.SqlKey {
  74. return cache.NewSQLKey(
  75. "product_option",
  76. time.Minute,
  77. clause, args...)
  78. }
  79. productVariantSelection = func(ln model.LanguageCode, currency generated.CurrencyCode) []string {
  80. return append([]string{
  81. "any(t.id) as id",
  82. "any(t.product_id) as product_id",
  83. "any(t.inventory_item_id) as inventory_item_id",
  84. "any(t.position) as position",
  85. "any(t.image) as image",
  86. "any(t.inventory_management) as inventory_management",
  87. "any(t.inventory_policy) as inventory_policy",
  88. "any(t.options) as options",
  89. "any(t.grams) as grams",
  90. "any(t.weight) as weight",
  91. "any(t.weight_unit) as weight_unit",
  92. "any(t.created_at) as created_at",
  93. "any(t.updated_at) as updated_at",
  94. "any(t.published_at) as published_at",
  95. "any(t.deleted_at) as deleted_at",
  96. "sum(inventory_level.available) as available",
  97. "any(inventory_item.sku) as sku",
  98. "any(inventory_item.barcode) as barcode",
  99. "any(inventory_item.requires_shipping) as requires_shipping",
  100. "any(inventory_item.tracked) as tracked",
  101. "if(tracked, if(available > 0, true, if(inventory_policy == 'continue', true, false)), true) as for_sale",
  102. },
  103. ln.SqlFieldSelection("title", "product", "any"),
  104. fmt.Sprintf("any(t.price['%s']) as price", currency),
  105. fmt.Sprintf("any(t.compare_at_price['%s']) as compare_at_price", currency),
  106. )
  107. }
  108. productVariantKey = func(clause string, args ...any) *cache.SqlKey {
  109. return cache.NewSQLKey(
  110. "product_variant",
  111. time.Minute,
  112. clause, args...)
  113. }
  114. )