dsql-admin/dsql-app/.local_dist/server/chunks/613.js
2024-11-05 12:12:42 +01:00

184 lines
8.4 KiB
JavaScript

"use strict";
exports.id = 613;
exports.ids = [613];
exports.modules = {
/***/ 613:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const fs = __webpack_require__(7147);
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const datasquirel = __webpack_require__(9538);
const serverError = __webpack_require__(2163);
const DB_HANDLER = __webpack_require__(2224);
const addDbEntry = __webpack_require__(5338);
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* Add Admin User on Login
* ==============================================================================
*
* @description this function handles admin users that have been invited by another
* admin user. This fires when the invited user has been logged in or a new account
* has been created for the invited user
*
* @param {object} params - parameters object
*
* @param {object} params.query - query object
* @param {number} params.query.invite - Invitation user id
* @param {string} params.query.database_access - String containing authorized databases
* @param {string} params.query.priviledge - String containing databases priviledges
* @param {string} params.query.email - Inviting user email address
*
* @param {import("@/package-shared/types").UserType} params.user - invited user object
*
* @returns {Promise<any>} new user auth object payload
*/ module.exports = async function addAdminUserOnLogin({ query , user }) {
try {
/**
* Fetch user
*
* @description Fetch user from db
*/ // @ts-ignore
const { invite , database_access , priviledge , email } = query;
const lastInviteTimeArray = await DB_HANDLER(`SELECT date_created_code FROM invitations WHERE inviting_user_id=? AND invited_user_email=?`, [
invite,
email
]);
// if (lastInviteTimeArray && lastInviteTimeArray[0]?.date_created_code) {
// const timeSinceLastInvite = Date.now() - parseInt(lastInviteTimeArray[0].date_created_code);
// if (timeSinceLastInvite > 21600000) {
// throw new Error("Invitation expired");
// }
// } else if (!lastInviteTimeArray || !lastInviteTimeArray[0]) {
// throw new Error("No Invitation Found");
// }
if (!lastInviteTimeArray || !lastInviteTimeArray[0]) {
throw new Error("No Invitation Found");
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
// @ts-ignore
const invitingUserDb = await DB_HANDLER(`SELECT first_name,last_name,email FROM users WHERE id=?`, [
invite
]);
if (invitingUserDb?.[0]) {
const existingUserUser = await DB_HANDLER(`SELECT email FROM user_users WHERE user_id=? AND invited_user_id=? AND user_type='admin' AND email=?`, [
invite,
user.id,
email
]);
if (existingUserUser?.[0]) {
console.log("User already added");
} else {
// const newUserUser = await DB_HANDLER(
// `INSERT IGNORE INTO user_users
// (user_id, invited_user_id, database_access, first_name, last_name, phone, email, username, user_type, user_priviledge)
// VALUES
// (?,?,?,?,?,?,?,?,?,?)
// )`,
// [
// invite,
// user.id,
// database_access,
// user.first_name,
// user.last_name,
// user.phone,
// user.email,
// user.username,
// "admin",
// priviledge,
// ]
// );
addDbEntry({
dbFullName: "datasquirel",
tableName: "user_users",
data: {
user_id: invite,
invited_user_id: user.id,
database_access: database_access,
first_name: user.first_name,
last_name: user.last_name,
phone: user.phone,
email: user.email,
username: user.username,
user_type: "admin",
user_priviledge: priviledge,
image: user.image,
image_thumbnail: user.image_thumbnail
}
});
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
// @ts-ignore
const dbTableData = await DB_HANDLER(`SELECT db_tables_data FROM invitations WHERE inviting_user_id=? AND invited_user_email=?`, [
invite,
email
]);
// @ts-ignore
const clearEntries = await DB_HANDLER(`DELETE FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=?`, [
invite,
user.id
]);
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
if (dbTableData && dbTableData[0]) {
const dbTableEntries = dbTableData[0].db_tables_data.split("|");
for(let i = 0; i < dbTableEntries.length; i++){
const dbTableEntry = dbTableEntries[i];
const dbTableEntryArray = dbTableEntry.split("-");
const [db_slug, table_slug] = dbTableEntryArray;
const newEntry = await addDbEntry({
dbFullName: "datasquirel",
tableName: "delegated_user_tables",
data: {
delegated_user_id: user.id,
root_user_id: invite,
database: db_slug,
table: table_slug,
priviledge: priviledge
}
});
}
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
}
// @ts-ignore
const inviteAccepted = await DB_HANDLER(`UPDATE invitations SET invitation_status='Accepted' WHERE inviting_user_id=? AND invited_user_email=?`, [
invite,
email
]);
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
} catch (/** @type {any} */ error) {
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
serverError({
component: "addAdminUserOnLogin",
message: error.message,
user: user
});
}
}; ////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
/***/ })
};
;