diff --git a/src/resolvers/donationResolver.test.ts b/src/resolvers/donationResolver.test.ts index 2101880f6..4eb65a784 100644 --- a/src/resolvers/donationResolver.test.ts +++ b/src/resolvers/donationResolver.test.ts @@ -2,7 +2,7 @@ import { assert } from 'chai'; import axios, { AxiosResponse } from 'axios'; import { In, Not } from 'typeorm'; import sinon from 'sinon'; -import { ExecutionResult, GraphQLError } from 'graphql'; +import { ExecutionResult } from 'graphql'; import qAccService from '../services/qAccService'; import { generateTestAccessToken, @@ -4942,7 +4942,7 @@ function qAccLimitTestCases() { assert.equal(donation?.earlyAccessRoundId, earlyAccessRound1.id); }); - it('should throw exceed user limit error in an active early access round', async () => { + it('should not associate to round when user limit exceed in an active early access round', async () => { const tokenPrice = 0.1; const roundUSDCapPerUserPerProject = 50000; earlyAccessRound1 = await EarlyAccessRound.create({ @@ -4956,32 +4956,40 @@ function qAccLimitTestCases() { const amount = roundUSDCapPerUserPerProject / tokenPrice + 1; // send create donation request - const donationsResponse: AxiosResponse< - ExecutionResult<{ createDonation: number }> - > = await axios.post( - graphqlUrl, - { - query: createDonationMutation, - variables: { - projectId: project.id, - transactionNetworkId: QACC_NETWORK_ID, - transactionId: generateRandomEvmTxHash(), - nonce: 1, - amount: amount, - token: QACC_DONATION_TOKEN_SYMBOL, + const result: AxiosResponse> = + await axios.post( + graphqlUrl, + { + query: createDonationMutation, + variables: { + projectId: project.id, + transactionNetworkId: QACC_NETWORK_ID, + transactionId: generateRandomEvmTxHash(), + nonce: 1, + amount: amount, + token: QACC_DONATION_TOKEN_SYMBOL, + }, }, - }, - { - headers: { - Authorization: `Bearer ${accessToken}`, + { + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ); + ); - assert.isOk(donationsResponse); - const errors = donationsResponse.data.errors as GraphQLError[]; - assert.isNotEmpty(errors); - assert.equal(errors[0]!.message, errorMessages.EXCEED_QACC_CAP); + assert.isOk(result); + const donationId = result.data.data?.createDonation as number; + + const donation = await Donation.findOneBy({ id: donationId }); + + assert.isNotOk(donation?.earlyAccessRoundId); + assert.isNotOk(donation?.qfRound); + assert.isNotOk(donation?.earlyAccessRoundId); + assert.isNotOk(donation?.qfRoundId); + + // const errors = donationsResponse.data.errors as GraphQLError[]; + // assert.isNotEmpty(errors); + // assert.equal(errors[0]!.message, errorMessages.EXCEED_QACC_CAP); }); } diff --git a/src/resolvers/donationResolver.ts b/src/resolvers/donationResolver.ts index 5a6aa7544..c7259a910 100644 --- a/src/resolvers/donationResolver.ts +++ b/src/resolvers/donationResolver.ts @@ -70,6 +70,8 @@ import { findActiveEarlyAccessRound } from '../repositories/earlyAccessRoundRepo import { updateOrCreateProjectRoundRecord } from '../repositories/projectRoundRecordRepository'; import { updateOrCreateProjectUserRecord } from '../repositories/projectUserRecordRepository'; import { findActiveQfRound } from '../repositories/qfRoundRepository'; +import { EarlyAccessRound } from '../entities/earlyAccessRound'; +import { QfRound } from '../entities/qfRound'; const draftDonationEnabled = process.env.ENABLE_DRAFT_DONATION === 'true'; @ObjectType() @@ -773,7 +775,7 @@ export class DonationResolver { ); } - await qacc.validateDonation({ + const hasCap = await qacc.validateDonation({ projectId, networkId, tokenSymbol: token, @@ -825,6 +827,16 @@ export class DonationResolver { // donationPercentage = (amount / totalValue) * 100; // } // } + + let earlyAccessRound: EarlyAccessRound | null = null; + let qfRound: QfRound | null = null; + + if (hasCap) { + earlyAccessRound = await findActiveEarlyAccessRound(); + if (!earlyAccessRound) { + qfRound = await findActiveQfRound(); + } + } const donation = Donation.create({ amount: Number(amount), transactionId: transactionTx, @@ -838,7 +850,9 @@ export class DonationResolver { isTokenEligibleForGivback, isCustomToken, isProjectVerified: project.verified, - createdAt: new Date(), + createdAt: donateTime, + earlyAccessRound: earlyAccessRound ?? undefined, + qfRound: qfRound ?? undefined, segmentNotified: false, toWalletAddress: toAddress, fromWalletAddress: fromAddress, @@ -871,13 +885,6 @@ export class DonationResolver { // logger.error('get chainvine wallet address error', e); // } // } - const earlyAccessRound = await findActiveEarlyAccessRound(); - if (!earlyAccessRound) { - donation.qfRound = await findActiveQfRound(); - } else { - donation.earlyAccessRound = earlyAccessRound; - } - await donation.save(); let priceChainId; diff --git a/src/resolvers/draftDonationResolver.ts b/src/resolvers/draftDonationResolver.ts index b8883d8b6..fcc2e5978 100644 --- a/src/resolvers/draftDonationResolver.ts +++ b/src/resolvers/draftDonationResolver.ts @@ -116,7 +116,7 @@ export class DraftDonationResolver { toAddress = toAddress?.toLowerCase(); fromAddress = fromAddress?.toLowerCase(); - await qacc.validateDonation({ + const hasCap = await qacc.validateDonation({ projectId, networkId, tokenSymbol: token, @@ -125,6 +125,10 @@ export class DraftDonationResolver { donateTime: new Date(), }); + if (!hasCap) { + throw new Error(i18n.__(translationErrorMessagesKeys.EXCEED_QACC_CAP)); + } + const draftDonationId = await DraftDonation.createQueryBuilder( 'draftDonation', ) diff --git a/src/services/donationService.test.ts b/src/services/donationService.test.ts index 99145da85..e49e0c0be 100644 --- a/src/services/donationService.test.ts +++ b/src/services/donationService.test.ts @@ -52,7 +52,7 @@ describe( fillStableCoinDonationsPriceTestCases, ); -describe.only( +describe( 'syncDonationStatusWithBlockchainNetwork test cases', syncDonationStatusWithBlockchainNetworkTestCases, ); diff --git a/src/utils/qacc.ts b/src/utils/qacc.ts index d0042dccd..81522caf7 100644 --- a/src/utils/qacc.ts +++ b/src/utils/qacc.ts @@ -22,7 +22,7 @@ const validateDonation = async (params: { tokenSymbol: string; amount: number; donateTime: Date; -}): Promise => { +}): Promise => { const { projectId, userAddress, tokenSymbol, networkId, donateTime } = params; let user = await findUserByWalletAddress(userAddress)!; @@ -36,9 +36,9 @@ const validateDonation = async (params: { donateTime, }); - if (cap < params.amount) { - throw new Error(i18n.__(translationErrorMessagesKeys.EXCEED_QACC_CAP)); - } + // if (cap < params.amount) { + // throw new Error(i18n.__(translationErrorMessagesKeys.EXCEED_QACC_CAP)); + // } // token is matched if ( @@ -67,6 +67,8 @@ const validateDonation = async (params: { throw new Error(i18n.__(translationErrorMessagesKeys.NOT_NFT_HOLDER)); } } + + return cap >= params.amount; }; export default {