Skip to content

Commit

Permalink
chore: all events route (#1607)
Browse files Browse the repository at this point in the history
* chore: add a route for debug addresses to get all events

* chore: add debug section to get all events
  • Loading branch information
1emu authored Jan 31, 2024
1 parent fc22123 commit e0feb30
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/back/models/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export default class EventModel extends Model<Event> {
static withTimestamps = false
static primaryKey = 'id'

static async getAll(): Promise<Event[]> {
const query = SQL`
SELECT *
FROM ${table(EventModel)}
ORDER BY created_at DESC
`
const result = await this.namedQuery<Event>('get_all_events', query)
return result
}

static async getLatest(): Promise<Event[]> {
const query = SQL`
SELECT *
Expand Down
9 changes: 8 additions & 1 deletion src/back/routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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()
}
4 changes: 4 additions & 0 deletions src/back/services/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/clients/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,12 @@ export class Governance extends API {
)
return response.data
}

async getAllEvents() {
const response = await this.fetch<ApiResponse<ActivityTickerEvent[]>>(
`/events/all`,
this.options().method('GET').authorization({ sign: true })
)
return response.data
}
}
41 changes: 41 additions & 0 deletions src/components/Debug/QueryData.tsx
Original file line number Diff line number Diff line change
@@ -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<string | undefined>()

const handleGetAllEvents = async () => {
setErrorMessage('')
try {
console.log(await Governance.get().getAllEvents())
} catch (e: unknown) {
setErrorMessage(`${e}`)
}
}

return (
<div className={className}>
<ContentSection>
<Heading size="sm">{'Query Data'}</Heading>
<div>
<Button primary onClick={handleGetAllEvents}>
{'Get All Events'}
</Button>
</div>
{errorMessage && <ErrorMessage label={'Latest error'} errorMessage={errorMessage} />}
</ContentSection>
</div>
)
}

export default QueryData
2 changes: 2 additions & 0 deletions src/pages/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -70,6 +71,7 @@ export default function DebugPage() {
<HttpStatus className="DebugPage__Section" />
<EnvStatus className="DebugPage__Section" />
<Snapshot className="DebugPage__Section" />
<QueryData className="DebugPage__Section" />
</>
)}
</WiderContainer>
Expand Down

0 comments on commit e0feb30

Please sign in to comment.