202 lines
8.1 KiB
JavaScript
202 lines
8.1 KiB
JavaScript
import fs from "fs";
|
|
import handleNodemailer from "../../backend/handleNodemailer";
|
|
import path from "path";
|
|
import addMariadbUser from "../../backend/addMariadbUser";
|
|
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
|
import encrypt from "../../dsql/encrypt";
|
|
import addDbEntry from "../../backend/db/addDbEntry";
|
|
import loginSocialUser from "./loginSocialUser";
|
|
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
|
/**
|
|
* # Handle Social DB
|
|
*/
|
|
export default async function handleSocialDb({ database, email, social_platform, payload, invitation, supEmail, additionalFields, debug, loginOnly, }) {
|
|
var _a, _b;
|
|
try {
|
|
const finalDbName = global.DSQL_USE_LOCAL
|
|
? undefined
|
|
: database
|
|
? database
|
|
: "datasquirel";
|
|
const dbAppend = global.DSQL_USE_LOCAL ? "" : `${finalDbName}.`;
|
|
const existingSocialUserQUery = `SELECT * FROM ${dbAppend}users WHERE email = ? AND social_login='1' AND social_platform = ? `;
|
|
const existingSocialUserValues = [email, social_platform];
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingSocialUserQUery", existingSocialUserQUery);
|
|
console.log("handleSocialDb:existingSocialUserValues", existingSocialUserValues);
|
|
}
|
|
let existingSocialUser = await varDatabaseDbHandler({
|
|
database: finalDbName,
|
|
queryString: existingSocialUserQUery,
|
|
queryValuesArray: existingSocialUserValues,
|
|
debug,
|
|
});
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingSocialUser", existingSocialUser);
|
|
}
|
|
if (existingSocialUser === null || existingSocialUser === void 0 ? void 0 : existingSocialUser[0]) {
|
|
return await loginSocialUser({
|
|
user: existingSocialUser[0],
|
|
social_platform,
|
|
invitation,
|
|
database: finalDbName,
|
|
additionalFields,
|
|
debug,
|
|
});
|
|
}
|
|
else if (loginOnly) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "User Does not Exist",
|
|
};
|
|
}
|
|
const finalEmail = email ? email : supEmail ? supEmail : null;
|
|
if (!finalEmail) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "No Email Present",
|
|
};
|
|
}
|
|
const existingEmailOnlyQuery = `SELECT * FROM ${dbAppend}users WHERE email='${finalEmail}'`;
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingEmailOnlyQuery", existingEmailOnlyQuery);
|
|
}
|
|
let existingEmailOnly = await varDatabaseDbHandler({
|
|
database: finalDbName,
|
|
queryString: existingEmailOnlyQuery,
|
|
debug,
|
|
});
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingEmailOnly", existingEmailOnly);
|
|
}
|
|
if (existingEmailOnly === null || existingEmailOnly === void 0 ? void 0 : existingEmailOnly[0]) {
|
|
return await loginSocialUser({
|
|
user: existingEmailOnly[0],
|
|
social_platform,
|
|
invitation,
|
|
database: finalDbName,
|
|
additionalFields,
|
|
debug,
|
|
});
|
|
}
|
|
else if (loginOnly) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Social Account Creation Not allowed",
|
|
};
|
|
}
|
|
const socialHashedPassword = encrypt({
|
|
data: email,
|
|
});
|
|
const data = {
|
|
social_login: "1",
|
|
verification_status: supEmail ? "0" : "1",
|
|
password: socialHashedPassword,
|
|
};
|
|
Object.keys(payload).forEach((key) => {
|
|
data[key] = payload[key];
|
|
});
|
|
const newUser = await addDbEntry({
|
|
dbContext: finalDbName ? "Dsql User" : undefined,
|
|
paradigm: finalDbName ? "Full Access" : undefined,
|
|
dbFullName: finalDbName,
|
|
tableName: "users",
|
|
duplicateColumnName: "email",
|
|
duplicateColumnValue: finalEmail,
|
|
data: Object.assign(Object.assign({}, data), { email: finalEmail }),
|
|
});
|
|
if ((_a = newUser === null || newUser === void 0 ? void 0 : newUser.payload) === null || _a === void 0 ? void 0 : _a.insertId) {
|
|
if (!database) {
|
|
/**
|
|
* Add a Mariadb User for this User
|
|
*/
|
|
await addMariadbUser({ userId: newUser.payload.insertId });
|
|
}
|
|
const newUserQueriedQuery = `SELECT * FROM ${dbAppend}users WHERE id='${newUser.payload.insertId}'`;
|
|
const newUserQueried = await varDatabaseDbHandler({
|
|
database: finalDbName,
|
|
queryString: newUserQueriedQuery,
|
|
debug,
|
|
});
|
|
if (!newUserQueried || !newUserQueried[0])
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "User Insertion Failed!",
|
|
};
|
|
if (supEmail && (database === null || database === void 0 ? void 0 : database.match(/^datasquirel$/))) {
|
|
/**
|
|
* Send email Verification
|
|
*
|
|
* @description Send verification email to newly created agent
|
|
*/
|
|
let generatedToken = encrypt({
|
|
data: JSON.stringify({
|
|
id: newUser.payload.insertId,
|
|
email: supEmail,
|
|
dateCode: Date.now(),
|
|
}),
|
|
});
|
|
handleNodemailer({
|
|
to: supEmail,
|
|
subject: "Verify Email Address",
|
|
text: "Please click the link to verify your email address",
|
|
html: fs
|
|
.readFileSync("./email/send-email-verification-link.html", "utf8")
|
|
.replace(/{{host}}/, process.env.DSQL_HOST || "")
|
|
.replace(/{{token}}/, generatedToken || ""),
|
|
}).then(() => { });
|
|
}
|
|
const { STATIC_ROOT } = grabDirNames();
|
|
if (!STATIC_ROOT) {
|
|
console.log("Static File ENV not Found!");
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Static File ENV not Found!",
|
|
};
|
|
}
|
|
/**
|
|
* Create new user folder and file
|
|
*
|
|
* @description Create new user folder and file
|
|
*/
|
|
if (!database || (database === null || database === void 0 ? void 0 : database.match(/^datasquirel$/))) {
|
|
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
|
let newUserMediaFolderPath = path.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}`);
|
|
fs.mkdirSync(newUserSchemaFolderPath);
|
|
fs.mkdirSync(newUserMediaFolderPath);
|
|
fs.writeFileSync(`${newUserSchemaFolderPath}/main.json`, JSON.stringify([]), "utf8");
|
|
}
|
|
return await loginSocialUser({
|
|
user: newUserQueried[0],
|
|
social_platform,
|
|
invitation,
|
|
database: finalDbName,
|
|
additionalFields,
|
|
debug,
|
|
});
|
|
}
|
|
else {
|
|
console.log("Social User Failed to insert in 'handleSocialDb.ts' backend function =>", newUser);
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Social User Failed to insert in 'handleSocialDb.ts' backend function",
|
|
};
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.log("ERROR in 'handleSocialDb.ts' backend function =>", error.message);
|
|
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `Handle Social DB Error`, error);
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: error.message,
|
|
};
|
|
}
|
|
}
|