Skip to content

Commit 82c8e90

Browse files
envio de mails desde opportunidaedes
1 parent f0a4b28 commit 82c8e90

File tree

8 files changed

+135
-29
lines changed

8 files changed

+135
-29
lines changed

Diff for: package-lock.json

+11-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"morgan": "1.10.0",
2222
"multer": "1.4.5-lts.1",
2323
"node-cron": "3.0.3",
24+
"nodemailer": "^6.9.9",
2425
"nodemon": "3.0.3",
2526
"path": "0.12.7",
2627
"pg": "8.11.3",
@@ -30,4 +31,4 @@
3031
"swagger-ui-express": "5.0.0",
3132
"uuid": "9.0.1"
3233
}
33-
}
34+
}

Diff for: src/controllers/sendMail/sendMail.controller.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { sendMailService } = require("../../services/sendMail/sendMail.service");
2+
3+
const sendMailController = async (req, res) => {
4+
try {
5+
const { from, to, subject, text } = req.body;
6+
if (!to || !subject || !text) {
7+
return res.status(400).json({ error: 'Faltan datos' });
8+
}
9+
10+
const result = await sendMailService(to, subject, text);
11+
if (result.success) {
12+
res.status(200).json(result);
13+
} else {
14+
res.status(500).json({ error: error.message });
15+
}
16+
}
17+
catch (error) {
18+
return res.status(401).json({ error: error.message });
19+
}
20+
}
21+
22+
module.exports = { sendMailController }

Diff for: src/routes/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const opportunitiesRouter = require("./opportunities.route.js");
88
const paymentsRouter = require("./payments.route.js");
99
const chatsRouter = require("./chats.route.js");
1010
const questionsRouter = require("./questions.route.js");
11-
const statsRouter = require("./stats.route.js")
11+
const statsRouter = require("./stats.route.js");
12+
const sendMailRouter = require("./sendMail.route.js");
1213

1314
router.use(categoriesRouter);
1415
router.use(peopleRouter);
@@ -18,5 +19,6 @@ router.use(paymentsRouter);
1819
router.use(chatsRouter);
1920
router.use(questionsRouter);
2021
router.use(statsRouter);
22+
router.use(sendMailRouter)
2123

2224
module.exports = router;

Diff for: src/routes/sendMail.route.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { Router } = require("express");
2+
const { sendMailController } = require("../controllers/sendMail/sendMail.controller");
3+
4+
5+
const sendMailRouter = Router();
6+
7+
sendMailRouter.post("/sendmail", sendMailController);
8+
9+
module.exports = sendMailRouter;

Diff for: src/services/opportunities/getOpportunities.service.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,19 @@ const getOpportunitiesService = async (params) => {
7777
order: orders
7878
}
7979

80-
if (idProvider) {
81-
options.include = [
82-
{
83-
model: People,
84-
attributes: ['fullName', 'image'],
85-
as: 'customer'
86-
}]
87-
}
88-
else {
89-
if (idCustomer) {
90-
options.include = [
91-
{
92-
model: People,
93-
attributes: ['fullName', 'image'],
94-
as: 'provider'
95-
}]
96-
}
97-
}
80+
options.include = [
81+
{
82+
model: People,
83+
attributes: ['fullName', 'image'],
84+
as: 'customer'
85+
}]
86+
options.include.push(
87+
{
88+
model: People,
89+
attributes: ['fullName', 'image'],
90+
as: 'provider'
91+
})
92+
9893
let result = await Opportunities.findAll(options)
9994

10095
const count = result.length;
@@ -107,8 +102,8 @@ const getOpportunitiesService = async (params) => {
107102
filter: filters,
108103
data: result
109104
}
110-
return { opportunities };
111-
105+
return { opportunities };
106+
112107
} catch (error) {
113108
throw error
114109
}

Diff for: src/services/opportunities/putOpportunities.services.js

+42-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { putPeopleService } = require('../people/putPeople.service');
88

99
const postChatsService = require('../chats/postChats.service');
1010
const { formatDate } = require('../../utils/formatDate');
11+
const { sendMailService } = require('../sendMail/sendMail.service');
1112

1213
const CHAT_MESSAGE_PENDING = `Lo he contratado para el %DIA% a las %HORA% hora(s), por %DURACION% hora(s) de %SERVICIO% por un valor de $%PRECIO%`
1314

@@ -41,6 +42,11 @@ const putOpportunitiesService = async (params) => {
4142

4243
const people = await People.findByPk(idPeople);
4344

45+
const customer = await People.findByPk(opportunitie.idCustomer);
46+
const provider = await People.findByPk(opportunitie.idProvider);
47+
48+
//busco el servicio
49+
const service = await Categories_options.findByPk(opportunitie.idService)
4450
let newChat = {}
4551
let message = ''
4652

@@ -61,6 +67,13 @@ const putOpportunitiesService = async (params) => {
6167
isRated: false
6268
}
6369
postChatsService(newChat)
70+
//enviarMail
71+
sendMailService(
72+
people.typeOfPerson === USER_CUSTOMER ? provider.email : customer.email, //to
73+
'El servicio fue Cancelado!',
74+
`El servicio ${service.description} del dia ${formatDate(dateOfService)} fue cancelado!`
75+
)
76+
6477

6578
} else {
6679
switch (opportunitie.state) {
@@ -80,9 +93,6 @@ const putOpportunitiesService = async (params) => {
8093
opportunitie.durationOfService = durationOfService
8194
opportunitie.state = STATE_PENDING
8295

83-
//busco el servicio
84-
const service = await Categories_options.findByPk(idService)
85-
console.log(service.dataValues.description)
8696

8797
//envio automaticamente el chat
8898

@@ -93,12 +103,20 @@ const putOpportunitiesService = async (params) => {
93103
.replace('%DIA%', formatDate(dateOfService))
94104
.replace('%HORA%', timeOfService)
95105
.replace('%DURACION%', durationOfService)
96-
.replace('%SERVICIO%', service.dataValues.description)
106+
.replace('%SERVICIO%', service.description)
97107
.replace('%PRECIO%', price),
98108
isRating: false,
99109
isRated: false
100110
}
101111
postChatsService(newChat)
112+
//enviarMail
113+
newChat.message = newChat.message +
114+
`\nMi nombre es ${customer.fullName}, por favor ingrese en https://carewithlove.onrender.com para confirmar la contratacion`
115+
sendMailService(
116+
people.typeOfPerson === USER_CUSTOMER ? provider.email : customer.email, //to
117+
'Servicio contratado!',
118+
newChat.message
119+
)
102120

103121
break;
104122

@@ -120,12 +138,22 @@ const putOpportunitiesService = async (params) => {
120138
newChat = {
121139
idOpportunitie: opportunitie.idOpportunitie,
122140
idPeople: opportunitie.idCustomer,
123-
message: 'Por favor ingrese su evaluacion',
141+
message: 'Por favor ingrese su evaluacion',
124142
isRating: true,
125143
isRated: false
126144
}
127145
postChatsService(newChat)
128146

147+
//enviar mail
148+
sendMailService(
149+
customer.email,
150+
'Evaluacion de servicio',
151+
`Por favor evalue el servicio de ${service.description} realizado por ${provider.fullName}\n` +
152+
`ingresando en https://carewithlove.onrender.com\n\n` +
153+
`Muchas Gracias !!!`
154+
)
155+
156+
//enviar chat
129157
newChat = {
130158
idOpportunitie: opportunitie.idOpportunitie,
131159
idPeople: opportunitie.idProvider,
@@ -134,6 +162,15 @@ const putOpportunitiesService = async (params) => {
134162
isRated: false
135163
}
136164
postChatsService(newChat)
165+
//enviar mail
166+
sendMailService(
167+
provider.email,
168+
'Evaluacion de servicio',
169+
`Por favor evalue a ${customer.fullName} por el servicio de ${service.description}\n` +
170+
`ingresando en https://carewithlove.onrender.com\n\n` +
171+
`Muchas Gracias !!!`
172+
)
173+
137174
break;
138175

139176
case STATE_RATINGPENDING:

Diff for: src/services/sendMail/sendMail.service.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const nodemailer = require('nodemailer');
2+
const { MAIL_HOST, MAIL_PORT, MAIL_SECURE, MAIL_USER, MAIL_PASS ,MAIL_NAME} = process.env;
3+
// Configuracion de servidor SMTP
4+
const transporter = nodemailer.createTransport({
5+
host: MAIL_HOST,
6+
port: MAIL_PORT,
7+
secure: MAIL_SECURE,
8+
auth: {
9+
user: MAIL_USER,
10+
pass: MAIL_PASS
11+
}
12+
});
13+
14+
const sendMailService = async ( to, subject, text) => {
15+
console.log(to,subject,text)
16+
const mailOptions = {
17+
from: `"${MAIL_NAME}" <${MAIL_USER}`, // Remitente
18+
to: to,
19+
subject: subject,
20+
text: text
21+
};
22+
23+
try {
24+
const info = await transporter.sendMail(mailOptions);
25+
return { success: true, message: 'Correo electrónico enviado correctamente' };
26+
} catch (error) {
27+
throw error
28+
}
29+
}
30+
31+
module.exports = { sendMailService };

0 commit comments

Comments
 (0)