2018-08-23 16:58:50 -07:00
import { Meteor } from 'meteor/meteor' ;
import { Mongo } from 'meteor/mongo' ;
import { check } from 'meteor/check' ;
2020-01-16 09:32:59 -08:00
import { Email } from 'meteor/email' ;
2018-08-23 16:58:50 -07:00
import SimpleSchema from 'simpl-schema' ;
let ContactUsMessages = new Mongo . Collection ( 'ContactUsMessages' ) ;
ContactUsMessages . attachSchema ( new SimpleSchema ( {
name : {
type : String ,
label : "Name" ,
optional : false ,
trim : false ,
} ,
email : {
type : String ,
label : "Email" ,
optional : false ,
trim : false
} ,
message : {
type : String ,
label : "Message" ,
optional : false ,
trim : false
} ,
createdAt : {
type : Date ,
label : "Created On" ,
optional : true
}
} ) ) ;
if ( Meteor . isServer ) Meteor . publish ( 'contactUsMessages' , function ( ) {
2020-01-16 09:32:59 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
return ContactUsMessages . find ( { } ) ;
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
2018-08-23 16:58:50 -07:00
} ) ;
if ( Meteor . isServer ) {
Meteor . methods ( {
submitContactForm : function ( name , email , message ) {
check ( name , String ) ;
check ( email , String ) ;
check ( message , String ) ;
2020-01-16 09:32:59 -08:00
ContactUsMessages . insert ( { name , email , message , createdAt : new Date ( ) } ) ;
try {
let settings = Meteor . collections . Settings . findOne ( ) ;
if ( settings && settings . forwardEmailsTo && settings . forwardEmailsTo . length > 0 ) {
Email . send ( { to : settings . forwardEmailsTo , from : "Do Not Reply <no-reply@andersonvalleyeducation.org>" , subject : "Contact Us Message" , text : "Email: " + email + "\n\n" + message } ) ;
}
}
catch ( err ) {
console . log ( err ) ;
2018-08-23 16:58:50 -07:00
}
}
} ) ;
}
export default ContactUsMessages ;