Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update verify approve conditions #1866

Open
wants to merge 15 commits into
base: staging
Choose a base branch
from
23 changes: 10 additions & 13 deletions src/repositories/projectVerificationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,21 @@ export const approveProject = async (params: {
return project.save();
};

export const approveMultipleProjects = async (params: {
export const approveMultipleProjects = async ({
approved,
projectsIds,
}: {
approved: boolean;
projectsIds: string[] | number[];
}): Promise<UpdateResult> => {
if (params.approved) {
await Project.query(`
UPDATE project
SET "verificationStatus" = NULL
WHERE id IN (${params.projectsIds?.join(',')})
`);
}

return Project.createQueryBuilder('project')
.update<Project>(Project, {
isGivbackEligible: params.approved,
.update<Project>(Project)
.set({
isGivbackEligible: approved,
verified: approved ? true : () => 'verified',
verificationStatus: approved ? null : () => '"verificationStatus"',
})
.where('project.id IN (:...ids)')
.setParameter('ids', params.projectsIds)
.where('project.id IN (:...ids)', { ids: projectsIds })
MohammadPCh marked this conversation as resolved.
Show resolved Hide resolved
.returning('*')
.updateEntity(true)
.execute();
Expand Down
23 changes: 21 additions & 2 deletions src/server/adminJs/tabs/projectVerificationTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ export const approveVerificationForms = async (
? PROJECT_VERIFICATION_STATUSES.VERIFIED
: PROJECT_VERIFICATION_STATUSES.REJECTED;
const formIds = request?.query?.recordIds?.split(',');

logger.info('approveVerificationForms() formIds', formIds);

// Preliminary check for DRAFT status
const draftProjects = await ProjectVerificationForm.createQueryBuilder(
'form',
)
.where('form.id IN (:...formIds)', { formIds })
.andWhere('form.status = :status', {
status: PROJECT_VERIFICATION_STATUSES.DRAFT,
})
.getMany();

if (draftProjects.length > 0) {
throw new Error(
`Cannot ${approved ? 'approve' : 'reject'} project${draftProjects.length > 1 ? 's' : ''} in DRAFT status.`,
);
}

// call repositories
const projectsForms = await verifyMultipleForms({
verificationStatus,
Expand All @@ -261,7 +280,7 @@ export const approveVerificationForms = async (
return project.id;
});

// need to requery them as the RAW is not an entity
// need to re-query them as the RAW is not an entity
const verificationForms = await ProjectVerificationForm.createQueryBuilder(
'projectVerificationForm',
)
Expand Down Expand Up @@ -294,7 +313,7 @@ export const approveVerificationForms = async (
}`;
} catch (error) {
logger.error('verifyVerificationForm() error', error);
responseMessage = `Bulk verify failed ${error.message}`;
responseMessage = `Bulk verify failed: ${error.message}`;
responseType = 'danger';
}
return {
Expand Down
47 changes: 34 additions & 13 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ export const verifyProjects = async (
?.split(',')
?.map(strId => Number(strId)) as number[];
const projectsBeforeUpdating = await findProjectsByIdArray(projectIds);

// Check if any project is Givback eligible and vouchedStatus is false\
if (!vouchedStatus) {
for (const project of projectsBeforeUpdating) {
if (project.isGivbackEligible) {
throw new Error(
`The project with ID ${project.id} is Givback-eligible, so the Vouched badge cannot be revoked.`,
);
}
}
}

MohammadPCh marked this conversation as resolved.
Show resolved Hide resolved
const updateParams = { verified: vouchedStatus };

const projects = await Project.createQueryBuilder('project')
Expand Down Expand Up @@ -231,22 +243,31 @@ export const verifyProjects = async (
refreshProjectPowerView(),
refreshProjectFuturePowerView(),
]);
return {
redirectUrl: '/admin/resources/Project',
records: records.map(record => {
record.toJSON(context.currentAdmin);
}),
MohammadPCh marked this conversation as resolved.
Show resolved Hide resolved
notice: {
message: `Project(s) successfully ${
vouchedStatus ? 'vouched' : 'unvouched'
}`,
type: 'success',
},
};
} catch (error) {
logger.error('verifyProjects() error', error);
throw error;
return {
redirectUrl: '/admin/resources/Project',
records: records.map(record => {
record.toJSON(context.currentAdmin);
}),
notice: {
message: error.message,
type: 'error',
},
};
}
return {
redirectUrl: '/admin/resources/Project',
records: records.map(record => {
record.toJSON(context.currentAdmin);
}),
notice: {
message: `Project(s) successfully ${
vouchedStatus ? 'vouched' : 'unvouched'
}`,
type: 'success',
},
};
};

export const updateStatusOfProjects = async (
Expand Down
Loading