fleet/website/api/controllers/customers/view-dashboard.js
Eric 896918bc79
Website: Fleet Premium subscription renewals (#9200)
https://github.com/fleetdm/fleet/issues/9172

Changes:
- Added
`website/api/controllers/webhooks/recieve-stripe-subscription-events.js`
a webhook for receiving Stripe events.
- If the stripe event received is from a user's subscription
automatically renewing, A new license key is generated, the subscription
record is updated, and a renewal confirmation email is sent.
- If the stripe event received is from a user's subscription's upcoming
renewal, a renewal notification email is sent.
- If any other event type is received from Stripe, the webhook returns a
200 response.
- Added new email templates:
   - `email-subscription-renewal-confirmation`
   - `email-upcoming-subscription-renewal`
- Updated `website/api/controllers/admin/view-email-template-preview.js`
to have fake data for the added email templates.
- Updated `website/api/controllers/customers/view-dashboard.js` to set
two boolean variables: `subscriptionExpiresSoon` and
`subscriptionHasBeenRecentlyRenewed`
- Updated the customer dashboard to display notifications on the top of
the page if a user's subscription will renew in the next 30 days, or if
the user's subscription has been renewed in the past 30 days.
- `website/views/layouts/layout-email.ejs` - Updated the font, padding,
and text color to match wireframes.
2023-01-06 18:36:29 -06:00

63 lines
1.8 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'View dashboard',
description: 'Display "Dashboard" page.',
exits: {
success: {
viewTemplatePath: 'pages/customers/dashboard'
},
redirect: {
description: 'The requesting user does not have a subscription, redirecting to the new license page.',
responseType: 'redirect',
},
},
fn: async function () {
const today = Date.now();
const oneYearInMs = (1000 * 60 * 60 * 24 * 365);
const oneYearAgoAt = today - oneYearInMs;
const thirtyDaysAgoAt = today - (1000 * 60 * 60 * 24 * 30);
const thirtyDaysFromNowAt = today + (1000 * 60 * 60 * 24 * 30);
let subscriptionHasBeenRecentlyRenewed = false;
let subscriptionExpiresSoon = false;
// Get subscription Info
let thisSubscription = await Subscription.findOne({user: this.req.me.id});
// If the user does not have a subscription, then help them subscribe.
if(!thisSubscription) {
throw {redirect: '/customers/new-license'};
}
// If this subscription is over a year old, and was renewed in the past 30 days set subscriptionHasBeenRecentlyRenewed to true.
if(thisSubscription.createdAt <= oneYearAgoAt && (thisSubscription.nextBillingAt - oneYearInMs) >= thirtyDaysAgoAt) {
subscriptionHasBeenRecentlyRenewed = true;
}
// If this subscription will renew in the next 30 days, set subscriptionExpiresSoon to true.
if(thisSubscription.nextBillingAt <= thirtyDaysFromNowAt){
subscriptionExpiresSoon = true;
}
// Respond with view.
return {
stripePublishableKey: sails.config.custom.enableBillingFeatures ? sails.config.custom.stripePublishableKey : undefined,
thisSubscription,
subscriptionExpiresSoon,
subscriptionHasBeenRecentlyRenewed,
};
}
};