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")
|
||||
@Post()
|
||||
public async toggleFixedCompanyPost(
|
||||
@Body() requestBody: FixCompanyPostParams
|
||||
@Body() requestBody: FixCompanyPostParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().toggleFixedPost(requestBody);
|
||||
@@ -62,7 +62,7 @@ export class MoveCompanyFixedPostController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async moveFixedCompanyPost(
|
||||
@Body() requestBody: MoveCompanyFixedPostParams
|
||||
@Body() requestBody: MoveCompanyFixedPostParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().moveFixedPost(requestBody);
|
||||
@@ -86,7 +86,7 @@ export class SetNotificationsStatusCompanyController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async setNotificationAuto(
|
||||
@Body() requestBody: SetNotificationAutoParams
|
||||
@Body() requestBody: SetNotificationAutoParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().setNotificationAuto(requestBody);
|
||||
@@ -110,7 +110,7 @@ export class SetPublishedStatusCompanyController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async setPublishedStatus(
|
||||
@Body() requestBody: SetOrganizationPublishedStatusParams
|
||||
@Body() requestBody: SetOrganizationPublishedStatusParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().setPublishedStatus(requestBody);
|
||||
@@ -134,7 +134,7 @@ export class CreateCompanyController extends Controller {
|
||||
@SuccessResponse(200, "La compañia fue creada con exito")
|
||||
@Post()
|
||||
public async createCompany(
|
||||
@Body() requestBody: CreateCompanyParams
|
||||
@Body() requestBody: CreateCompanyParams,
|
||||
): Promise<ICompany | ApiValidationError> {
|
||||
try {
|
||||
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")
|
||||
@Post()
|
||||
public async deleteCompany(
|
||||
@Body() requestBody: DeleteCompanyParams
|
||||
@Body() requestBody: DeleteCompanyParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().deleteCompany(requestBody);
|
||||
@@ -179,7 +179,7 @@ export class UpdateCompanyController extends Controller {
|
||||
@SuccessResponse(200, "La compañia fue modificada con exito")
|
||||
@Post()
|
||||
public async updateCompany(
|
||||
@Body() requestBody: UpdateCompanyParams
|
||||
@Body() requestBody: UpdateCompanyParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().updateCompany(requestBody);
|
||||
@@ -202,7 +202,7 @@ export class GetCompanyByIdController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async getById(
|
||||
@Body() requestBody: OranizationsViewByIdParams
|
||||
@Body() requestBody: OranizationsViewByIdParams,
|
||||
): Promise<MyOranizationsView | ApiValidationError> {
|
||||
try {
|
||||
const companies = await new CompaniesService().getById(requestBody);
|
||||
@@ -222,7 +222,7 @@ export class GetCompanyByUserController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async getByUserId(
|
||||
@Body() requestBody: MyOranizationsViewParams
|
||||
@Body() requestBody: MyOranizationsViewParams,
|
||||
): Promise<MyOranizationsView[] | ApiValidationError> {
|
||||
try {
|
||||
const companies = await new CompaniesService().getByUserId(requestBody);
|
||||
@@ -242,7 +242,7 @@ export class PaginateCompanyController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async paginateUsers(
|
||||
@Body() requestBody: PaginateCompaniesParams
|
||||
@Body() requestBody: PaginateCompaniesParams,
|
||||
): Promise<PaginateCompaniesResults | ApiValidationError> {
|
||||
try {
|
||||
const users = await new CompaniesService().paginateCompanies(requestBody);
|
||||
@@ -265,7 +265,7 @@ export class UploadCompanyHeaderController extends Controller {
|
||||
public async uploadHeader(
|
||||
@FormField() companyId: string,
|
||||
@FormField() sessionUser: string,
|
||||
@UploadedFile() file: Express.Multer.File
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().setCompanyHeader({
|
||||
@@ -295,7 +295,7 @@ export class UploadCompanyIconController extends Controller {
|
||||
public async uploadIcon(
|
||||
@FormField() companyId: string,
|
||||
@FormField() sessionUser: string,
|
||||
@UploadedFile() file: Express.Multer.File
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().setCompanyIcon({
|
||||
@@ -325,7 +325,7 @@ export class DeleteCompanyHeaderController extends Controller {
|
||||
@SuccessResponse(200, "El archivo fue cargado con exito")
|
||||
@Post()
|
||||
public async deleteHeader(
|
||||
@Body() data: DeleteCompanyFileParams
|
||||
@Body() data: DeleteCompanyFileParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().deleteCompanyHeader(data);
|
||||
@@ -349,7 +349,7 @@ export class DeleteCompanyIconController extends Controller {
|
||||
@SuccessResponse(200, "El archivo fue cargado con exito")
|
||||
@Post()
|
||||
public async deleteIcon(
|
||||
@Body() data: DeleteCompanyFileParams
|
||||
@Body() data: DeleteCompanyFileParams,
|
||||
): Promise<ApiVoidResult | ApiValidationError> {
|
||||
try {
|
||||
await new CompaniesService().deleteCompanyIcon(data);
|
||||
@@ -372,7 +372,7 @@ export class filterAllOrganizationsController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async filterAllOrganizations(
|
||||
@Body() requestBody: PaginateCompaniesParams
|
||||
@Body() requestBody: PaginateCompaniesParams,
|
||||
): Promise<TextObjectFilterResult[] | ApiValidationError> {
|
||||
try {
|
||||
const clients = await new CompaniesService().filterAll(requestBody);
|
||||
@@ -392,7 +392,7 @@ export class CheckCompanyNameController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async checkName(
|
||||
@Body() requestBody: { name: string }
|
||||
@Body() requestBody: { name: string },
|
||||
): Promise<{ valid: boolean } | ApiValidationError> {
|
||||
try {
|
||||
const valid = await new CompaniesService().checkNameExists(requestBody.name);
|
||||
@@ -413,12 +413,12 @@ export class CheckCompanyAdminController extends Controller {
|
||||
@SuccessResponse(200, "Done")
|
||||
@Post()
|
||||
public async checkAdmin(
|
||||
@Body() requestBody: { companyId: string; sessionUser: string }
|
||||
): Promise<{ isAdmin: boolean } | ApiValidationError> {
|
||||
@Body() requestBody: { companyId: string; sessionUser: string },
|
||||
): Promise<boolean | ApiValidationError> {
|
||||
try {
|
||||
const isAdmin = await new CompaniesService().checkCompanyAdmin(requestBody);
|
||||
this.setStatus(200);
|
||||
return { isAdmin };
|
||||
return isAdmin;
|
||||
} catch (e) {
|
||||
const errorOccurred: Error = e as Error;
|
||||
this.setStatus(500);
|
||||
|
||||
@@ -104,14 +104,33 @@ export default function OrganizationPublicProfile() {
|
||||
const [currentPage, setCurrentPage] = useState<number>(1);
|
||||
const [isOrgAdmin, setIsOrgAdmin] = 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(() => {
|
||||
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");
|
||||
if (!wrapper) return;
|
||||
|
||||
if (isAdmin) {
|
||||
const existingBtn = document.getElementById("edit-header-btn");
|
||||
if (existingBtn) existingBtn.remove();
|
||||
|
||||
const btn = document.createElement("div");
|
||||
btn.id = "edit-header-btn";
|
||||
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>
|
||||
</button>
|
||||
`;
|
||||
btn.querySelector("button")!.addEventListener("click", () => setShowEditPopup(true));
|
||||
btn.querySelector("button")!.addEventListener("click", () =>
|
||||
setShowEditPopup(true),
|
||||
);
|
||||
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 () => {
|
||||
cancelled = true;
|
||||
const existing = document.getElementById("edit-header-btn");
|
||||
if (existing) existing.remove();
|
||||
};
|
||||
}, [isOrgAdmin]);
|
||||
}, [SessionInfo.userId, oid]);
|
||||
|
||||
const SessionInfo = useSessionStore();
|
||||
const eventHandler = useEventHandlerStore();
|
||||
const alert = useAlert();
|
||||
const confirm = useConfirmStore();
|
||||
@@ -186,22 +222,6 @@ export default function OrganizationPublicProfile() {
|
||||
});
|
||||
}, [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(() => {
|
||||
if (!OrganizationData.orgData) return;
|
||||
if (!oid) return; //if (!OrganizationData.orgData.id) return;
|
||||
|
||||
Reference in New Issue
Block a user