Skip to content

Commit

Permalink
Merge pull request #103 from GeneralMagicio/feat/include-pending-dona…
Browse files Browse the repository at this point in the history
…tion-in-cap

Fixed issue in update place
  • Loading branch information
ae2079 authored Oct 18, 2024
2 parents 9a36a95 + 5bcc552 commit 143bada
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/repositories/projectRoundRecordRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export async function updateOrCreateProjectRoundRecord(
): Promise<ProjectRoundRecord> {
try {
let query = Donation.createQueryBuilder('donation')
.select('SUM(donation.amount)', 'totalDonationAmount')
.addSelect('SUM(donation.valueUsd)', 'totalDonationUsdAmount')
.select('SUM(COALESCE(donation.amount))', 'totalDonationAmount')
.addSelect('SUM(COALESCE(donation.valueUsd,0))', 'totalDonationUsdAmount')
.where('donation.projectId = :projectId', { projectId })
.andWhere('donation.status IN (:...status)', {
status: [DONATION_STATUS.VERIFIED, DONATION_STATUS.PENDING],
Expand Down Expand Up @@ -143,8 +143,8 @@ export async function getCumulativePastRoundsDonationAmounts({
'cumulativePastRoundsDonationAmounts',
)
.where('donation.projectId = :projectId', { projectId })
.andWhere('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
.andWhere('donation.status IN (:...status)', {
status: [DONATION_STATUS.VERIFIED, DONATION_STATUS.PENDING],
});

if (earlyAccessRoundId) {
Expand Down
90 changes: 90 additions & 0 deletions src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { QACC_DONATION_TOKEN_SYMBOL } from '../constants/qacc';
import { EarlyAccessRound } from '../entities/earlyAccessRound';
import { ProjectRoundRecord } from '../entities/projectRoundRecord';
import { ProjectUserRecord } from '../entities/projectUserRecord';
import { CoingeckoPriceAdapter } from '../adapters/price/CoingeckoPriceAdapter';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const moment = require('moment');
Expand Down Expand Up @@ -102,6 +103,8 @@ describe('donationMetrics() test cases', donationMetricsTestCases);

describe('qAcc limit tests', qAccLimitTestCases);

describe('qAcc cap change on donation creation', qAccCapChangeTestCases);

// // describe('tokens() test cases', tokensTestCases);

// // TODO I think we can delete addUserVerification query
Expand Down Expand Up @@ -4982,3 +4985,90 @@ function qAccLimitTestCases() {
assert.equal(errors[0]!.message, errorMessages.EXCEED_QACC_CAP);
});
}

function qAccCapChangeTestCases() {
let ea;
const tokenPrice = 0.1;
beforeEach(async () => {
ea = await EarlyAccessRound.create({
roundNumber: generateEARoundNumber(),
startDate: moment().subtract(1, 'days').toDate(),
endDate: moment().add(3, 'days').toDate(),
roundUSDCapPerProject: 1000000,
roundUSDCapPerUserPerProject: 50000,
tokenPrice,
}).save();
sinon
.stub(qAccService, 'getQAccDonationCap')
.resolves(Number.MAX_SAFE_INTEGER);
sinon
.stub(CoingeckoPriceAdapter.prototype, 'getTokenPrice')
.resolves(tokenPrice);
});

afterEach(async () => {
sinon.restore();
if (ea) {
await ProjectRoundRecord.delete({});
await ProjectUserRecord.delete({});
await Donation.delete({ earlyAccessRoundId: ea.id });
await EarlyAccessRound.delete({});
ea = null;
}
});

it('should update projectUserRecord and projectRoundRecord when donation is created', async () => {
const project = await saveProjectDirectlyToDb(createProjectData());
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);

const usdAmount = 100;
const donationAmount = usdAmount / ea.tokenPrice;

const saveDonationResponse = await axios.post(
graphqlUrl,
{
query: createDonationMutation,
variables: {
projectId: project.id,
transactionNetworkId: QACC_NETWORK_ID,
transactionId: generateRandomEvmTxHash(),
nonce: 1,
amount: donationAmount,
token: QACC_DONATION_TOKEN_SYMBOL,
},
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
assert.isOk(saveDonationResponse.data.data.createDonation);
const donation = await Donation.findOne({
where: {
id: saveDonationResponse.data.data.createDonation,
},
});

assert.equal(donation?.status, DONATION_STATUS.PENDING);
assert.equal(donation?.earlyAccessRoundId, ea.id);

const projectUserRecord = await ProjectUserRecord.findOneBy({
projectId: project.id,
userId: user.id,
});

assert.isOk(projectUserRecord);
assert.equal(projectUserRecord?.totalDonationAmount, donationAmount);

const projectRoundRecord = await ProjectRoundRecord.findOneBy({
projectId: project.id,
earlyAccessRoundId: ea.id,
});

assert.isOk(projectRoundRecord);
assert.equal(projectRoundRecord?.totalDonationAmount, donationAmount);
assert.equal(projectRoundRecord?.totalDonationUsdAmount, usdAmount);
});
}
20 changes: 10 additions & 10 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,16 +921,6 @@ export class DonationResolver {
await donation.save();
}

await updateOrCreateProjectRoundRecord(
donation.projectId,
donation.qfRoundId,
donation.earlyAccessRoundId,
);
await updateOrCreateProjectUserRecord({
projectId: donation.projectId,
userId: donation.userId,
});

let priceChainId;

switch (transactionNetworkId) {
Expand Down Expand Up @@ -958,6 +948,16 @@ export class DonationResolver {
priceChainId,
);

await updateOrCreateProjectRoundRecord(
donation.projectId,
donation.qfRoundId,
donation.earlyAccessRoundId,
);
await updateOrCreateProjectUserRecord({
projectId: donation.projectId,
userId: donation.userId,
});

if (chainType === ChainType.EVM) {
await markDraftDonationStatusMatched({
matchedDonationId: donation.id,
Expand Down

0 comments on commit 143bada

Please sign in to comment.