Skip to content

Commit

Permalink
chapter-9 edits
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Sep 18, 2020
1 parent 7383f0d commit cb174e8
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 359 deletions.
2 changes: 1 addition & 1 deletion book/8-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"dev": "nodemon server/server.js --watch server",
"build": "NODE_ENV=production next build",
"build": "next build",
"start": "node server/app.js",
"lint": "eslint components pages lib server",
"test": "jest --coverage"
Expand Down
2 changes: 1 addition & 1 deletion book/8-end/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { stripeCheckoutCallback } = require('./stripe');
require('dotenv').config();

const dev = process.env.NODE_ENV !== 'production';
const MONGO_URL = dev ? process.env.MONGO_URL_TEST : process.env.MONGO_URL;
const MONGO_URL = process.env.MONGO_URL_TEST;

const options = {
useNewUrlParser: true,
Expand Down
2 changes: 1 addition & 1 deletion book/9-begin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"dev": "nodemon server/server.js --watch server",
"build": "NODE_ENV=production next build",
"build": "next build",
"start": "node server/server.js",
"lint": "eslint components pages lib server",
"test": "jest --coverage"
Expand Down
8 changes: 4 additions & 4 deletions book/9-begin/server/api/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const express = require('express');
const Book = require('../models/Book');
const User = require('../models/User');
const { getRepos } = require('../github');
const logger = require('../logger');
// const logger = require('../logger');

const router = express.Router();

Expand All @@ -29,7 +29,7 @@ router.post('/books/add', async (req, res) => {
const book = await Book.add({ userId: req.user.id, ...req.body });
res.json(book);
} catch (err) {
logger.error(err);
console.error(err);
res.json({ error: err.message || err.toString() });
}
});
Expand Down Expand Up @@ -68,7 +68,7 @@ router.post('/books/sync-content', async (req, res) => {
await Book.syncContent({ id: bookId, user, request: req });
res.json({ done: 1 });
} catch (err) {
logger.error(err);
console.error(err);
res.json({ error: err.message || err.toString() });
}
});
Expand All @@ -85,7 +85,7 @@ router.get('/github/repos', async (req, res) => {
const response = await getRepos({ user, request: req });
res.json({ repos: response.data });
} catch (err) {
logger.error(err);
console.error(err);
res.json({ error: err.message || err.toString() });
}
});
Expand Down
4 changes: 2 additions & 2 deletions book/9-begin/server/api/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const express = require('express');
const Book = require('../models/Book');
const Purchase = require('../models/Purchase');
const { createSession } = require('../stripe');
const logger = require('../logger');
// const logger = require('../logger');

const router = express.Router();

Expand Down Expand Up @@ -53,7 +53,7 @@ router.post('/stripe/fetch-checkout-session', async (req, res) => {

res.json({ sessionId: session.id });
} catch (err) {
logger.error(err);
console.error(err);
res.json({ error: err.message || err.toString() });
}
});
Expand Down
8 changes: 4 additions & 4 deletions book/9-begin/server/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fetch = require('node-fetch');
const { oauthLoginUrl } = require('@octokit/oauth-login-url');
const _ = require('lodash');

const logger = require('./logger');
// const logger = require('./logger');
const User = require('./models/User');

require('dotenv').config();
Expand All @@ -19,7 +19,7 @@ function getAPI({ user, previews = [], request }) {
request: { timeout: 10000 },
log: {
info(msg, info) {
logger.info(`Github API log: ${msg}`, {
console.log(`Github API log: ${msg}`, {
..._.omit(info, 'headers', 'request', 'body'),
user: _.pick(user, '_id', 'githubUsername', 'githubId'),
..._.pick(request, 'ip', 'hostname'),
Expand Down Expand Up @@ -89,7 +89,7 @@ function setupGithub({ server, ROOT_URL }) {
clientId: CLIENT_ID,
redirectUrl: `${ROOT_URL}/auth/github/callback`,
scopes: ['repo', 'user:email'],
log: { warn: (message) => logger.warn(message) },
log: { warn: (message) => console.log(message) },
});

req.session.githubAuthState = state;
Expand Down Expand Up @@ -148,7 +148,7 @@ function setupGithub({ server, ROOT_URL }) {
profile: profile.data,
});
} catch (error) {
logger.error(error.toString());
console.error(error.toString());

res.redirect(`${redirectUrl}/admin?error=${error.toString()}`);
}
Expand Down
8 changes: 4 additions & 4 deletions book/9-begin/server/models/Book.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const User = require('./User');
const { addToMailchimp } = require('../mailchimp');
const { getCommits, getRepoDetail } = require('../github');

const logger = require('../logger');
// const logger = require('../logger');

const { Schema } = mongoose;

Expand Down Expand Up @@ -151,9 +151,9 @@ class BookClass {

try {
await Chapter.syncContent({ book, data });
logger.info('Content is synced', { path: f.path });
console.log('Content is synced', { path: f.path });
} catch (error) {
logger.error('Content sync has error', { path: f.path, error });
console.error('Content sync has error', { path: f.path, error });
}
}),
);
Expand Down Expand Up @@ -181,7 +181,7 @@ class BookClass {
try {
await addToMailchimp({ email: user.email, listName: 'purchased' });
} catch (error) {
logger.error('buy error:', error);
console.error('buy error:', error);
}

return Purchase.create({
Expand Down
4 changes: 2 additions & 2 deletions book/9-begin/server/models/EmailTemplate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');
const _ = require('lodash');
const logger = require('../logger');
// const logger = require('../logger');

const { Schema } = mongoose;

Expand Down Expand Up @@ -66,7 +66,7 @@ function insertTemplates() {
}

EmailTemplate.create(template).catch((error) => {
logger.error('EmailTemplate insertion error:', error);
console.error('EmailTemplate insertion error:', error);
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions book/9-begin/server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { addToMailchimp } = require('../mailchimp');
const generateSlug = require('../utils/slugify');
const sendEmail = require('../aws-ses');
const { getEmailTemplate } = require('./EmailTemplate');
const logger = require('../logger');
// const logger = require('../logger');

const { Schema } = mongoose;

Expand Down Expand Up @@ -116,13 +116,13 @@ class UserClass {
body: template.message,
});
} catch (err) {
logger.error('Email sending error:', err);
console.error('Email sending error:', err);
}

try {
await addToMailchimp({ email, listName: 'signedup' });
} catch (error) {
logger.error('Mailchimp error:', error);
console.error('Mailchimp error:', error);
}

return _.pick(newUser, UserClass.publicFields());
Expand Down
2 changes: 1 addition & 1 deletion book/9-begin/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { stripeCheckoutCallback } = require('./stripe');
require('dotenv').config();

const dev = process.env.NODE_ENV !== 'production';
const MONGO_URL = dev ? process.env.MONGO_URL_TEST : process.env.MONGO_URL;
const MONGO_URL = process.env.MONGO_URL_TEST;

const port = process.env.PORT || 8000;
const ROOT_URL = `http://localhost:${port}`;
Expand Down
4 changes: 2 additions & 2 deletions book/9-begin/server/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const lodash = require('lodash');

const Book = require('./models/Book');
const User = require('./models/User');
const logger = require('./logger');
// const logger = require('./logger');

const dev = process.env.NODE_ENV !== 'production';
const API_KEY = dev ? process.env.Stripe_Test_SecretKey : process.env.Stripe_Live_SecretKey;
Expand Down Expand Up @@ -97,7 +97,7 @@ function stripeCheckoutCallback({ server }) {

res.redirect(`${ROOT_URL}${session.metadata.redirectUrl}`);
} catch (err) {
logger.error(err);
console.error(err);
res.redirect(
`${ROOT_URL}${session.metadata.redirectUrl}?error=${err.message || err.toString()}`,
);
Expand Down
Loading

0 comments on commit cb174e8

Please sign in to comment.