8000 Share link by jainsneha23 · Pull Request #49 · jainsneha23/cvmaker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Share link #49

New issue
8000

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions api/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@ class ResumeSchema {

const ResumeType = new GraphQLObjectType({
name: 'resume',
fields: function () {
return {
id: {
type: GraphQLID
},
userid: {
type: GraphQLFloat
},
resumeid: {
type: GraphQLInt
},
cvdata: {
type: GraphQLString
},
10000 template: {
type: GraphQLString
}
};
}
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID)
},
userid: {
type: new GraphQLNonNull(GraphQLFloat)
},
resumeid: {
type: new GraphQLNonNull(GraphQLInt)
},
cvdata: {
type: GraphQLString
},
template: {
type: GraphQLString
},
share: {
type: GraphQLString
}
})
});

const MutationAdd = {
Expand All @@ -72,13 +73,17 @@ class ResumeSchema {
template: {
name: 'Resume template',
type: new GraphQLNonNull(GraphQLString)
},
share: {
name: 'Resume share',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (root, {id, userid, resumeid, cvdata, template}) => {
resolve: (root, {id, userid, resumeid, cvdata, template, share}) => {
return new Promise((resolve, reject) => {
db.insert(dbName, {id, userid, resumeid, cvdata, template})
db.insert(dbName, {id, userid, resumeid, cvdata, template, share})
.then(() => db.selectAll(dbName))
.then(resumes => resolve(resumes))
.then(() => resolve())
.catch(err => reject(err));
});
}
Expand All @@ -97,7 +102,7 @@ class ResumeSchema {
return new Promise((resolve, reject) => {
db.delete(dbName, {id})
.then(() => db.selectAll(dbName))
.then(resumes => resolve(resumes))
.then(() => resolve())
.catch(err => reject(err));
});
}
Expand All @@ -108,7 +113,7 @@ class ResumeSchema {
description: 'Update a Resume',
args: {
id: {
name: 'Id',
name: 'Resume Id',
type: new GraphQLNonNull(GraphQLID)
},
cvdata: {
Expand All @@ -118,15 +123,20 @@ class ResumeSchema {
template: {
name: 'Resume template',
type: GraphQLString
},
share: {
name: 'Resume share',
type: GraphQLString
}
},
resolve: (root, {id, cvdata, template}) => {
resolve: (root, {id, cvdata, template, share}) => {
return new Promise((resolve, reject) => {
const dataToUpdate = {};
if(cvdata) dataToUpdate.cvdata = cvdata;
if(template) dataToUpdate.template = template;
if(share) dataToUpdate.share = share;
db.update(dbName, {id}, {$set:dataToUpdate})
.then(() => db.selectAll(dbName))
.then(() => db.find(dbName, {id}))
.then(resumes => resolve(resumes))
.catch(err => reject(err));
});
Expand All @@ -143,12 +153,17 @@ class ResumeSchema {
args: {
userid: {
name: 'User ID',
type: new GraphQLNonNull(GraphQLFloat)
type: GraphQLFloat
},
id: {
name: 'Resume ID',
type: GraphQLID
}
},
resolve: (root, {userid}) => {
resolve: (root, {id, userid}) => {
return new Promise((resolve, reject) => {
db.find(dbName, {userid})
const idToFind = id ? {id} : {userid};
db.find(dbName, idToFind)
.then(resumes => resolve(resumes))
.catch(err => reject(err));
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"draft-js-import-html": "0.3.2",
"es6-promise": "4.1.1",
"express": "4.14.1",
"express-graphql": "0.6.6",
"express-graphql": "0.6.12",
"express-session": "1.15.3",
"extract-text-webpack-plugin": "2.1.2",
"file-loader": "0.9.0",
Expand Down
20 changes: 20 additions & 0 deletions tools/middlewares/cryptoModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import crypto from 'crypto';

const key = process.env.cryptoKey;
const nonce = process.env.cryptoNonce;

const encrypt = (text) => {
const cipher = crypto.createCipheriv('aes256', key, nonce);
let ciphertext = cipher.update(text, 'utf8', 'hex');
ciphertext += cipher.final('hex');
return ciphertext;
};

const decrypt = (ciphertext) => {
const decipher = crypto.createDecipheriv('aes256', key, nonce);
let plainText = decipher.update(ciphertext, 'hex', 'utf8');
plainText += decipher.final('utf8');
return plainText;
};

export {encrypt, decrypt};
24 changes: 24 additions & 0 deletions tools/middlewares/graphql-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { graphql } from 'graphql';

import ResumeSchema from '../../api/graphql';

const graphqlQuery = (query) => {

return new Promise((resolve, reject) => {
graphql(getSchema(), query).then((result) => {
if (result.errors) reject(result.errors);
if (result.data.resumes && result.data.resumes.length === 1)
resolve(result.data.resumes[0]);
if (result.data.resumes && result.data.resumes.length > 1)
resolve(result.data.resumes);
resolve();
}).catch(e => reject(e));
});
};

const getSchema = () => {
const resumeSchema = new ResumeSchema().getSchema();
return resumeSchema;
};

export {getSchema, graphqlQuery};
22 changes: 12 additions & 10 deletions tools/routes/api.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { graphql } from 'graphql';
import graphqlHTTP from 'express-graphql';
import bodyParser from 'body-parser';

import ResumeSchema from '../../api/graphql';
import {getSchema, graphqlQuery} from '../middlewares/graphql-query';

const ApiRouter = (app, express) => {

const router = express.Router();
const resumeSchema = new ResumeSchema().getSchema();
const resumeSchema = getSchema();

router.use('/graphql', graphqlHTTP(() => ({
resumeSchema,
pretty: true
})));
router.use('/graphql', graphqlHTTP({
schema: resumeSchema,
pretty: true,
graphiql: true
}));

router.post('/resume', bodyParser.text(), (req, res) => {
graphql(resumeSchema, req.body).then( function(result) {
res.send(JSON.stringify(result,null,' '));
}).catch(e => res.send(e));
graphqlQuery(req.body).then( function(result) {
res.send({result});
}).catch(e => {
res.status(502).send({errors: e});
});
});

return router;
Expand Down
40 changes: 34 additions & 6 deletions tools/routes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path';
import Mailer from '../middlewares/mailer_sendgrid';
import Messager from '../middlewares/messager';
import {generatePDF} from '../middlewares/generate-pdf';
import {encrypt, decrypt} from '../middlewares/cryptoModule';
import {graphqlQuery} from '../middlewares/graphql-query';
import * as Templates from '../../web/templates';

const AppRoutes = (app, express) => {
Expand All @@ -11,19 +13,19 @@ const AppRoutes = (app, express) => {
const mailService = new Mailer();
const messager = new Messager();

router.post('/download', bodyParser.json() , function(req, res){
router.post('/download', bodyParser.json() , (req, res) => {
let Comp = Templates[`Template${req.body.templateId}`];
const filename = `Resume-${req.body.templateId}-${new Date().getTime()}`;
const filePath = path.join(__dirname,`../generated_files/${filename}.pdf`);
generatePDF({html: app.locals.getComponentAsHTML(Comp, req.body.cvdata, req.body.templateColor), filename, filePath})
.then((response)=> res.send(response))
.catch((error) => {
console.error('Error downloading resume', error);
res.status(500).send('Error downloading resume. Please try again after some time');
res.status(502).send({errors: 'Error downloading resume. Please try again after some time'});
});
});

router.post('/email', bodyParser.json() , function(req, res){
router.post('/email', bodyParser.json() , (req, res) => {
let Comp = Templates[`Template${req.body.templateId}`];
const filename = `Resume-${req.body.templateId}-${new Date().getTime()}`;
const filePath = path.join(__dirname,`../generated_files/${filename}.pdf`);
Expand All @@ -32,18 +34,44 @@ const AppRoutes = (app, express) => {
.then(() => res.send({ok: true, statusText: 'Email sent successfully'}))
.catch((error) => {
console.error('Error emailing resume', error);
res.status(500).send({ok: false, statusText: 'Error sending resume. Please try again after sometime'});
res.status(502).send({errors: 'Error sending resume. Please try again after sometime'});
});
});

router.get('/template/:id', bodyParser.json() , function(req, res){
router.post('/share', bodyParser.json() , (req, res) => {
const ciphertext = encrypt(req.body.id);
const link = `${req.protocol}://${req.headers.host}/resume/${ciphertext}`;
const share = JSON.stringify(JSON.stringify({link}));
var query = `mutation { update (id: "${req.body.id}", share: ${share}) { id } }`;
graphqlQuery(query).then(() => {
res.send({result: {link}});
}).catch((error) => {
console.error('Error sharing resume', error);
res.status(502).send({errors: 'Error sharing resume. Please try again after sometime'});
});
});

router.get('/resume/:id', (req, res) => {
const decryptedId = decrypt(req.params.id);
var query = `query { resumes (id: "${decryptedId}") { id, resumeid, cvdata, template } }`;
graphqlQuery(query).then((result) => {
let Comp = Templates[`Template${JSON.parse(result.template).id}`];
const html = app.locals.getComponentAsHTML(Comp, JSON.parse(result.cvdata), JSON.parse(result.template).color);
res.send(html);
}).catch((error) => {
console.error('Error getting resume', error);
res.status(502).send({errors: 'Error getting resume. Please try again after sometime'});
});
});

router.get('/template/:id', bodyParser.json() , (req, res) => {
const json = require('../../mock/snehajain.json');
let Comp = Templates[`Template${req.params.id}`];
const html = app.locals.getComponentAsHTML(Comp, json);
res.send(html);
});

router.post('/feedback', bodyParser.json() , function(req, res){
router.post('/feedback', bodyParser.json() , (req, res) => {
messager.sendFeedback(req.body);
mailService.sendFeedback(req.body);
res.sendStatus(204);
Expand Down
4 changes: 2 additions & 2 deletions tools/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ app.locals.renderIndex = (res, data) => {
}, res);
};

app.locals.getComponentAsHTML = (Component, cvdata, designColor) => {
app.locals.getComponentAsHTML = (Component, cvdata, templateColor) => {
try {
return ReactDOMServer.renderToStaticMarkup(<Component data={cvdata} designColor={designColor} />);
return ReactDOMServer.renderToStaticMarkup(<Component data={cvdata} templateColor={templateColor} />);
} catch (e) {
console.error(e);
return '<div>Some Error Occured</div>';
Expand Down
1 change: 1 addition & 0 deletions web/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './analytics';
export * from './cvform/';
export * from './app';
export * from './template';
export * from './share';
6 changes: 6 additions & 0 deletions web/actions/share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const changeShareLink = (link) => ({
type: 'CHANGE_SHARE_LINK',
payload: link
});

export {changeShareLink};
Loading
0