Skip to content

Commit

Permalink
Implement doesDonatedToProjectInQfRound webservice
Browse files Browse the repository at this point in the history
related #1138
  • Loading branch information
mohammadranjbarz committed Oct 2, 2023
1 parent 3e2d607 commit 6519399
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 6 deletions.
193 changes: 193 additions & 0 deletions src/repositories/donationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
findDonationsByTransactionId,
findStableCoinDonationsWithoutPrice,
getPendingDonationsIds,
isVerifiedDonationExistsInQfRound,
sumDonationValueUsd,
sumDonationValueUsdForQfRound,
} from './donationRepository';
Expand Down Expand Up @@ -64,6 +65,10 @@ describe(
describe('countUniqueDonors() test cases', countUniqueDonorsTestCases);
describe('sumDonationValueUsd() test cases', sumDonationValueUsdTestCases);
describe('estimatedMatching() test cases', estimatedMatchingTestCases);
describe(
'isVerifiedDonationExistsInQfRound() test cases',
isVerifiedDonationExistsInQfRoundTestCases,
);

function fillQfRoundDonationsUserScoresTestCases() {
let qfRound: QfRound;
Expand Down Expand Up @@ -1262,3 +1267,191 @@ function sumDonationValueUsdTestCases() {
assert.equal(donationSum, valueUsd1 + valueUsd2 + valueUsd3);
});
}

function isVerifiedDonationExistsInQfRoundTestCases() {
it('should return true when there is a verified donation', async () => {
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const qfRound = await QfRound.create({
isActive: true,
name: new Date().toString(),
allocatedFund: 100,
minimumPassportScore: 12,
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
}).save();
project.qfRounds = [qfRound];
await project.save();
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
qfRoundId: qfRound.id,
},
donor.id,
project.id,
);
const doesExist = await isVerifiedDonationExistsInQfRound({
projectId: project.id,
qfRoundId: qfRound.id,
userId: donor.id,
});
assert.isTrue(doesExist);

qfRound.isActive = false;
await qfRound.save();
});
it('should return false when there is a non-verified donation', async () => {
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const qfRound = await QfRound.create({
isActive: true,
name: new Date().toString(),
allocatedFund: 100,
minimumPassportScore: 12,
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
}).save();
project.qfRounds = [qfRound];
await project.save();
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'pending',
qfRoundId: qfRound.id,
},
donor.id,
project.id,
);
const doesExist = await isVerifiedDonationExistsInQfRound({
projectId: project.id,
qfRoundId: qfRound.id,
userId: donor.id,
});
assert.isFalse(doesExist);

qfRound.isActive = false;
await qfRound.save();
});
it('should return false when sending invalid userId', async () => {
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const qfRound = await QfRound.create({
isActive: true,
name: new Date().toString(),
allocatedFund: 100,
minimumPassportScore: 12,
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
}).save();
project.qfRounds = [qfRound];
await project.save();
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
qfRoundId: qfRound.id,
},
donor.id,
project.id,
);
const doesExist = await isVerifiedDonationExistsInQfRound({
projectId: project.id,
qfRoundId: qfRound.id,
userId: 999999,
});
assert.isFalse(doesExist);

qfRound.isActive = false;
await qfRound.save();
});
it('should return false when sending invalid projectId', async () => {
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const qfRound = await QfRound.create({
isActive: true,
name: new Date().toString(),
allocatedFund: 100,
minimumPassportScore: 12,
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
}).save();
project.qfRounds = [qfRound];
await project.save();
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
qfRoundId: qfRound.id,
},
donor.id,
project.id,
);
const doesExist = await isVerifiedDonationExistsInQfRound({
projectId: 9999900,
qfRoundId: qfRound.id,
userId: donor.id,
});
assert.isFalse(doesExist);

qfRound.isActive = false;
await qfRound.save();
});
it('should return false when sending invalid qfRoundId', async () => {
const project = await saveProjectDirectlyToDb({
...createProjectData(),
title: String(new Date().getTime()),
slug: String(new Date().getTime()),
});
const qfRound = await QfRound.create({
isActive: true,
name: new Date().toString(),
allocatedFund: 100,
minimumPassportScore: 12,
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
}).save();
project.qfRounds = [qfRound];
await project.save();
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

await saveDonationDirectlyToDb(
{
...createDonationData(),
status: 'verified',
qfRoundId: qfRound.id,
},
donor.id,
project.id,
);
const doesExist = await isVerifiedDonationExistsInQfRound({
projectId: project.id,
qfRoundId: 9999999,
userId: donor.id,
});
assert.isFalse(doesExist);

qfRound.isActive = false;
await qfRound.save();
});
}
31 changes: 31 additions & 0 deletions src/repositories/donationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import moment from 'moment';
import { ProjectEstimatedMatchingView } from '../entities/ProjectEstimatedMatchingView';
import { AppDataSource } from '../orm';
import { getProjectDonationsSqrtRootSum } from './qfRoundRepository';
import { logger } from '../utils/logger';

export const fillQfRoundDonationsUserScores = async (): Promise<void> => {
await Donation.query(`
Expand Down Expand Up @@ -379,3 +380,33 @@ export async function sumDonationValueUsd(projectId: number): Promise<number> {

return result[0]?.sumVerifiedDonations || 0;
}

export async function isVerifiedDonationExistsInQfRound(params: {
qfRoundId: number;
projectId: number;
userId: number;
}): Promise<boolean> {
try {
const result = await Donation.query(
`
SELECT EXISTS (
SELECT 1
FROM donation
WHERE
status = 'verified' AND
"qfRoundId" = $1 AND
"projectId" = $2 AND
"userId" = $3
) AS exists;
`,
[params.qfRoundId, params.projectId, params.userId],
);
return result?.[0]?.exists || false;
} catch (err) {
logger.error(
'Error executing the query in isVerifiedDonationExists() function',
err,
);
return false;
}
}
1 change: 1 addition & 0 deletions src/repositories/qfRoundRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { QfRound } from '../entities/qfRound';
import { AppDataSource } from '../orm';
import { logger } from '../utils/logger';

const qfRoundEstimatedMatchingParamsCacheDuration = Number(
process.env.QF_ROUND_ESTIMATED_MATCHING_CACHE_DURATION || 60000,
Expand Down
Loading

0 comments on commit 6519399

Please sign in to comment.