Skip to content

Commit

Permalink
add redirect util function for project custom actions
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Oct 28, 2024
1 parent 5e9e255 commit 01cd0fd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const envVars = [
'GIVBACK_MAX_FACTOR',
'GIVBACK_MIN_FACTOR',
'DONATION_VERIFICAITON_EXPIRATION_HOURS',
'SYNC_USER_MODEL_SCORE_CRONJOB_EXPRESSION',
];

interface requiredEnv {
Expand Down
24 changes: 24 additions & 0 deletions src/server/adminJs/adminJsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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');

Check failure on line 8 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Replace `··const·refererIndex·=·request.rawHeaders.findIndex(h·=>·h.toLowerCase()·===·'referer');` with `const·refererIndex·=·request.rawHeaders.findIndex(⏎····h·=>·h.toLowerCase()·===·'referer',`
const referrerUrl = refererIndex !== -1 ? request.rawHeaders[refererIndex + 1] : '';

Check failure on line 9 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Replace `··const·referrerUrl·=` with `);⏎··const·referrerUrl·=⏎···`

Check failure on line 10 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `····`
// Default fallback URL if no referer is found

Check failure on line 11 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `··`
const defaultUrl = `/admin/resources/${resourceName}`;

Check failure on line 12 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `··`

Check failure on line 13 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `····`
try {

Check failure on line 14 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `··`
if (referrerUrl) {

Check failure on line 15 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `··`
const url = new URL(referrerUrl);

Check failure on line 16 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Replace `········` with `······`
return url.pathname + url.search;

Check failure on line 17 in src/server/adminJs/adminJsUtils.ts

View workflow job for this annotation

GitHub Actions / test

Delete `··`
}
} catch (error) {
console.warn('Error parsing referrer URL:', error);
}

return defaultUrl;
};
12 changes: 9 additions & 3 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}),
Expand All @@ -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(',')
Expand Down Expand Up @@ -319,7 +322,7 @@ export const updateStatusOfProjects = async (
]);
}
return {
redirectUrl: '/admin/resources/Project',
redirectUrl: redirectUrl,
records: records.map(record => {
record.toJSON(context.currentAdmin);
}),
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}),
Expand Down

0 comments on commit 01cd0fd

Please sign in to comment.