Skip to content

Commit

Permalink
finish project endpoint for superfluid to read
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Dec 16, 2024
1 parent d12453d commit 23e1f84
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/resolvers/recurringDonationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import { QfRound } from '../entities/qfRound';
import { generateRandomString } from '../utils/utils';
import { ORGANIZATION_LABELS } from '../entities/organization';

describe(
'recurringDonationEligibleProjects test cases',
recurringDonationEligibleProjectsTestCases,
);

describe(
'createRecurringDonation test cases',
createRecurringDonationTestCases,
Expand Down Expand Up @@ -63,6 +68,85 @@ describe(
recurringDonationsByProjectDateTestCases,
);

function recurringDonationEligibleProjectsTestCases() {
it('should return eligible projects with their anchor contracts', async () => {
const projectOwner = await saveUserDirectlyToDb(
generateRandomEtheriumAddress(),
);

const project = await saveProjectDirectlyToDb(
{
...createProjectData(),
isGivbackEligible: true,
},
projectOwner,
);

const anchorContractAddress = await addNewAnchorAddress({
project,
owner: projectOwner,
creator: projectOwner,
address: generateRandomEtheriumAddress(),
networkId: NETWORK_IDS.OPTIMISTIC,
txHash: generateRandomEvmTxHash(),
});

const anchorContractAddressBASE = await addNewAnchorAddress({
project,
owner: projectOwner,
creator: projectOwner,
address: generateRandomEtheriumAddress(),
networkId: NETWORK_IDS.BASE_MAINNET,
txHash: generateRandomEvmTxHash(),
});

const result = await axios.post(graphqlUrl, {
query: `
query {
recurringDonationEligibleProjects {
id
slug
title
anchorContracts {
address
networkId
isActive
}
}
}
`,
});

assert.isNotNull(result.data.data.recurringDonationEligibleProjects);
const foundProject =
result.data.data.recurringDonationEligibleProjects.find(
p => p.id === project.id,
);

assert.isNotNull(
foundProject,
'Project should be found in eligible projects',
);
assert.equal(foundProject.slug, project.slug);
assert.equal(foundProject.title, project.title);
assert.equal(foundProject.anchorContracts.length, 2);

// Assert Optimistic anchor contract
const optimisticContract = foundProject.anchorContracts.find(
contract => contract.networkId === NETWORK_IDS.OPTIMISTIC,
);
assert.isNotNull(optimisticContract);
assert.equal(optimisticContract.address, anchorContractAddress.address);

// Assert BASE anchor contract
const baseContract = foundProject.anchorContracts.find(
contract => contract.networkId === NETWORK_IDS.BASE_MAINNET,
);
assert.isNotNull(baseContract);
assert.equal(baseContract.address, anchorContractAddressBASE.address);
});
}

function createRecurringDonationTestCases() {
it('should create recurringDonation successfully', async () => {
const projectOwner = await saveUserDirectlyToDb(
Expand Down
59 changes: 59 additions & 0 deletions src/resolvers/recurringDonationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ import {
} from '../services/recurringDonationService';
import { markDraftRecurringDonationStatusMatched } from '../repositories/draftRecurringDonationRepository';
import { ResourcePerDateRange } from './donationResolver';
import { Project } from '../entities/project';

@ObjectType()
class RecurringDonationEligibleProject {
@Field()
id: number;

@Field()
slug: string;

@Field()
title: string;

@Field(_type => [AnchorContractAddress])
anchorContracts: AnchorContractAddress[];
}

@InputType()
class RecurringDonationSortBy {
@Field(_type => RecurringDonationSortField)
Expand Down Expand Up @@ -708,6 +725,48 @@ export class RecurringDonationResolver {
}
}

@Query(_return => [RecurringDonationEligibleProject], { nullable: true })
async recurringDonationEligibleProjects(
@Arg('networkId', { nullable: true }) networkId?: string,
@Arg('page', _type => Int, { nullable: true, defaultValue: 1 })
page: number = 1,
@Arg('limit', _type => Int, { nullable: true, defaultValue: 50 })
limit: number = 50,
): Promise<RecurringDonationEligibleProject[]> {
try {
const offset = (page - 1) * limit;

let networkFilter = '';
if (networkId) {
networkFilter = `AND anchor_contract_address."networkId" = ${networkId}`;
}

return await Project.getRepository().query(`
SELECT
project.id,
project.slug,
project.title,
array_agg(json_build_object(
'address', anchor_contract_address.address,
'networkId', anchor_contract_address."networkId",
'isActive', anchor_contract_address."isActive"
)) as "anchorContracts"
FROM project
INNER JOIN anchor_contract_address ON project.id = anchor_contract_address."projectId"
WHERE project."isGivbackEligible" = true
AND anchor_contract_address."isActive" = true
${networkFilter}
GROUP BY project.id, project.slug, project.title
OFFSET ${offset}
LIMIT ${limit}
`);
} catch (error) {
throw new Error(
`Error fetching eligible projects for donation: ${error}`,
);
}
}

@Query(_returns => RDRessourcePerDateRange, { nullable: true })
async recurringDonationsCountPerDate(
// fromDate and toDate should be in this format YYYYMMDD HH:mm:ss
Expand Down

0 comments on commit 23e1f84

Please sign in to comment.