Updates
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import path from "path";
|
||||
|
||||
import { OutgoingHttpHeaders } from "http";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DataCrudRequestMethods,
|
||||
DataCrudRequestMethodsLowerCase,
|
||||
} from "../../types";
|
||||
import grabHostNames from "../../utils/grab-host-names";
|
||||
import serializeQuery from "../../utils/serialize-query";
|
||||
|
||||
type Param<T = { [k: string]: any }> = {
|
||||
key?: string;
|
||||
body?: T;
|
||||
query?: T;
|
||||
useDefault?: boolean;
|
||||
path: string;
|
||||
method?:
|
||||
| (typeof DataCrudRequestMethods)[number]
|
||||
| (typeof DataCrudRequestMethodsLowerCase)[number];
|
||||
};
|
||||
|
||||
/**
|
||||
* # Query DSQL API
|
||||
*/
|
||||
export default async function queryDSQLAPI<
|
||||
T = { [k: string]: any },
|
||||
P = { [k: string]: any }
|
||||
>({
|
||||
key,
|
||||
body,
|
||||
query,
|
||||
useDefault,
|
||||
path: passedPath,
|
||||
method,
|
||||
}: Param<T>): Promise<APIResponseObject<P>> {
|
||||
const grabedHostNames = grabHostNames({ useDefault });
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
try {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = await new Promise((resolve, reject) => {
|
||||
const reqPayload = body ? JSON.stringify(body) : undefined;
|
||||
|
||||
let headers: OutgoingHttpHeaders = {
|
||||
"Content-Type": "application/json",
|
||||
Authorization:
|
||||
key ||
|
||||
(!method || method == "GET" || method == "get"
|
||||
? process.env.DSQL_READ_ONLY_API_KEY
|
||||
: undefined) ||
|
||||
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
||||
process.env.DSQL_API_KEY,
|
||||
};
|
||||
|
||||
if (reqPayload) {
|
||||
headers["Content-Length"] = Buffer.from(reqPayload).length;
|
||||
}
|
||||
|
||||
let finalPath = path.join("/", passedPath);
|
||||
|
||||
if (query) {
|
||||
const queryString = serializeQuery(query);
|
||||
finalPath += `${queryString}`;
|
||||
}
|
||||
|
||||
const httpsRequest = scheme.request(
|
||||
{
|
||||
method: method || "GET",
|
||||
headers,
|
||||
port,
|
||||
hostname: host,
|
||||
path: finalPath,
|
||||
},
|
||||
|
||||
/**
|
||||
* 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);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (reqPayload) {
|
||||
httpsRequest.write(reqPayload);
|
||||
}
|
||||
httpsRequest.end();
|
||||
});
|
||||
|
||||
return httpResponse as APIResponseObject<P>;
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -73,11 +73,8 @@ export default async function apiGet<
|
||||
if (targetTable) {
|
||||
const clonedTargetTable = _.cloneDeep(targetTable);
|
||||
delete clonedTargetTable.childTable;
|
||||
delete clonedTargetTable.childTableDbFullName;
|
||||
delete clonedTargetTable.childTableName;
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.tableNameOld;
|
||||
delete clonedTargetTable.indexes;
|
||||
tableSchema = clonedTargetTable;
|
||||
}
|
||||
|
||||
@@ -48,9 +48,9 @@ export default async function apiPost({
|
||||
*/
|
||||
try {
|
||||
let { result, error } = await runQuery({
|
||||
dbFullName: dbFullName,
|
||||
query: query,
|
||||
dbSchema: dbSchema,
|
||||
dbFullName,
|
||||
query,
|
||||
dbSchema,
|
||||
queryValuesArray: queryValues,
|
||||
tableName,
|
||||
dbContext,
|
||||
@@ -89,11 +89,8 @@ export default async function apiPost({
|
||||
const clonedTargetTable = _.cloneDeep(targetTable);
|
||||
|
||||
delete clonedTargetTable.childTable;
|
||||
delete clonedTargetTable.childTableDbFullName;
|
||||
delete clonedTargetTable.childTableName;
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.tableNameOld;
|
||||
delete clonedTargetTable.indexes;
|
||||
|
||||
tableSchema = clonedTargetTable;
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
APILoginFunctionReturn,
|
||||
HandleSocialDbFunctionParams,
|
||||
} from "../../../types";
|
||||
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
||||
|
||||
/**
|
||||
* # Handle Social DB
|
||||
@@ -151,15 +152,15 @@ export default async function handleSocialDb({
|
||||
},
|
||||
});
|
||||
|
||||
if (newUser?.insertId) {
|
||||
if (newUser?.payload?.insertId) {
|
||||
if (!database) {
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
await addMariadbUser({ userId: newUser.payload.insertId });
|
||||
}
|
||||
|
||||
const newUserQueriedQuery = `SELECT * FROM ${dbAppend}users WHERE id='${newUser.insertId}'`;
|
||||
const newUserQueriedQuery = `SELECT * FROM ${dbAppend}users WHERE id='${newUser.payload.insertId}'`;
|
||||
|
||||
const newUserQueried = await varDatabaseDbHandler({
|
||||
database: finalDbName,
|
||||
@@ -182,7 +183,7 @@ export default async function handleSocialDb({
|
||||
*/
|
||||
let generatedToken = encrypt({
|
||||
data: JSON.stringify({
|
||||
id: newUser.insertId,
|
||||
id: newUser.payload.insertId,
|
||||
email: supEmail,
|
||||
dateCode: Date.now(),
|
||||
}),
|
||||
@@ -202,7 +203,7 @@ export default async function handleSocialDb({
|
||||
}).then(() => {});
|
||||
}
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
const { STATIC_ROOT } = grabDirNames();
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
@@ -219,11 +220,11 @@ export default async function handleSocialDb({
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
if (!database || database?.match(/^datasquirel$/)) {
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// @ts-check
|
||||
|
||||
import { findDbNameInSchemaDir } from "../../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import { APICreateUserFunctionParams } from "../../../types";
|
||||
import addUsersTableToDb from "../../backend/addUsersTableToDb";
|
||||
import addDbEntry from "../../backend/db/addDbEntry";
|
||||
@@ -39,6 +38,19 @@ export default async function apiCreateUser({
|
||||
};
|
||||
}
|
||||
|
||||
const targetDbSchema = findDbNameInSchemaDir({
|
||||
dbName: dbFullName,
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetDbSchema?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "targetDbSchema not found",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
const hashedPassword = hashPassword({
|
||||
encryptionKey: finalEncryptionKey,
|
||||
password: String(payload.password),
|
||||
@@ -57,8 +69,8 @@ export default async function apiCreateUser({
|
||||
const newTable = await addUsersTableToDb({
|
||||
userId: Number(API_USER_ID),
|
||||
database: dbFullName,
|
||||
|
||||
payload: payload,
|
||||
dbId: targetDbSchema.id,
|
||||
});
|
||||
|
||||
fields = await varDatabaseDbHandler({
|
||||
@@ -87,6 +99,7 @@ export default async function apiCreateUser({
|
||||
newPayload: {
|
||||
[key]: payload[key],
|
||||
},
|
||||
dbId: targetDbSchema.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -143,8 +156,8 @@ export default async function apiCreateUser({
|
||||
},
|
||||
});
|
||||
|
||||
if (addUser?.insertId) {
|
||||
const newlyAddedUserQuery = `SELECT id,uuid,first_name,last_name,email,username,image,image_thumbnail,verification_status FROM ${dbFullName}.users WHERE id='${addUser.insertId}'`;
|
||||
if (addUser?.payload?.insertId) {
|
||||
const newlyAddedUserQuery = `SELECT id,uuid,first_name,last_name,email,username,image,image_thumbnail,verification_status FROM ${dbFullName}.users WHERE id='${addUser.payload.insertId}'`;
|
||||
|
||||
const newlyAddedUser = await varDatabaseDbHandler({
|
||||
queryString: newlyAddedUserQuery,
|
||||
|
||||
@@ -26,6 +26,14 @@ export default async function apiLoginUser({
|
||||
debug,
|
||||
}: APILoginFunctionParams): Promise<APILoginFunctionReturn> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
if (!dbFullName) {
|
||||
console.log(`Database Full Name couldn't be grabbed`);
|
||||
return {
|
||||
success: false,
|
||||
msg: `Database Full Name couldn't be grabbed`,
|
||||
};
|
||||
}
|
||||
const dbAppend = global.DSQL_USE_LOCAL ? "" : `${dbFullName}.`;
|
||||
|
||||
/**
|
||||
@@ -152,6 +160,8 @@ export default async function apiLoginUser({
|
||||
|
||||
let userPayload: DATASQUIREL_LoggedInUser = {
|
||||
id: foundUser[0].id,
|
||||
uid: foundUser[0].uid,
|
||||
uuid: foundUser[0].uuid,
|
||||
first_name: foundUser[0].first_name,
|
||||
last_name: foundUser[0].last_name,
|
||||
username: foundUser[0].username,
|
||||
|
||||
@@ -30,11 +30,13 @@ export default async function apiSendResetPasswordLink({
|
||||
}: Param): Promise<Return> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
*
|
||||
* @description Check input validity
|
||||
*/
|
||||
if (!dbFullName) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Couldn't get database full name`,
|
||||
};
|
||||
}
|
||||
|
||||
if (email?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
|
||||
Reference in New Issue
Block a user