More changes for db migration

This commit is contained in:
Jonathan Cremin 2016-08-07 14:38:05 +01:00
parent de0284e48a
commit 889dc02945
33 changed files with 740 additions and 100 deletions

View file

@ -54,7 +54,7 @@ export function* list() {
const files = yield models.file.findAll({
where: {
userId: this.user.id,
status: 'active',
processed: true,
},
order: '"createdAt" DESC',
offset,
@ -70,9 +70,6 @@ export function* get() {
const file = yield models.file.findOne({
where: {
id: this.params.id,
status: {
$in: ['active', 'uploading'],
},
},
});
this.assert(file, 404, '{"error": {"message": "File not found", "code": 604}}');
@ -83,20 +80,6 @@ export function* get() {
}
export function* put() {
if (this.request.body.trashed) {
const file = yield models.file.findOne({
where: {
id: this.params.id,
userId: this.user.id,
},
});
file.status = this.request.body.trashed ? 'trashed' : 'active';
yield file.save();
}
}
export function* del() {
const file = yield models.file.findOne({
where: {
@ -105,8 +88,7 @@ export function* del() {
},
});
this.assert(file, 401, '{"error": {"message": "File not found", "code": 604}}');
file.status = 'deleted';
yield file.save();
yield file.destroy();
const event = { type: 'file-deleted', data: { id: this.params.id } };
yield this.redis.publish(`/file/${this.params.id}`, JSON.stringify(event));
yield this.redis.publish(`/user/${this.user.id}`, JSON.stringify(event));

85
api/routes/pro.js Normal file
View file

@ -0,0 +1,85 @@
import path from 'path';
import views from 'co-views';
const render = views(path.join(__dirname, '/../views'), { default: 'ejs' });
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
import sendgridInit from 'sendgrid';
const sendgrid = sendgridInit(process.env.SENDGRID_KEY);
import models from '../../models';
const from = process.env.EMAIL_FROM;
const fromname = process.env.EMAIL_NAME;
export function* create() {
const stripeToken = this.request.body.stripeToken;
const ip = this.request.headers['x-real-ip'] || this.req.connection.remoteAddress;
const createCustomer = {
card: stripeToken.id,
plan: 'usd_monthly',
email: this.user.email,
};
const customer = yield stripe.customers.create(createCustomer);
this.assert(customer.subscription.status === 'active', 400, '{"status": "error"}');
delete customer.subscriptions;
const user = yield models.user.findById(this.user.id);
user.plan = 'Pro';
yield user.save();
const transaction = yield models.transaction.create({
userId: this.user.id,
amount: customer.subscription.plan.amount,
description: customer.subscription.plan.name,
data: customer,
type: 'direct',
ip,
});
yield transaction.save();
this.user.plan = 'Pro';
this.body = { status: 'active' };
const html = yield render('email/inlined/pro');
const text = `Hey, thanks for upgrading to Hostr Pro!
You've signed up for Hostr Pro Monthly at $6/Month.
Jonathan Cremin, Hostr Founder
`;
const mail = new sendgrid.Email({
to: this.user.email,
subject: 'Hostr Pro',
from,
fromname,
html,
text,
});
mail.addCategory('pro-upgrade');
sendgrid.send(mail);
}
export function* cancel() {
const user = yield models.user.findById(this.user.id);
const transactions = yield user.getTransactions();
const transaction = transactions[0];
yield stripe.customers.cancelSubscription(
transaction.data.id,
transaction.data.subscription.id,
{ at_period_end: false }
);
user.plan = 'Free';
yield user.save();
this.user.plan = 'Free';
this.body = { status: 'inactive' };
}

View file

@ -20,7 +20,11 @@ export function* token() {
}
export function* transaction() {
const transactions = yield models.transaction.findAll({ userId: this.user.id });
const transactions = yield models.transaction.findAll({
where: {
userId: this.user.id,
},
});
this.body = transactions.map((item) => {
return {