Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add personnel #1814

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/back/models/Personnel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Model } from 'decentraland-gatsby/dist/entities/Database/model'
import isEthereumAddress from 'validator/lib/isEthereumAddress'
import { ZodSchema, z } from 'zod'

import { TeamMember } from '../../entities/Grant/types'

Expand All @@ -11,6 +13,21 @@ export type PersonnelAttributes = TeamMember & {
created_at: Date
}

const addressCheck = (data: string) => !data || (!!data && isEthereumAddress(data))

export type PersonnelInCreation = Pick<
PersonnelAttributes,
'name' | 'address' | 'role' | 'about' | 'relevantLink' | 'project_id'
>
export const PersonnelInCreationSchema: ZodSchema<PersonnelInCreation> = z.object({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move this to entities > project > types ?

name: z.string().min(1).max(80),
address: z.string().refine(addressCheck).optional().or(z.null()),
role: z.string().min(1).max(80),
about: z.string().min(1).max(750),
relevantLink: z.string().min(0).max(200).url().optional().or(z.literal('')),
project_id: z.string().min(0),
})

export default class PersonnelModel extends Model<PersonnelAttributes> {
static tableName = 'personnel'
static withTimestamps = false
Expand Down
23 changes: 23 additions & 0 deletions src/back/routes/project.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { WithAuth, auth } from 'decentraland-gatsby/dist/entities/Auth/middleware'
import RequestError from 'decentraland-gatsby/dist/entities/Route/error'
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 { isProjectAuthorOrCoauthor } from '../../utils/projects'
import { PersonnelAttributes, PersonnelInCreationSchema } from '../models/Personnel'
import { isValidDate, validateId } from '../utils/validations'

export default routes((route) => {
const withAuth = auth()
route.get('/projects', handleJSON(getProjects))
route.post('/projects/personnel/', withAuth, handleAPI(addPersonnel))
route.get('/projects/:project', handleAPI(getProject))
route.get('/projects/pitches-total', handleJSON(getOpenPitchesTotal))
route.get('/projects/tenders-total', handleJSON(getOpenTendersTotal))
Expand Down Expand Up @@ -60,3 +65,21 @@ async function getOpenPitchesTotal() {
async function getOpenTendersTotal() {
return await ProjectService.getOpenTendersTotal()
}

async function addPersonnel(req: WithAuth): Promise<PersonnelAttributes> {
const user = req.auth!
const { personnel } = req.body
validateId(personnel.project_id)
const project = await ProjectService.getProject(personnel.project_id)
if (!project) {
throw new RequestError(`Project "${personnel.project_id}" not found`, RequestError.NotFound)
}
if (!isProjectAuthorOrCoauthor(user, project)) {
throw new RequestError("Only the project's authors and coauthors can create personnel", RequestError.Unauthorized)
}
const parsedPersonnel = PersonnelInCreationSchema.safeParse(personnel)
if (!parsedPersonnel.success) {
throw new RequestError(`Invalid personnel: ${parsedPersonnel.error.message}`, RequestError.BadRequest)
}
return await ProjectService.addPersonnel(parsedPersonnel.data, user)
}
2 changes: 1 addition & 1 deletion src/entities/Grant/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export type GrantRequestDueDiligence = {

export type TeamMember = {
name: string
address?: string
address?: string | null
role: string
about: string
relevantLink?: string
Expand Down
14 changes: 13 additions & 1 deletion src/services/ProjectService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'crypto'

import PersonnelModel, { PersonnelAttributes } from '../back/models/Personnel'
import PersonnelModel, { PersonnelAttributes, PersonnelInCreation } from '../back/models/Personnel'
import ProjectModel, { ProjectAttributes } from '../back/models/Project'
import ProjectMilestoneModel, { ProjectMilestone, ProjectMilestoneStatus } from '../back/models/ProjectMilestone'
import { TransparencyVesting } from '../clients/Transparency'
Expand Down Expand Up @@ -234,4 +234,16 @@ export class ProjectService {

return project
}

static async addPersonnel(newPersonnel: PersonnelInCreation, user?: string) {
const { address } = newPersonnel
return await PersonnelModel.create({
...newPersonnel,
address: address && address?.length > 0 ? address : null,
id: crypto.randomUUID(),
updated_by: user,
created_at: new Date(),
deleted: false,
})
}
}
6 changes: 6 additions & 0 deletions src/utils/projects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Project } from '../back/models/Project'
import { TransparencyVesting } from '../clients/Transparency'
import { ProjectStatus, TransparencyProjectStatus } from '../entities/Grant/types'
import { ProposalAttributes, ProposalProject } from '../entities/Proposal/types'
import { isSameAddress } from '../entities/Snapshot/utils'

import Time from './date/Time'

Expand Down Expand Up @@ -80,3 +82,7 @@ export function createProposalProject(proposal: ProposalAttributes, vesting?: Tr
...vestingData,
}
}

export function isProjectAuthorOrCoauthor(user: string, project: Project) {
return isSameAddress(user, project.author) || !!project.coauthors?.some((coauthor) => isSameAddress(user, coauthor))
}