Skip to content

Commit

Permalink
Ensure correct emails are sent for project status changes related to …
Browse files Browse the repository at this point in the history
…decentralized verification
  • Loading branch information
RamRamez committed Oct 2, 2024
1 parent 046b6e8 commit 9cfdfc5
Showing 1 changed file with 65 additions and 55 deletions.
120 changes: 65 additions & 55 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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>(Project, updateParams)
Expand All @@ -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;
Expand All @@ -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([
Expand All @@ -286,7 +297,7 @@ export const verifyProjects = async (
}),
notice: {
message: `Project(s) successfully ${
verificationStatus ? 'verified' : 'unverified'
vouchedStatus ? 'vouched' : 'unvouched'
}`,
type: 'success',
},
Expand Down Expand Up @@ -1273,7 +1284,7 @@ export const projectsTab = {
},
component: false,
},
verify: {
approveVouched: {
actionType: 'bulk',
isVisible: true,
isAccessible: ({ currentAdmin }) =>
Expand All @@ -1286,7 +1297,7 @@ export const projectsTab = {
},
component: false,
},
reject: {
removeVouched: {
actionType: 'bulk',
isVisible: true,
isAccessible: ({ currentAdmin }) =>
Expand All @@ -1299,17 +1310,16 @@ export const projectsTab = {
},
component: false,
},
// the difference is that it sends another segment event
revokeBadge: {
revokeGivbacksEligible: {
actionType: 'bulk',
isVisible: true,
isAccessible: ({ currentAdmin }) =>
canAccessProjectAction(
{ 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,
},
Expand Down

0 comments on commit 9cfdfc5

Please sign in to comment.