/app/services/marketplace_service/listing.rb

https://gitlab.com/atonb117/sharetribe · Ruby · 145 lines · 119 code · 23 blank · 3 comment · 2 complexity · a1d5e2adaeb7fbd6067ba633e82bd4a4 MD5 · raw file

  1. module MarketplaceService
  2. module Listing
  3. ListingModel = ::Listing
  4. module Entity
  5. Listing = EntityUtils.define_builder(
  6. [:id, :mandatory, :fixnum],
  7. [:title, :mandatory, :string],
  8. [:author_id, :mandatory, :string],
  9. [:price, :optional, :money],
  10. [:quantity, :optional, :string],
  11. [:transaction_type_id, :mandatory, :fixnum],
  12. [:deleted, :to_bool]
  13. )
  14. TransactionType = EntityUtils.define_builder(
  15. [:id, :mandatory, :fixnum],
  16. [:type, :mandatory, :string],
  17. [:price_per, :optional, :string],
  18. [:price_field, :optional, :to_bool],
  19. [:preauthorize_payment, :optional, :to_bool],
  20. [:url, :optional, :to_bool],
  21. [:action_button_label_translations, :optional])
  22. module_function
  23. def transaction_direction(transaction_type)
  24. direction_map = {
  25. ["Give", "Lend", "Rent", "Sell", "Service", "ShareForFree", "Swap", "Offer"] => "offer",
  26. ["Request"] => "request",
  27. ["Inquiry"] => "inquiry"
  28. }
  29. _, direction = direction_map.find { |(transaction_types, direction)| transaction_types.include? transaction_type }
  30. if direction.nil?
  31. raise("Unknown listing type: #{transaction_type}")
  32. else
  33. direction
  34. end
  35. end
  36. def discussion_type(transaction_type)
  37. case transaction_direction(transaction_type)
  38. when "request"
  39. "offer"
  40. when "offer"
  41. "request"
  42. else
  43. raise("No discussion type for transaction type: #{transaction_type}")
  44. end
  45. end
  46. def listing(listing_model)
  47. Listing.call(EntityUtils.model_to_hash(listing_model).merge(price: listing_model.price))
  48. end
  49. def transaction_type(transaction_type_model)
  50. translations = transaction_type_model.translations
  51. .map { |translation|
  52. {
  53. locale: translation.locale,
  54. action_button_label: translation.action_button_label
  55. }
  56. }
  57. TransactionType.call(EntityUtils
  58. .model_to_hash(transaction_type_model)
  59. .merge(action_button_label_translations: translations)
  60. )
  61. end
  62. def send_payment_settings_reminder?(listing_id, community_id)
  63. listing = ListingModel.find(listing_id)
  64. payment_type = MarketplaceService::Community::Query.payment_type(community_id)
  65. query_info = {
  66. transaction: {
  67. payment_gateway: payment_type,
  68. listing_author_id: listing.author.id,
  69. community_id: community_id
  70. }
  71. }
  72. payment_type &&
  73. listing.transaction_type.is_offer? &&
  74. !TransactionService::Transaction.can_start_transaction(query_info).data[:result]
  75. end
  76. end
  77. module Command
  78. module_function
  79. #
  80. # DELETE /listings/:author_id
  81. def delete_listings(author_id)
  82. listings = ListingModel.where(author_id: author_id)
  83. listings.update_all(
  84. # Delete listing info
  85. description: nil,
  86. origin: nil,
  87. open: false,
  88. deleted: true
  89. )
  90. ids = listings.pluck(:id)
  91. ListingImage.where(listing_id: ids).destroy_all
  92. Result::Success.new
  93. end
  94. end
  95. module Query
  96. module_function
  97. def listing(listing_id)
  98. listing_model = ListingModel.find(listing_id)
  99. MarketplaceService::Listing::Entity.listing(listing_model)
  100. end
  101. def listing_with_transaction_type(listing_id)
  102. listing_model = ListingModel.find(listing_id)
  103. listing = MarketplaceService::Listing::Entity.listing(listing_model)
  104. listing.delete(:transaction_type_id)
  105. listing.merge(transaction_type: MarketplaceService::Listing::Entity.transaction_type(listing_model.transaction_type))
  106. end
  107. def open_listings_with_price_for(community_id, person_id)
  108. ListingModel
  109. .includes(:communities)
  110. .includes(:transaction_type)
  111. .where(
  112. {
  113. communities: { id: community_id },
  114. transaction_types: { price_field: true },
  115. author_id: person_id,
  116. open: true
  117. })
  118. .map { |l| MarketplaceService::Listing::Entity.listing(l) }
  119. end
  120. end
  121. end
  122. end