Created
November 1, 2019 16:15
-
-
Save deresegetachew/f4babb2cbf31c55855df70a7bf46b23a to your computer and use it in GitHub Desktop.
sendInvitation: showcasing using mongoose . The Transaction considers the success of send email message to Rabbit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sendInvitation = async (parent, { invite }, ctx, info) => { | |
try { | |
const blackListEmail = req.user.email; | |
await isInviteToSendValid(invite, blackListEmail); | |
} catch (err) { | |
return formatYupError(err); | |
} | |
let { companyName, email, message } = invite; | |
//check if an invitation is already sent | |
const db_session = await global.dbConnection.startSession(); | |
db_session.startTransaction(); | |
try { | |
let inviteToSave = { | |
companyName: invite.companyName, | |
message: invite.message, | |
email: invite.email, | |
status: 'Pending', | |
dateModified: moment().format('MMM DD YY') | |
}; | |
let invitingCompany = await Company.find({ | |
_id: ObjectId(req.user.company) | |
}) | |
.select( | |
'invitations businessType businessArea companyIsVerified country companyName myAgents myImporters myVendors image' | |
) | |
.session(db_session) | |
.exec(); | |
if (invitingCompany) { | |
let existingInvitation = find(invitingCompany.invitations, function( | |
invite | |
) { | |
return String(invite.email) == String(email); | |
}); | |
if (existingInvitation) { | |
if ( | |
existingInvitation.status == 'Confirmed' || | |
existingInvitation.status == 'Cancelled' | |
) { | |
await db_session.abortTransaction(); | |
return [ | |
errors.inviteAlreadyCancelledOrConfirmed({ | |
companyName: existingInvitation.companyName, | |
status: existingInvitation.status | |
}) | |
]; | |
} else { | |
existingInvitation.dateModified = moment().format('MMM DD YY'); | |
} | |
} else { | |
//new Invitation | |
invitingCompany.invitations.push(inviteToSave); | |
} | |
} else { | |
await db_session.abortTransaction(); | |
return [errors.errorSendingInvitation()]; | |
} | |
let save_invitation = await invitingCompany.save({ session: db_session }); | |
if (!save_invitation) { | |
await db_session.abortTransaction(); | |
return [errors.errorSendingInvitation()]; | |
} else { | |
//send email and commit the transaction. | |
let a = | |
base_url_web + `register/invite/${companyId}/${inviteToSave.email}`; | |
let accept_a = | |
base_url_web + | |
`register/existing/${invitingCompany._id}/${inviteToSave.email}/accept`; | |
let decline_a = | |
base_url_web + | |
`register/existing/${invitingCompany._id}/${inviteToSave.email}/decline`; | |
let companyLogo = invitingCompany.image; | |
let rfpiCount = 0; | |
let piCount = 0; | |
if (invitingCompany.businessType == 'Importer') { | |
rfpiCount = await PI.countDocuments({ importerId: req.user.company }); | |
piCount = await PI.countDocuments({ | |
importerId: req.user.company, | |
status: { $eq: 'Converted to PI' } | |
}); | |
} else if (invitingCompany.businessType == 'Exporter') { | |
rfpiCount = await PI.countDocuments({ vendorId: req.user.company }); | |
piCount = await PI.countDocuments({ | |
vendorId: req.user.company, | |
status: { $eq: 'Converted to PI' } | |
}); | |
} | |
let payload = { | |
email: inviteToSave.email, | |
subject: `Invitation to join or Connect on LightTrading Platform`, | |
invited: inviteToSave.companyName, | |
inviter: invitingCompany.companyName, | |
inviterBussType: invitingCompany.businessType, | |
inviterBussArea: invitingCompany.businessArea, | |
inviterConnections: | |
invitingCompany.myAgents.length + | |
invitingCompany.myVendors.length + | |
invitingCompany.myImporters.length, | |
inviterRFPI: rfpiCount, | |
inviterPI: piCount, | |
inviterCompletedOrder: 0, | |
inviterReview: 0, | |
inviterVerified: invitingCompany.companyIsVerified, | |
inviterBussCountry: invitingCompany.country, | |
message: inviteToSave.message, | |
logo: companyLogo, | |
link: a, | |
link_accept: accept_a, | |
link_decline: decline_a, | |
eventId: new mongoose.mongo.ObjectId(), | |
dateCreated: moment(), | |
eventType: eventTypeEnums.invitationSent, | |
actionUrl: '', | |
summary: `Invitation sent to ${inviteToSave.companyName} to connect on light trading.` | |
}; | |
let paramRabbit = { | |
chanelName: ltChanel.chanelName, | |
exchange: eventTypeEnums.invitationSent, //event name | |
routingKey: | |
eventTypeEnums.invitationSent + | |
'.' + | |
invitingCompany._id.toString() + | |
'.*' | |
}; | |
let send_email = await publishToRabbit( | |
paramRabbit, | |
new Buffer(JSON.stringify(payload)), | |
{ persistent: true } | |
); | |
if (send_email) { | |
await db_session.commitTransaction(); | |
return inviteToSave; | |
} else { | |
await db_session.abortTransaction(); | |
return [errors.errorSendingInvitation()]; | |
} | |
} | |
} catch (e) { | |
await db_session.abortTransaction(); | |
await db_session.endSession(); | |
throw e.message; | |
} finally { | |
await db_session.endSession(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment