Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const bcrypt = require('bcrypt-nodejs');
  3.  
  4. const Schema = mongoose.Schema;
  5.  
  6. /**
  7. * Business Schema
  8. */
  9.  
  10. const businessSchema = Schema({
  11. name: {
  12. type: String,
  13. required: true,
  14. },
  15. email: {
  16. type: String,
  17. required: true,
  18. },
  19. password: {
  20. type: String,
  21. required: true,
  22. },
  23. categories: [{
  24. type: Schema.Types.ObjectId,
  25. ref: 'Category',
  26. required: true,
  27. }],
  28. branches: [{
  29. type: Schema.Types.ObjectId,
  30. ref: 'Branch',
  31. required: true,
  32. }],
  33. workingHours: {
  34. type: String,
  35. required: true,
  36. },
  37. shortDescription: {
  38. type: String,
  39. },
  40. description: {
  41. type: String,
  42. },
  43. });
  44.  
  45. /**
  46. * Hash password before saving the document
  47. */
  48.  
  49. businessSchema.pre('save', (done) => {
  50. if (!this.isModified('password')) {
  51. done();
  52. } else {
  53. bcrypt.hash(this.password, null, null, (err, hashedPassword) => {
  54. if (err) {
  55. return done(err);
  56. }
  57.  
  58. this.password = hashedPassword;
  59. return done();
  60. });
  61. }
  62. });
  63.  
  64. /**
  65. * Check the password
  66. */
  67.  
  68. businessSchema.methods.checkPassword = (guess, done) => {
  69. bcrypt.compare(guess, this.password, (err, matching) => {
  70. done(err, matching);
  71. });
  72. };
  73.  
  74. module.exports = mongoose.model('Business', businessSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement