From 9cfdfc56569e80427af0615f3c8cc3fa3e56f09b Mon Sep 17 00:00:00 2001 From: Ramin Date: Thu, 3 Oct 2024 02:06:04 +0330 Subject: [PATCH] Ensure correct emails are sent for project status changes related to decentralized verification --- src/server/adminJs/tabs/projectsTab.ts | 120 +++++++++++++------------ 1 file changed, 65 insertions(+), 55 deletions(-) diff --git a/src/server/adminJs/tabs/projectsTab.ts b/src/server/adminJs/tabs/projectsTab.ts index a61aedfa6..49e2e29b9 100644 --- a/src/server/adminJs/tabs/projectsTab.ts +++ b/src/server/adminJs/tabs/projectsTab.ts @@ -184,29 +184,72 @@ 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, 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, - verified: boolean = true, - revokeBadge: boolean = false, + vouchedStatus: boolean = true, ) => { const { records, currentAdmin } = context; - // prioritize revokeBadge - const verificationStatus = revokeBadge ? false : verified; try { const projectIds = request?.query?.recordIds ?.split(',') ?.map(strId => Number(strId)) as number[]; const projectsBeforeUpdating = await findProjectsByIdArray(projectIds); - const updateParams = { verified: verificationStatus }; - - if (verificationStatus) { - await Project.query(` - UPDATE project - SET "verificationStatus" = NULL - WHERE id IN (${request?.query?.recordIds}) - `); - } + const updateParams = { verified: vouchedStatus }; const projects = await Project.createQueryBuilder('project') .update(Project, updateParams) @@ -219,11 +262,11 @@ export const verifyProjects = async ( for (const project of projects.raw) { if ( projectsBeforeUpdating.find(p => p.id === project.id)?.verified === - verificationStatus + vouchedStatus ) { logger.debug('verifying/unVerifying project but no changes happened', { projectId: project.id, - verificationStatus, + verificationStatus: vouchedStatus, }); // if project.verified have not changed, so we should not execute rest of the codes continue; @@ -232,42 +275,10 @@ export const verifyProjects = async ( project, status: project.status, userId: currentAdmin.id, - description: verified + description: vouchedStatus ? HISTORY_DESCRIPTIONS.CHANGED_TO_VERIFIED : HISTORY_DESCRIPTIONS.CHANGED_TO_UNVERIFIED, }); - const projectWithAdmin = (await findProjectById(project.id)) as Project; - - if (revokeBadge) { - projectWithAdmin.verificationStatus = RevokeSteps.Revoked; - await projectWithAdmin.save(); - await getNotificationAdapter().projectBadgeRevoked({ - project: projectWithAdmin, - }); - } else if (verificationStatus) { - await getNotificationAdapter().projectVerified({ - project: projectWithAdmin, - }); - } else { - await getNotificationAdapter().projectUnVerified({ - project: projectWithAdmin, - }); - } - - const verificationForm = await getVerificationFormByProjectId(project.id); - if (verificationForm) { - if (verificationStatus) { - await makeFormVerified({ - formId: verificationForm.id, - adminId: currentAdmin.id, - }); - } else { - await makeFormDraft({ - formId: verificationForm.id, - adminId: currentAdmin.id, - }); - } - } } await Promise.all([ @@ -286,7 +297,7 @@ export const verifyProjects = async ( }), notice: { message: `Project(s) successfully ${ - verificationStatus ? 'verified' : 'unverified' + vouchedStatus ? 'vouched' : 'unvouched' }`, type: 'success', }, @@ -1273,7 +1284,7 @@ export const projectsTab = { }, component: false, }, - verify: { + approveVouched: { actionType: 'bulk', isVisible: true, isAccessible: ({ currentAdmin }) => @@ -1286,7 +1297,7 @@ export const projectsTab = { }, component: false, }, - reject: { + removeVouched: { actionType: 'bulk', isVisible: true, isAccessible: ({ currentAdmin }) => @@ -1299,8 +1310,7 @@ export const projectsTab = { }, component: false, }, - // the difference is that it sends another segment event - revokeBadge: { + revokeGivbacksEligible: { actionType: 'bulk', isVisible: true, isAccessible: ({ currentAdmin }) => @@ -1308,8 +1318,8 @@ export const projectsTab = { { currentAdmin }, ResourceActions.REVOKE_BADGE, ), - handler: async (request, response, context) => { - return verifyProjects(context, request, false, true); + handler: async (request, _response, context) => { + return revokeGivbacksEligibility(context, request); }, component: false, },