Advertisement
Guest User

Untitled

a guest
Jun 29th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import mongoose, { Schema } from 'mongoose';
  2.  
  3. const RefreshToken = new Schema({
  4. token: {
  5. type: String,
  6. },
  7. expiry: {
  8. type: Number,
  9. },
  10. user: {
  11. type: Schema.Types.ObjectId,
  12. ref: 'User',
  13. }
  14. });
  15.  
  16. export default mongoose.model('RefreshToken', RefreshToken);
  17.  
  18. import mongoose, { Schema } from 'mongoose';
  19. import bycrpt from 'bcrypt-nodejs';
  20. import {
  21. isEmail,
  22. isURL,
  23. isAlphanumeric,
  24. isLength,
  25. } from 'mongoose-validators';
  26. import md5 from 'md5';
  27.  
  28. const UserSchema = new Schema({
  29. nickname: {
  30. type: String,
  31. required: false,
  32. },
  33. givenName: {
  34. type: String,
  35. required: false,
  36. },
  37. familyName: {
  38. type: String,
  39. required: false,
  40. },
  41. email: {
  42. type: String,
  43. required: true,
  44. validate: isEmail(),
  45. unique: true,
  46. },
  47. emailVerified: {
  48. type: Boolean,
  49. default: false,
  50. },
  51. resetPasswordToken: {
  52. type: String,
  53. required: false,
  54. default: undefined,
  55. },
  56. resetPasswordExpiry: {
  57. type: Date,
  58. required: false,
  59. default: undefined,
  60. },
  61. emailVerifyToken: {
  62. type: String,
  63. required: false,
  64. default: undefined,
  65. },
  66. emailVerifyExpiry: {
  67. type: Date,
  68. required: false,
  69. default: undefined,
  70. },
  71. password: {
  72. type: String,
  73. required: true,
  74. validate: [isAlphanumeric(), isLength(8, 30)],
  75. },
  76. profileImage: {
  77. type: String,
  78. required: false,
  79. validate: isURL(),
  80. }
  81. }, {
  82. strict: true,
  83. });
  84.  
  85. UserSchema.methods.compare = function (candidatePassword) {
  86. return new Promise((resolve, reject) => {
  87. bycrpt.compare(candidatePassword, this.password, function(err, isMatch) {
  88. if (err) {
  89. reject(err);
  90. } else {
  91. resolve(isMatch);
  92. }
  93. });
  94. })
  95. };
  96.  
  97. UserSchema.pre('save', function (next) {
  98. const user = this;
  99.  
  100. if (!user.isModified('password')) return next();
  101.  
  102. bycrpt.genSalt(10, (err, salt) => {
  103. if (err) return next(err);
  104.  
  105. bycrpt.hash(user.password, salt, null, (err, hash) => {
  106. if (err) return next(err);
  107.  
  108. user.password = hash;
  109. next();
  110. })
  111. });
  112. });
  113.  
  114. UserSchema.pre('save', function (next) {
  115. let name;
  116. let hash = md5(this.email);
  117.  
  118. if (this.givenName && this.familyName) {
  119. name = `${this.givenName}+${this.familyName}`
  120. }
  121.  
  122. if (this.givenName) {
  123. name = `${this.givenName}`
  124. }
  125.  
  126. name = `${this.email}`
  127.  
  128. this.profileImage = `https://www.gravatar.com/avatar/${hash}?d=${encodeURIComponent(`https://ui-avatars.com/api/${name}/128`)}`;
  129.  
  130. next()
  131. })
  132.  
  133. export default mongoose.model('User', UserSchema);
  134.  
  135.  
  136. import mongoose, { Schema } from 'mongoose';
  137.  
  138. const ProductSchema = new Schema({
  139. name: String,
  140. price: Number,
  141. sku: {
  142. type: String,
  143. required: false,
  144. }
  145. });
  146.  
  147. ProductSchema.path('price').get(function(num) {
  148. return (num / 100).toFixed(2);
  149. });
  150.  
  151. ProductSchema.path('price').set(function(num) {
  152. return num * 100;
  153. });
  154.  
  155. export default mongoose.model('Product', ProductSchema);
  156.  
  157. import mongoose, { Schema } from 'mongoose';
  158.  
  159. const MechantSchema = new Schema({
  160. name: {
  161. type: String,
  162. required: true,
  163. unique: true,
  164. },
  165. logo: {
  166. type: String,
  167. required: false,
  168. },
  169. createdAt: {
  170. type: Date,
  171. default: Date.now(),
  172. },
  173. });
  174.  
  175. export default mongoose.model('Merchant', MechantSchema);
  176.  
  177. import mongoose, { Schema } from 'mongoose';
  178. import uuidv4 from 'uuid/v4';
  179.  
  180. const AccountSchema = new Schema({
  181. name: {
  182. type: String,
  183. required: true,
  184. },
  185. accountType: {
  186. type: String,
  187. enum: ['savings', 'current']
  188. },
  189. user: {
  190. type: Schema.Types.ObjectId,
  191. ref: 'User',
  192. required: true,
  193. },
  194. uuid: {
  195. type: String,
  196. required: true,
  197. unique: true,
  198. default: uuidv4(),
  199. },
  200. createdAt: {
  201. type: Date,
  202. default: Date.now(),
  203. required: true,
  204. },
  205. });
  206.  
  207. AccountSchema.pre('save', function (next) {
  208. var account = this;
  209.  
  210. if (!account.createdAt) {
  211. account.createdAt = Date.now();
  212. }
  213.  
  214. next();
  215. });
  216.  
  217. export default mongoose.model('Account', AccountSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement