1
1
import { Request , Response } from "express" ;
2
2
import axios from "axios" ;
3
3
import { StatusCodes } from "http-status-codes" ;
4
- import { GetEmailsFromGmailSchema } from "../schemas/mail.schema" ;
5
- import { ConnectedServices , User } from "../models/user.model" ;
4
+ import { GetEmailsFromGmailSchema , PostSendGmailSchema } from "../schemas/mail.schema" ;
5
+ import { ConnectedServices } from "../models/user.model" ;
6
6
import logger from "../utils/logger.util" ;
7
7
import { URLSearchParams } from "url" ;
8
+ import { createTransport , SendMailOptions , Transporter } from "nodemailer" ;
9
+ import DOMPurify from "isomorphic-dompurify" ;
8
10
9
11
/**
10
12
* This function will fetch all the emails from gmail
@@ -18,20 +20,8 @@ export const getEmailsFromGmailHandler = async (
18
20
res : Response ,
19
21
) => {
20
22
try {
21
- const { email } = req . params ;
22
23
const { maxResults, pageToken, q, includeSpamTrash } = req . query ;
23
- const user = res . locals . user as User ;
24
-
25
- // get email accessToken
26
- const foundService = user . connected_services . find (
27
- ( service : ConnectedServices ) => service . email === email ,
28
- ) ;
29
-
30
- if ( ! foundService ) {
31
- return res . status ( StatusCodes . NOT_FOUND ) . json ( {
32
- error : "Account not connected" ,
33
- } ) ;
34
- }
24
+ const currentConnectedService = res . locals . currentConnectedService as ConnectedServices ;
35
25
36
26
const fetchEmailsQueryURL = new URLSearchParams ( {
37
27
maxResults : maxResults || "100" ,
@@ -41,10 +31,12 @@ export const getEmailsFromGmailHandler = async (
41
31
} ) ;
42
32
43
33
const response = await axios . get (
44
- `https://gmail.googleapis.com/gmail/v1/users/${ email } /messages?${ fetchEmailsQueryURL . toString ( ) } ` ,
34
+ `https://gmail.googleapis.com/gmail/v1/users/${
35
+ currentConnectedService . email
36
+ } /messages?${ fetchEmailsQueryURL . toString ( ) } `,
45
37
{
46
38
headers : {
47
- Authorization : `Bearer ${ foundService . access_token } ` ,
39
+ Authorization : `Bearer ${ currentConnectedService . access_token } ` ,
48
40
"Content-type" : "application/json" ,
49
41
} ,
50
42
} ,
@@ -56,11 +48,59 @@ export const getEmailsFromGmailHandler = async (
56
48
size : response . data . messages . length ,
57
49
nextPageToken : response . data . nextPageToken ,
58
50
} ) ;
59
- } catch ( err ) {
60
- logger . error ( err ) ;
51
+ } catch ( err : any ) {
52
+ logger . error ( err . response ) ;
61
53
62
54
return res . status ( StatusCodes . INTERNAL_SERVER_ERROR ) . json ( {
63
55
error : "Internal Server Error" ,
64
56
} ) ;
65
57
}
66
58
} ;
59
+
60
+ /**
61
+ * This controller will send a email from users gmail account
62
+ * @param req express request
63
+ * @param res express response
64
+ *
65
+ * @author aayushchugh
66
+ */
67
+ export const postSendGmailHandler = async (
68
+ req : Request < PostSendGmailSchema [ "params" ] , { } , PostSendGmailSchema [ "body" ] > ,
69
+ res : Response ,
70
+ ) => {
71
+ const { to, subject, html } = req . body ;
72
+ const currentConnectedService = res . locals . currentConnectedService as ConnectedServices ;
73
+
74
+ try {
75
+ const cleanedHTML = DOMPurify . sanitize ( html ) ;
76
+
77
+ const transporter : Transporter = createTransport ( {
78
+ service : "gmail" ,
79
+ auth : {
80
+ type : "OAuth2" ,
81
+ user : currentConnectedService . email ,
82
+ clientId : process . env . GOOGLE_CLIENT_ID ,
83
+ clientSecret : process . env . GOOGLE_CLIENT_SECRET ,
84
+ refreshToken : currentConnectedService . refresh_token ,
85
+ accessToken : currentConnectedService . access_token ,
86
+ } ,
87
+ } ) ;
88
+
89
+ const mailOptions : SendMailOptions = {
90
+ from : currentConnectedService . email ,
91
+ to,
92
+ subject,
93
+ html : cleanedHTML ,
94
+ } ;
95
+
96
+ await transporter . sendMail ( mailOptions ) ;
97
+
98
+ return res . status ( StatusCodes . OK ) . json ( {
99
+ message : "Email sent successfully" ,
100
+ } ) ;
101
+ } catch ( err ) {
102
+ return res . status ( StatusCodes . INTERNAL_SERVER_ERROR ) . json ( {
103
+ error : "Internal Server Error" ,
104
+ } ) ;
105
+ }
106
+ } ;
0 commit comments