datasquirel/users/send-email-code.js

182 lines
6.0 KiB
JavaScript
Raw Normal View History

2024-07-13 07:09:22 +00:00
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const http = require("http");
const fs = require("fs");
const path = require("path");
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 apiSendEmailCode = require("../package-shared/functions/api/users/api-send-email-code");
2024-07-13 07:09:22 +00:00
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Send Email Code to a User
* ==============================================================================
* @async
*
* @param {object} params - Single Param object containing params
2024-12-10 13:09:21 +00:00
* @param {String} [params.key] - FULL ACCESS API Key
* @param {String} [params.database] - Target Database
2024-07-13 07:09:22 +00:00
* @param {string} params.email Login Email/Username and Password
2024-12-10 13:09:21 +00:00
* @param {string} [params.temp_code_field_name] - Database table field name for temporary code
2024-12-10 14:10:32 +00:00
* @param {http.ServerResponse & Object<string,any>} [params.response]
2024-07-13 10:37:46 +00:00
* @param {string} [params.mail_domain]
* @param {string} [params.mail_username]
* @param {string} [params.mail_password]
2024-07-13 10:17:30 +00:00
* @param {number} [params.mail_port]
* @param {string} [params.sender]
* @param {boolean} [params.user_id] - User ID
2024-12-06 11:55:03 +00:00
* @param {boolean} [params.useLocal]
2024-07-13 07:09:22 +00:00
*
2024-12-10 14:10:32 +00:00
* @returns { Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse>}
2024-07-13 07:09:22 +00:00
*/
async function sendEmailCode({
key,
email,
database,
2024-12-10 13:09:21 +00:00
temp_code_field_name,
2024-07-13 10:17:30 +00:00
mail_domain,
mail_password,
mail_username,
mail_port,
sender,
user_id,
2024-12-06 11:55:03 +00:00
useLocal,
2024-12-10 14:10:32 +00:00
response,
2024-07-13 07:09:22 +00:00
}) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
2024-07-13 07:09:22 +00:00
const defaultTempLoginFieldName = "temp_login_code";
2024-12-10 13:09:21 +00:00
const emailLoginTempCodeFieldName = temp_code_field_name
? temp_code_field_name
2024-07-13 07:09:22 +00:00
: defaultTempLoginFieldName;
2024-12-06 10:31:24 +00:00
const emailHtml = fs.readFileSync(
path.resolve(__dirname, "../package-shared/html/one-time-code.html"),
"utf-8"
);
2024-07-13 07:09:22 +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 } =
process.env;
2024-07-13 07:09:22 +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 11:55:03 +00:00
DSQL_DB_NAME?.match(/./) &&
useLocal
2024-07-13 07:09:22 +00:00
) {
2024-10-19 16:45:42 +00:00
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
2024-07-13 07:09:22 +00:00
let dbSchema;
try {
const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
2024-12-10 14:10:32 +00:00
return await apiSendEmailCode({
2024-12-09 11:45:39 +00:00
database: DSQL_DB_NAME,
email,
email_login_field: emailLoginTempCodeFieldName,
html: emailHtml,
mail_domain,
mail_password,
mail_port,
mail_username,
sender,
useLocal,
2024-12-10 14:10:32 +00:00
response,
2024-12-09 11:45:39 +00:00
});
2024-07-13 07:09:22 +00:00
} else {
/**
* Make https request
*
* @description make a request to datasquirel.com
*
2024-12-10 14:10:32 +00:00
* @type {import("../package-shared/types").SendOneTimeCodeEmailResponse}
2024-07-13 07:09:22 +00:00
*/
2024-12-10 14:10:32 +00:00
const httpResponse = await new Promise((resolve, reject) => {
2024-07-13 07:09:22 +00:00
const reqPayload = JSON.stringify({
email,
database,
email_login_field: emailLoginTempCodeFieldName,
2024-07-13 10:17:30 +00:00
mail_domain,
mail_password,
mail_username,
mail_port,
sender,
2024-12-06 10:31:24 +00:00
html: emailHtml,
2024-07-13 07:09:22 +00:00
});
2024-11-13 13:13:10 +00:00
const httpsRequest = scheme.request(
2024-07-13 07:09:22 +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,
2024-07-13 07:09:22 +00:00
},
2024-11-13 13:13:10 +00:00
port,
hostname: host,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/send-email-code`,
2024-07-13 07:09:22 +00:00
},
/**
* Callback Function
*
* @description https request callback
*/
(res) => {
var str = "";
res.on("data", function (chunk) {
str += chunk;
});
res.on("end", function () {
resolve(JSON.parse(str));
});
res.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
2024-12-10 14:10:32 +00:00
return httpResponse;
2024-07-13 07:09:22 +00:00
}
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = sendEmailCode;