Skip to content

Commit

Permalink
chore: add project endpoint (#1789)
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed May 10, 2024
1 parent 2a05f1c commit bac0ebc
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/back/routes/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Budget, BudgetWithContestants, CategoryBudget } from '../../entities/Bu
import { QuarterBudgetAttributes } from '../../entities/QuarterBudget/types'
import { toNewGrantCategory } from '../../entities/QuarterCategoryBudget/utils'
import { BudgetService } from '../../services/BudgetService'
import { validateProposalId } from '../utils/validations'
import { validateId } from '../utils/validations'

export default routes((route) => {
const withAuth = auth()
Expand Down Expand Up @@ -48,6 +48,6 @@ async function getCurrentContestedBudget(): Promise<BudgetWithContestants> {
}

async function getBudgetWithContestants(req: Request<{ proposal: string }>): Promise<BudgetWithContestants> {
const id = validateProposalId(req.params.proposal)
const id = validateId(req.params.proposal)
return await BudgetService.getBudgetWithContestants(id)
}
4 changes: 2 additions & 2 deletions src/back/routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EventsService } from '../services/events'
import {
validateDebugAddress,
validateEventTypesFilters,
validateProposalId,
validateId,
validateRequiredString,
} from '../utils/validations'

Expand All @@ -26,7 +26,7 @@ async function getLatestEvents(req: Request) {
async function voted(req: WithAuth) {
const user = req.auth!

validateProposalId(req.body.proposalId)
validateId(req.body.proposalId)
validateRequiredString('proposalTitle', req.body.proposalTitle)
validateRequiredString('choice', req.body.choice)
return await EventsService.voted(req.body.proposalId, req.body.proposalTitle, req.body.choice, user)
Expand Down
14 changes: 12 additions & 2 deletions src/back/routes/project.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import RequestError from 'decentraland-gatsby/dist/entities/Route/error'
import { handleJSON } from 'decentraland-gatsby/dist/entities/Route/handle'
import handleAPI, { handleJSON } from 'decentraland-gatsby/dist/entities/Route/handle'
import routes from 'decentraland-gatsby/dist/entities/Route/routes'
import { Request } from 'express'

import CacheService, { TTL_1_HS } from '../../services/CacheService'
import { ProjectService } from '../../services/ProjectService'
import { isValidDate } from '../utils/validations'
import { isValidDate, validateId } from '../utils/validations'

export default routes((route) => {
route.get('/projects', handleJSON(getProjects))
route.get('/projects/:project', handleAPI(getProject))
route.get('/projects/pitches-total', handleJSON(getOpenPitchesTotal))
route.get('/projects/tenders-total', handleJSON(getOpenTendersTotal))
})
Expand Down Expand Up @@ -42,6 +43,15 @@ async function getProjects(req: Request) {
return filterProjectsByDate(projects, from, to)
}

async function getProject(req: Request<{ project: string }>) {
const id = validateId(req.params.project)
try {
return await ProjectService.getProject(id)
} catch (e) {
throw new RequestError(`Project "${id}" not found`, RequestError.NotFound)
}
}

async function getOpenPitchesTotal() {
return await ProjectService.getOpenPitchesTotal()
}
Expand Down
6 changes: 3 additions & 3 deletions src/back/routes/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import { isProdEnv } from '../../utils/governanceEnvs'
import logger from '../../utils/logger'
import { NotificationService } from '../services/notification'
import { UpdateService } from '../services/update'
import { validateAddress, validateProposalId } from '../utils/validations'
import { validateAddress, validateId } from '../utils/validations'

export default routes((route) => {
const withAuth = auth()
Expand Down Expand Up @@ -525,7 +525,7 @@ async function getPriorityProposals(req: Request) {
}

export async function getProposal(req: Request<{ proposal: string }>) {
const id = validateProposalId(req.params.proposal)
const id = validateId(req.params.proposal)
try {
return await ProposalService.getProposal(id)
} catch (e) {
Expand All @@ -534,7 +534,7 @@ export async function getProposal(req: Request<{ proposal: string }>) {
}

export async function getProposalWithProject(req: Request<{ proposal: string }>) {
const id = validateProposalId(req.params.proposal)
const id = validateId(req.params.proposal)
try {
return await ProposalService.getProposalWithProject(id)
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/back/routes/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ProposalService } from '../../services/ProposalService'
import { SnapshotService } from '../../services/SnapshotService'
import Time from '../../utils/date/Time'
import { VoteService } from '../services/vote'
import { validateAddress, validateProposalId } from '../utils/validations'
import { validateAddress, validateId } from '../utils/validations'

export default routes((route) => {
route.get('/proposals/:proposal/votes', handleAPI(getVotesByProposal))
Expand All @@ -27,7 +27,7 @@ export default routes((route) => {

export async function getVotesByProposal(req: Request<{ proposal: string }>) {
const refresh = req.query.refresh === 'true'
const id = validateProposalId(req.params.proposal)
const id = validateId(req.params.proposal)

const proposal = await ProposalService.getProposal(id)
const latestVotes = await VoteService.getVotes(proposal.id)
Expand Down
10 changes: 5 additions & 5 deletions src/back/utils/validations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import RequestError from 'decentraland-gatsby/dist/entities/Route/error'

import { EventType } from '../../shared/types/events'

import { validateEventTypesFilters, validateProposalId } from './validations'
import { validateEventTypesFilters, validateId } from './validations'

describe('validateProposalId', () => {
const UUID = '00000000-0000-0000-0000-000000000000'

it('should not throw an error for a valid proposal id', () => {
expect(() => validateProposalId(UUID)).not.toThrow()
expect(() => validateId(UUID)).not.toThrow()
})

it('should throw an error for a missing required proposal id', () => {
expect(() => validateProposalId(undefined)).toThrow(RequestError)
expect(() => validateId(undefined)).toThrow(RequestError)
})

it('should throw an error for an empty required proposal id', () => {
expect(() => validateProposalId('')).toThrow(RequestError)
expect(() => validateId('')).toThrow(RequestError)
})

it('should throw an error for proposal id with spaces', () => {
expect(() => validateProposalId(' ')).toThrow(RequestError)
expect(() => validateId(' ')).toThrow(RequestError)
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/back/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export function validateProposalFields(fields: unknown) {
}
}

export function validateProposalId(id?: string) {
export function validateId(id?: string) {
if (!(id && isUUID(id))) {
throw new RequestError('Invalid proposal id', RequestError.BadRequest)
throw new RequestError('Invalid id', RequestError.BadRequest)
}
return id
}
Expand Down
10 changes: 10 additions & 0 deletions src/services/ProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,14 @@ export class ProjectService {

return await ProjectModel.create(newProject)
}

static async getProject(id: string) {
//TODO: add all data to project from other tables (updates, personnel, milestones, etc) & return Project type, instead of ProjectAttributes
const project = ProjectModel.findOne<ProjectAttributes>({ id })
if (!project) {
throw new Error(`Project not found: "${id}"`)
}

return project
}
}

0 comments on commit bac0ebc

Please sign in to comment.