datasquirel/package-shared/functions/api/users/api-update-user.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-12-06 10:31:24 +00:00
// @ts-check
const updateDbEntry = require("../../backend/db/updateDbEntry");
/**
* # Update API User Function
*
* @param {object} params
* @param {{ id: string | number } & Object<string, (string | number | null | undefined)>} params.payload
* @param {string} params.dbFullName
2024-12-06 11:55:03 +00:00
* @param {boolean} [params.useLocal]
2024-12-06 10:31:24 +00:00
*
* @returns {Promise<{ success: boolean, payload: any }>}
*/
2024-12-06 11:55:03 +00:00
module.exports = async function apiUpdateUser({
payload,
dbFullName,
useLocal,
}) {
2024-12-06 10:31:24 +00:00
const data = (() => {
const reqBodyKeys = Object.keys(payload);
/** @type {any} */
const finalData = {};
reqBodyKeys.forEach((key) => {
if (key?.match(/^date_|^id$/)) return;
finalData[key] = payload[key];
});
return finalData;
})();
const updateUser = await updateDbEntry({
dbContext: "Dsql User",
paradigm: "Full Access",
dbFullName,
tableName: "users",
identifierColumnName: "id",
identifierValue: payload.id,
data: data,
2024-12-06 11:55:03 +00:00
useLocal,
2024-12-06 10:31:24 +00:00
});
return {
success: true,
payload: updateUser,
};
};