Skip to content

Commit

Permalink
Merge pull request #439 from jniles/chore-binary-uuid-conversion
Browse files Browse the repository at this point in the history
chore(bid): remove all custom convert()s
  • Loading branch information
jniles committed May 30, 2016
2 parents 3027221 + 02a5409 commit d4da174
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 326 deletions.
148 changes: 60 additions & 88 deletions server/controllers/admin/employees.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,43 @@
/**
* The /employees HTTP API endpoint
*
* @module admin/employees
*
* @description This controller is responsible for implementing all crud on the
* employees table through the `/employees` endpoint.
*
* @requires lib/db
*
* NOTE: This api does not handle the deletion of employees because
* that subject is not in the actuality.
*/
* @module admin/employees
*
* @description
* This controller is responsible for implementing all crud on the
* employees table through the `/employees` endpoint.
* The /employees HTTP API endpoint
*
* @requires lib/db
*
* NOTE: This api does not handle the deletion of employees because
* that subject is not in the actuality.
*/

'use strict';

var db = require('./../../lib/db');
var uuid = require('node-uuid');
var NotFound = require('./../../lib/errors/NotFound');
var BadRequest = require('../../lib/errors/BadRequest');
const db = require('./../../lib/db');
const uuid = require('node-uuid');
const NotFound = require('./../../lib/errors/NotFound');
const BadRequest = require('../../lib/errors/BadRequest');

/**
* Preprocess INSERT/UPDATE data to convert uuids into binary uuids, as well as
* preprocess dates into date objects
* Returns an array of each employee in the database
*
* @param {Object} data - the data to be posted/put into the database
* @returns {Object} data - the same data object, with dates and uuids converted
* @example
* // GET /employees : Get list of employees
* var employees = require('admin/employees');
* employees.list(request, response, next);
*/
function convert(data) {

if (data.grade_id) {
data.grade_id = db.bid(data.grade_id);
}

if (data.debtor_group_uuid) {
data.debtor_group_uuid = db.bid(data.debtor_group_uuid);
}

if (data.creditor_group_uuid) {
data.creditor_group_uuid = db.bid(data.creditor_group_uuid);
}

if (data.creditor_uuid) {
data.creditor_uuid = db.bid(data.creditor_uuid);
}

if (data.debtor_uuid) {
data.debtor_uuid = db.bid(data.debtor_uuid);
}

if (data.location_id) {
data.location_id = db.bid(data.location_id);
}

if (data.dob) {
data.dob = new Date(data.dob);
}

if (data.date_embauche) {
data.date_embauche = new Date(data.date_embauche);
}

return data;
}

/**
* Returns an array of each employee in the database
*
* @param {object} request The express request object
* @param {object} response The express response object
* @param {object} next The express middleware next object
*
* @example
* // GET /employees : Get list of employees
* var employees = require('admin/employees');
* employees.list(request, response, next);
*/
exports.list = function (req, res, next) {
var sql =
`SELECT
employee.id, employee.code AS code_employee, employee.prenom, employee.name,
`SELECT employee.id, employee.code AS code_employee, employee.prenom, employee.name,
employee.postnom, employee.sexe, employee.dob, employee.date_embauche, employee.service_id,
employee.nb_spouse, employee.nb_enfant, BUID(employee.grade_id) as grade_id, employee.locked, grade.text, grade.basic_salary,
fonction.id AS fonction_id, fonction.fonction_txt,
employee.phone, employee.email, employee.adresse, employee.bank, employee.bank_account, employee.daily_salary, BUID(employee.location_id) AS location_id,
grade.code AS code_grade, BUID(debtor.uuid) as debtor_uuid, debtor.text AS debtor_text, BUID(debtor.group_uuid) as debtor_group_uuid,
BUID(creditor.uuid) as creditor_uuid, creditor.text AS creditor_text, BUID(creditor.group_uuid) as creditor_group_uuid, creditor_group.account_id
employee.nb_spouse, employee.nb_enfant, BUID(employee.grade_id) as grade_id, employee.locked,
grade.text, grade.basic_salary, fonction.id AS fonction_id, fonction.fonction_txt,
employee.phone, employee.email, employee.adresse, employee.bank, employee.bank_account,
employee.daily_salary, BUID(employee.location_id) AS location_id, grade.code AS code_grade,
BUID(debtor.uuid) as debtor_uuid, debtor.text AS debtor_text, BUID(debtor.group_uuid) as debtor_group_uuid,
BUID(creditor.uuid) as creditor_uuid, creditor.text AS creditor_text,
BUID(creditor.group_uuid) as creditor_group_uuid, creditor_group.account_id
FROM employee
JOIN grade ON employee.grade_id = grade.uuid
JOIN fonction ON employee.fonction_id = fonction.id
Expand All @@ -102,17 +55,17 @@ exports.list = function (req, res, next) {
};

/**
* Get list of availaible holidays for an employee
*/
* Get list of availaible holidays for an employee
*/
exports.listHolidays = function (req, res, next) {
var pp = JSON.parse(req.params.pp);
var sql =
`SELECT holiday.id, holiday.label, holiday.dateFrom, holiday.percentage, holiday.dateTo
FROM holiday WHERE
((holiday.dateFrom >= ? AND holiday.dateFrom <= ?)
(holiday.dateTo >= ? AND holiday.dateTo <= ?) OR
(holiday.dateFrom <= ? AND holiday.dateTo >a ?)) AND
holiday.employee_id = ? ;`;
`SELECT holiday.id, holiday.label, holiday.dateFrom, holiday.percentage, holiday.dateTo
FROM holiday WHERE
((holiday.dateFrom >= ? AND holiday.dateFrom <= ?)
(holiday.dateTo >= ? AND holiday.dateTo <= ?) OR
(holiday.dateFrom <= ? AND holiday.dateTo >= ?)) AND
holiday.employee_id = ?;`;

var data = [
pp.dateFrom, pp.dateTo,
Expand Down Expand Up @@ -171,8 +124,7 @@ exports.checkOffday = function checkHoliday(req, res, next) {
};

function lookupEmployee(id) {

var sql =
let sql =
`SELECT employee.id, employee.code AS code_employee, employee.prenom, employee.name,
employee.postnom, employee.sexe, employee.dob, employee.date_embauche, employee.service_id,
employee.nb_spouse, employee.nb_enfant, BUID(employee.grade_id) as grade_id,
Expand Down Expand Up @@ -231,7 +183,17 @@ exports.detail = function detail(req, res, next) {
* employees.update(req, res, next);
*/
exports.update = function update(req, res, next) {
var employee = convert(req.body);
var employee = db.convert(req.body, [
'grade_id', 'debtor_group_uuid', 'creditor_group_uuid', 'creditor_uuid', 'debtor_uuid', 'location_id'
]);

if (employee.dob) {
employee.dob = new Date(employee.dob);
}

if (employee.date_embauche) {
employee.date_embauche = new Date(employee.date_embauche);
}

var transaction;

Expand Down Expand Up @@ -313,7 +275,17 @@ exports.create = function create(req, res, next) {
data.debtor_uuid = uuid.v4();

// convert uuids to binary uuids as necessary
var employee = convert(data);
var employee = db.convert(req.body, [
'grade_id', 'debtor_group_uuid', 'creditor_group_uuid', 'creditor_uuid', 'debtor_uuid', 'location_id'
]);

if (employee.dob) {
employee.dob = new Date(employee.dob);
}

if (employee.date_embauche) {
employee.date_embauche = new Date(employee.date_embauche);
}

var creditor = {
uuid : employee.creditor_uuid,
Expand Down
87 changes: 27 additions & 60 deletions server/controllers/admin/suppliers.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
/**
* Supplier Controller
*
* This controller exposes an API to the client for reading and writing Supplier
*/
* Supplier Controller
*
* This controller exposes an API to the client for reading and writing Supplier
*/
'use strict';

const db = require('../../lib/db');
const uuid = require('node-uuid');
var NotFound = require('../../lib/errors/NotFound');
const NotFound = require('../../lib/errors/NotFound');

function lookupSupplier(uuid) {
'use strict';

var record;

var sql =
`SELECT BUID(supplier.uuid) as uuid, BUID(supplier.creditor_uuid) as creditor_uuid, supplier.name,
const sql = `
SELECT BUID(supplier.uuid) as uuid, BUID(supplier.creditor_uuid) as creditor_uuid, supplier.name,
supplier.address_1, supplier.address_2, supplier.email, supplier.fax, supplier.note,
supplier.phone, supplier.international, supplier.locked
FROM supplier
WHERE supplier.uuid = ?`;

return db.exec(sql, [uuid])
return db.exec(sql, [db.bid(uuid)])
.then(function (rows) {

if (rows.length === 0) {
throw new NotFound(`Could not find a Supplier with uuid ${uuid}`);
if (!rows.length) {
throw new NotFound(`Could not find a supplier with uuid ${uuid}`);
}

return rows[0];
});
}


// The Supplier is assumed from the session.
function list(req, res, next) {
'use strict';

var sql;

sql =
Expand All @@ -61,44 +53,27 @@ function list(req, res, next) {
}

/**
* GET /supplier/:uuid
*
* Returns the detail of a single Supplier
*/
* GET /supplier/:uuid
*
* Returns the detail of a single Supplier
*/
function detail(req, res, next) {
'use strict';

const uid = db.bid(req.params.uuid);

lookupSupplier(uid)
lookupSupplier(req.params.uuid)
.then(function (record) {
res.status(200).json(record);
})
.catch(next)
.done();
}

// convert uuids to binary uuids in preparation for database insertion
function convert(data) {

if (data.creditor_uuid) {
data.creditor_uuid = db.bid(data.creditor_uuid);
}

return data;
}


// POST /supplier
function create(req, res, next) {
'use strict';

var data = convert(req.body);
const data = db.convert(req.body, ['creditor_uuid']);

// provide uuid if the client has not specified
data.uuid = db.bid(data.uuid || uuid());

var sql =
let sql =
'INSERT INTO supplier SET ? ';

db.exec(sql, [data])
Expand All @@ -112,21 +87,17 @@ function create(req, res, next) {

// PUT /supplier/:uuid
function update(req, res, next) {
'use strict';

const uid = db.bid(req.params.uuid);

var sql =
let sql =
'UPDATE supplier SET ? WHERE uuid = ?;';

var data = convert(req.body);
const data = db.convert(req.body, ['creditor_uuid']);

// prevent updating the uuid
delete data.uuid;

db.exec(sql, [data, uid])
db.exec(sql, [data, db.bid(req.params.uuid)])
.then(function () {
return lookupSupplier(uid);
return lookupSupplier(req.params.uuid);
})
.then(function (record) {
res.status(200).json(record);
Expand All @@ -135,26 +106,22 @@ function update(req, res, next) {
.done();
}

// GET /SUPPLIER/SEARCH
// GET /supplier/search
function search(req, res, next) {
var sql;
var limit = Number(req.query.limit);
let limit = Number(req.query.limit);

var name = req.query.name;
var condition = '%' + name + '%';

sql =
let sql =
`SELECT BUID(supplier.uuid) as uuid, BUID(supplier.creditor_uuid) as creditor_uuid, supplier.name,
supplier.address_1, supplier.address_2, supplier.email,
supplier.fax, supplier.note, supplier.phone, supplier.international, supplier.locked
FROM supplier
WHERE supplier.name LIKE ?;`;
WHERE supplier.name LIKE "%?%"`;

if (limit) {
sql += ' LIMIT ' + Math.floor(limit) + ';';
}

db.exec(sql, [condition])
db.exec(sql, [req.query.name])
.then(function (rows) {
res.status(200).json(rows);
})
Expand Down
Loading

0 comments on commit d4da174

Please sign in to comment.