diff --git a/models/camp.js b/models/camp.js index 56b80e386..df42ee086 100644 --- a/models/camp.js +++ b/models/camp.js @@ -37,7 +37,6 @@ var Camp = bookshelf.Model.extend({ users[i].can_remove = ['rejected', 'pending_mgr',].indexOf(_status) > -1; users[i].can_approve = ['pending', 'rejected'].indexOf(_status) > -1 && users[i].validated; users[i].can_reject = ['pending', 'approved'].indexOf(_status) > -1 && _this.attributes.main_contact !== users[i].user_id; - if (((_this.attributes.main_contact === users[i].user_id || common.__hasRole('camp_manager', users[i].roles)) && users[i].member_status === 'approved') || (users[i].member_status === 'approved_mgr')) { @@ -58,10 +57,10 @@ var Camp = bookshelf.Model.extend({ } } }, - isUserInCamp: function (user_id) { + isUserInCamp: function (user_id, include_deleted) { user_id = parseInt(user_id); for (var i in this.attributes.users) { - if (this.attributes.users[i].user_id === user_id && this.attributes.users[i].member_status !== 'deleted') { + if (this.attributes.users[i].user_id === user_id && (this.attributes.users[i].member_status !== 'deleted' || include_deleted)) { return this.attributes.users[i]; } } @@ -69,7 +68,7 @@ var Camp = bookshelf.Model.extend({ isUserCampMember: function (user_id) { user_id = parseInt(user_id); for (var i in this.attributes.users) { - if (this.attributes.users[i].user_id === user_id && ['approved','approved_mgr'].indexOf(this.attributes.users[i].member_status)>-1) { + if (this.attributes.users[i].user_id === user_id && ['approved', 'approved_mgr'].indexOf(this.attributes.users[i].member_status) > -1) { return this.attributes.users[i]; } } diff --git a/models/user.js b/models/user.js index 9f22088c3..b3547f072 100644 --- a/models/user.js +++ b/models/user.js @@ -52,9 +52,10 @@ var User = bookshelf.Model.extend({ .innerJoin(_camps_members, _camps + '.id', _camps_members + '.camp_id') .where({ user_id: this.attributes.user_id, event_id: constants.CURRENT_EVENT_ID, __prototype: constants.prototype_camps.THEME_CAMP.id }) .then((camps) => { - var first_camp; + var first_camp = null; var is_manager = false; var member_type_array = ['approved', 'pending', 'pending_mgr', 'approved_mgr', 'supplier']; + // var _camps = []; for (var i in camps) { let _status = camps[i].member_status; if (t !== undefined) { // translate function @@ -71,6 +72,7 @@ var User = bookshelf.Model.extend({ is_manager = true; break; } + // _camps.push(camps[i]); } _this_user.attributes.camps = camps; _this_user.attributes.camp = first_camp; @@ -79,7 +81,6 @@ var User = bookshelf.Model.extend({ done(camps); }); }, - validPassword: function (password) { return bcrypt.compareSync(password, this.attributes.password); }, diff --git a/routes/api_camps_routes.js b/routes/api_camps_routes.js index 4f424564e..7be3b3521 100644 --- a/routes/api_camps_routes.js +++ b/routes/api_camps_routes.js @@ -7,6 +7,151 @@ const knex = require('../libs/db').knex; const userRole = require('../libs/user_role'); var mail = require('../libs/mail'), mailConfig = config.get('mail'); +var emailDeliver = (recipient, subject, template, props) => { + /** + * Deliver email request to camp manager + * notifiying a user wants to join his camp + * @return {boolean} should return true if mail delivered. FIXME: in mail.js + */ + console.log('Trying to send mail to ' + recipient + ' from ' + mailConfig.from + ': ' + subject + ', template ' + template); + mail.send( + recipient, + mailConfig.from, + subject, + template, props + ) +} +var __camps_update_status = (camp_id, user_id, action, camp_mgr, res) => { + var isAdmin = false; + var camp_mgr_id; + if (camp_mgr instanceof User) { + camp_mgr_id = camp_mgr.id; + isAdmin = camp_mgr.isAdmin; + } else { + camp_mgr_id = parseInt(camp_mgr); + } + console.log(action + " from camp " + camp_id + " of user " + user_id + " / mgr id: " + camp_mgr_id); + Camp.forge({ id: camp_id }).fetch().then((camp) => { + camp.getCampUsers((users) => { + var new_status; + var save_method = { require: true }; + var mail_delivery = { + template: '', + subject: '', + to_mail: '', + }; + var user = camp.isUserInCamp(user_id, true); + var camp_manager = camp.isCampManager(camp.attributes.main_contact); + // camp manager commands + if (action === 'approve_new_mgr' && (camp_mgr_id === camp.attributes.main_contact || isAdmin)) { + new_status = 'approved'; + if (!user) { + save_method.require = false; + save_method.method = 'insert'; + } + } else if (camp.isCampManager(camp_mgr_id) || isAdmin) { + if (user && action === "approve" && user.can_approve) { + mail_delivery.to_mail = user.email; + mail_delivery.subject = 'Spark: you have been approved!'; + mail_delivery.template = 'emails/camps/member_approved'; + new_status = 'approved'; + } else if (user && action === "remove" && user.can_remove) { + new_status = 'deleted'; + } else if (user && action === "reject" && user.can_reject) { + mail_delivery.to_mail = user.email; + mail_delivery.subject = 'Spark: you have been Rejected!'; + mail_delivery.template = 'emails/camps/member_rejected'; + new_status = 'rejected'; + } else if (user && action === "revive") { + new_status = 'pending'; + } else if (action === "request_mgr") { + new_status = 'pending_mgr'; + mail_delivery.to_mail = ''; + mail_delivery.subject = 'Spark: you have been requested to join camp'; + mail_delivery.template = 'emails/camps/member_request'; + if (!user) { + save_method.require = false; + save_method.method = 'insert'; + } else if (user.member_status === 'approved') { + new_status = null; + } + } + } else { + if (action === 'join') { + new_status = 'pending'; + if (camp_manager) { + mail_delivery.subject = 'Spark: Someone wants to join your camp!'; + mail_delivery.template = 'emails/camps/join_request'; + mail_delivery.to_mail = camp_manager.email; + } + if (!user) { + save_method.require = false; + save_method.method = 'insert'; + } else if (user.member_status === 'approved') { + new_status = null; + } + } else if (action === 'join_mgr' && user && user.member_status === 'pending_mgr' && camp_mgr_id === user_id) { + new_status = 'approved'; + if (camp_manager) { + mail_delivery.subject = 'Spark: A member approved himself to your camp!'; + mail_delivery.template = 'emails/camps/member_approved'; + mail_delivery.to_mail = camp_manager.email; + } + } else if (action === 'join_cancel' && user && user.member_status !== 'deleted' && camp_mgr_id === user_id) { + new_status = 'deleted'; + if (camp_manager) { + mail_delivery.subject = 'Spark: ' + user.email + ' canceled himself from your camp!'; + mail_delivery.template = 'emails/camps/join_cancel'; + mail_delivery.to_mail = camp_manager.email; + } + } + } + if (new_status) { + var data = { + camp_id: camp.attributes.id, + user_id: user_id, + status: new_status + }; + var query = ''; + if (save_method.method === 'insert') { + query = knex(constants.CAMP_MEMBERS_TABLE_NAME).insert(data).toString(); + } else { + query = 'UPDATE ' + constants.CAMP_MEMBERS_TABLE_NAME + ' SET status="' + data.status + '" WHERE camp_id=' + data.camp_id + ' AND user_id=' + data.user_id + ';'; + } + var _after_update = () => { + console.log(action + " from camp " + data.camp_id + " of user " + data.user_id + " / status: " + data.status); + if (mail_delivery.template !== '') { + // let props={}; + if (mail_delivery.to_mail !== '') { + emailDeliver(mail_delivery.to_mail, mail_delivery.subject, mail_delivery.template, { user: user, camp: camp.toJSON(), camp_manager: camp_manager }); // notify the user + } else { + User.forge({ user_id: user_id }).fetch().then((user) => { + emailDeliver(user.attributes.email, mail_delivery.subject, mail_delivery.template, { user: user.toJSON(), camp: camp.toJSON(), camp_manager: camp_manager }); // notify the user + }); + } + } + var res_data = { data: { member: data } }; + if (action === 'approve_new_mgr') { + res_data.data.message = 'camp created'; + res_data.data.camp_id = camp_id; + } + res.status(200).json(res_data); + + } + knex.raw(query).then(_after_update); + } else { + res.status(404).json({ error: true, data: { message: "Cannot execute this command." } }); + } + }); + }).catch((e) => { + res.status(500).json({ + error: true, + data: { + message: e.message + } + }) + }); +} module.exports = (app, passport) => { /** @@ -86,7 +231,7 @@ module.exports = (app, passport) => { } } if (isNew) { - data.created_at =(new Date()).toISOString().substring(0, 19).replace('T', ' '); + data.created_at = (new Date()).toISOString().substring(0, 19).replace('T', ' '); } if (isNew || req.user.isAdmin) { __update_prop('camp_name_en'); @@ -206,137 +351,6 @@ module.exports = (app, passport) => { }); }); - __camps_update_status = (camp_id, user_id, action, camp_mgr, res) => { - var isAdmin = false; - var camp_mgr_id; - if (camp_mgr instanceof User) { - camp_mgr_id = camp_mgr.id; - isAdmin = camp_mgr.isAdmin; - } else { - camp_mgr_id = parseInt(camp_mgr); - } - console.log(action + " from camp " + camp_id + " of user " + user_id + " / mgr id: " + camp_mgr_id); - Camp.forge({ id: camp_id }).fetch().then((camp) => { - camp.getCampUsers((users) => { - var new_status; - var save_method = { require: true }; - var mail_delivery = { - template: '', - subject: '', - to_mail: '', - }; - var user = camp.isUserInCamp(user_id); - - // camp manager commands - if (action === 'approve_new_mgr' && (camp_mgr_id === camp.attributes.main_contact || isAdmin)) { - new_status = 'approved'; - if (!user) { - save_method.require = false; - save_method.method = 'insert'; - } - } else if (camp.isCampManager(camp_mgr_id) || isAdmin) { - if (user && action === "approve" && user.can_approve) { - mail_delivery.to_mail = user.email; - mail_delivery.subject = 'Spark: you have been approved!'; - mail_delivery.template = 'emails/camps/member_approved'; - new_status = 'approved'; - } else if (user && action === "remove" && user.can_remove) { - new_status = 'deleted'; - } else if (user && action === "reject" && user.can_reject) { - mail_delivery.to_mail = user.email; - mail_delivery.subject = 'Spark: you have been Rejected!'; - mail_delivery.template = 'emails/camps/member_rejected'; - new_status = 'rejected'; - } else if (user && action === "revive") { - new_status = 'pending'; - } else if (action === "request_mgr") { - new_status = 'pending_mgr'; - mail_delivery.to_mail = ''; - mail_delivery.subject = 'Spark: you have been requested to join camp'; - mail_delivery.template = 'emails/camps/member_request'; - if (!user) { - save_method.require = false; - save_method.method = 'insert'; - } else if (user.member_status === 'approved') { - new_status = null; - } - } - } else { - var camp_manager = camp.isCampManager(camp.attributes.main_contact); - if (action === 'join') { - new_status = 'pending'; - if (camp_manager) { - mail_delivery.subject = 'Spark: Someone wants to join your camp!'; - mail_delivery.template = 'emails/camps/join_request'; - mail_delivery.to_mail = camp_manager.email; - } - if (!user) { - save_method.require = false; - save_method.method = 'insert'; - } else if (user.member_status === 'approved') { - new_status = null; - } - } else if (action === 'join_mgr' && user && user.member_status === 'pending_mgr' && camp_mgr_id === user_id) { - new_status = 'approved'; - if (camp_manager) { - mail_delivery.subject = 'Spark: A member approved himself to your camp!'; - mail_delivery.template = 'emails/camps/member_approved'; - mail_delivery.to_mail = camp_manager.email; - } - } else if (action === 'join_cancel' && user && user.member_status !== 'deleted' && camp_mgr_id === user_id) { - new_status = 'deleted'; - if (camp_manager) { - mail_delivery.subject = 'Spark: A member canceled himself from your camp!'; - mail_delivery.template = 'emails/camps/join_cancel'; - mail_delivery.to_mail = camp_manager.email; - } - } - } - if (new_status) { - var data = { - camp_id: camp.attributes.id, - user_id: user_id, - status: new_status - }; - var query = ''; - if (save_method.method === 'insert') { - query = knex(constants.CAMP_MEMBERS_TABLE_NAME).insert(data).toString(); - } else { - query = 'UPDATE ' + constants.CAMP_MEMBERS_TABLE_NAME + ' SET status="' + data.status + '" WHERE camp_id=' + data.camp_id + ' AND user_id=' + data.user_id + ';'; - } - var _after_update = () => { - console.log(action + " from camp " + data.camp_id + " of user " + data.user_id + " / status: " + data.status); - if (mail_delivery.template !== '') { - if (mail_delivery.to_mail !== '') { - emailDeliver(mail_delivery.to_mail, mail_delivery.subject, mail_delivery.template, { user: user, camp: camp, camp_manger: camp_manager }); // notify the user - } else { - User.forge({ user_id: user_id }).fetch().then((user) => { - emailDeliver(user.attributes.email, mail_delivery.subject, mail_delivery.template, { user: user, camp: camp, camp_manager: camp_manager }); // notify the user - }); - } - } - var res_data = { data: { member: data } }; - if (action === 'approve_new_mgr') { - res_data.data.message = 'camp created'; - res_data.data.camp_id = camp_id; - } - res.status(200).json(res_data); - - } - knex.raw(query).then(_after_update); - } else { - res.status(404).json({ error: true, data: { message: "Cannot execute this command." } }); - } - }); - }).catch((e) => { - res.status(500).json({ - error: true, - data: { - message: e.message - } - }) - }); - } /** * approve user request */ @@ -605,21 +619,6 @@ module.exports = (app, passport) => { } }); - var emailDeliver = (recipient, subject, template, props) => { - /** - * Deliver email request to camp manager - * notifiying a user wants to join his camp - * @return {boolean} should return true if mail delivered. FIXME: in mail.js - */ - console.log('Trying to send mail to ' + recipient + ' from ' + mailConfig.from + ': ' + subject + ', template ' + template); - mail.send( - recipient, - mailConfig.from, - subject, - template, props - ) - } - /** * API: (POST) create Program * request => /camps/program @@ -679,10 +678,16 @@ module.exports = (app, passport) => { if (user !== null) { // check that user is only at one camp! user.getUserCamps((camps) => { - if (camps.length === 0 || user.isUserInCamp(camp_id)) { + if (camps.length === 0 || !user.attributes.camp) { __camps_update_status(camp_id, user.attributes.user_id, 'request_mgr', req.user, res); } else { - res.status(500).json({ error: true, data: { message: 'Already applied to different camp!' } }); + let message; + if (user.isUserInCamp(camp_id)) { + message = 'Already applied to this camp'; + } else { + message = 'Already applied to different camp!'; + } + res.status(500).json({ error: true, data: { message: message } }); } }); } else { diff --git a/routes/camps_routes.js b/routes/camps_routes.js index bcb545f60..3ff55d8c2 100644 --- a/routes/camps_routes.js +++ b/routes/camps_routes.js @@ -26,7 +26,7 @@ var __render_camp = function (camp, req, res) { breadcrumbs: req.breadcrumbs(), details: camp.toJSON(), isUserCampMember: (camp.isUserCampMember(req.user.id) || req.user.isAdmin), - isUserInCamp: (camp.isUserCampMember(req.user.id) || req.user.isAdmin), + isUserInCamp: (camp.isUserInCamp(req.user.id) || req.user.isAdmin), main_contact: camp.isUserInCamp(camp.attributes.main_contact), moop_contact: camp.isUserInCamp(camp.attributes.moop_contact), safety_contact: camp.isUserInCamp(camp.attributes.moop_contact), diff --git a/views/emails/camps/join_cancel.jade b/views/emails/camps/join_cancel.jade index db01d6bf5..7fa8fa8e1 100644 --- a/views/emails/camps/join_cancel.jade +++ b/views/emails/camps/join_cancel.jade @@ -49,10 +49,19 @@ body .wrapper .card.card__text--center span Midburn Spark - h1 Camp join canceled! + h1='ביטול חברות במחנה' + p.subtitle(dir='rtl') + span שלום #{camp_manager.first_name}, + br + span #{user.name} (#{user.email}) ביטל את השתתפותו במחנה שלך "#{camp.camp_name_he}" + p(dir='rtl') + span לפאנל ניהול חברים היכנס בלינק הבא + a(href='//spark.midburn.org/') Spark + h1 Cancellation of camp membership p.subtitle - strong Someone, - span has canceled the join request. + span Hello #{camp_manager.first_name}, + br + span #{user.name} (#{user.email}) has canceled his join request of your camp "#{camp.camp_name_en}" p - You can visit pending join requests dashboard in - a(href='//spark.midburn.org/') Spark \ No newline at end of file + span For camp members management panel follow this link + a(href='//spark.midburn.org/') Spark \ No newline at end of file diff --git a/views/emails/camps/join_request.jade b/views/emails/camps/join_request.jade index 247e5fa4a..44d9e2f4a 100644 --- a/views/emails/camps/join_request.jade +++ b/views/emails/camps/join_request.jade @@ -49,10 +49,19 @@ body .wrapper .card.card__text--center span Midburn Spark - h1 Camp join request! + h1='בקשה להצטרפות למחנה' + p.subtitle(dir='rtl') + span שלום #{camp_manager.first_name}, + br + span #{user.name} (#{user.email}) ביקש להצטרף למחנה שלך "#{camp.camp_name_he}" + p(dir='rtl') + span לאישור חברים היכנס בלינק הבא + a(href='//spark.midburn.org/') Spark + h1 Cancellation of camp membership p.subtitle - strong Someone, - span requested to join your camp + span Hello #{camp_manager.first_name}, + br + span #{user.name} (#{user.email}) requested to join your camp "#{camp.camp_name_en}" p - You can visit pending join requests dashboard in - a(href='//spark.midburn.org/') Spark \ No newline at end of file + span For camp members management panel follow this link + a(href='//spark.midburn.org/') Spark \ No newline at end of file diff --git a/views/emails/camps/member_approved.jade b/views/emails/camps/member_approved.jade index 8a1badab5..e530e2dc8 100644 --- a/views/emails/camps/member_approved.jade +++ b/views/emails/camps/member_approved.jade @@ -49,9 +49,23 @@ body .wrapper .card.card__text--center span Midburn Spark - h1 Camp join request! + h1='אשור חברות במחנה' + p.subtitle(dir='rtl') + span שלום #{user.first_name}, + br + span בקשת החברות שלך במחנה "#{camp.camp_name_he}" אושרה. + br + span אם חלה טעות, תוכל ליצור קשר עם מנהל המחנה, #{camp_manager.name} + p(dir='rtl') + span לבחירת מחנה אחר הכנס ל- + a(href='//spark.midburn.org/') Spark + h1 Your membership has been approved p.subtitle - strong Someone, - span thank you for approving the request to join a camp - p You can view the details of the camp on - a(href='//spark.midburn.org/') Spark \ No newline at end of file + span Hello #{user.first_name}, + br + span Your camp membership request to "#{camp.camp_name_en}" has been approved. + br + span If this was done by mistake, please contact #{camp_manager.name}, the camp manager. + p + span To select different camp, login to: + a(href='//spark.midburn.org/') Spark \ No newline at end of file diff --git a/views/emails/camps/member_rejected.jade b/views/emails/camps/member_rejected.jade index 8c88d0fe2..01483334d 100644 --- a/views/emails/camps/member_rejected.jade +++ b/views/emails/camps/member_rejected.jade @@ -49,10 +49,22 @@ body .wrapper .card.card__text--center span Midburn Spark - h1 Camp join request! + h1='סרוב חברות במחנה' + p.subtitle(dir='rtl') + span שלום #{user.first_name}, + br + span בקשת החברות שלך במחנה "#{camp.camp_name_he}" נדחתה. + br + span אם חלה טעות, תוכל ליצור קשר עם מנהל המחנה, #{camp_manager.name} + p(dir='rtl') + span לבחירת מחנה אחר הכנס ל- + a(href='//spark.midburn.org/') Spark + h1 Your membership has been declined p.subtitle - strong Someone, - span thank you for your reply. - p We are sorry to hear that you have rejected the request to join a camp. - p You can request to join another camp on - a(href='//spark.midburn.org/') Spark \ No newline at end of file + span Hello #{user.first_name}, + br + span Your camp membership request to "#{camp.camp_name_en}" has been rejected. If this was + span done by mistake, please contact #{camp_manager.name}, the camp manager. + p + span To select different camp, login to: + a(href='//spark.midburn.org/') Spark \ No newline at end of file diff --git a/views/emails/camps/member_request.jade b/views/emails/camps/member_request.jade index 57b77b0ae..e67a4816b 100644 --- a/views/emails/camps/member_request.jade +++ b/views/emails/camps/member_request.jade @@ -49,9 +49,23 @@ body .wrapper .card.card__text--center span Midburn Spark - h1 Theme Camp + h1='בקשה להצטרפות למחנה' + p.subtitle(dir='rtl') + span שלום #{user.first_name}, + br + span נשלחה לך בקשת חברות ממחנה "#{camp.camp_name_he}" . + br + span לאישור / דחיית הבקשה התחבר בקישור הבא- + a(href='//spark.midburn.org/') Spark + p(dir='rtl') + span אם חלה טעות, תוכל ליצור קשר עם מנהל המחנה, #{camp_manager.name} + h1 Request to join camp p.subtitle - strong You, - span have been requested to join a camp. - p You can view the details of the camp on - a(href='//spark.midburn.org/') Spark, and approve join. \ No newline at end of file + span Hello #{user.first_name}, + br + span You got request to join camp "#{camp.camp_name_en}". + br + span To Approve/Decline yourself to this camp, login to: + a(href='//spark.midburn.org/') Spark + p + span If this was done by mistake, please contact #{camp_manager.name}, the camp manager.