feat(produser): added in the function to create a standard user based on there username

This commit is contained in:
2025-06-12 21:10:46 -05:00
parent 3283972809
commit 99ad79c662
13 changed files with 438 additions and 35 deletions

View File

@@ -0,0 +1,23 @@
import { db } from "../../../../database/dbclient.js";
import { prodPermissions } from "../../../../database/schema/prodPermissions.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
export const getProdRoles = async () => {
const { data, error } = (await tryCatch(
db.select().from(prodPermissions)
)) as any;
if (error) {
return {
success: false,
message: "Error getting prod permissions",
data: error,
};
}
return {
success: true,
message: "Current prod permissions",
data: data,
};
};

View File

@@ -28,7 +28,7 @@ export const prodUser = async (data: any) => {
if (pe) {
console.log(pe);
return {
succes: false,
success: false,
message: "There was an error getting the base prod permissions",
data: pe,
};
@@ -39,7 +39,7 @@ export const prodUser = async (data: any) => {
if (permRoleCheck.length === 0) {
return {
succes: false,
success: false,
message: `Role: ${data.role}, dose note exist please check the role you have selected and try again.`,
data: [],
};
@@ -55,6 +55,8 @@ export const prodUser = async (data: any) => {
console.log(userError);
}
//console.log(permRoleCheck);
if (usercheck?.data.length === 0) {
// create the user
const newUser: any = {
@@ -69,9 +71,7 @@ export const prodUser = async (data: any) => {
const { data: newU, error: newE } = (await tryCatch(
axios.post(newurl, newUser, {
headers: {
Authorization: `Basic ${btoa(
`matthes01:99Monsters200Scary!`
)}`,
"X-API-Key": process.env.TEC_API_KEY || "",
"Content-Type": "application/json",
},
})
@@ -80,42 +80,40 @@ export const prodUser = async (data: any) => {
if (newE) {
console.log(newE);
return {
succes: false,
success: false,
message: `${data.username} encountered an error creating..`,
data: newE.response.data,
};
}
return {
succes: true,
success: true,
message: `${data.username} was just created or updated.`,
data: [],
};
} else {
// revoke and readd
const revokePerms: any = {
roles:
JSON.parse(usercheck.data[0].roles.replaceAll("\\", "\\\\")) ||
[],
rolesLegacy: JSON.parse(usercheck.data[0].legacyRoles) || [],
roles: JSON.parse(
usercheck.data[0].roles.replaceAll("\\", "\\\\")
) || ["Manufacturing\\IssueMaterial\\MaterialHandler"],
rolesLegacy: JSON.parse(usercheck.data[0].legacyRoles) || [3],
};
const { data: newU, error: newE } = (await tryCatch(
axios.patch(revoke, revokePerms, {
headers: {
Authorization: `Basic ${btoa(
`matthes01:99Monsters200Scary!`
)}`,
"X-API-Key": process.env.TEC_API_KEY || "",
"Content-Type": "application/json",
},
})
)) as any;
if (newE) {
console.log(newE.response.data);
console.log("Revoke failed with: ", newE.response.data);
return {
succes: false,
success: false,
message: `${data.username} encountered an error updating..`,
data: newE.response.data,
};
@@ -130,9 +128,7 @@ export const prodUser = async (data: any) => {
const { data: grant, error: grante } = (await tryCatch(
axios.patch(grantUrl, grantRole, {
headers: {
Authorization: `Basic ${btoa(
`matthes01:99Monsters200Scary!`
)}`,
"X-API-Key": process.env.TEC_API_KEY || "",
"Content-Type": "application/json",
},
})
@@ -141,7 +137,7 @@ export const prodUser = async (data: any) => {
if (grante) {
console.log(newE.response.data);
return {
succes: false,
success: false,
message: `${data.username} encountered an error updating..`,
data: newE.response.data,
};
@@ -149,8 +145,59 @@ export const prodUser = async (data: any) => {
}
return {
succes: true,
success: true,
message: `${data.username} was just created or updated.`,
data: [],
};
};
const deleteUser = async (data: any, permRoleCheck: any) => {
const remove = await prodEndpointCreation(
`/public/v1.0/Administration/User/${data.username}`
);
const newurl = await prodEndpointCreation(
`/public/v1.0/Administration/User`
);
const { data: removal, error: grante } = (await tryCatch(
axios.delete(remove, {
headers: {
"X-API-Key": process.env.TEC_API_KEY || "",
"Content-Type": "application/json",
},
})
)) as any;
const newUser: any = {
userId: data.username,
remark: data.remark,
languageCode: "en",
active: true,
roles: permRoleCheck[0].roles,
rolesLegacy: permRoleCheck[0].rolesLegacy,
};
const { data: newU, error: newE } = (await tryCatch(
axios.post(newurl, newUser, {
headers: {
"X-API-Key": process.env.TEC_API_KEY || "",
"Content-Type": "application/json",
},
})
)) as any;
if (newE) {
console.log(newE);
return {
success: false,
message: `${data.username} encountered an error creating..`,
data: newE.response.data,
};
}
return {
success: true,
message: `${data.username} was just created.`,
data: [],
};
};