Skip to content

Commit

Permalink
Merge pull request #1637 from Giveth/optimize-check-project-verification
Browse files Browse the repository at this point in the history
Optimize check project verification
  • Loading branch information
RamRamez authored Jun 22, 2024
2 parents 86107c8 + 6f81446 commit 076c518
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 70 deletions.
18 changes: 11 additions & 7 deletions src/adapters/notifications/NotificationCenterAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
},
trackId: `project-badge-revoked-${
project.id
}-${user.walletAddress?.toLowerCase()}-${now}`,
}-${user?.walletAddress?.toLowerCase()}-${now}`,
});
}

Expand All @@ -394,13 +394,15 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
},
trackId: `project-badge-revoke-reminder-${
project.id
}-${user.walletAddress?.toLowerCase()}-${now}`,
}-${user?.walletAddress?.toLowerCase()}-${now}`,
});
}

async projectBadgeRevokeWarning(params: { project: Project }): Promise<void> {
const { project } = params;
const user = project.adminUser as User;
if (!project.adminUser?.email) {
project.adminUser = (await findUserById(project.adminUserId))!;
}
const now = new Date();

await sendProjectRelatedNotificationsQueue.add({
Expand All @@ -414,15 +416,17 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
},
trackId: `project-badge-revoke-warning-${
project.id
}-${user.walletAddress?.toLowerCase()}-${now}`,
}-${project.adminUser.walletAddress?.toLowerCase()}-${now}`,
});
}

async projectBadgeRevokeLastWarning(params: {
project: Project;
}): Promise<void> {
const { project } = params;
const user = project.adminUser as User;
if (!project.adminUser?.email) {
project.adminUser = (await findUserById(project.adminUserId))!;
}
const now = Date.now();
await sendProjectRelatedNotificationsQueue.add({
project,
Expand All @@ -435,7 +439,7 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
},
trackId: `project-badge-revoke-last-warning-${
project.id
}-${user.walletAddress?.toLowerCase()}-${now}`,
}-${project.adminUser?.walletAddress?.toLowerCase()}-${now}`,
});
}

Expand All @@ -455,7 +459,7 @@ export class NotificationCenterAdapter implements NotificationAdapterInterface {
},
trackId: `project-badge-up-for-revoking-${
project.id
}-${user.walletAddress?.toLowerCase()}-${now}`,
}-${user?.walletAddress?.toLowerCase()}-${now}`,
});
}

Expand Down
26 changes: 11 additions & 15 deletions src/repositories/projectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ export const projectsWithoutUpdateAfterTimeFrame = async (
.leftJoin('project.projectUpdates', 'projectUpdates')
.select('project.id', 'projectId')
.addSelect('MAX(projectUpdates.createdAt)', 'latestUpdate')
.where('project.isImported = false')
.andWhere('project.verified = true')
.andWhere(
'(project.verificationStatus NOT IN (:...statuses) OR project.verificationStatus IS NULL)',
{
statuses: [RevokeSteps.UpForRevoking, RevokeSteps.Revoked],
},
)
.groupBy('project.id')
.having('MAX(projectUpdates.createdAt) < :date', { date })
.getRawMany();
Expand All @@ -267,21 +275,9 @@ export const projectsWithoutUpdateAfterTimeFrame = async (
);

const projects = await Project.createQueryBuilder('project')
.where('project.isImported = false')
.andWhere('project.verified = true')
.andWhere(
'(project.verificationStatus NOT IN (:...statuses) OR project.verificationStatus IS NULL)',
{
statuses: [RevokeSteps.UpForRevoking, RevokeSteps.Revoked],
},
)
.andWhereInIds(validProjectIds)
.leftJoinAndSelect(
'project.projectVerificationForm',
'projectVerificationForm',
)
.leftJoinAndSelect('project.adminUser', 'user')
.leftJoinAndSelect('project.projectUpdates', 'projectUpdates')
.whereInIds(validProjectIds)
.leftJoin('project.projectUpdates', 'projectUpdates')
.addSelect(['projectUpdates.createdAt', 'projectUpdates.id'])
.getMany();

projects.forEach(project => {
Expand Down
8 changes: 4 additions & 4 deletions src/services/cronJobs/checkProjectVerificationStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ function checkProjectVerificationStatusTestCases() {
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
verified: true,
updatedAt: moment().subtract(46, 'days').endOf('day').toDate(),
projectUpdateCreationDate: moment().subtract(46, 'days').endOf('day'),
projectUpdateCreationDate: moment()
.subtract(46, 'days')
.endOf('day')
.toDate(),
verificationStatus: RevokeSteps.Reminder,
});

Expand All @@ -46,7 +48,6 @@ function checkProjectVerificationStatusTestCases() {
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
verified: true,
updatedAt: moment().subtract(91, 'days').endOf('day').toDate(),
projectUpdateCreationDate: moment().subtract(91, 'days').endOf('day'),
verificationStatus: RevokeSteps.Warning,
});
Expand All @@ -67,7 +68,6 @@ function checkProjectVerificationStatusTestCases() {
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
verified: true,
updatedAt: moment().subtract(105, 'days').endOf('day').toDate(),
projectUpdateCreationDate: moment().subtract(105, 'days').endOf('day'),
verificationStatus: RevokeSteps.LastChance,
});
Expand Down
52 changes: 8 additions & 44 deletions src/services/cronJobs/checkProjectVerificationStatus.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { schedule } from 'node-cron';
import moment = require('moment');
import { Project, RevokeSteps } from '../../entities/project';
import { HISTORY_DESCRIPTIONS } from '../../entities/projectStatusHistory';
import config from '../../config';
import { logger } from '../../utils/logger';
import { projectsWithoutUpdateAfterTimeFrame } from '../../repositories/projectRepository';
import { i18n, translationErrorMessagesKeys } from '../../utils/errorMessages';
import { makeFormDraft } from '../../repositories/projectVerificationRepository';
import { sleep } from '../../utils/utils';
import { getNotificationAdapter } from '../../adapters/adaptersFactory';
import { refreshUserProjectPowerView } from '../../repositories/userProjectPowerViewRepository';
import {
refreshProjectFuturePowerView,
refreshProjectPowerView,
} from '../../repositories/projectPowerViewRepository';

const cronJobTime =
(config.get(
Expand Down Expand Up @@ -60,7 +52,6 @@ export const checkProjectVerificationStatus = async () => {
maxDaysForSendingUpdateWarning,
);
logger.debug('checkProjectVerificationStatus() has been called', {
maxDaysForSendingUpdateWarning,
foundProjectsCount: projects.length,
projects: projects.map(p => {
return {
Expand All @@ -76,29 +67,25 @@ export const checkProjectVerificationStatus = async () => {
} catch (error) {
logger.error('Error in remindUpdatesOrRevokeVerification', {
projectId: project.id,
projectSlug: project.slug,
projectVerificationStatus: project.verificationStatus,
error,
});
}
}

if (projects.length > 0) {
await Promise.all([
refreshUserProjectPowerView(),
refreshProjectPowerView(),
refreshProjectFuturePowerView(),
]);
}
};

const remindUpdatesOrRevokeVerification = async (project: Project) => {
// We don't revoke verification badge for any projects.
const latestUpdate =
project.projectUpdates?.[0].createdAt || project.updatedAt;
if (
!project ||
!project.projectUpdates ||
project.projectUpdates.length === 0
) {
return;
}
const latestUpdate = project.projectUpdates[0].createdAt;
logger.debug('remindUpdatesOrRevokeVerification() has been called', {
projectId: project.id,
projectSlug: project.slug,
projectVerificationStatus: project.verificationStatus,
latestUpdate,
});
Expand Down Expand Up @@ -127,31 +114,9 @@ const remindUpdatesOrRevokeVerification = async (project: Project) => {
await sendProperNotification(project, project.verificationStatus as string);
logger.debug('remindUpdatesOrRevokeVerification() save project', {
projectId: project.id,
slug: project.slug,
verificationStatus: project.verificationStatus,
});
}

// draft the verification form to allow to reapply
if (
project.projectVerificationForm &&
project.verificationStatus === RevokeSteps.Revoked
) {
await makeFormDraft({
formId: project.projectVerificationForm.id,
});
}

// save status changes history
if (project.verificationStatus === RevokeSteps.Revoked) {
await Project.addProjectStatusHistoryRecord({
project,
status: project.status,
description: HISTORY_DESCRIPTIONS.CHANGED_TO_UNVERIFIED_BY_CRONJOB,
});
}

await sleep(300);
};

const sendProperNotification = (
Expand All @@ -160,7 +125,6 @@ const sendProperNotification = (
) => {
logger.debug('sendProperNotification()', {
projectId: project.id,
slug: project.slug,
verificationStatus: project.verificationStatus,
});
switch (projectVerificationStatus) {
Expand Down

0 comments on commit 076c518

Please sign in to comment.