Skip to content

Commit

Permalink
Merge pull request #3444 from fjordllc/bug/subscription-status
Browse files Browse the repository at this point in the history
サブスクステータスの表示を非同期化
  • Loading branch information
komagata committed Dec 5, 2021
2 parents 53d3e27 + 90219ba commit 900ce60
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class API::SubscriptionsController < API::BaseController
def index
@subscriptions = Subscription.new.all
end
end
1 change: 1 addition & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ require('../companies.js')
require('../worried-users.js')
require('../report_template.js')
require('../user-recent-reports.js')
require('../subscription-status.js')
49 changes: 49 additions & 0 deletions app/javascript/subscription-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
document.addEventListener('DOMContentLoaded', () => {
const statusMap = {
trialing: 'is-primary',
active: 'is-success',
canceled: 'is-danger',
past_due: 'is-warning'
}
const statusLabel = {
trialing: 'お試し中',
active: '有効',
canceled: 'キャンセル',
past_due: '期日経過'
}
const selector = '.subscription-status'
const statuses = document.querySelectorAll(selector)
if (statuses) {
let subs = []

fetch('/api/subscriptions.json', {
method: 'GET',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
credentials: 'same-origin',
redirect: 'manual'
})
.then((response) => {
return response.json()
})
.then((json) => {
subs = json.subscriptions

statuses.forEach((status) => {
const subId = status.getAttribute('data-subscription-id')
subs.forEach((sub) => {
if (sub.id === subId) {
const level = statusMap[sub.status]
const label = statusLabel[sub.status]
status.classList.add('a-button')
status.classList.add('is-sm')
status.classList.add(level)
status.innerHTML = label
}
})
})
})
.catch((error) => {
console.warn('Failed to parsing', error)
})
}
})
8 changes: 5 additions & 3 deletions app/views/admin/users/_table.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@
td.admin-table__item-value.is-text-align-center
- if user.subscription_id?
= link_to user.subscription_url,
class: 'a-button is-sm is-info',
target: '_blank', rel: 'noopener' do
i.fas.fa-credit-card
class: 'subscription-status',
target: '_blank',
rel: 'noopener',
data: { 'subscription-id': user.subscription_id } do
i.fas.fa-spinner.fa-pulse
- else
| -
td.admin-table__item-value.is-text-align-center
Expand Down
6 changes: 6 additions & 0 deletions app/views/api/subscriptions/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.subscriptions do
json.array! @subscriptions do |subscription|
json.id subscription['id']
json.status subscription['status']
end
end
2 changes: 1 addition & 1 deletion app/views/api/watches/_watch.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ json.created_at matched_document(watch.watchable).created_at
json.updated_at matched_document(watch.watchable).updated_at
json.url searchable_url(watch.watchable)
json.title matched_document(watch.watchable).title
json.watch_class_name watch.watchable_type.to_s.tableize.chop
json.watch_class_name watch.watchable_type.to_s.tableize.chop
json.model_name_with_i18n t("activerecord.models.#{matched_document(watch.watchable_type).to_s.tableize.singularize}")
json.summary searchable_summary(filtered_message(watch.watchable), 90)
1 change: 1 addition & 0 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
resource :position, only: %i(update), controller: "courses_categories/position"
end
resources :notifications, only: %i(index)
resources :subscriptions, only: %i(index)
resources :comments, only: %i(index create update destroy)
resources :answers, only: %i(index create update destroy) do
resource :correct_answer, only: %i(create update)
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/bootcamp.rake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ namespace :bootcamp do
Rake::Task['db:reset'].execute if ENV['DB_NAME'] == 'bootcamp_staging'
end

desc 'Reset Stripe test DB.'
task reset_stripe: :environment do
customer = Stripe::Customer.create(email: 'hatsuno@fjord.jp')
Subscription.new.create(
customer.id
)
end

namespace :oneshot do
desc 'Cloud Build Task'
task cloudbuild: :environment do
Expand Down
144 changes: 144 additions & 0 deletions test/cassettes/subscription/all.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions test/integration/api/subscriptions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'test_helper'

class API::SubscriptionsTest < ActionDispatch::IntegrationTest
test 'GET /api/subscriptions.json' do
get api_subscriptions_path(format: :json)
assert_response :unauthorized

token = create_token('hatsuno', 'testtest')
VCR.use_cassette 'subscription/all' do
get api_subscriptions_path(format: :json),
headers: { 'Authorization' => "Bearer #{token}" }
end

id = JSON.parse(@response.body)['subscriptions'][0]['id']
assert_equal users(:hatsuno).subscription_id, id
end
end
2 changes: 1 addition & 1 deletion test/supports/vcr_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'vcr'

VCR.configure do |c|
# c.allow_http_connections_when_no_cassette = true
c.allow_http_connections_when_no_cassette = true
c.ignore_localhost = true
c.cassette_library_dir = 'test/cassettes'
c.hook_into :webmock
Expand Down
4 changes: 2 additions & 2 deletions test/system/sign_up_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class SignUpTest < ApplicationSystemTestCase

fill_stripe_element('4242 4242 4242 4242', '12 / 21', '111')

VCR.use_cassette 'sign_up/valid-card' do
VCR.use_cassette 'sign_up/valid-card', record: :once do
click_button '参加する'
assert_text 'に使用できない文字列が含まれています'
end
Expand Down Expand Up @@ -257,7 +257,7 @@ class SignUpTest < ApplicationSystemTestCase

fill_stripe_element('5555 5555 5555 4444', '12 / 21', '111')

VCR.use_cassette 'sign_up/valid-card' do
VCR.use_cassette 'sign_up/valid-card', record: :once do
click_button '参加する'
assert_text '自己紹介を入力してください'
end
Expand Down

0 comments on commit 900ce60

Please sign in to comment.