Skip to content

Commit

Permalink
Merge pull request #1859 from Giveth/move-revoke-badge-to-project-ver…
Browse files Browse the repository at this point in the history
…ification-form-section-in-adminJS-

move revoke badge to project verification form section in adminJS
  • Loading branch information
RamRamez authored Oct 7, 2024
2 parents f06ba5f + d4c8068 commit 67b3adb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 71 deletions.
83 changes: 82 additions & 1 deletion src/server/adminJs/tabs/projectVerificationTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ProjectVerificationForm,
} from '../../../entities/projectVerificationForm';
import {
canAccessProjectAction,
canAccessProjectVerificationFormAction,
ResourceActions,
} from '../adminJsPermissions';
Expand All @@ -21,6 +22,7 @@ import {
approveMultipleProjects,
approveProject,
findProjectVerificationFormById,
getVerificationFormByProjectId,
makeFormDraft,
verifyForm,
verifyMultipleForms,
Expand All @@ -35,8 +37,13 @@ import {
} from '../../../repositories/projectRepository';
import { getNotificationAdapter } from '../../../adapters/adaptersFactory';
import { logger } from '../../../utils/logger';
import { Project } from '../../../entities/project';
import { Project, RevokeSteps } from '../../../entities/project';
import { fillSocialProfileAndQfRounds } from './projectsTab';
import { refreshUserProjectPowerView } from '../../../repositories/userProjectPowerViewRepository';
import {
refreshProjectFuturePowerView,
refreshProjectPowerView,
} from '../../../repositories/projectPowerViewRepository';

const extractLastComment = (verificationForm: ProjectVerificationForm) => {
const commentsSorted = verificationForm.commentsSection?.comments?.sort(
Expand Down Expand Up @@ -303,6 +310,67 @@ export const approveVerificationForms = async (
};
};

export const revokeGivbacksEligibility = async (
context: AdminJsContextInterface,
request: AdminJsRequestInterface,
) => {
const { records, currentAdmin } = context;
try {
const projectFormIds = request?.query?.recordIds
?.split(',')
?.map(strId => Number(strId)) as number[];
const projectIds: number[] = [];
for (const formId of projectFormIds) {
const verificationForm = await findProjectVerificationFormById(formId);
if (verificationForm) {
projectIds.push(verificationForm.projectId);
}
}
const updateParams = { isGivbackEligible: false };
const projects = await Project.createQueryBuilder('project')
.update<Project>(Project, updateParams)
.where('project.id IN (:...ids)')
.setParameter('ids', projectIds)
.returning('*')
.updateEntity(true)
.execute();

for (const project of projects.raw) {
const projectWithAdmin = (await findProjectById(project.id)) as Project;
projectWithAdmin.verificationStatus = RevokeSteps.Revoked;
await projectWithAdmin.save();
await getNotificationAdapter().projectBadgeRevoked({
project: projectWithAdmin,
});
const verificationForm = await getVerificationFormByProjectId(project.id);
if (verificationForm) {
await makeFormDraft({
formId: verificationForm.id,
adminId: currentAdmin.id,
});
}
}
await Promise.all([
refreshUserProjectPowerView(),
refreshProjectPowerView(),
refreshProjectFuturePowerView(),
]);
} catch (error) {
logger.error('revokeGivbacksEligibility() error', error);
throw error;
}
return {
redirectUrl: '/admin/resources/ProjectVerificationForm',
records: records.map(record => {
record.toJSON(context.currentAdmin);
}),
notice: {
message: 'Project(s) successfully revoked from Givbacks eligibility',
type: 'success',
},
};
};

export const projectVerificationTab = {
resource: ProjectVerificationForm,
options: {
Expand Down Expand Up @@ -678,6 +746,19 @@ export const projectVerificationTab = {
},
component: false,
},
revokeGivbacksEligible: {
actionType: 'bulk',
isVisible: true,
isAccessible: ({ currentAdmin }) =>
canAccessProjectAction(
{ currentAdmin },
ResourceActions.REVOKE_BADGE,
),
handler: async (request, _response, context) => {
return revokeGivbacksEligibility(context, request);
},
component: false,
},
},
},
};
4 changes: 2 additions & 2 deletions src/server/adminJs/tabs/projectsTab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import {
addFeaturedProjectUpdate,
exportProjectsWithFiltersToCsv,
listDelist,
revokeGivbacksEligibility,
updateStatusOfProjects,
verifyProjects,
} from './projectsTab';
import { messages } from '../../../utils/messages';
import { ProjectStatus } from '../../../entities/projectStatus';
import { revokeGivbacksEligibility } from './projectVerificationTab';

describe(
'verifyMultipleProjects() test cases',
Expand Down Expand Up @@ -464,7 +464,7 @@ function verifyProjectsTestCases() {
},
{
query: {
recordIds: String(project.id),
recordIds: String(projectVerificationForm.id),
},
},
);
Expand Down
68 changes: 0 additions & 68 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ProjectUpdate,
ProjStatus,
ReviewStatus,
RevokeSteps,
} from '../../../entities/project';
import { canAccessProjectAction, ResourceActions } from '../adminJsPermissions';
import {
Expand Down Expand Up @@ -184,60 +183,6 @@ export const addFeaturedProjectUpdate = async (
};
};

export const revokeGivbacksEligibility = async (
context: AdminJsContextInterface,
request: AdminJsRequestInterface,
) => {
const { records, currentAdmin } = context;
try {
const projectIds = request?.query?.recordIds
?.split(',')
?.map(strId => Number(strId)) as number[];
const updateParams = { isGivbackEligible: false };
const projects = await Project.createQueryBuilder('project')
.update<Project>(Project, updateParams)
.where('project.id IN (:...ids)')
.setParameter('ids', projectIds)
.returning('*')
.updateEntity(true)
.execute();

for (const project of projects.raw) {
const projectWithAdmin = (await findProjectById(project.id)) as Project;
projectWithAdmin.verificationStatus = RevokeSteps.Revoked;
await projectWithAdmin.save();
await getNotificationAdapter().projectBadgeRevoked({
project: projectWithAdmin,
});
const verificationForm = await getVerificationFormByProjectId(project.id);
if (verificationForm) {
await makeFormDraft({
formId: verificationForm.id,
adminId: currentAdmin.id,
});
}
}
await Promise.all([
refreshUserProjectPowerView(),
refreshProjectPowerView(),
refreshProjectFuturePowerView(),
]);
} catch (error) {
logger.error('revokeGivbacksEligibility() error', error);
throw error;
}
return {
redirectUrl: '/admin/resources/Project',
records: records.map(record => {
record.toJSON(context.currentAdmin);
}),
notice: {
message: 'Project(s) successfully revoked from Givbacks eligibility',
type: 'success',
},
};
};

export const verifyProjects = async (
context: AdminJsContextInterface,
request: AdminJsRequestInterface,
Expand Down Expand Up @@ -1310,19 +1255,6 @@ export const projectsTab = {
},
component: false,
},
revokeGivbacksEligible: {
actionType: 'bulk',
isVisible: true,
isAccessible: ({ currentAdmin }) =>
canAccessProjectAction(
{ currentAdmin },
ResourceActions.REVOKE_BADGE,
),
handler: async (request, _response, context) => {
return revokeGivbacksEligibility(context, request);
},
component: false,
},
activate: {
actionType: 'bulk',
isVisible: true,
Expand Down

0 comments on commit 67b3adb

Please sign in to comment.