From f6d7b3f78c35b9bd55b770ecbc7117f828e5be41 Mon Sep 17 00:00:00 2001 From: CarlosQ96 <92376054+CarlosQ96@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:05:36 +0100 Subject: [PATCH] add redirect util function for project custom actions (#1865) --- src/config.ts | 1 - src/server/adminJs/adminJs-types.ts | 1 + src/server/adminJs/adminJsUtils.ts | 33 ++++++++++++++++++++++++++ src/server/adminJs/tabs/projectsTab.ts | 12 +++++++--- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/server/adminJs/adminJsUtils.ts diff --git a/src/config.ts b/src/config.ts index d94dba840..be3f52e62 100644 --- a/src/config.ts +++ b/src/config.ts @@ -56,7 +56,6 @@ const envVars = [ 'GIVBACK_MAX_FACTOR', 'GIVBACK_MIN_FACTOR', 'DONATION_VERIFICAITON_EXPIRATION_HOURS', - 'SYNC_USER_MODEL_SCORE_CRONJOB_EXPRESSION', ]; interface requiredEnv { diff --git a/src/server/adminJs/adminJs-types.ts b/src/server/adminJs/adminJs-types.ts index 952f60738..a24cd2e2a 100644 --- a/src/server/adminJs/adminJs-types.ts +++ b/src/server/adminJs/adminJs-types.ts @@ -13,6 +13,7 @@ export interface AdminJsContextInterface { export interface AdminJsRequestInterface { payload?: any; record?: any; + rawHeaders?: any; query?: { recordIds?: string; }; diff --git a/src/server/adminJs/adminJsUtils.ts b/src/server/adminJs/adminJsUtils.ts new file mode 100644 index 000000000..ce800ca0e --- /dev/null +++ b/src/server/adminJs/adminJsUtils.ts @@ -0,0 +1,33 @@ +import { logger } from '../../utils/logger'; + +/** + * Extracts the redirect URL from request headers for AdminJS actions + * @param request - The AdminJS action request object + * @param resourceName - The name of the resource (e.g., 'Project', 'User') + * @returns The URL to redirect to after action completion + */ +export const getRedirectUrl = (request: any, resourceName: string): string => { + const refererIndex = + request?.rawHeaders?.findIndex(h => h.toLowerCase() === 'referer') || -1; + const referrerUrl = + refererIndex !== -1 ? request.rawHeaders[refererIndex + 1] : false; + + // Default fallback URL if no referer is found + const defaultUrl = `/admin/resources/${resourceName}`; + + try { + if (referrerUrl) { + const url = new URL(referrerUrl); + // If it's the main list view (no search params), add a timestamp to force refresh + if (url.pathname === `/admin/resources/${resourceName}` && !url.search) { + return `${url.pathname}?timestamp=${Date.now()}`; + } + return url.pathname + url.search; + } + } catch (error) { + logger.error('Error parsing referrer URL:', error); + } + + // Add timestamp to default URL as well + return `${defaultUrl}?timestamp=${Date.now()}`; +}; diff --git a/src/server/adminJs/tabs/projectsTab.ts b/src/server/adminJs/tabs/projectsTab.ts index b52aa85b8..604c2d5ae 100644 --- a/src/server/adminJs/tabs/projectsTab.ts +++ b/src/server/adminJs/tabs/projectsTab.ts @@ -55,6 +55,7 @@ import { refreshProjectEstimatedMatchingView } from '../../../services/projectVi import { extractAdminJsReferrerUrlParams } from '../adminJs'; import { relateManyProjectsToQfRound } from '../../../repositories/qfRoundRepository2'; import { Category } from '../../../entities/category'; +import { getRedirectUrl } from '../adminJsUtils'; // add queries depending on which filters were selected export const buildProjectsQuery = ( @@ -188,6 +189,7 @@ export const verifyProjects = async ( request: AdminJsRequestInterface, vouchedStatus: boolean = true, ) => { + const redirectUrl = getRedirectUrl(request, 'Project'); const { records, currentAdmin } = context; try { const projectIds = request?.query?.recordIds @@ -236,7 +238,7 @@ export const verifyProjects = async ( throw error; } return { - redirectUrl: '/admin/resources/Project', + redirectUrl: redirectUrl, records: records.map(record => { record.toJSON(context.currentAdmin); }), @@ -254,6 +256,7 @@ export const updateStatusOfProjects = async ( request: AdminJsRequestInterface, status, ) => { + const redirectUrl = getRedirectUrl(request, 'Project'); const { records, currentAdmin } = context; const projectIds = request?.query?.recordIds ?.split(',') @@ -319,7 +322,7 @@ export const updateStatusOfProjects = async ( ]); } return { - redirectUrl: '/admin/resources/Project', + redirectUrl: redirectUrl, records: records.map(record => { record.toJSON(context.currentAdmin); }), @@ -415,6 +418,7 @@ export const addSingleProjectToQfRound = async ( request: AdminJsRequestInterface, add: boolean = true, ) => { + const redirectUrl = getRedirectUrl(request, 'Project'); const { record, currentAdmin } = context; let message = messages.PROJECTS_RELATED_TO_ACTIVE_QF_ROUND_SUCCESSFULLY; const projectId = Number(request?.params?.recordId); @@ -431,6 +435,7 @@ export const addSingleProjectToQfRound = async ( message = messages.THERE_IS_NOT_ANY_ACTIVE_QF_ROUND; } return { + redirectUrl: redirectUrl, record: record.toJSON(currentAdmin), notice: { message, @@ -523,6 +528,7 @@ export const listDelist = async ( request, reviewStatus: ReviewStatus = ReviewStatus.Listed, ) => { + const redirectUrl = getRedirectUrl(request, 'Project'); const { records, currentAdmin } = context; let listed; switch (reviewStatus) { @@ -586,7 +592,7 @@ export const listDelist = async ( throw error; } return { - redirectUrl: '/admin/resources/Project', + redirectUrl: redirectUrl, records: records.map(record => { record.toJSON(context.currentAdmin); }),