|
|
@ -1,5 +1,6 @@ |
|
|
const prisma = require("../utils/prisma"); |
|
|
const prisma = require("../utils/prisma"); |
|
|
const { EventLogs } = require("./eventLogs"); |
|
|
const { EventLogs } = require("./eventLogs"); |
|
|
|
|
|
const bcrypt = require("bcrypt"); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @typedef {Object} User |
|
|
* @typedef {Object} User |
|
|
@ -109,6 +110,44 @@ const User = { |
|
|
return { user: null, error: error.message }; |
|
|
return { user: null, error: error.message }; |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
createNew: async function ({ |
|
|
|
|
|
username, |
|
|
|
|
|
password, |
|
|
|
|
|
role = "default", |
|
|
|
|
|
dailyMessageLimit = null, |
|
|
|
|
|
},prisma) { |
|
|
|
|
|
const passwordCheck = this.checkPasswordComplexity(password); |
|
|
|
|
|
console.log("6666666666",passwordCheck) |
|
|
|
|
|
if (!passwordCheck.checkedOK) { |
|
|
|
|
|
return { user: null, error: passwordCheck.error }; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// Do not allow new users to bypass validation
|
|
|
|
|
|
if (!this.usernameRegex.test(username)) |
|
|
|
|
|
throw new Error( |
|
|
|
|
|
"Username must only contain lowercase letters, numbers, underscores, and hyphens with no spaces" |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const bcrypt = require("bcrypt"); |
|
|
|
|
|
const hashedPassword = bcrypt.hashSync(password, 10); |
|
|
|
|
|
const user = await prisma.users.create({ |
|
|
|
|
|
data: { |
|
|
|
|
|
username: this.validations.username(username), |
|
|
|
|
|
password: hashedPassword, |
|
|
|
|
|
role: this.validations.role(role), |
|
|
|
|
|
dailyMessageLimit: |
|
|
|
|
|
this.validations.dailyMessageLimit(dailyMessageLimit), |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
// const vue = this.filterFields(user);
|
|
|
|
|
|
console.log("6666666666",user); |
|
|
|
|
|
return { user: this.filterFields(user), error: null }; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error("FAILED TO CREATE USER.", error.message); |
|
|
|
|
|
return { user: null, error: error.message }; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
// Log the changes to a user object, but omit sensitive fields
|
|
|
// Log the changes to a user object, but omit sensitive fields
|
|
|
// that are not meant to be logged.
|
|
|
// that are not meant to be logged.
|
|
|
loggedChanges: function (updates, prev = {}) { |
|
|
loggedChanges: function (updates, prev = {}) { |
|
|
@ -218,6 +257,15 @@ const User = { |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
getNew: async function (clause = {},prisma) { |
|
|
|
|
|
try { |
|
|
|
|
|
const user = await prisma.users.findFirst({ where: clause }); |
|
|
|
|
|
return user ? this.filterFields({ ...user }) : null; |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error(error.message); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
// Returns user object with all fields
|
|
|
// Returns user object with all fields
|
|
|
_get: async function (clause = {}) { |
|
|
_get: async function (clause = {}) { |
|
|
try { |
|
|
try { |
|
|
|