Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mongoose, { Schema } from 'mongoose';
- const RefreshToken = new Schema({
- token: {
- type: String,
- },
- expiry: {
- type: Number,
- },
- user: {
- type: Schema.Types.ObjectId,
- ref: 'User',
- }
- });
- export default mongoose.model('RefreshToken', RefreshToken);
- import mongoose, { Schema } from 'mongoose';
- import bycrpt from 'bcrypt-nodejs';
- import {
- isEmail,
- isURL,
- isAlphanumeric,
- isLength,
- } from 'mongoose-validators';
- import md5 from 'md5';
- const UserSchema = new Schema({
- nickname: {
- type: String,
- required: false,
- },
- givenName: {
- type: String,
- required: false,
- },
- familyName: {
- type: String,
- required: false,
- },
- email: {
- type: String,
- required: true,
- validate: isEmail(),
- unique: true,
- },
- emailVerified: {
- type: Boolean,
- default: false,
- },
- resetPasswordToken: {
- type: String,
- required: false,
- default: undefined,
- },
- resetPasswordExpiry: {
- type: Date,
- required: false,
- default: undefined,
- },
- emailVerifyToken: {
- type: String,
- required: false,
- default: undefined,
- },
- emailVerifyExpiry: {
- type: Date,
- required: false,
- default: undefined,
- },
- password: {
- type: String,
- required: true,
- validate: [isAlphanumeric(), isLength(8, 30)],
- },
- profileImage: {
- type: String,
- required: false,
- validate: isURL(),
- }
- }, {
- strict: true,
- });
- UserSchema.methods.compare = function (candidatePassword) {
- return new Promise((resolve, reject) => {
- bycrpt.compare(candidatePassword, this.password, function(err, isMatch) {
- if (err) {
- reject(err);
- } else {
- resolve(isMatch);
- }
- });
- })
- };
- UserSchema.pre('save', function (next) {
- const user = this;
- if (!user.isModified('password')) return next();
- bycrpt.genSalt(10, (err, salt) => {
- if (err) return next(err);
- bycrpt.hash(user.password, salt, null, (err, hash) => {
- if (err) return next(err);
- user.password = hash;
- next();
- })
- });
- });
- UserSchema.pre('save', function (next) {
- let name;
- let hash = md5(this.email);
- if (this.givenName && this.familyName) {
- name = `${this.givenName}+${this.familyName}`
- }
- if (this.givenName) {
- name = `${this.givenName}`
- }
- name = `${this.email}`
- this.profileImage = `https://www.gravatar.com/avatar/${hash}?d=${encodeURIComponent(`https://ui-avatars.com/api/${name}/128`)}`;
- next()
- })
- export default mongoose.model('User', UserSchema);
- import mongoose, { Schema } from 'mongoose';
- const ProductSchema = new Schema({
- name: String,
- price: Number,
- sku: {
- type: String,
- required: false,
- }
- });
- ProductSchema.path('price').get(function(num) {
- return (num / 100).toFixed(2);
- });
- ProductSchema.path('price').set(function(num) {
- return num * 100;
- });
- export default mongoose.model('Product', ProductSchema);
- import mongoose, { Schema } from 'mongoose';
- const MechantSchema = new Schema({
- name: {
- type: String,
- required: true,
- unique: true,
- },
- logo: {
- type: String,
- required: false,
- },
- createdAt: {
- type: Date,
- default: Date.now(),
- },
- });
- export default mongoose.model('Merchant', MechantSchema);
- import mongoose, { Schema } from 'mongoose';
- import uuidv4 from 'uuid/v4';
- const AccountSchema = new Schema({
- name: {
- type: String,
- required: true,
- },
- accountType: {
- type: String,
- enum: ['savings', 'current']
- },
- user: {
- type: Schema.Types.ObjectId,
- ref: 'User',
- required: true,
- },
- uuid: {
- type: String,
- required: true,
- unique: true,
- default: uuidv4(),
- },
- createdAt: {
- type: Date,
- default: Date.now(),
- required: true,
- },
- });
- AccountSchema.pre('save', function (next) {
- var account = this;
- if (!account.createdAt) {
- account.createdAt = Date.now();
- }
- next();
- });
- export default mongoose.model('Account', AccountSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement