feat(donate): remove unauthenticated routes
This commit is contained in:
@ -8,7 +8,6 @@ const log = debug('fcc:boot:donate');
|
|||||||
|
|
||||||
export default function donateBoot(app, done) {
|
export default function donateBoot(app, done) {
|
||||||
let stripe = false;
|
let stripe = false;
|
||||||
const { User } = app.models;
|
|
||||||
const api = app.loopback.Router();
|
const api = app.loopback.Router();
|
||||||
const donateRouter = app.loopback.Router();
|
const donateRouter = app.loopback.Router();
|
||||||
|
|
||||||
@ -105,8 +104,16 @@ export default function donateBoot(app, done) {
|
|||||||
function createStripeDonation(req, res) {
|
function createStripeDonation(req, res) {
|
||||||
const { user, body } = req;
|
const { user, body } = req;
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res
|
||||||
|
.status(500)
|
||||||
|
.send({ error: 'User must be signed in for this request.' });
|
||||||
|
}
|
||||||
|
|
||||||
if (!body || !body.amount || !body.duration) {
|
if (!body || !body.amount || !body.duration) {
|
||||||
return res.status(400).send({ error: 'Amount and duration Required.' });
|
return res.status(500).send({
|
||||||
|
error: 'The donation form had invalid values for this submission.'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -116,29 +123,11 @@ export default function donateBoot(app, done) {
|
|||||||
} = body;
|
} = body;
|
||||||
|
|
||||||
if (!validStripeForm(amount, duration, email)) {
|
if (!validStripeForm(amount, duration, email)) {
|
||||||
return res
|
return res.status(500).send({
|
||||||
.status(500)
|
error: 'The donation form had invalid values for this submission.'
|
||||||
.send({ error: 'Invalid donation form values submitted' });
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const isOneTime = duration === 'onetime' ? true : false;
|
|
||||||
|
|
||||||
const fccUser = user
|
|
||||||
? Promise.resolve(user)
|
|
||||||
: new Promise((resolve, reject) =>
|
|
||||||
User.findOrCreate(
|
|
||||||
{ where: { email } },
|
|
||||||
{ email },
|
|
||||||
(err, instance, isNew) => {
|
|
||||||
log('is new user instance: ', isNew);
|
|
||||||
if (err) {
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
return resolve(instance);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let donatingUser = {};
|
let donatingUser = {};
|
||||||
let donation = {
|
let donation = {
|
||||||
email,
|
email,
|
||||||
@ -188,20 +177,20 @@ export default function donateBoot(app, done) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return fccUser
|
return Promise.resolve(user)
|
||||||
.then(user => {
|
.then(nonDonatingUser => {
|
||||||
const { isDonating } = user;
|
const { isDonating } = nonDonatingUser;
|
||||||
if (isDonating) {
|
if (isDonating) {
|
||||||
throw {
|
throw {
|
||||||
message: `User already has active donation(s).`,
|
message: `User already has active donation(s).`,
|
||||||
type: 'AlreadyDonatingError'
|
type: 'AlreadyDonatingError'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return user;
|
return nonDonatingUser;
|
||||||
})
|
})
|
||||||
.then(createCustomer)
|
.then(createCustomer)
|
||||||
.then(customer => {
|
.then(customer => {
|
||||||
return isOneTime
|
return duration === 'onetime'
|
||||||
? createOneTimeCharge(customer).then(charge => {
|
? createOneTimeCharge(customer).then(charge => {
|
||||||
donation.subscriptionId = 'one-time-charge-prefix-' + charge.id;
|
donation.subscriptionId = 'one-time-charge-prefix-' + charge.id;
|
||||||
return res.send(charge);
|
return res.send(charge);
|
||||||
@ -241,7 +230,6 @@ export default function donateBoot(app, done) {
|
|||||||
donateRouter.use('/donate', api);
|
donateRouter.use('/donate', api);
|
||||||
app.use(donateRouter);
|
app.use(donateRouter);
|
||||||
app.use('/internal', donateRouter);
|
app.use('/internal', donateRouter);
|
||||||
app.use('/unauthenticated', donateRouter);
|
|
||||||
connectToStripe().then(done);
|
connectToStripe().then(done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import { injectStripe } from 'react-stripe-elements';
|
|||||||
import StripeCardForm from './StripeCardForm';
|
import StripeCardForm from './StripeCardForm';
|
||||||
import DonateCompletion from './DonateCompletion';
|
import DonateCompletion from './DonateCompletion';
|
||||||
import { postChargeStripe } from '../../../utils/ajax';
|
import { postChargeStripe } from '../../../utils/ajax';
|
||||||
import { userSelector, isSignedInSelector } from '../../../redux';
|
import { userSelector } from '../../../redux';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
donationAmount: PropTypes.number.isRequired,
|
donationAmount: PropTypes.number.isRequired,
|
||||||
@ -39,8 +39,7 @@ const initialState = {
|
|||||||
|
|
||||||
const mapStateToProps = createSelector(
|
const mapStateToProps = createSelector(
|
||||||
userSelector,
|
userSelector,
|
||||||
isSignedInSelector,
|
({ email, theme }) => ({ email, theme })
|
||||||
({ email, theme }, isSignedIn) => ({ email, theme, isSignedIn })
|
|
||||||
);
|
);
|
||||||
|
|
||||||
class DonateFormChildViewForHOC extends Component {
|
class DonateFormChildViewForHOC extends Component {
|
||||||
@ -122,7 +121,6 @@ class DonateFormChildViewForHOC extends Component {
|
|||||||
|
|
||||||
postDonation(token) {
|
postDonation(token) {
|
||||||
const { donationAmount: amount, donationDuration: duration } = this.state;
|
const { donationAmount: amount, donationDuration: duration } = this.state;
|
||||||
const { isSignedIn } = this.props;
|
|
||||||
this.setState(state => ({
|
this.setState(state => ({
|
||||||
...state,
|
...state,
|
||||||
donationState: {
|
donationState: {
|
||||||
@ -131,7 +129,7 @@ class DonateFormChildViewForHOC extends Component {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return postChargeStripe(isSignedIn, {
|
return postChargeStripe({
|
||||||
token,
|
token,
|
||||||
amount,
|
amount,
|
||||||
duration
|
duration
|
||||||
|
@ -50,11 +50,8 @@ export function getArticleById(shortId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** POST **/
|
/** POST **/
|
||||||
export function postChargeStripe(isSignedIn, body) {
|
export function postChargeStripe(body) {
|
||||||
const donatePath = '/donate/charge-stripe';
|
return post(`/donate/charge-stripe`, body);
|
||||||
return isSignedIn
|
|
||||||
? post(donatePath, body)
|
|
||||||
: postUnauthenticated(donatePath, body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function putUpdateLegacyCert(body) {
|
export function putUpdateLegacyCert(body) {
|
||||||
|
Reference in New Issue
Block a user