feat: unificar lógica de verificación de administrador y gestión del botón de edición en OrganizationPublicProfile
This commit is contained in:
@@ -38,7 +38,7 @@ export class ToggleCompanyFixedPostController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async toggleFixedCompanyPost(
|
public async toggleFixedCompanyPost(
|
||||||
@Body() requestBody: FixCompanyPostParams
|
@Body() requestBody: FixCompanyPostParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().toggleFixedPost(requestBody);
|
await new CompaniesService().toggleFixedPost(requestBody);
|
||||||
@@ -62,7 +62,7 @@ export class MoveCompanyFixedPostController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async moveFixedCompanyPost(
|
public async moveFixedCompanyPost(
|
||||||
@Body() requestBody: MoveCompanyFixedPostParams
|
@Body() requestBody: MoveCompanyFixedPostParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().moveFixedPost(requestBody);
|
await new CompaniesService().moveFixedPost(requestBody);
|
||||||
@@ -86,7 +86,7 @@ export class SetNotificationsStatusCompanyController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async setNotificationAuto(
|
public async setNotificationAuto(
|
||||||
@Body() requestBody: SetNotificationAutoParams
|
@Body() requestBody: SetNotificationAutoParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().setNotificationAuto(requestBody);
|
await new CompaniesService().setNotificationAuto(requestBody);
|
||||||
@@ -110,7 +110,7 @@ export class SetPublishedStatusCompanyController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async setPublishedStatus(
|
public async setPublishedStatus(
|
||||||
@Body() requestBody: SetOrganizationPublishedStatusParams
|
@Body() requestBody: SetOrganizationPublishedStatusParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().setPublishedStatus(requestBody);
|
await new CompaniesService().setPublishedStatus(requestBody);
|
||||||
@@ -134,7 +134,7 @@ export class CreateCompanyController extends Controller {
|
|||||||
@SuccessResponse(200, "La compañia fue creada con exito")
|
@SuccessResponse(200, "La compañia fue creada con exito")
|
||||||
@Post()
|
@Post()
|
||||||
public async createCompany(
|
public async createCompany(
|
||||||
@Body() requestBody: CreateCompanyParams
|
@Body() requestBody: CreateCompanyParams,
|
||||||
): Promise<ICompany | ApiValidationError> {
|
): Promise<ICompany | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const company = await new CompaniesService().createCompany(requestBody);
|
const company = await new CompaniesService().createCompany(requestBody);
|
||||||
@@ -155,7 +155,7 @@ export class DeleteCompanyController extends Controller {
|
|||||||
@SuccessResponse(200, "La compañia fue modificada con exito")
|
@SuccessResponse(200, "La compañia fue modificada con exito")
|
||||||
@Post()
|
@Post()
|
||||||
public async deleteCompany(
|
public async deleteCompany(
|
||||||
@Body() requestBody: DeleteCompanyParams
|
@Body() requestBody: DeleteCompanyParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().deleteCompany(requestBody);
|
await new CompaniesService().deleteCompany(requestBody);
|
||||||
@@ -179,7 +179,7 @@ export class UpdateCompanyController extends Controller {
|
|||||||
@SuccessResponse(200, "La compañia fue modificada con exito")
|
@SuccessResponse(200, "La compañia fue modificada con exito")
|
||||||
@Post()
|
@Post()
|
||||||
public async updateCompany(
|
public async updateCompany(
|
||||||
@Body() requestBody: UpdateCompanyParams
|
@Body() requestBody: UpdateCompanyParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().updateCompany(requestBody);
|
await new CompaniesService().updateCompany(requestBody);
|
||||||
@@ -202,7 +202,7 @@ export class GetCompanyByIdController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async getById(
|
public async getById(
|
||||||
@Body() requestBody: OranizationsViewByIdParams
|
@Body() requestBody: OranizationsViewByIdParams,
|
||||||
): Promise<MyOranizationsView | ApiValidationError> {
|
): Promise<MyOranizationsView | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const companies = await new CompaniesService().getById(requestBody);
|
const companies = await new CompaniesService().getById(requestBody);
|
||||||
@@ -222,7 +222,7 @@ export class GetCompanyByUserController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async getByUserId(
|
public async getByUserId(
|
||||||
@Body() requestBody: MyOranizationsViewParams
|
@Body() requestBody: MyOranizationsViewParams,
|
||||||
): Promise<MyOranizationsView[] | ApiValidationError> {
|
): Promise<MyOranizationsView[] | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const companies = await new CompaniesService().getByUserId(requestBody);
|
const companies = await new CompaniesService().getByUserId(requestBody);
|
||||||
@@ -242,7 +242,7 @@ export class PaginateCompanyController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async paginateUsers(
|
public async paginateUsers(
|
||||||
@Body() requestBody: PaginateCompaniesParams
|
@Body() requestBody: PaginateCompaniesParams,
|
||||||
): Promise<PaginateCompaniesResults | ApiValidationError> {
|
): Promise<PaginateCompaniesResults | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const users = await new CompaniesService().paginateCompanies(requestBody);
|
const users = await new CompaniesService().paginateCompanies(requestBody);
|
||||||
@@ -265,7 +265,7 @@ export class UploadCompanyHeaderController extends Controller {
|
|||||||
public async uploadHeader(
|
public async uploadHeader(
|
||||||
@FormField() companyId: string,
|
@FormField() companyId: string,
|
||||||
@FormField() sessionUser: string,
|
@FormField() sessionUser: string,
|
||||||
@UploadedFile() file: Express.Multer.File
|
@UploadedFile() file: Express.Multer.File,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().setCompanyHeader({
|
await new CompaniesService().setCompanyHeader({
|
||||||
@@ -295,7 +295,7 @@ export class UploadCompanyIconController extends Controller {
|
|||||||
public async uploadIcon(
|
public async uploadIcon(
|
||||||
@FormField() companyId: string,
|
@FormField() companyId: string,
|
||||||
@FormField() sessionUser: string,
|
@FormField() sessionUser: string,
|
||||||
@UploadedFile() file: Express.Multer.File
|
@UploadedFile() file: Express.Multer.File,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().setCompanyIcon({
|
await new CompaniesService().setCompanyIcon({
|
||||||
@@ -325,7 +325,7 @@ export class DeleteCompanyHeaderController extends Controller {
|
|||||||
@SuccessResponse(200, "El archivo fue cargado con exito")
|
@SuccessResponse(200, "El archivo fue cargado con exito")
|
||||||
@Post()
|
@Post()
|
||||||
public async deleteHeader(
|
public async deleteHeader(
|
||||||
@Body() data: DeleteCompanyFileParams
|
@Body() data: DeleteCompanyFileParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().deleteCompanyHeader(data);
|
await new CompaniesService().deleteCompanyHeader(data);
|
||||||
@@ -349,7 +349,7 @@ export class DeleteCompanyIconController extends Controller {
|
|||||||
@SuccessResponse(200, "El archivo fue cargado con exito")
|
@SuccessResponse(200, "El archivo fue cargado con exito")
|
||||||
@Post()
|
@Post()
|
||||||
public async deleteIcon(
|
public async deleteIcon(
|
||||||
@Body() data: DeleteCompanyFileParams
|
@Body() data: DeleteCompanyFileParams,
|
||||||
): Promise<ApiVoidResult | ApiValidationError> {
|
): Promise<ApiVoidResult | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
await new CompaniesService().deleteCompanyIcon(data);
|
await new CompaniesService().deleteCompanyIcon(data);
|
||||||
@@ -372,7 +372,7 @@ export class filterAllOrganizationsController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async filterAllOrganizations(
|
public async filterAllOrganizations(
|
||||||
@Body() requestBody: PaginateCompaniesParams
|
@Body() requestBody: PaginateCompaniesParams,
|
||||||
): Promise<TextObjectFilterResult[] | ApiValidationError> {
|
): Promise<TextObjectFilterResult[] | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const clients = await new CompaniesService().filterAll(requestBody);
|
const clients = await new CompaniesService().filterAll(requestBody);
|
||||||
@@ -392,7 +392,7 @@ export class CheckCompanyNameController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async checkName(
|
public async checkName(
|
||||||
@Body() requestBody: { name: string }
|
@Body() requestBody: { name: string },
|
||||||
): Promise<{ valid: boolean } | ApiValidationError> {
|
): Promise<{ valid: boolean } | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const valid = await new CompaniesService().checkNameExists(requestBody.name);
|
const valid = await new CompaniesService().checkNameExists(requestBody.name);
|
||||||
@@ -413,12 +413,12 @@ export class CheckCompanyAdminController extends Controller {
|
|||||||
@SuccessResponse(200, "Done")
|
@SuccessResponse(200, "Done")
|
||||||
@Post()
|
@Post()
|
||||||
public async checkAdmin(
|
public async checkAdmin(
|
||||||
@Body() requestBody: { companyId: string; sessionUser: string }
|
@Body() requestBody: { companyId: string; sessionUser: string },
|
||||||
): Promise<{ isAdmin: boolean } | ApiValidationError> {
|
): Promise<boolean | ApiValidationError> {
|
||||||
try {
|
try {
|
||||||
const isAdmin = await new CompaniesService().checkCompanyAdmin(requestBody);
|
const isAdmin = await new CompaniesService().checkCompanyAdmin(requestBody);
|
||||||
this.setStatus(200);
|
this.setStatus(200);
|
||||||
return { isAdmin };
|
return isAdmin;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const errorOccurred: Error = e as Error;
|
const errorOccurred: Error = e as Error;
|
||||||
this.setStatus(500);
|
this.setStatus(500);
|
||||||
|
|||||||
@@ -104,14 +104,33 @@ export default function OrganizationPublicProfile() {
|
|||||||
const [currentPage, setCurrentPage] = useState<number>(1);
|
const [currentPage, setCurrentPage] = useState<number>(1);
|
||||||
const [isOrgAdmin, setIsOrgAdmin] = useState<boolean>(false);
|
const [isOrgAdmin, setIsOrgAdmin] = useState<boolean>(false);
|
||||||
const [showEditPopup, setShowEditPopup] = useState<boolean>(false);
|
const [showEditPopup, setShowEditPopup] = useState<boolean>(false);
|
||||||
|
const SessionInfo = useSessionStore();
|
||||||
|
|
||||||
// Append edit button to header wrapper when admin
|
// Unified effect: check admin status and manage edit button DOM element
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isOrgAdmin) return;
|
if (!SessionInfo.userId || !oid) {
|
||||||
|
setIsOrgAdmin(false);
|
||||||
|
const existing = document.getElementById("edit-header-btn");
|
||||||
|
if (existing) existing.remove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
checkOrgAdmin(oid, SessionInfo.userId)
|
||||||
|
.then((isAdmin) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
console.log("isAdmin:", isAdmin);
|
||||||
|
setIsOrgAdmin(isAdmin);
|
||||||
|
|
||||||
|
// Append or remove button based on the result
|
||||||
const wrapper = document.getElementById("header-wrapper");
|
const wrapper = document.getElementById("header-wrapper");
|
||||||
if (!wrapper) return;
|
if (!wrapper) return;
|
||||||
|
|
||||||
|
if (isAdmin) {
|
||||||
|
const existingBtn = document.getElementById("edit-header-btn");
|
||||||
|
if (existingBtn) existingBtn.remove();
|
||||||
|
|
||||||
const btn = document.createElement("div");
|
const btn = document.createElement("div");
|
||||||
btn.id = "edit-header-btn";
|
btn.id = "edit-header-btn";
|
||||||
btn.innerHTML = `
|
btn.innerHTML = `
|
||||||
@@ -135,16 +154,33 @@ export default function OrganizationPublicProfile() {
|
|||||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34a.9959.9959 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/></svg>
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34a.9959.9959 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/></svg>
|
||||||
</button>
|
</button>
|
||||||
`;
|
`;
|
||||||
btn.querySelector("button")!.addEventListener("click", () => setShowEditPopup(true));
|
btn.querySelector("button")!.addEventListener("click", () =>
|
||||||
|
setShowEditPopup(true),
|
||||||
|
);
|
||||||
wrapper.appendChild(btn);
|
wrapper.appendChild(btn);
|
||||||
|
} else {
|
||||||
|
// Remove button for non-admin users
|
||||||
|
const existingBtn = document.getElementById("edit-header-btn");
|
||||||
|
if (existingBtn) existingBtn.remove();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setIsOrgAdmin(false);
|
||||||
|
|
||||||
|
// Ensure button is removed on error
|
||||||
|
const existingBtn = document.getElementById("edit-header-btn");
|
||||||
|
if (existingBtn) existingBtn.remove();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cleanup: mark as cancelled and remove button
|
||||||
return () => {
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
const existing = document.getElementById("edit-header-btn");
|
const existing = document.getElementById("edit-header-btn");
|
||||||
if (existing) existing.remove();
|
if (existing) existing.remove();
|
||||||
};
|
};
|
||||||
}, [isOrgAdmin]);
|
}, [SessionInfo.userId, oid]);
|
||||||
|
|
||||||
const SessionInfo = useSessionStore();
|
|
||||||
const eventHandler = useEventHandlerStore();
|
const eventHandler = useEventHandlerStore();
|
||||||
const alert = useAlert();
|
const alert = useAlert();
|
||||||
const confirm = useConfirmStore();
|
const confirm = useConfirmStore();
|
||||||
@@ -186,22 +222,6 @@ export default function OrganizationPublicProfile() {
|
|||||||
});
|
});
|
||||||
}, [SessionInfo, authorizedUsers]);
|
}, [SessionInfo, authorizedUsers]);
|
||||||
|
|
||||||
// Check if the logged-in user is admin of this organization
|
|
||||||
useEffect(() => {
|
|
||||||
if (!SessionInfo.userId || !oid) {
|
|
||||||
setIsOrgAdmin(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
checkOrgAdmin(oid, SessionInfo.userId)
|
|
||||||
.then((isAdmin) => {
|
|
||||||
setIsOrgAdmin(isAdmin);
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
setIsOrgAdmin(false);
|
|
||||||
});
|
|
||||||
}, [SessionInfo.userId, oid]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!OrganizationData.orgData) return;
|
if (!OrganizationData.orgData) return;
|
||||||
if (!oid) return; //if (!OrganizationData.orgData.id) return;
|
if (!oid) return; //if (!OrganizationData.orgData.id) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user