|
1 | 1 | const { google } = require('googleapis');
|
2 | 2 | const inviteStudent = require('../model/quires/invite_student');
|
| 3 | +const emailSetup = require('./email-setup'); |
3 | 4 | require('env2')('./config.env');
|
4 | 5 |
|
5 |
| -const oauth2Client = new google.auth.OAuth2( |
6 |
| - process.env.GMAILCLIENTID, |
7 |
| - process.env.GMAILSECRETE, |
8 |
| - 'http://localhost:3000/gmail/cb' |
9 |
| -); |
| 6 | +const gmailCbUrl = process.env.ENV === 'production' ? 'https://facgtracker.herokuapp.com/gmail/cb' : 'http://localhost:3000/gmail/cb'; |
10 | 7 | const scopes = [
|
11 | 8 | 'https://www.googleapis.com/auth/gmail.send'
|
12 | 9 | ];
|
13 |
| -const url = oauth2Client.generateAuthUrl({ |
14 |
| - access_type: 'online', |
15 |
| - response_type: 'code', |
16 |
| - scope: scopes |
17 |
| -}); |
| 10 | +const cohortId = 1; |
18 | 11 | let studentEmail;
|
19 |
| -exports.get = (req, res) => { |
20 |
| - const cohortId = 1; |
21 |
| - inviteStudent.getunregisteredStudents(cohortId, (err, emails) => { |
22 |
| - if (err) { |
23 |
| - console.error('get unregistered students', err); |
| 12 | + |
| 13 | +exports.get = (req, res, next) => { |
| 14 | + inviteStudent.getunregisteredStudents(cohortId, (getUnregisteredError, emails) => { |
| 15 | + if (getUnregisteredError) { |
| 16 | + console.error('get unregistered students', getUnregisteredError); |
| 17 | + next(getUnregisteredError); |
| 18 | + } else { |
| 19 | + res.render('inviteSt', { |
| 20 | + emails, |
| 21 | + style: [ |
| 22 | + 'manage_student_style.css', 'invite_students.css' |
| 23 | + ], |
| 24 | + script: [ |
| 25 | + 'manage_st_dom.js', |
| 26 | + 'invite_st.js' |
| 27 | + ] |
| 28 | + }); |
24 | 29 | }
|
25 |
| - res.render('inviteSt', { emails, style: ['manage_student_style.css', 'invite_students.css'], script: ['manage_st_dom.js', 'invite_st.js'] }); |
26 | 30 | });
|
27 | 31 | };
|
28 | 32 |
|
29 |
| -exports.getcode = (req, res) => { |
30 |
| - studentEmail = req.body.studentEmail; |
31 |
| - res.redirect(url); |
32 |
| -}; |
33 |
| -exports.gettoken = (req, res) => { |
34 |
| - const cohortId = 1; |
| 33 | +exports.getcode = (req, res, next) => { |
| 34 | + studentEmail = req.body.email; |
35 | 35 | inviteStudent.checkInviteEmail(studentEmail, (checkInviteEmailError, exist) => {
|
36 | 36 | if (checkInviteEmailError) {
|
37 |
| - // handel error |
38 | 37 | console.error('checkInviteEmail error', checkInviteEmailError);
|
| 38 | + next(checkInviteEmailError); |
39 | 39 | } else if (exist[0].exists) {
|
40 |
| - // sweet alert email already exist |
41 |
| - console.log('checkInviteEmail', exist); |
42 |
| - res.redirect('/inviteSt'); |
| 40 | + res.send({ exist: 'The email exist' }); |
43 | 41 | } else {
|
44 |
| - const githubloginLink = 'http://localhost:3000/github'; |
45 |
| - oauth2Client.getToken(req.query.code, (err, tokens) => { |
46 |
| - console.log('tokens', tokens); |
| 42 | + const data = { |
| 43 | + client_id: process.env.GMAILCLIENTID, |
| 44 | + redirect_uri: gmailCbUrl, |
| 45 | + response_type: 'code', |
| 46 | + scope: scopes[0] |
| 47 | + }; |
| 48 | + res.send(data); |
| 49 | + } |
| 50 | + }); |
| 51 | +}; |
| 52 | + |
| 53 | +exports.gettoken = (req, res, next) => { |
| 54 | + const oauth2Client = new google.auth.OAuth2( |
| 55 | + process.env.GMAILCLIENTID, |
| 56 | + process.env.GMAILSECRETE, |
| 57 | + gmailCbUrl |
| 58 | + ); |
| 59 | + const githubloginLink = 'https://facgtracker.herokuapp.com/login'; |
| 60 | + if (!req.query.code) { |
| 61 | + console.error('No auth code'); |
| 62 | + next('No autherisation code'); |
| 63 | + } else { |
| 64 | + oauth2Client.getToken(req.query.code, (err, tokens) => { |
| 65 | + if (err) { |
| 66 | + console.error('getTokenerr', err); |
| 67 | + next(err); |
| 68 | + } else { |
47 | 69 | oauth2Client.setCredentials(tokens);
|
48 | 70 | const gmail = google.gmail('v1');
|
49 |
| - const emailLines = []; |
50 |
| - emailLines.push('From: [email protected]'); |
51 |
| - emailLines.push(`To: ${studentEmail}`); |
52 |
| - emailLines.push('Content-type: text/html;charset=iso-8859-1'); |
53 |
| - emailLines.push('MIME-Version: 1.0'); |
54 |
| - emailLines.push('Subject: Invitation for facgtracker'); |
55 |
| - emailLines.push(''); |
56 |
| - emailLines.push('<h2>Invitation to sign up at facgtracker</h2>'); |
57 |
| - emailLines.push('<b>Hello facg5 students</b><br>'); |
58 |
| - emailLines.push(`you are invited to Set up your account at facgtracker web app, please use<br> this <a href="${githubloginLink}">link</a> to log in with your github account that associated with this email ${studentEmail}`); |
59 |
| - emailLines.push('<br>if you have any trouble loging in please reply to this email'); |
60 |
| - const email = emailLines.join('\r\n').trim(); |
61 |
| - let base64EncodedEmail = Buffer.from(email).toString('base64'); |
62 |
| - base64EncodedEmail = base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_'); |
| 71 | + const base64EncodedEmail = emailSetup.email(studentEmail, 'FACG4', githubloginLink); |
63 | 72 | gmail.users.messages.send({
|
64 | 73 | userId: 'me',
|
65 | 74 | auth: oauth2Client,
|
66 | 75 | resource: {
|
67 | 76 | raw: base64EncodedEmail
|
68 | 77 | }
|
69 |
| - }, (err1, result) => { |
70 |
| - if (err1) { |
71 |
| - console.log('send gmail api error', err1); |
| 78 | + }, (sendGmailError, result) => { |
| 79 | + if (sendGmailError) { |
| 80 | + console.error('send gmail api error', sendGmailError); |
| 81 | + next(sendGmailError); |
72 | 82 | } else {
|
73 | 83 | console.log('done', result.data.labelIds);
|
74 |
| - // sweet alert (email has been sent to ${studentEmail}) |
75 | 84 | inviteStudent.insertInviteEmail(studentEmail, 'student', cohortId, (insertInviteEmailError, insertResponse) => {
|
76 | 85 | if (insertInviteEmailError) {
|
77 |
| - // sweet alert (email haven't been add to database, please try again) |
78 | 86 | console.error('insertInviteEmailError', insertInviteEmailError);
|
| 87 | + next(insertInviteEmailError); |
79 | 88 | } else {
|
80 |
| - console.log('insertEmailToDb ', insertResponse); |
81 | 89 | res.redirect('/inviteSt');
|
82 | 90 | }
|
83 | 91 | });
|
84 | 92 | }
|
85 | 93 | });
|
86 |
| - }); |
87 |
| - } |
88 |
| - }); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
89 | 97 | };
|
0 commit comments