-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9e58c1
commit d5f6cb8
Showing
3 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const MODULE_NAME = 'GET-USER-BY-EMAIL'; | ||
|
||
const TABLE_NAME = 'users'; | ||
|
||
const logger = require('./logger'); | ||
const mysql = require('./mysql'); | ||
|
||
/** | ||
* @typedef User | ||
* @type {object} | ||
* @property {string} email | ||
* @property {string} password | ||
* @property {Date} created | ||
* @property {Date} ts | ||
* @property {number} disabled | ||
* @property {number} super | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} xid | ||
* @param {string} email | ||
* @param {boolean} includeDisabled | ||
* @returns {Promise<User>} | ||
*/ | ||
module.exports = async (xid, email, includeDisabled) => { | ||
const conditions = []; | ||
const values = [TABLE_NAME]; | ||
|
||
conditions.push('email = ?'); | ||
values.push(email); | ||
|
||
if (!includeDisabled) { | ||
conditions.push('disabled = 0'); | ||
} | ||
|
||
const query = ` | ||
SELECT * FROM ?? | ||
WHERE | ||
${conditions.join(' AND ')} | ||
LIMIT 1 | ||
`.trim(); | ||
|
||
try { | ||
const [result] = await mysql.poolPromise.query(query, values); | ||
return (result || [])[0]; | ||
} catch (e) { | ||
const newE = new Error(`${MODULE_NAME} 2B93AED6: Exception`); | ||
|
||
logger.error(newE.message, { | ||
xid, | ||
eCode: e.code, | ||
eMessage: e.message, | ||
query: mysql.formatSimplified(query, values), | ||
}); | ||
|
||
throw newE; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const MODULE_NAME = 'INSERT-TARGET'; | ||
|
||
const TABLE_NAME = 'targets'; | ||
|
||
const logger = require('./logger'); | ||
const mysql = require('./mysql'); | ||
|
||
module.exports = async (xid, email, hostname, name, targetUrl) => { | ||
const sanitizedName = (name || '') | ||
.trim() | ||
.replace(/^\/+/, '') | ||
.replace(/\/$/g, '') | ||
.trim(); | ||
|
||
if (!sanitizedName) { | ||
return null; | ||
} | ||
|
||
let sanitizedTargetUrl = targetUrl.trim(); | ||
if (sanitizedTargetUrl.search(/^http(s)*:\/\//) < 0) { | ||
sanitizedTargetUrl = `https://${sanitizedTargetUrl}`; | ||
} | ||
|
||
if (!sanitizedTargetUrl) { | ||
return null; | ||
} | ||
|
||
const query = 'INSERT INTO ?? SET ?'; | ||
const values = [ | ||
TABLE_NAME, | ||
{ | ||
name: sanitizedName, | ||
hostname, | ||
user_email: email, | ||
target_url: sanitizedTargetUrl, | ||
}, | ||
]; | ||
|
||
try { | ||
const [result] = await mysql.poolPromise.query(query, values); | ||
return result; | ||
} catch (e) { | ||
const newE = new Error(`${MODULE_NAME} 2340DA44: Exception`); | ||
|
||
logger.warn(newE.message, { | ||
xid, | ||
eCode: e.code, | ||
eMessage: e.message, | ||
query: mysql.formatSimplified(query, values), | ||
}); | ||
|
||
throw newE; | ||
} | ||
}; |