134 lines
3.6 KiB
JavaScript
134 lines
3.6 KiB
JavaScript
|
|
import {
|
|
JsonDB,
|
|
Config
|
|
} from 'node-json-db';
|
|
|
|
import {
|
|
default as ValidateSchema
|
|
} from 'validate'
|
|
|
|
|
|
var userDB = new JsonDB(new Config("datastore/database/userDB.json", true, true, '/'));
|
|
|
|
const userSchema = new ValidateSchema({
|
|
username: {
|
|
type: String,
|
|
required: true,
|
|
match: /^[a-zA-Z0-9\ \-\_]+$/,
|
|
length: { min: 3, max: 32 },
|
|
message: {
|
|
type: '[username] must be of type String.',
|
|
required: '[username] is required.',
|
|
match: '[username] is invalid. Allowed characters: (a-z|A-Z|0-9| |-|_)',
|
|
length: '[username] must consist of 3 to 32 characters.'
|
|
}
|
|
},
|
|
firstName: {
|
|
type: String,
|
|
required: true,
|
|
match: /^[a-zA-Z0-9\ \-\_]+$/,
|
|
length: { min: 1, max: 32 },
|
|
message: {
|
|
type: '[firstName] must be of type String.',
|
|
required: '[firstName] is required.',
|
|
match: '[firstName] is invalid. Allowed characters: (a-z|A-Z|0-9| |-|_)',
|
|
length: '[firstName] must consist of 1 to 32 characters.'
|
|
}
|
|
},
|
|
lastName: {
|
|
type: String,
|
|
required: true,
|
|
match: /^[a-zA-Z0-9\ \-\_]+$/,
|
|
length: { min: 1, max: 32 },
|
|
message: {
|
|
type: '[lastName] must be of type String.',
|
|
required: '[lastName] is required.',
|
|
match: '[lastName] is invalid. Allowed characters: (a-z|A-Z|0-9| |-|_)',
|
|
length: '[lastName] must consist of 1 to 32 characters.'
|
|
}
|
|
},
|
|
emailAddress: {
|
|
type: String,
|
|
required: true,
|
|
match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/,
|
|
message: {
|
|
type: '[emailAddress] must be of type String.',
|
|
required: '[emailAddress] is required.',
|
|
match: '[emailAddress] is invalid (e.g. admin@example.com).'
|
|
}
|
|
},
|
|
enabled: {
|
|
type: String,
|
|
required: true,
|
|
enum: [
|
|
'true',
|
|
'false'
|
|
],
|
|
message: {
|
|
type: '[enabled] must be of type String.',
|
|
required: '[enabled] is required.',
|
|
enum: '[enabled] must be one of [true|false].'
|
|
}
|
|
},
|
|
permissions: {
|
|
type: Array,
|
|
required: true,
|
|
elements: [{
|
|
type: String
|
|
}],
|
|
message: {
|
|
type: '[permissions] must be of type Array.',
|
|
required: '[permissions] is required.',
|
|
elements: '[permissions] elements must be of type String.'
|
|
}
|
|
}
|
|
})
|
|
|
|
|
|
export async function saveUser(sUserID, oUserData) {
|
|
let dataErrorList = userSchema.validate(oUserData);
|
|
|
|
dataErrorList = dataErrorList.map((configError) => {
|
|
return configError.message;
|
|
})
|
|
|
|
if (dataErrorList.length > 0) {
|
|
return {
|
|
status: "error",
|
|
errors: dataErrorList
|
|
}
|
|
}
|
|
|
|
await userDB.push(`/${sUserID}`, oUserData);
|
|
|
|
return {
|
|
status: "ok"
|
|
}
|
|
}
|
|
|
|
export async function getUser(userID) {
|
|
let requestData = await userDB.getData(`/${userID}`);
|
|
return {
|
|
username: requestData.username,
|
|
firstName: requestData.firstName,
|
|
lastName: requestData.lastName,
|
|
emailAddress: requestData.emailAddress
|
|
};
|
|
}
|
|
|
|
export async function getAllUsers() {
|
|
let userList = await userDB.getData(`/`);
|
|
return userList.map((userData) => {
|
|
return {
|
|
username: userData.username,
|
|
firstName: userData.firstName,
|
|
lastName: userData.lastName,
|
|
emailAddress: userData.emailAddress
|
|
}
|
|
})
|
|
}
|
|
|
|
export async function deleteUser(userID) {
|
|
await userDB.delete(`/${userID}`);
|
|
}
|