Permalink
Browse files

Move from arrays to new rows for data storage

  • Loading branch information...
joebuhlig committed Jun 9, 2017
1 parent f633dd3 commit aa7e18df81310b4a963dcd07782a7450895224c2
@@ -11,15 +11,15 @@ def index
def submit_payment
gateway = DiscourseLeague::Billing::Gateways.new.gateway

products = PluginStore.get("discourse_league", "levels")
products = PluginStore.get("discourse_league", "levels") || []
product = products.select{|level| level[:id] == params[:level_id]}

if params[:update]
subscriptions = PluginStore.get("discourse_league", "subscriptions") || []
user_subscription = subscriptions.select{|subscription| subscription[:product_id].to_i == params[:level_id].to_i && subscription[:user_id] == current_user.id} || []
subscriptions = PluginStore.get("discourse_league", "s:" + current_user.id.to_s) || []
user_subscription = subscriptions.select{|subscription| subscription[:product_id].to_i == params[:level_id].to_i} || []

if user_subscription.empty?
render_json_error("Subscription cannot be found.")
return render_json_error("Subscription cannot be found.")
end

response = gateway.update_payment(current_user.id, product[0], user_subscription[0][:subscription_id], params[:nonce])
@@ -7,8 +7,8 @@ def show
levels = PluginStore.get("discourse_league", "levels")
level = levels.select{|level| level[:id] == params[:id].to_i}

subscriptions = PluginStore.get("discourse_league", "subscriptions") || []
user_subscription = subscriptions.select{|subscription| subscription[:product_id].to_i == level[0][:id].to_i && subscription[:user_id] == current_user.id} || []
subscriptions = PluginStore.get("discourse_league", "s:" + current_user.id.to_s) || []
user_subscription = subscriptions.select{|subscription| subscription[:product_id].to_i == level[0][:id].to_i} || []

if user_subscription.empty?
level[0][:user_subscribed] = false
@@ -5,14 +5,12 @@ class SubscriptionsController < ApplicationController

def all
if current_user.id == params[:user_id].to_i
subscriptions = PluginStore.get("discourse_league", "subscriptions")
subscriptions = PluginStore.get("discourse_league", "s:" + params[:user_id].to_s) || []

if !subscriptions.nil?
subscriptions = subscriptions.select{|subscription| subscription[:user_id] == params[:user_id].to_i}
subscriptions = subscriptions.flatten.map{|subscription| Subscription.new(subscription)} if !subscriptions.empty?
else
subscriptions = []
if !subscriptions.empty?
subscriptions = subscriptions.flatten.map{|subscription| Subscription.new(subscription)}
end

render_json_dump(serialize_data(subscriptions, SubscriptionSerializer))
else
render_json_error(params[:user_id])
@@ -28,36 +26,33 @@ def show
end

def cancel
subscriptions = PluginStore.get("discourse_league", "subscriptions") || []
subscription = subscriptions.select{|subscription| subscription[:id] == params[:id].to_i}
subscriptions = PluginStore.get("discourse_league", "s:" + current_user.id.to_s) || []
subscription = subscriptions.select{|subscription| subscription[:subscription_id] == params[:id].to_s}

if current_user.id == subscription[0][:user_id].to_i
gateway = DiscourseLeague::Billing::Gateways.new.gateway
response = gateway.unsubscribe(subscription[0][:subscription_id])
gateway = DiscourseLeague::Billing::Gateways.new.gateway
response = gateway.unsubscribe(subscription[0][:subscription_id])

if response.success?
products = PluginStore.get("discourse_league", "levels")
product = products.select{|level| level[:id] == subscription[0][:product_id].to_i}
if response.success?

league_gateway = DiscourseLeague::Billing::Gateways.new
league_gateway.unstore_subscription(subscription[0][:id])
league_gateway = DiscourseLeague::Billing::Gateways.new(:user_id => current_user.id, :product_id => subscription[0][:product_id])
league_gateway.unstore_subscription

group = Group.find(product[0][:group].to_i)
if group.users.include?(current_user)
group.remove(current_user)
GroupActionLogger.new(current_user, group).log_remove_user_from_group(current_user)
end
levels = PluginStore.get("discourse_league", "levels")
level = levels.select{|level| level[:id] == subscription[0][:product_id].to_i}

group = Group.find(level[0][:group].to_i)
if group.users.include?(current_user)
group.remove(current_user)
GroupActionLogger.new(current_user, group).log_remove_user_from_group(current_user)
end

if group.save
render json: PluginStore.get("discourse_league", "subscriptions") || []
else
return render_json_error(group)
end
if group.save
render json: PluginStore.get("discourse_league", "s:" + current_user.id.to_s) || []
else
render_json_error(response.message)
return render_json_error(group)
end
else
render_json_error(params[:id])
render_json_error(response.message)
end
end

@@ -3,10 +3,9 @@ class TransactionsController < ApplicationController

def all
if current_user.id == params[:user_id].to_i
transactions = PluginStore.get("discourse_league", "transactions")
transactions = PluginStore.get("discourse_league", "t:" + params[:user_id].to_s) || []

if !transactions.nil?
transactions = transactions.select{|transaction| transaction[:user_id] == params[:user_id].to_i}
if !transactions.empty?
transactions = transactions.flatten.map{|transaction| Transaction.new(transaction)} if !transactions.empty?
else
transactions = []
@@ -19,13 +18,14 @@ def all

def show
if current_user.id == params[:user_id].to_i
transactions = PluginStore.get("discourse_league", "transactions") || []
transactions = PluginStore.get("discourse_league", "t:" + params[:user_id].to_s) || []

if transactions.empty?
render_json_error(params[:user_id])
else
transaction = transactions.select{|transaction| transaction[:id] == params[:id].to_i}
if !transaction.empty? && transaction[0][:user_id] == params[:user_id].to_i
transaction = transactions.select{|transaction| transaction[:transaction_id] == params[:id]}

if !transaction.empty?
transaction = transaction.flatten.map{|transaction| Transaction.new(transaction)}
render_json_dump(serialize_data(transaction[0], TransactionSerializer))
else
@@ -246,14 +246,24 @@ export default Ember.Component.extend({
this.set("braintreeLoading", true);
var result = Payment.submitNonce(this.get('leagueLevel')[0].id, nonce, this.get("update"));
result.then(response => {
console.log(response);
if (response.success){
self.set('checkoutState', 'completed');
self.set('showBilling', false);
self.set('showVerify', false);
self.set('showCompleted', true);
}
this.set("braintreeLoading", false);
})
}).catch(e => {
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
bootbox.alert(I18n.t("generic_error_with_reason", {error: e.jqXHR.responseJSON.errors.join('. ')}));
} else {
bootbox.alert(I18n.t("generic_error"));
}
self.set('checkoutState', 'billing-payment');
self.set('showVerify', false);
self.set('showBilling', true);
this.set("braintreeLoading", false);
});
},

actions: {
@@ -45,12 +45,6 @@ export default Ember.Controller.extend({
this.set('checkoutState', 'billing-payment');
this.set('showBilling', true);
this.set('showDescription', true);
if (this.get('memberSubscription') || (!this.get('memberExists'))){
this.set('showPayment', true);
}
else{
this.set('showPayment', false);
};
this.set('memberDetails', this.get('initMemberDetails'));
if (Discourse.SiteSettings.league_gateway == "Braintree"){
this.set('showBraintree', true);
@@ -6,11 +6,10 @@ export default Ember.Controller.extend({
actions: {
cancelSubscription: function(subscription){
var self = this;

return bootbox.confirm(I18n.t("league.cancel_confirmation"), I18n.t("no_value"), I18n.t("yes_value"), function(result) {
if (result) {
self.set('model.loading', true);
Subscriptions.destroy(subscription.id).then(function(result){
Subscriptions.destroy(subscription.subscription_id).then(function(result){
self.set('model.subscriptions', result);
self.set('model.loading', false);
});
@@ -19,7 +18,7 @@ export default Ember.Controller.extend({
},

viewReceipt: function(receipt){
window.location.href = "/league/transactions/" + this.currentUser.id + "/" + receipt.id;
window.location.href = "/league/transactions/" + this.currentUser.id + "/" + receipt.transaction_id;
},

updateBilling: function(subscription){
@@ -20,6 +20,19 @@ export default Discourse.Route.extend({
else{
var memberSubscription = false;
}
controller.setProperties({ model, memberExists: memberExists, memberSubscription: memberSubscription });

if (!memberExists){
var showPayment = true;
}
else if (memberExists && memberSubscription){
var showPayment = true;
}
else if (memberExists && !memberSubscription){
var showPayment = false;
}
else{
var showPayment = false;
};
controller.setProperties({ model, memberExists: memberExists, memberSubscription: memberSubscription, showPayment: showPayment });
}
});
@@ -12,7 +12,7 @@
<div class="billing-section">
{{#if showDescription}}
{{#if memberSubscription}}
<p>{{{i18n 'legaue.update_billing.description' }}}</p>
<p>{{{i18n 'league.checkout.update_billing.description' }}}</p>
{{else}}
<p>{{{model.0.description_cooked}}}</p>
{{/if}} {{!-- memberSubscription --}}
@@ -93,6 +93,7 @@ en:
completed: "Completed"
completed_message: '<h2>Success!</h2><p>Thanks for signing up. You should receive a PM shortly with more details about your membership.</p>'
update_billing:
description: 'Enter the billing details below to make changes to your payment method.'
completed_message: '<h2>Success!</h2><p>Your payment method has been updated.</p>'
currency:
CAD: "$"
@@ -23,18 +23,10 @@ def paypal
end

def store_token
tokens = PluginStore.get("discourse_league", "user_payment_tokens")
tokens = [] if tokens.nil?
id = SecureRandom.random_number(1000000)
tokens = PluginStore.get("discourse_league", "tokens:" + @options[:user_id].to_s) || []
time = Time.now

until tokens.select{|token| token[:id] == id}.empty?
id = SecureRandom.random_number(1000000)
end

new_token = {
id: id,
user_id: @options[:user_id],
product_id: @options[:product_id],
token: @options[:token],
created_at: time,
@@ -43,13 +35,13 @@ def store_token

tokens.push(new_token)

PluginStore.set("discourse_league", "user_payment_tokens", tokens)
PluginStore.set("discourse_league", "tokens:" + @options[:user_id].to_s, tokens)
end

def update_token
tokens = PluginStore.get("discourse_league", "user_payment_tokens") || []
tokens = PluginStore.get("discourse_league", "tokens:" + @options[:user_id].to_s) || []

token = tokens.select{|token| token[:user_id] == @options[:user_id] && token[:product_id] == @options[:product_id]}
token = tokens.select{|token| token[:product_id] == @options[:product_id]}

time = Time.now

@@ -58,22 +50,14 @@ def update_token
token[0][:updated_at] = time
end

PluginStore.set("discourse_league", "user_payment_tokens", tokens)
PluginStore.set("discourse_league", "tokens:" + @options[:user_id].to_s, tokens)
end

def store_subscription(subscription_id, subscription_end_date)
subscriptions = PluginStore.get("discourse_league", "subscriptions")
subscriptions = [] if subscriptions.nil?
id = SecureRandom.random_number(1000000)
subscriptions = PluginStore.get("discourse_league", "s:" + @options[:user_id].to_s) || []
time = Time.now

until subscriptions.select{|subscription| subscription[:id] == id}.empty?
id = SecureRandom.random_number(1000000)
end

new_subscription = {
id: id,
user_id: @options[:user_id],
product_id: @options[:product_id],
subscription_id: subscription_id,
subscription_end_date: subscription_end_date,
@@ -84,22 +68,14 @@ def store_subscription(subscription_id, subscription_end_date)

subscriptions.push(new_subscription)

PluginStore.set("discourse_league", "subscriptions", subscriptions)
PluginStore.set("discourse_league", "s:" + @options[:user_id].to_s, subscriptions)
end

def store_transaction(transaction_id, transaction_amount, transaction_date, credit_card = {}, paypal = {})
transactions = PluginStore.get("discourse_league", "transactions")
transactions = [] if transactions.nil?
id = SecureRandom.random_number(1000000)
transactions = PluginStore.get("discourse_league", "t:" + @options[:user_id].to_s) || []
time = Time.now

until transactions.select{|transaction| transaction[:id] == id}.empty?
id = SecureRandom.random_number(1000000)
end

new_transaction = {
id: id,
user_id: @options[:user_id],
product_id: @options[:product_id],
transaction_id: transaction_id,
transaction_amount: transaction_amount,
@@ -111,15 +87,15 @@ def store_transaction(transaction_id, transaction_amount, transaction_date, cred

transactions.push(new_transaction)

PluginStore.set("discourse_league", "transactions", transactions)
PluginStore.set("discourse_league", "t:" + @options[:user_id].to_s, transactions)
end

def unstore_subscription(id)
subscriptions = PluginStore.get("discourse_league", "subscriptions")
subscription = subscriptions.select{|subscription| subscription[:id] = id.to_i}
def unstore_subscription
subscriptions = PluginStore.get("discourse_league", "s:" + @options[:user_id].to_s)
subscription = subscriptions.select{|subscription| subscription[:product_id] = @options[:product_id].to_i}

subscriptions.delete(subscription[0])
PluginStore.set("discourse_league", "subscriptions", subscriptions)
PluginStore.set("discourse_league", "s:" + @options[:user_id].to_s, subscriptions)
end

end

0 comments on commit aa7e18d

Please sign in to comment.