Skip to content

Commit

Permalink
Merge pull request #1077 from Giveth/staging
Browse files Browse the repository at this point in the history
Release 1.15.2
  • Loading branch information
CarlosQ96 authored Jul 27, 2023
2 parents f1e50ed + 86c3bcb commit bf7be4d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giveth-graphql-api",
"version": "1.15.1",
"version": "1.15.2",
"description": "Backend GraphQL server for Giveth originally forked from Topia",
"main": "./dist/index.js",
"dependencies": {
Expand Down
6 changes: 5 additions & 1 deletion src/server/adminJs/adminJs-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export interface AdminJsProjectsQuery {
verified?: string;
// listed?: string;
isImported?: string;
reviewStatus: ReviewStatus;
reviewStatus?: ReviewStatus;
}

export interface AdminJsDonationsQuery {
id?: string;
projectId?: string;
contactEmail?: string;
referrerWallet?: string;
Expand All @@ -43,6 +44,7 @@ export interface AdminJsDonationsQuery {
currency?: string;
transactionNetworkId?: string;
isProjectVerified?: string;
qfRoundId?: string;
}

// headers defined by the verification team for exporting
Expand Down Expand Up @@ -94,4 +96,6 @@ export const donationHeaders = [
'createdAt',
'referrerWallet',
'isTokenEligibleForGivback',
'qfRoundId',
'qfRoundUserScore',
];
50 changes: 19 additions & 31 deletions src/server/adminJs/adminJs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adminJs, { AdminJSOptions } from 'adminjs';
import adminJs, { ActionContext, AdminJSOptions } from 'adminjs';
import { User } from '../../entities/user';
import adminJsExpress from '@adminjs/express';
import config from '../../config';
Expand Down Expand Up @@ -72,37 +72,25 @@ export const getAdminJsRouter = async () => {
);
};

// Express Middleware to save query of a search
export const adminJsQueryCache = async (req, res, next) => {
if (
req.url.startsWith('/admin/api/resources/') &&
req.headers.cookie.includes('adminbro')
) {
const admin = await getCurrentAdminJsSession(req);
if (!admin) return next(); // skip saving queries

const matches = req.url.match(/\/admin\/api\/resources\/(.+?)(\/|$)/);
if (!matches) return next(); // invalid URL

const resourceName = matches[1];
const queryStrings = {};

// Extract filter names and values from URL query string parameters
for (const key of Object.keys(req.query)) {
const [_, filter] = key.split('.');
if (!filter) continue;

queryStrings[filter] = req.query[key];
}
// Save query strings to Redis hash with an expiration
await redis.hset(
`adminbro:${admin.id}:${resourceName}`,
queryStrings,
'ex',
1800,
);
// Extract Referrer Header Parameter Filter Params
export const extractAdminJsReferrerUrlParams = (req: ActionContext) => {
const queryStrings = {};

const refererUrlHeaderIndex = req?.rawHeaders?.indexOf('Referer');
if (!refererUrlHeaderIndex || refererUrlHeaderIndex < 0) return {};

const refererUrl = new URL(req.rawHeaders[refererUrlHeaderIndex + 1]);
const searchParams = refererUrl.searchParams;

// Extract filter names and values from URL query string parameters
for (const [key, value] of searchParams.entries()) {
const [_, filter] = key.split('.');
if (!filter) continue;

queryStrings[filter] = value;
}
next();

return queryStrings;
};

// Get CurrentSession for external express middlewares
Expand Down
43 changes: 37 additions & 6 deletions src/server/adminJs/tabs/donationTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
addDonationsSheetToSpreadsheet,
} from '../../../services/googleSheets';
import { SelectQueryBuilder } from 'typeorm';
import { ActionContext } from 'adminjs';
import { extractAdminJsReferrerUrlParams } from '../adminJs';

export const createDonation = async (
request: AdminJsRequestInterface,
Expand Down Expand Up @@ -183,14 +185,25 @@ export const buildDonationsQuery = (
const query = Donation.createQueryBuilder('donation')
.leftJoinAndSelect('donation.user', 'user')
.leftJoinAndSelect('donation.project', 'project')
.leftJoinAndSelect('donation.qfRound', 'qfRound')
.where('donation.amount > 0')
.addOrderBy('donation.createdAt', 'DESC');

if (queryStrings.id)
query.andWhere('donation.id = :id', {
id: queryStrings.id,
});

if (queryStrings.projectId)
query.andWhere('donation.projectId = :projectId', {
projectId: queryStrings.projectId,
});

if (queryStrings.qfRoundId)
query.andWhere('donation.qfRoundId = :qfRoundId', {
qfRoundId: queryStrings.qfRoundId,
});

if (queryStrings.userId)
query.andWhere('donation.userId = :userId', {
userId: queryStrings.userId,
Expand Down Expand Up @@ -250,16 +263,13 @@ export const buildDonationsQuery = (
};

export const exportDonationsWithFiltersToCsv = async (
_request: AdminJsRequestInterface,
_request: ActionContext,
_response,
context: AdminJsContextInterface,
) => {
try {
const { records } = context;
const rawQueryStrings = await redis.get(
`adminbro:${context.currentAdmin.id}:Donation`,
);
const queryStrings = rawQueryStrings ? JSON.parse(rawQueryStrings) : {};
const queryStrings = extractAdminJsReferrerUrlParams(_request);
const projectsQuery = buildDonationsQuery(queryStrings);
const projects = await projectsQuery.getMany();

Expand Down Expand Up @@ -316,6 +326,8 @@ const sendDonationsToGoogleSheet = async (
createdAt: donation?.createdAt.toISOString(),
referrerWallet: donation?.referrerWallet || '',
isTokenEligibleForGivback: Boolean(donation?.isTokenEligibleForGivback),
qfRoundId: donation?.qfRound?.id || '',
qfRoundUserScore: donation?.qfRoundUserScore || '',
};
});

Expand All @@ -339,6 +351,16 @@ export const donationTab = {
new: false,
},
},
qfRoundId: {
type: Number,
isVisible: {
list: false,
filter: true,
show: true,
edit: false,
new: false,
},
},
nonce: {
isVisible: false,
},
Expand Down Expand Up @@ -448,6 +470,15 @@ export const donationTab = {
new: false,
},
},
qfRoundUserScore: {
isVisible: {
list: false,
filter: false,
show: true,
edit: false,
new: false,
},
},
tokenAddress: {
isVisible: false,
},
Expand All @@ -462,7 +493,7 @@ export const donationTab = {
},
toWalletAddress: {
isVisible: {
list: true,
list: false,
filter: true,
show: true,
edit: false,
Expand Down
10 changes: 4 additions & 6 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ReviewStatus,
RevokeSteps,
} from '../../../entities/project';
import adminJs from 'adminjs';
import adminJs, { ActionContext } from 'adminjs';
import {
canAccessProjectAction,
canAccessQfRoundAction,
Expand Down Expand Up @@ -62,6 +62,7 @@ import {
refreshProjectDonationSummaryView,
refreshProjectEstimatedMatchingView,
} from '../../../services/projectViewsService';
import { extractAdminJsReferrerUrlParams } from '../adminJs';

// add queries depending on which filters were selected
export const buildProjectsQuery = (
Expand Down Expand Up @@ -605,16 +606,13 @@ export const listDelist = async (
};

export const exportProjectsWithFiltersToCsv = async (
_request: AdminJsRequestInterface,
_request,
_response,
context: AdminJsContextInterface,
) => {
try {
const { records } = context;
const rawQueryStrings = await redis.get(
`adminbro:${context.currentAdmin.id}:Project`,
);
const queryStrings = rawQueryStrings ? JSON.parse(rawQueryStrings) : {};
const queryStrings = extractAdminJsReferrerUrlParams(_request);
const projectsQuery = buildProjectsQuery(queryStrings);
const projects = await projectsQuery.getMany();

Expand Down
7 changes: 1 addition & 6 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import { runCheckPendingProjectListingCronJob } from '../services/cronJobs/syncP
import { runCheckProjectVerificationStatus } from '../services/cronJobs/checkProjectVerificationStatus';
import { webhookHandler } from '../services/transak/webhookHandler';

import {
adminJsQueryCache,
adminJsRootPath,
getAdminJsRouter,
} from './adminJs/adminJs';
import { adminJsRootPath, getAdminJsRouter } from './adminJs/adminJs';
import { redis } from '../redis';
import { logger } from '../utils/logger';
import { runNotifyMissingDonationsCronJob } from '../services/cronJobs/notifyDonationsWithSegment';
Expand Down Expand Up @@ -337,7 +333,6 @@ export async function bootstrap() {
);

// AdminJs!
app.use(adminJsQueryCache);
app.use(adminJsRootPath, await getAdminJsRouter());

if (!isTestEnv) {
Expand Down

0 comments on commit bf7be4d

Please sign in to comment.