Skip to content

Commit

Permalink
Merge pull request #1121 from Giveth/f_3021_update_totalReceived_of_p…
Browse files Browse the repository at this point in the history
…roject_owners_correctly

Update totalReceived of project owners correctly after verifying donations
  • Loading branch information
CarlosQ96 authored Sep 6, 2023
2 parents ea91acc + 0092f5a commit bc25160
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
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

0 comments on commit bc25160

Please sign in to comment.