datasquirel/users/update-user.js

103 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-05-12 14:54:58 +00:00
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
2023-05-18 11:43:52 +00:00
/**
2023-05-18 11:51:11 +00:00
* @typedef {object} FunctionReturn
2023-05-18 11:43:52 +00:00
* @property {boolean} success - Did the function run successfully?
* @property {(Object[]|string)} [payload=[]] - Payload
*/
2023-05-12 14:54:58 +00:00
/**
* ==============================================================================
* Main Function
* ==============================================================================
2023-05-18 11:43:52 +00:00
* @async
*
* @param {object} params - API Key
* @param {String} params.key - API Key
* @param {String} params.database - Target Database
2023-05-23 13:01:49 +00:00
* @param {{ id: number }} params.payload - User Object
2023-05-18 11:43:52 +00:00
*
2023-05-22 07:51:33 +00:00
* @returns { Promise<FunctionReturn>}
2023-05-12 14:54:58 +00:00
*/
2023-07-07 19:13:13 +00:00
async function updateUser({ key, payload, database }) {
2023-08-12 15:46:00 +00:00
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
2023-05-12 14:54:58 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
payload,
database,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
2023-05-22 10:52:27 +00:00
"Content-Length": Buffer.from(reqPayload).length,
2023-05-12 14:54:58 +00:00
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/update-user`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
2023-07-07 19:13:13 +00:00
}
2023-05-12 14:54:58 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
module.exports = updateUser;