222 lines
10 KiB
JavaScript
222 lines
10 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = handleSocialDb;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const handleNodemailer_1 = __importDefault(require("../../backend/handleNodemailer"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const addMariadbUser_1 = __importDefault(require("../../backend/addMariadbUser"));
|
|
const varDatabaseDbHandler_1 = __importDefault(require("../../backend/varDatabaseDbHandler"));
|
|
const encrypt_1 = __importDefault(require("../../dsql/encrypt"));
|
|
const addDbEntry_1 = __importDefault(require("../../backend/db/addDbEntry"));
|
|
const loginSocialUser_1 = __importDefault(require("./loginSocialUser"));
|
|
/**
|
|
* # Handle Social DB
|
|
*/
|
|
function handleSocialDb(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ database, social_id, email, social_platform, payload, invitation, supEmail, additionalFields, debug, }) {
|
|
var _b;
|
|
try {
|
|
const finalDbName = global.DSQL_USE_LOCAL
|
|
? undefined
|
|
: database
|
|
? database
|
|
: "datasquirel";
|
|
const dbAppend = global.DSQL_USE_LOCAL ? "" : `${finalDbName}.`;
|
|
const existingSocialIdUserQuery = `SELECT * FROM ${dbAppend}users WHERE social_id = ? AND social_login='1' AND social_platform = ? `;
|
|
const existingSocialIdUserValues = [
|
|
social_id.toString(),
|
|
social_platform,
|
|
];
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingSocialIdUserQuery", existingSocialIdUserQuery);
|
|
console.log("handleSocialDb:existingSocialIdUserValues", existingSocialIdUserValues);
|
|
}
|
|
let existingSocialIdUser = yield (0, varDatabaseDbHandler_1.default)({
|
|
database: finalDbName,
|
|
queryString: existingSocialIdUserQuery,
|
|
queryValuesArray: existingSocialIdUserValues,
|
|
debug,
|
|
});
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingSocialIdUser", existingSocialIdUser);
|
|
}
|
|
if (existingSocialIdUser === null || existingSocialIdUser === void 0 ? void 0 : existingSocialIdUser[0]) {
|
|
return yield (0, loginSocialUser_1.default)({
|
|
user: existingSocialIdUser[0],
|
|
social_platform,
|
|
invitation,
|
|
database: finalDbName,
|
|
additionalFields,
|
|
debug,
|
|
});
|
|
}
|
|
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 = yield (0, varDatabaseDbHandler_1.default)({
|
|
database: finalDbName,
|
|
queryString: existingEmailOnlyQuery,
|
|
debug,
|
|
});
|
|
if (debug) {
|
|
console.log("handleSocialDb:existingEmailOnly", existingEmailOnly);
|
|
}
|
|
if (existingEmailOnly && existingEmailOnly[0]) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "This Email is already taken",
|
|
};
|
|
}
|
|
const foundUserQuery = `SELECT * FROM ${dbAppend}users WHERE email=? AND social_login='1' AND social_platform=? AND social_id=?`;
|
|
const foundUserQueryValues = [finalEmail, social_platform, social_id];
|
|
const foundUser = yield (0, varDatabaseDbHandler_1.default)({
|
|
database: finalDbName,
|
|
queryString: foundUserQuery,
|
|
queryValuesArray: foundUserQueryValues,
|
|
debug,
|
|
});
|
|
if (foundUser && foundUser[0]) {
|
|
return yield (0, loginSocialUser_1.default)({
|
|
user: payload,
|
|
social_platform,
|
|
invitation,
|
|
database: finalDbName,
|
|
additionalFields,
|
|
debug,
|
|
});
|
|
}
|
|
const socialHashedPassword = (0, encrypt_1.default)({
|
|
data: social_id.toString(),
|
|
});
|
|
const data = {
|
|
social_login: "1",
|
|
verification_status: supEmail ? "0" : "1",
|
|
password: socialHashedPassword,
|
|
};
|
|
Object.keys(payload).forEach((key) => {
|
|
data[key] = payload[key];
|
|
});
|
|
const newUser = yield (0, addDbEntry_1.default)({
|
|
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 (newUser === null || newUser === void 0 ? void 0 : newUser.insertId) {
|
|
if (!database) {
|
|
/**
|
|
* Add a Mariadb User for this User
|
|
*/
|
|
yield (0, addMariadbUser_1.default)({ userId: newUser.insertId });
|
|
}
|
|
const newUserQueriedQuery = `SELECT * FROM ${dbAppend}users WHERE id='${newUser.insertId}'`;
|
|
const newUserQueried = yield (0, varDatabaseDbHandler_1.default)({
|
|
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 = (0, encrypt_1.default)({
|
|
data: JSON.stringify({
|
|
id: newUser.insertId,
|
|
email: supEmail,
|
|
dateCode: Date.now(),
|
|
}),
|
|
});
|
|
(0, handleNodemailer_1.default)({
|
|
to: supEmail,
|
|
subject: "Verify Email Address",
|
|
text: "Please click the link to verify your email address",
|
|
html: fs_1.default
|
|
.readFileSync("./email/send-email-verification-link.html", "utf8")
|
|
.replace(/{{host}}/, process.env.DSQL_HOST || "")
|
|
.replace(/{{token}}/, generatedToken || ""),
|
|
}).then(() => { });
|
|
}
|
|
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
|
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.insertId}`;
|
|
let newUserMediaFolderPath = path_1.default.join(STATIC_ROOT, `images/user-images/user-${newUser.insertId}`);
|
|
fs_1.default.mkdirSync(newUserSchemaFolderPath);
|
|
fs_1.default.mkdirSync(newUserMediaFolderPath);
|
|
fs_1.default.writeFileSync(`${newUserSchemaFolderPath}/main.json`, JSON.stringify([]), "utf8");
|
|
}
|
|
return yield (0, loginSocialUser_1.default)({
|
|
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,
|
|
};
|
|
}
|
|
});
|
|
}
|