obrand-admin-server/server/units/db-accounts.js
Martin Donnelly 40a8337ab3 Server up and running and serving all the previous mocked stuff.
Also now serving api/pages as a list page and viewer.
2016-03-31 14:24:21 +01:00

205 lines
5.3 KiB
JavaScript

'use strict';
var mdValidator = require('node-validator');
var mdErrors = require('./md-errors');
var bcrypt = require('bcrypt-as-promised');
var newId = require('uuid-pure').newId;
module.exports = function(db) {
var module = {};
module.connectPGP = function() {
return new Promise(function(resolve, reject) {
db.query('select $1::int as number', [1])
.then((data)=> {
resolve(data);
})
.catch((error)=> {
reject(error);
});
});
};
module.sqlInsertAccount = function(data) {
let _data = data;
_data.uid = newId();
return new Promise(function(resolve, reject) {
db.func('insert_user',
[_data.username, _data.email, _data.hash, _data.uid])
.then(()=> {
return resolve('ok');
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlGetAccount = function(email) {
return new Promise(function(resolve, reject) {
db.oneOrNone('select * from logins where email=$1;', [email])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlGetAccountDetails = function(uid) {
return new Promise(function(resolve, reject) {
db.one('select * from getAccountDetails($1);',[uid])
.then(function(d) {
console.log('+ sqlGetAccountDetails');
console.log(d);
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlUpsertProfile = function(data) {
var propArray = [
data.uid,
data.forename,
data.surname,
data.gender,
data.dob,
data.bio
];
return new Promise(function(resolve, reject) {
db.func('upsert_profile', propArray)
.then(()=> {
return resolve(true);
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlMakeMemberOf = function(cid, uid) {
var propArray = [cid,uid];
return new Promise(function(resolve, reject) {
db.func('makeMemberOf', propArray)
.then(()=> {
return resolve(true);
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlGetProfile = function(uid) {
return new Promise(function(resolve, reject) {
db.oneOrNone('select * from profile where uid=$1;', [uid])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
module.addNewAccount = function(data) {
var self = this;
return new Promise((resolve, reject) => {
let _data = {};
_data.username = data.username.trim();
_data.password = data.password.trim();
_data.email = data.email.trim();
if (Object.keys(data).length === 3) {
if (_data.username.length === 0 || _data.password.length === 0 || _data.email.length === 0) {
return reject(mdErrors.error(1000));
}
if (mdValidator.Email(data.email) === false) {
return reject(mdErrors.error(1001));
} else {
// It should be possible to insert the user now.
bcrypt.hash(data.password,10).then((d) => {
_data.hash = d;
self.sqlInsertAccount(_data)
.then(()=> {
return resolve({reply: 'user added'});
})
.catch((err)=> {
return reject(err);
});
});
}
} else {
// Error - required details missing
return reject(mdErrors.error(1002));
}
});
};
module.findAccount = function(data) {
var _data = data;
return new Promise((resolve, reject) => {
this.sqlGetAccount(_data.email)
.then((row) => {
if (row === null) {
return reject(mdErrors.error(1004));
}
// Check password against hash held in db
// noinspection JSUnresolvedVariable
bcrypt.compare(_data.password, row.password_hash)
.then(function() {
let loginDetails = {
id: row.id,
username: row.username,
email: row.email,
uid: row.uid
};
return resolve(loginDetails);
})
// Failure, reject
.catch(function() {
return reject(mdErrors.error(1004));
});
})
.catch(function(err) {
return reject(err);
});
});
};
module.addInsertProfile = function(data) {
return new Promise((resolve, reject) => {
this.sqlUpsertProfile(data)
.then((d)=> {
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
//makeMemberOf(cid, uid)
module.makeMemberOf = function(cid, uid) {
return new Promise((resolve, reject) => {
this.sqlMakeMemberOf(cid, uid)
.then((d)=> {
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
return module;
};