2
0

product.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. 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. "any(created_at) as created_at", "any(updated_at) as updated_at", "any(published_at) as published_at", "any(deleted_at) as deleted_at",
  34. },
  35. ln.SqlFieldSelection("title", "", "any"),
  36. ln.SqlFieldSelection("description", "", "any"),
  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. productOptionKey = func(clause string, args ...any) *cache.SqlKey {
  63. return cache.NewSQLKey(
  64. "product_option",
  65. time.Minute,
  66. clause, args...)
  67. }
  68. productVariantKey = func(clause string, args ...any) *cache.SqlKey {
  69. return cache.NewSQLKey(
  70. "product_variant",
  71. time.Minute,
  72. clause, args...)
  73. }
  74. )