Skip to content

Commit

Permalink
wire up gcloud datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik committed Feb 29, 2016
1 parent 3a4c566 commit abb1c9c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bundle.js
bundle.min.js
bundle.min.js.map
bundle.js.map
key.json
1 change: 0 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import env from 'node-env-file';
const ENV = env('.env');
console.log(ENV);
export default ENV;
35 changes: 32 additions & 3 deletions create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import nodemailer from 'nodemailer';
import smtpTransport from 'nodemailer-smtp-transport';
import XOAuth2 from 'xoauth2';
import config from './config';
import gcloud from 'gcloud';

var projectId = config.PROJECT_ID;

if (!projectId) {
var MISSING_ID = [
'Cannot find your project ID. Please set an environment variable named ',
'"DATASET_ID", holding the ID of your project.'
].join('');
throw new Error(MISSING_ID);
}

var ds = gcloud.datastore.dataset({
projectId: config.PROJECT_ID
});

const today = new Date();
const due = new Date()
Expand All @@ -30,7 +45,7 @@ const input = {
const generator = require('xoauth2').createXOAuth2Generator({
user: config.USERNAME,
clientId: config.CLIENT_ID,
clientSecret: confg.CLIENT_SECRET,
clientSecret: config.CLIENT_SECRET,
refreshToken: config.REFRESH_TOKEN
});
const transporter = nodemailer.createTransport(({
Expand All @@ -47,7 +62,15 @@ const schedule = require('node-schedule');
schedule.scheduleJob('20 * * * * *', function(){
const invoice = new Invoice();
const stream = fs.createWriteStream('invoice.pdf');
invoice.generatePDFStream(input).pipe(stream)
invoice.generatePDFStream(input).pipe(stream);
var entity = {
key: ds.key("Invoice"),
data: {
invoice_number: 1,
amount: config.INVOICE_AMOUNT,
email: config.EMAIL_TO,
}
};
stream.on('close', function(err){
if(!err){
// send mail with defined transport object
Expand All @@ -68,7 +91,13 @@ schedule.scheduleJob('20 * * * * *', function(){
if (error) {
console.log(error);
} else {
console.log('Message sent');
ds.save(entity, function(err, key) {
if (err) {
console.log(err);
return;
}
console.log(key);
});
}
});
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"better-npm-run": "0.0.7",
"gcloud": "^0.28.0",
"invoice-ninja": "^0.2.2",
"json-loader": "^0.5.4",
"node-env-file": "^0.1.8",
"node-schedule": "^1.0.0",
"nodemailer": "^2.2.1",
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
test: /(\.js?)$/,
loader: 'babel',
exclude: /node_modules/,
loaders: ['babel-loader'],
query: {
"presets": ["es2015"]
}
Expand Down

0 comments on commit abb1c9c

Please sign in to comment.