moved api from monorepo
This commit is contained in:
		
							
								
								
									
										104
									
								
								models/Game.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								models/Game.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,104 @@
 | 
			
		||||
import mongoose from 'mongoose'
 | 
			
		||||
import slugify from 'slugify'
 | 
			
		||||
 | 
			
		||||
const {Schema, model} = mongoose
 | 
			
		||||
 | 
			
		||||
const GameSchema = new Schema(
 | 
			
		||||
    {
 | 
			
		||||
        title: {type: String, trim: true, required: true},
 | 
			
		||||
        series: {type: String, trim: true},
 | 
			
		||||
        steamId: {
 | 
			
		||||
            type: String,
 | 
			
		||||
            index: {unique: true, partialFilterExpression: {steamId: {$exists: true, $gt: 0, $type: String}}}
 | 
			
		||||
        },
 | 
			
		||||
        slug: String,
 | 
			
		||||
        frontImage: {type: String, required: true},
 | 
			
		||||
        screenshots: Array,
 | 
			
		||||
        genre: {type: Array, sparse: true},
 | 
			
		||||
        os: {
 | 
			
		||||
            windows: {type: Boolean},
 | 
			
		||||
            mac: {type: Boolean},
 | 
			
		||||
            linux: {type: Boolean},
 | 
			
		||||
            android: {type: Boolean},
 | 
			
		||||
            ios: {type: Boolean}
 | 
			
		||||
        },
 | 
			
		||||
        wine: {type: String, enum: ['Not Tested', 'Yes', 'No']},
 | 
			
		||||
        controller: {
 | 
			
		||||
            type: String,
 | 
			
		||||
            enum: [
 | 
			
		||||
                'No Controller Support',
 | 
			
		||||
                'Partial Controller Support',
 | 
			
		||||
                'Full Controller Support',
 | 
			
		||||
            ],
 | 
			
		||||
        },
 | 
			
		||||
        developer: {type: Array, sparse: true},
 | 
			
		||||
        publisher: {type: Array, sparse: true},
 | 
			
		||||
        releaseDate: {type: String},
 | 
			
		||||
        shortDesc: {type: String},
 | 
			
		||||
        reviews: String,
 | 
			
		||||
        summary: String,
 | 
			
		||||
        intel: {type: String, enum: ['Not Tested', 'Yes', 'No']},
 | 
			
		||||
        systemRequirements: {
 | 
			
		||||
            windows: {
 | 
			
		||||
                minimum: String,
 | 
			
		||||
                recommended: String,
 | 
			
		||||
            },
 | 
			
		||||
            mac: {
 | 
			
		||||
                minimum: String,
 | 
			
		||||
                recommended: String,
 | 
			
		||||
            },
 | 
			
		||||
            linux: {
 | 
			
		||||
                minimum: String,
 | 
			
		||||
                recommended: String,
 | 
			
		||||
            },
 | 
			
		||||
        },
 | 
			
		||||
        steamRating: {type: Number, min: 0, max: 10, default: 0},
 | 
			
		||||
        createdBy: {
 | 
			
		||||
            type: Schema.ObjectId,
 | 
			
		||||
            ref: 'User',
 | 
			
		||||
            required: true,
 | 
			
		||||
        },
 | 
			
		||||
        accessedBy: [
 | 
			
		||||
            {
 | 
			
		||||
                user: {
 | 
			
		||||
                    type: Schema.ObjectId,
 | 
			
		||||
                    ref: 'User',
 | 
			
		||||
                    required: true,
 | 
			
		||||
                },
 | 
			
		||||
                store: {type: Array, required: true},
 | 
			
		||||
                playStatus: {
 | 
			
		||||
                    type: String,
 | 
			
		||||
                    enum: ['Never Played', 'Up Next', 'Playing', 'Finished', 'Will Not Play'],
 | 
			
		||||
                    default: 'Never Played',
 | 
			
		||||
                },
 | 
			
		||||
                soundtrack: {type: String, enum: ['Yes', 'No'], default: 'No'},
 | 
			
		||||
                rating: {type: Number, min: 0, max: 10, default: 0}
 | 
			
		||||
            },
 | 
			
		||||
        ],
 | 
			
		||||
        lastModifiedBy: {
 | 
			
		||||
            type: Schema.ObjectId,
 | 
			
		||||
            ref: 'User',
 | 
			
		||||
            required: true,
 | 
			
		||||
        },
 | 
			
		||||
    },
 | 
			
		||||
    {timestamps: {createdAt: 'createDate', updatedAt: 'lastUpdateDate'}}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
GameSchema.pre('save', function (next) {
 | 
			
		||||
    this.slug = slugify(this.title, {lower: true})
 | 
			
		||||
    next()
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
GameSchema.index({accessedBy: {user: 1}}, {unique: true})
 | 
			
		||||
 | 
			
		||||
GameSchema.index({
 | 
			
		||||
    title: 'text',
 | 
			
		||||
    series: 'text',
 | 
			
		||||
    steamId: 'text',
 | 
			
		||||
    genre: 'text',
 | 
			
		||||
    developer: 'text',
 | 
			
		||||
    publisher: 'text',
 | 
			
		||||
    slug: 'text',
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
export default model('Game', GameSchema)
 | 
			
		||||
							
								
								
									
										95
									
								
								models/User.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								models/User.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,95 @@
 | 
			
		||||
import mongoose from 'mongoose'
 | 
			
		||||
const { Schema } = mongoose
 | 
			
		||||
import gravatar from 'gravatar'
 | 
			
		||||
// import crypt from 'argon2'
 | 
			
		||||
import jwt from 'jsonwebtoken'
 | 
			
		||||
import crypto from 'crypto'
 | 
			
		||||
 | 
			
		||||
const UserSchema = new Schema({
 | 
			
		||||
  name: {
 | 
			
		||||
    type: String,
 | 
			
		||||
    trim: true,
 | 
			
		||||
    required: [true, 'Please add a name'],
 | 
			
		||||
  },
 | 
			
		||||
  email: {
 | 
			
		||||
    type: String,
 | 
			
		||||
    required: [true, 'Please enter a valid email address'],
 | 
			
		||||
    match: [
 | 
			
		||||
      /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
 | 
			
		||||
      'Please add a valid email',
 | 
			
		||||
    ],
 | 
			
		||||
    unique: true,
 | 
			
		||||
  },
 | 
			
		||||
  password: {
 | 
			
		||||
    type: String,
 | 
			
		||||
    required: [true, 'Please enter a password with a minimum of 6 characters'],
 | 
			
		||||
    minlength: 6,
 | 
			
		||||
    select: false
 | 
			
		||||
  },
 | 
			
		||||
  avatar: {
 | 
			
		||||
    type: String,
 | 
			
		||||
  },
 | 
			
		||||
  displayName: {
 | 
			
		||||
    type: String,
 | 
			
		||||
    required: [true, 'Please add a name to show with your icon'],
 | 
			
		||||
    unique: true,
 | 
			
		||||
  },
 | 
			
		||||
  createDate: {
 | 
			
		||||
    type: Date,
 | 
			
		||||
    default: Date.now,
 | 
			
		||||
  },
 | 
			
		||||
  updateDate: Date,
 | 
			
		||||
  role: {
 | 
			
		||||
    type: String,
 | 
			
		||||
    enum: ['user', 'admin'],
 | 
			
		||||
    default: 'user'
 | 
			
		||||
  },
 | 
			
		||||
  resetPasswordToken: String,
 | 
			
		||||
  resetPasswordExpire: Date,
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
UserSchema.pre('save', async function (next) {
 | 
			
		||||
  if(!this.isModified('password')) next()
 | 
			
		||||
 | 
			
		||||
  this.password = await Bun.password.hash(this.password)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
UserSchema.pre('save', async function(next) {
 | 
			
		||||
  if(!this.isModified('email')) next()
 | 
			
		||||
  this.avatar = await gravatar.url(this.email, {
 | 
			
		||||
    s: '200',
 | 
			
		||||
    r: 'pg',
 | 
			
		||||
    d: 'retro',
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
// Sign JWT and return
 | 
			
		||||
UserSchema.methods.getSignedJwtToken = function () {
 | 
			
		||||
  return jwt.sign({ id: this._id }, Bun.env.ACCESS_TOKEN_SECRET, {
 | 
			
		||||
    expiresIn: Bun.env.JWT_EXPIRE,
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Match user entered password to hashed password in database
 | 
			
		||||
UserSchema.methods.matchPassword = async function (enteredPassword) {
 | 
			
		||||
  return await Bun.password.verify(enteredPassword, this.password)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Generate and hash password token
 | 
			
		||||
UserSchema.methods.getResetPasswordToken = async function () {
 | 
			
		||||
  // Generate token
 | 
			
		||||
  const resetToken = crypto.randomBytes(20).toString('hex')
 | 
			
		||||
 | 
			
		||||
  // Hash token and set to resetPasswordToken field
 | 
			
		||||
  this.resetPasswordToken = crypto
 | 
			
		||||
      .createHash('sha256')
 | 
			
		||||
      .update(resetToken)
 | 
			
		||||
      .digest('hex')
 | 
			
		||||
 | 
			
		||||
  // Set expire
 | 
			
		||||
  this.resetPasswordExpire = Date.now() + 10 * 60 * 1000
 | 
			
		||||
 | 
			
		||||
  return resetToken
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default mongoose.model('User', UserSchema)
 | 
			
		||||
		Reference in New Issue
	
	Block a user