Files
discourse-subscriptions/app/controllers/products_controller.rb
T
Justin DiRose fb4fac197b REFACTOR: Use models to store data (#11)
* REFACTOR: Use api to add subscribe link

* FIX: I18n subscribe link

* REFACTOR: Use models to store some data

This enables the plugin to show only subscription information which was
generated on Discourse. Subscription data storage is limited to the
external identifiers Stripe generates so we can interact with the API.

* DEV: Test/linting fixes/rake task
2020-05-22 11:20:05 -05:00

64 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module DiscourseSubscriptions
class ProductsController < ::ApplicationController
include DiscourseSubscriptions::Stripe
before_action :set_api_key
def index
begin
product_ids = Product.all.pluck(:external_id)
products = []
if product_ids.present?
response = ::Stripe::Product.list({
ids: product_ids,
active: true
})
products = response[:data].map do |p|
serialize(p)
end
end
render_json_dump products
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
def show
begin
product = ::Stripe::Product.retrieve(params[:id])
render_json_dump serialize(product)
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
private
def serialize(product)
{
id: product[:id],
name: product[:name],
description: product[:metadata][:description],
subscribed: current_user_products.include?(product[:id])
}
end
def current_user_products
return [] if current_user.nil?
Customer
.select(:product_id)
.where(user_id: current_user.id)
.map { |c| c.product_id }.compact
end
end
end