Last active
August 29, 2015 13:57
-
-
Save t2k/9873272 to your computer and use it in GitHub Desktop.
mongoose coffee
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
module.exports = (app, config, mongoose, nodemailer) -> | |
crypto = require("crypto") | |
Status = new mongoose.Schema( | |
name: | |
first: | |
type: String | |
last: | |
type: String | |
status: | |
type: String | |
) | |
schemaOptions = | |
toJSON: | |
virtuals: true | |
toObject: | |
virtuals: true | |
Contact = new mongoose.Schema( | |
name: | |
first: | |
type: String | |
last: | |
type: String | |
accountId: | |
type: mongoose.Schema.ObjectId | |
added: | |
type: Date | |
# When the contact was added | |
updated: | |
type: Date | |
# When the contact last updated | |
, schemaOptions) | |
Contact.virtual("online").get -> | |
app.isAccountOnline @get("accountId") | |
AccountSchema = new mongoose.Schema( | |
email: | |
type: String | |
unique: true | |
password: | |
type: String | |
name: | |
first: | |
type: String | |
last: | |
type: String | |
full: | |
type: String | |
birthday: | |
day: | |
type: Number | |
min: 1 | |
max: 31 | |
required: false | |
month: | |
type: Number | |
min: 1 | |
max: 12 | |
required: false | |
year: | |
type: Number | |
photoUrl: | |
type: String | |
biography: | |
type: String | |
contacts: [Contact] | |
status: [Status] # My own status updates only | |
activity: [Status] # All status updates including friends | |
) | |
Account = mongoose.model("Account", AccountSchema) | |
registerCallback = (err) -> | |
return console.log(err) if err | |
console.log "Account was created" | |
changePassword = (accountId, newpassword) -> | |
shaSum = crypto.createHash("sha256") | |
shaSum.update newpassword | |
hashedPassword = shaSum.digest("hex") | |
Account.update | |
_id: accountId | |
, | |
$set: | |
password: hashedPassword | |
, | |
upsert: false | |
, changePasswordCallback = (err) -> | |
console.log "Change password done for account " + accountId | |
return | |
return | |
forgotPassword = (email, resetPasswordUrl, callback) -> | |
user = Account.findOne( | |
email: email | |
, findAccount = (err, doc) -> | |
if err | |
# Email address is not a valid user | |
callback false | |
else | |
smtpTransport = nodemailer.createTransport("SMTP", config.mail) | |
resetPasswordUrl += "?account=" + doc._id | |
smtpTransport.sendMail | |
from: "[email protected]" | |
to: doc.email | |
subject: "SocialNet Password Request" | |
text: "Click here to reset your password: " + resetPasswordUrl | |
, forgotPasswordResult = (err) -> | |
if err | |
callback false | |
else | |
callback true | |
return | |
return | |
) | |
return | |
login = (email, password, callback) -> | |
shaSum = crypto.createHash("sha256") | |
shaSum.update password | |
Account.findOne | |
email: email | |
password: shaSum.digest("hex") | |
, (err, doc) -> | |
callback doc | |
return | |
return | |
findByString = (searchStr, callback) -> | |
searchRegex = new RegExp(searchStr, "i") | |
Account.find | |
$or: [ | |
{ | |
"name.full": | |
$regex: searchRegex | |
} | |
{ | |
email: | |
$regex: searchRegex | |
} | |
] | |
, callback | |
return | |
findById = (accountId, callback) -> | |
Account.findOne | |
_id: accountId | |
, (err, doc) -> | |
callback doc | |
return | |
return | |
addContact = (account, addcontact) -> | |
console.log " >>models.addContact:" | |
#console.dir(addcontact); | |
contact = | |
name: | |
first: addcontact.name.first | |
last: addcontact.name.last | |
accountId: addcontact._id | |
added: new Date() | |
updated: new Date() | |
#console.dir(contact); | |
account.contacts.push contact | |
account.save (err) -> | |
console.log "Error saving account: " + err if err | |
return | |
return | |
removeContact = (account, contactId) -> | |
return if null is account.contacts | |
account.contacts.forEach (contact) -> | |
account.contacts.remove contact if contact.accountId is contactId | |
return | |
account.save() | |
return | |
hasContact = (account, contactId) -> | |
return false if null is account.contacts | |
account.contacts.forEach (contact) -> | |
true if contact.accountId is contactId | |
false | |
register = (email, password, firstName, lastName) -> | |
shaSum = crypto.createHash("sha256") | |
shaSum.update password | |
console.log "Registering " + email | |
user = new Account( | |
email: email | |
name: | |
first: firstName | |
last: lastName | |
full: firstName + " " + lastName | |
password: shaSum.digest("hex") | |
) | |
user.save registerCallback | |
console.log "Save command was sent" | |
return | |
findById: findById | |
register: register | |
hasContact: hasContact | |
forgotPassword: forgotPassword | |
changePassword: changePassword | |
findByString: findByString | |
addContact: addContact | |
removeContact: removeContact | |
login: login | |
Account: Account |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment