Skip to content

Commit

Permalink
Merge pull request #124 from metagov/feat/snapshot
Browse files Browse the repository at this point in the history
feat: support pagination to get proposal from snapshot.
  • Loading branch information
crazyyuan authored Aug 23, 2023
2 parents 191647e + 946164f commit 0c0dbc1
Showing 1 changed file with 83 additions and 77 deletions.
160 changes: 83 additions & 77 deletions Implementations/API/backend/functions/snapshot/getProposals.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
import { APIGatewayProxyHandlerV2 } from 'aws-lambda'
import { snapshotApiConfig } from 'functions/config'
import fetch from 'node-fetch'
import { APIGatewayProxyHandlerV2 } from "aws-lambda";
import { snapshotApiConfig } from "functions/config";
import fetch from "node-fetch";

function apiRequest(path: string, method: string, data: any) {
return fetch(path, {
headers: {
'Content-Type': 'application/json',
},
method,
redirect: 'follow',
body: JSON.stringify(data),
}).then((res) => res.json())
return fetch(path, {
headers: {
"Content-Type": "application/json",
},
method,
redirect: "follow",
body: JSON.stringify(data),
}).then((res) => res.json());
}

const calculateSatatus = (
processed: boolean,
cancelled: boolean,
passed: boolean,
votingStarts: number,
votingEnds: number,
graceEnds: number,
sponsored: boolean
processed: boolean,
cancelled: boolean,
passed: boolean,
votingStarts: number,
votingEnds: number,
graceEnds: number,
sponsored: boolean
) => {
const now = Date.now()
if (processed) {
return 'processed'
} else if (passed) {
return 'passed'
} else if (cancelled) {
return 'cancelled'
} else if (now >= votingEnds && graceEnds > now) {
return 'grace'
} else if (now >= votingStarts && votingEnds > now) {
return 'voting'
} else if (sponsored) {
return 'sponsored'
} else {
return 'submitted'
}
}
const now = Date.now();
if (processed) {
return "processed";
} else if (passed) {
return "passed";
} else if (cancelled) {
return "cancelled";
} else if (now >= votingEnds && graceEnds > now) {
return "grace";
} else if (now >= votingStarts && votingEnds > now) {
return "voting";
} else if (sponsored) {
return "sponsored";
} else {
return "submitted";
}
};

export const handler: APIGatewayProxyHandlerV2 = async (event) => {
console.log({graphConfig: snapshotApiConfig})
const path = snapshotApiConfig['1']
if (!path) return { statusCode: 400, message: 'Missing config for network' }
console.log({ graphConfig: snapshotApiConfig });
const path = snapshotApiConfig["1"];
if (!path) return { statusCode: 400, message: "Missing config for network" };

const eventId = event?.pathParameters?.id;
if (!eventId) return { statusCode: 400, message: "Missing id" };

const eventId = event?.pathParameters?.id
if (!eventId) return { statusCode: 400, message: 'Missing id' }
const take = parseInt(<string>event?.queryStringParameters?.take) || 20; // Get the requested page number
const skip = parseInt(<string>event?.queryStringParameters?.skip) || 0; // Number of items per page

const template = {
'@context': {
'@vocab': 'http://daostar.org/',
},
type: 'DAO',
name: eventId,
}
const template = {
"@context": {
"@vocab": "http://daostar.org/",
},
type: "DAO",
name: eventId,
};

const query = `query GetProposals($dao: String!) {
const query = `query GetProposals($dao: String!, $first: Int!, $skip: Int!) {
proposals (
where: {
space_in: [$dao],
},
orderBy: "created",
orderDirection: desc
orderDirection: desc,
first: $first,
skip: $skip
) {
id
title
Expand All @@ -78,37 +83,38 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => {
name
}
}
}`
}`;

const data = {
query,
variables: { dao: eventId },
}
const data = {
query,
variables: { dao: eventId, first: take, skip },
};

const res = (await apiRequest(path, 'POST', data)) as any
const res = (await apiRequest(path, "POST", data)) as any;

if (!(res.data.proposals && res.data.proposals.length > 0)) return { statusCode: 404, message: 'DAO not found' }
console.log({ data: res.data.proposals })
const proposals = res.data.proposals
if (!(res.data.proposals && res.data.proposals.length > 0))
return { statusCode: 404, message: "DAO not found" };
console.log({ data: res.data.proposals });
const proposals = res.data.proposals;

const proposalsFormatted = proposals.map((p: any) => {
const status = p.state
return {
id: p.id,
type: 'proposal',
status: status,
}
})
const proposalsFormatted = proposals.map((p: any) => {
const status = p.state;
return {
id: p.id,
type: "proposal",
status: status,
};
});

const transformed = { proposals: proposalsFormatted, ...template }
const transformed = { proposals: proposalsFormatted, ...template };

return transformed
? {
statusCode: 200,
body: JSON.stringify(transformed),
}
: {
statusCode: 404,
body: JSON.stringify({ error: true }),
}
}
return transformed
? {
statusCode: 200,
body: JSON.stringify(transformed),
}
: {
statusCode: 404,
body: JSON.stringify({ error: true }),
};
};

0 comments on commit 0c0dbc1

Please sign in to comment.