Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add insiders settings #7151

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions app/controllers/api/generate_bootcamp_affiliate_coupon_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class API::GenerateBootcampAffiliateCouponCode < API::BaseController
skip_before_action :authenticate_user!
before_action :authenticate_user

def create
return unless current_user.insider?
return if current_user.bootcamp_affiliate_coupon_code.present?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are better places into the command below (the one is already there and I'll add the insiders guard too). I prefer keeping all the logic in one place.

Also, you can't just "return" here as you need to render json, so it should be return render(json: {}) to be a valid response for a JSON API. You could also raise an error of some description like raise "Not insider" which would get caught by your block, but as we have standardised error messages in the API, that's probably not ideal.

So I would make this method:

code = User::GenerateBootcampAffiliateCouponCode.(current_user)
render json: { coupon_code: code }

or if you want to raise an error if the code is missing:

code = User::GenerateBootcampAffiliateCouponCode.(current_user)
if code 
  render json: { coupon_code: code }
else
  render_403(:could_not_generate_coupon_code)

And then you need to add translations for could_not_generate_coupon_code (see config/locales/api/en.yml)


User::GenerateBootcampAffiliateCouponCode.(current_user)

render json: { coupon_code: current_user.bootcamp_affiliate_coupon_code }
rescue StandardError => e
render json: { error: e.message }, status: :unprocessable_entity
end
end
15 changes: 15 additions & 0 deletions app/controllers/api/generate_bootcamp_free_coupon_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class API::GenerateBootcampFreeCouponCode < API::BaseController
skip_before_action :authenticate_user!
before_action :authenticate_user

def create
return if current_user.insiders_status != :active_lifetime
return if current_user.bootcamp_free_coupon_code.present?

User::GenerateBootcampFreeCouponCode.(current_user)

render json: { coupon_code: current_user.bootcamp_free_coupon_code }
rescue StandardError => e
render json: { error: e.message }, status: :unprocessable_entity
end
end
9 changes: 0 additions & 9 deletions app/javascript/components/settings/BootcampAffiliateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ export default function BootcampAffiliateForm({
)
}

const SuccessMessage = () => {
return (
<div className="status success">
<Icon icon="completed-check-circle" alt="Success" />
Your preferences have been updated
</div>
)
}

export function InfoMessage({
insidersStatus,
insidersPath,
Expand Down
3 changes: 3 additions & 0 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@
get :lookup, on: :collection
end

resources :generate_bootcamp_free_coupon_codes, only: %i[create]
resources :generate_bootcamp_affiliate_coupon_codes, only: %i[create]

resources :community_stories, only: %i[index]

namespace :training_data do
Expand Down
Loading