Skip to content

Commit

Permalink
Merge pull request #74 from devplus-project/feat/offer-sendMail
Browse files Browse the repository at this point in the history
Feat/offer-sendMail
  • Loading branch information
quynguyen20072000 authored Sep 21, 2022
2 parents 28ac555 + 0f3c150 commit f1402c7
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
10 changes: 10 additions & 0 deletions configs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const acceptedLink = process.env.ACCEPTED_LINK;
const rejectedLink = process.env.REJECTED_LINK;
const frontendUrl = process.env.FRONTEND_URL;

module.exports = {
acceptedLink,
rejectedLink,
frontendUrl,
};
47 changes: 46 additions & 1 deletion src/api/v1/offer/offer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {
} = require('./offer.service');
const { createOffersValidate } = require('./offer.validate');

const { sendMailOfferService, findOfferService, changeOfferStatusService } = require('./offer.service');
const { secretKey, frontendUrl } = require('../../../configs/index');

const createOffer = async (req, res) => {
try {
var { cvs, content, startDate } = req.body;
Expand All @@ -29,6 +32,46 @@ const createOffer = async (req, res) => {
}
};

const sendMailOffer = async (req, res) => {
const request = req.body
for (item of request) {
try {
const offer = await findOfferService(item)
if (!offer) {
return res.status(404).json({ message: "Offer is not exists" })
}

const link = `${frontendUrl}/offers/accepted/${offer._id}`;

sendMailOfferService(offer, link);

return res.status(200).json({
message: 'Send email successfully',
});
} catch (error) {
return res.status(500).json({ message: error.message });
}
}
};

const changeOfferStatus = async (req, res) => {
try {

const offer = await findOfferService(req.body)
if (!offer) {
return res.status(404).json({ message: "This offer is not exists" })
}

changeOfferStatusService({ id: offer._id, status: offer.status })

return res.status(200).json({
message: 'Change offer status successfully',
});
} catch (error) {
return res.status(500).json({ message: error.message });
}
};

const sendMailRejectOffer = async (req, res) => {
try {
sendMailRejectOfferService(req.body);
Expand All @@ -41,4 +84,6 @@ const sendMailRejectOffer = async (req, res) => {
}
};

module.exports = { createOffer, sendMailRejectOffer };
module.exports = {
sendMailOffer, changeOfferStatus, createOffer, sendMailRejectOffer
};
1 change: 0 additions & 1 deletion src/api/v1/offer/offer.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ offerSchema.plugin(mongoosePaginate);
offerSchema.plugin(mongooseDelete);

const offer = mongoose.model('Offer', offerSchema);

module.exports = offer;
14 changes: 11 additions & 3 deletions src/api/v1/offer/offer.route.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const express = require('express');
const router = express.Router();

const { createOffer, sendMailRejectOffer } = require('./offer.controller');
const {
sendMailOffer,
changeOfferStatus,
createOffer,
sendMailRejectOffer
} = require('./offer.controller');

router.post('/', createOffer);

router.post('/send-mail-reject-offer', sendMailRejectOffer);

module.exports = router;
router.post('/send-mail-offer', sendMailOffer);

router.put('/update-status/:id', changeOfferStatus);

module.exports = router;
43 changes: 38 additions & 5 deletions src/api/v1/offer/offer.service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const { sendMail } = require('../services/mailer');
const { mailTemplates } = require('../../../configs/mailer');
const { replaceContent } = require('../helpers');

const Offer = require('./offer.model');

const { rejectOffer } = mailTemplates;
const { rejectOffer, offerConfirm } = mailTemplates;

const createOffersService = async (cvs, detail) => {
const { content, startDate } = detail;

const cvsDetail = cvs.map((cv) => {
return {
cv: cv.cvId,
Expand Down Expand Up @@ -36,7 +33,43 @@ const sendMailRejectOfferService = (emails) => {
}
};

const sendMailOfferService = async (offer, link) => {

const mailParams = {
mailTo: offer.email,
subject: offerConfirm.subject,
content: replaceContent(offerConfirm.content, {
name: offer.name,
link: link,
content: offer.content
}),
};
sendMail(mailParams);

}

const findOfferService = async (data) => {
return await Offer.findOne({ email: data.email })
}

const changeOfferStatusService = async (id, status) => {
const changeStatus = await User.updateOne(
{
_id: id,
},
{
$set: {
status: status,
},
}
);
return changeStatus;
}

module.exports = {
sendMailRejectOfferService,
sendMailOfferService,
findOfferService,
changeOfferStatusService,
createOffersService,
sendMailRejectOfferService,
};
11 changes: 11 additions & 0 deletions src/configs/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const mailTemplates = {
We wish you good luck with your search and future endeavors. </p>
`,
},
offerConfirm: {
subject: '[DEVPLUS] INTERNSHIP OFFER LETTER',
content: `
<p><b>Hi \${this.name}</b>,</p>
<p>Đã Trúng tuyển váo công ty chúng tôi.......</p>
<p>\${this.content}</p>
<p>Please click this to confirm:</p>
<a style='font-family:"Proxima Nova","proxima-nova",Helvetica,Arial sans-serif;color:rgb(0,105,255);text-align:center;display:inline-block;width:150px;padding:10px 0;margin:20px 5px 0 5px;color:#fff;font-size:14px;text-decoration:none;border-width:1px;border-style:solid;border-color:#0069ff;background-color:#0069ff' href='\${this.link}}'>Accept</a>
<a style='font-family:"Proxima Nova","proxima-nova",Helvetica,Arial sans-serif;color:rgb(0,105,255);text-align:center;display:inline-block;width:150px;padding:10px 0;margin:20px 5px 0 5px;color:#fff;font-size:14px;text-decoration:none;border-width:1px;border-style:solid;border-color:#0069ff;background-color:#0069ff' href='\${this.link}'>Reject</a>
`,
},
};

module.exports = {
Expand Down

0 comments on commit f1402c7

Please sign in to comment.