From e0feb3087819fdb8cc6175cdb92862d993edd935 Mon Sep 17 00:00:00 2001 From: lemu Date: Wed, 31 Jan 2024 13:02:06 -0300 Subject: [PATCH] chore: all events route (#1607) * chore: add a route for debug addresses to get all events * chore: add debug section to get all events --- src/back/models/Event.ts | 10 ++++++++ src/back/routes/events.ts | 9 ++++++- src/back/services/events.ts | 4 +++ src/clients/Governance.ts | 8 ++++++ src/components/Debug/QueryData.tsx | 41 ++++++++++++++++++++++++++++++ src/pages/debug.tsx | 2 ++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/components/Debug/QueryData.tsx diff --git a/src/back/models/Event.ts b/src/back/models/Event.ts index 91724270f..8db5d0804 100644 --- a/src/back/models/Event.ts +++ b/src/back/models/Event.ts @@ -10,6 +10,16 @@ export default class EventModel extends Model { static withTimestamps = false static primaryKey = 'id' + static async getAll(): Promise { + const query = SQL` + SELECT * + FROM ${table(EventModel)} + ORDER BY created_at DESC + ` + const result = await this.namedQuery('get_all_events', query) + return result + } + static async getLatest(): Promise { const query = SQL` SELECT * diff --git a/src/back/routes/events.ts b/src/back/routes/events.ts index 844acd9ec..f60052069 100644 --- a/src/back/routes/events.ts +++ b/src/back/routes/events.ts @@ -3,13 +3,14 @@ import handleAPI from 'decentraland-gatsby/dist/entities/Route/handle' import routes from 'decentraland-gatsby/dist/entities/Route/routes' import { EventsService } from '../services/events' -import { validateProposalId, validateRequiredString } from '../utils/validations' +import { validateDebugAddress, validateProposalId, validateRequiredString } from '../utils/validations' import { discourseComment } from './webhooks' export default routes((route) => { const withAuth = auth() route.get('/events', handleAPI(getLatestEvents)) + route.get('/events/all', withAuth, handleAPI(getAllEvents)) route.post('/events/voted', withAuth, handleAPI(voted)) route.post('/events/discourse/new', handleAPI(discourseComment)) //TODO: deprecate }) @@ -26,3 +27,9 @@ async function voted(req: WithAuth) { validateRequiredString('choice', req.body.choice) return await EventsService.voted(req.body.proposalId, req.body.proposalTitle, req.body.choice, user) } + +async function getAllEvents(req: WithAuth) { + const user = req.auth! + validateDebugAddress(user) + return await EventsService.getAll() +} diff --git a/src/back/services/events.ts b/src/back/services/events.ts index 9a52a091d..88b1fe1fb 100644 --- a/src/back/services/events.ts +++ b/src/back/services/events.ts @@ -65,6 +65,10 @@ export class EventsService { } } + public static async getAll() { + return await EventModel.getAll() + } + private static async getAddressesToProfiles(addresses: string[]) { try { const profiles = await this.getProfilesWithCache(addresses) diff --git a/src/clients/Governance.ts b/src/clients/Governance.ts index d25555171..d2a3906a9 100644 --- a/src/clients/Governance.ts +++ b/src/clients/Governance.ts @@ -777,4 +777,12 @@ export class Governance extends API { ) return response.data } + + async getAllEvents() { + const response = await this.fetch>( + `/events/all`, + this.options().method('GET').authorization({ sign: true }) + ) + return response.data + } } diff --git a/src/components/Debug/QueryData.tsx b/src/components/Debug/QueryData.tsx new file mode 100644 index 000000000..4aaadd275 --- /dev/null +++ b/src/components/Debug/QueryData.tsx @@ -0,0 +1,41 @@ +import { useState } from 'react' + +import { Button } from 'decentraland-ui/dist/components/Button/Button' + +import { Governance } from '../../clients/Governance' +import Heading from '../Common/Typography/Heading' +import ErrorMessage from '../Error/ErrorMessage' +import { ContentSection } from '../Layout/ContentLayout' + +interface Props { + className?: string +} + +function QueryData({ className }: Props) { + const [errorMessage, setErrorMessage] = useState() + + const handleGetAllEvents = async () => { + setErrorMessage('') + try { + console.log(await Governance.get().getAllEvents()) + } catch (e: unknown) { + setErrorMessage(`${e}`) + } + } + + return ( +
+ + {'Query Data'} +
+ +
+ {errorMessage && } +
+
+ ) +} + +export default QueryData diff --git a/src/pages/debug.tsx b/src/pages/debug.tsx index 2ab5636a4..9ad6a93f3 100644 --- a/src/pages/debug.tsx +++ b/src/pages/debug.tsx @@ -13,6 +13,7 @@ import ErrorReporting from '../components/Debug/ErrorReporting' import HttpStatus from '../components/Debug/HttpStatus' import InvalidateCache from '../components/Debug/InvalidateCache' import Notifications from '../components/Debug/Notifications' +import QueryData from '../components/Debug/QueryData' import Snapshot from '../components/Debug/Snapshot' import TriggerFunction from '../components/Debug/TriggerFunction' import LogIn from '../components/Layout/LogIn' @@ -70,6 +71,7 @@ export default function DebugPage() { + )}