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 totalReceived of project owners correctly after verifying donations #1121

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions src/services/authorizationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from 'chai';
import {
generateRandomEtheriumAddress,
generateTestAccessToken,
saveUserDirectlyToDb,
} from '../../test/testUtils';
import { User } from '../entities/user';
import Axios from 'axios';
Expand All @@ -20,31 +21,16 @@ const origin = 'https://serve.giveth.io';

function authorizationHandlerTestCases() {
it('should decode user jwt with current impact graph authorization', async () => {
const userData = {
firstName: 'firstName',
lastName: 'lastName',
email: `${new Date().getTime()}[email protected]`,
url: 'website url',
loginType: 'wallet',
walletAddress: generateRandomEtheriumAddress(),
};
const user = await User.create(userData).save();
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const jwtUser = await authorizationHandler('1', accessToken);
assert.equal(jwtUser.userId, user.id);
});
it('should decode user jwt with the auth microservice', async () => {
const privateKey = process.env.PRIVATE_ETHERS_TEST_KEY as string;
const publicKey = process.env.PUBLIC_ETHERS_TEST_KEY as string;
const userData = {
firstName: 'firstName',
lastName: 'lastName',
email: `${new Date().getTime()}[email protected]`,
url: 'website url',
loginType: 'wallet',
walletAddress: publicKey,
};
const user = await User.create(userData).save();

const user = await saveUserDirectlyToDb(publicKey);
const nonceRoute = config.get('AUTH_MICROSERVICE_NONCE_URL') as string;
const nonceResult = await Axios.get(nonceRoute);
const wallet = new ethers.Wallet(privateKey);
Expand Down Expand Up @@ -73,6 +59,7 @@ function authorizationHandlerTestCases() {
const accessToken = authenticationResult.data.jwt;
const jwtUser = await authorizationHandler('2', accessToken);
assert.equal(jwtUser.userId, user.id);
await User.delete(user.id);
});
it('should decode jwt and create user if it is nonexistent', async () => {
const privateKey = process.env.PRIVATE_ETHERS_SECONDARY_TEST_KEY as string;
Expand Down
24 changes: 19 additions & 5 deletions src/services/donationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createDonationData,
createProjectData,
DONATION_SEED_DATA,
generateRandomEtheriumAddress,
saveDonationDirectlyToDb,
saveProjectDirectlyToDb,
saveUserDirectlyToDb,
Expand All @@ -25,6 +26,7 @@ import { errorMessages } from '../utils/errorMessages';
import { findDonationById } from '../repositories/donationRepository';
import { findProjectById } from '../repositories/projectRepository';
import { CHAIN_ID } from '@giveth/monoswap/dist/src/sdk/sdkFactory';
import { findUserById } from '../repositories/userRepository';

describe('isProjectAcceptToken test cases', isProjectAcceptTokenTestCases);
describe(
Expand Down Expand Up @@ -64,7 +66,7 @@ function sendSegmentEventForDonationTestCases() {
}

function syncDonationStatusWithBlockchainNetworkTestCases() {
it('should verify a goerli donation', async () => {
it('should verify a goerli donation and update donor.totalDonated and projectOwner.totalReceived', async () => {
// https://goerli.etherscan.io/tx/0x43cb1c61a81f007abd3de766a6029ffe62d0324268d7781469a3d7879d487cb1

const transactionInfo = {
Expand All @@ -78,10 +80,16 @@ function syncDonationStatusWithBlockchainNetworkTestCases() {
timestamp: 1661114988,
};
const user = await saveUserDirectlyToDb(transactionInfo.fromAddress);
const project = await saveProjectDirectlyToDb({
...createProjectData(),
walletAddress: transactionInfo.toAddress,
});
const projectOwner = await saveUserDirectlyToDb(
generateRandomEtheriumAddress(),
);
const project = await saveProjectDirectlyToDb(
{
...createProjectData(),
walletAddress: transactionInfo.toAddress,
},
projectOwner,
);
const donation = await saveDonationDirectlyToDb(
{
amount: transactionInfo.amount,
Expand All @@ -105,6 +113,12 @@ function syncDonationStatusWithBlockchainNetworkTestCases() {
assert.equal(updateDonation.id, donation.id);
assert.isTrue(updateDonation.segmentNotified);
assert.equal(updateDonation.status, DONATION_STATUS.VERIFIED);

const donor = await findUserById(user.id);
assert.equal(donor?.totalDonated, 100);

const updatedProjectOwner = await findUserById(projectOwner.id);
assert.equal(updatedProjectOwner?.totalReceived, 100);
});

it('should verify a Polygon donation', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/services/donationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ export const syncDonationStatusWithBlockchainNetwork = async (params: {

// After updating price we update totalDonations
await updateTotalDonationsOfProject(donation.projectId);
await updateUserTotalReceived(donation.userId);
const project = await findProjectById(donation.projectId);
await updateUserTotalReceived(project!.adminUser.id);
await sendSegmentEventForDonation({
donation,
});
Expand Down
Loading