datasquirel/package-shared/functions/api/users/api-get-user.ts

38 lines
891 B
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import {
APIGetUserFunctionParams,
GetUserFunctionReturn,
} from "../../../types";
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
2024-12-06 10:31:24 +00:00
2025-01-10 19:10:28 +00:00
/**
* # API Get User
*/
export default async function apiGetUser({
2024-12-06 11:55:03 +00:00
fields,
dbFullName,
userId,
useLocal,
2025-01-10 19:10:28 +00:00
}: APIGetUserFunctionParams): Promise<GetUserFunctionReturn> {
2024-12-06 10:31:24 +00:00
const query = `SELECT ${fields.join(",")} FROM users WHERE id=?`;
2024-12-09 11:45:39 +00:00
const API_USER_ID = userId || process.env.DSQL_API_USER_ID;
2024-12-06 10:31:24 +00:00
2024-12-08 08:58:57 +00:00
let foundUser = await varDatabaseDbHandler({
queryString: query,
2024-12-09 11:45:39 +00:00
queryValuesArray: [API_USER_ID],
2024-12-08 08:58:57 +00:00
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
useLocal,
});
2024-12-06 10:31:24 +00:00
if (!foundUser || !foundUser[0]) {
return {
success: false,
payload: null,
};
}
return {
success: true,
payload: foundUser[0],
};
2025-01-10 19:10:28 +00:00
}