90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { AddUpdateMariadbUserAPIReqBody, UserType } from "../../../types";
|
|
import {
|
|
DSQL_DATASQUIREL_MARIADB_USERS,
|
|
DsqlTables,
|
|
} from "../../../types/dsql";
|
|
import grabSQLUserName from "../../../utils/grab-sql-user-name";
|
|
import addDbEntry from "../../backend/db/addDbEntry";
|
|
import encrypt from "../../dsql/encrypt";
|
|
import dbGrabUserResource from "../db/grab-user-resource";
|
|
|
|
type Params = AddUpdateMariadbUserAPIReqBody & {
|
|
user: UserType;
|
|
};
|
|
|
|
type Return = {
|
|
existingRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
|
updatedRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
|
msg?: string;
|
|
};
|
|
|
|
export default async function handleMariadbUserRecord({
|
|
mariadbUser,
|
|
accessedDatabases,
|
|
grants,
|
|
isAllDbsAccess,
|
|
isAllGrants,
|
|
user,
|
|
}: Params): Promise<Return> {
|
|
const { name: finalMariadbUserName } = grabSQLUserName({
|
|
name: mariadbUser.username,
|
|
user,
|
|
});
|
|
|
|
const finalPassword = mariadbUser.password?.replace(/ /g, "");
|
|
if (!finalPassword) return { msg: `Couldn't get password` };
|
|
|
|
const encryptedFinalPassword = encrypt({ data: finalPassword });
|
|
const finalHost = mariadbUser.host?.replace(/ /g, "");
|
|
|
|
const newMariadbUser: DSQL_DATASQUIREL_MARIADB_USERS = {
|
|
password: encryptedFinalPassword || undefined,
|
|
username: finalMariadbUserName,
|
|
all_databases: isAllDbsAccess ? 1 : 0,
|
|
all_grants: isAllGrants ? 1 : 0,
|
|
host: finalHost,
|
|
user_id: user.id,
|
|
};
|
|
|
|
let { single: existingRecord } =
|
|
await dbGrabUserResource<DSQL_DATASQUIREL_MARIADB_USERS>({
|
|
tableName: "mariadb_users",
|
|
userId: user.id,
|
|
query: {
|
|
query: {
|
|
id: {
|
|
value: String(mariadbUser.id || 0),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const record = await addDbEntry<
|
|
DSQL_DATASQUIREL_MARIADB_USERS,
|
|
(typeof DsqlTables)[number]
|
|
>({
|
|
tableName: "mariadb_users",
|
|
data: newMariadbUser,
|
|
update: true,
|
|
duplicateColumnName: "id",
|
|
duplicateColumnValue: (existingRecord?.id || 0).toString(),
|
|
});
|
|
|
|
let { single: updatedRecord } =
|
|
await dbGrabUserResource<DSQL_DATASQUIREL_MARIADB_USERS>({
|
|
tableName: "mariadb_users",
|
|
userId: user.id,
|
|
query: {
|
|
query: {
|
|
id: {
|
|
value: String(
|
|
existingRecord?.id || record?.payload?.insertId || 0
|
|
),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return { existingRecord, updatedRecord };
|
|
}
|