datasquirel/users/add-user.js

136 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
const path = require("path");
const fs = require("fs");
2024-11-13 13:13:10 +00:00
const grabHostNames = require("../package-shared/utils/grab-host-names");
2024-12-06 10:31:24 +00:00
const apiCreateUser = require("../package-shared/functions/api/users/api-create-user");
2023-09-21 14:00:04 +00:00
/**
* Add User to Database
2024-12-06 10:31:24 +00:00
* ============================================
2023-09-21 14:00:04 +00:00
* @async
*
* @param {object} param - Single object passed
2024-12-08 08:58:57 +00:00
* @param {string} [param.key] - FULL ACCESS API Key
2024-12-10 12:45:30 +00:00
* @param {string} [param.database] - Database Name
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
2024-12-06 11:55:03 +00:00
* @param {string} [param.encryptionKey]
* @param {string} [param.encryptionSalt]
2024-12-06 10:31:24 +00:00
* @param {string | number} [param.user_id]
2024-12-06 10:53:26 +00:00
* @param {string | number} [param.apiUserId]
2024-12-06 10:31:24 +00:00
* @param {boolean} [param.useLocal]
2023-09-21 14:00:04 +00:00
*
2024-10-19 16:45:42 +00:00
* @returns { Promise<import("../package-shared/types").AddUserFunctionReturn> }
2023-09-21 14:00:04 +00:00
*/
2024-08-22 11:59:50 +00:00
async function addUser({
key,
payload,
database,
encryptionKey,
user_id,
2024-12-06 10:31:24 +00:00
useLocal,
apiUserId,
2024-08-22 11:59:50 +00:00
}) {
2023-09-21 14:00:04 +00:00
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
2024-12-09 11:45:39 +00:00
const {
DSQL_DB_HOST,
DSQL_DB_USERNAME,
DSQL_DB_PASSWORD,
DSQL_DB_NAME,
DSQL_API_USER_ID,
} = process.env;
2023-09-21 14:00:04 +00:00
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
2023-09-21 16:51:08 +00:00
if (
2024-12-09 11:45:39 +00:00
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
2024-12-06 10:31:24 +00:00
DSQL_DB_NAME?.match(/./) &&
useLocal
2023-09-21 16:51:08 +00:00
) {
2024-10-19 16:45:42 +00:00
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
2023-09-21 14:00:04 +00:00
let dbSchema;
try {
2023-09-21 16:51:08 +00:00
const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
2023-09-21 14:00:04 +00:00
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
2024-12-09 11:45:39 +00:00
return await apiCreateUser({
database: DSQL_DB_NAME,
encryptionKey,
payload,
userId: apiUserId,
useLocal,
});
2023-09-21 14:00:04 +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,
2024-08-22 11:59:50 +00:00
encryptionKey,
2023-09-21 14:00:04 +00:00
});
2024-11-13 13:13:10 +00:00
const httpsRequest = scheme.request(
2023-09-21 14:00:04 +00:00
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
2024-11-15 14:37:53 +00:00
Authorization:
key ||
process.env.DSQL_FULL_ACCESS_API_KEY ||
process.env.DSQL_API_KEY,
2023-09-21 14:00:04 +00:00
},
2024-11-13 13:13:10 +00:00
port,
hostname: host,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/add-user`,
2023-09-21 14:00:04 +00:00
},
/**
* 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;
}
module.exports = addUser;