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

fetch project company (admin only) #440

Merged
merged 1 commit into from
Feb 6, 2025
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
7 changes: 6 additions & 1 deletion backend/.sqlc/queries/companies.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ ORDER BY created_at DESC;

-- name: GetCompanyWithAuth :one
SELECT * FROM companies
WHERE (owner_id = $1 OR $2 = 'admin') AND id = $3;
WHERE (owner_id = $1 OR $2 = 'admin') AND id = $3;

-- name: GetCompanyByProjectID :one
SELECT c.* FROM projects p
JOIN companies c ON c.id = p.company_id
WHERE p.id = $1;
25 changes: 25 additions & 0 deletions backend/db/companies.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/internal/v1/v1_companies/companies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"net/http"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -237,3 +238,30 @@ func (h *Handler) handleGetCompany(c echo.Context) error {
UpdatedAt: company.UpdatedAt,
})
}

func (h *Handler) handleGetCompanyByProject(c echo.Context) error {
projectID := c.Param("id")
if _, err := uuid.Parse(projectID); err != nil {
return v1_common.Fail(c, http.StatusBadRequest, "Invalid project id", err)
}

company, err := h.server.GetQueries().GetCompanyByProjectID(c.Request().Context(), projectID)
if err != nil {
log.Error().Err(err).Msg("failed to get company")
return v1_common.NewNotFoundError("company")
}

return c.JSON(http.StatusOK, CompanyResponse{
ID: company.ID,
OwnerID: company.OwnerID,
Name: company.Name,
Description: company.Description,
DateFounded: company.DateFounded,
Stages: company.Stages,
Website: company.Website,
WalletAddress: company.WalletAddress,
LinkedinURL: company.LinkedinUrl,
CreatedAt: company.CreatedAt,
UpdatedAt: company.UpdatedAt,
})
}
2 changes: 1 addition & 1 deletion backend/internal/v1/v1_companies/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func SetupCompanyRoutes(e *echo.Group, s interfaces.CoreServer) {

// Setup all the routes for getting a single company
// Auth: Admins only
companies.GET("/company/:id", h.handleGetCompany,
companies.GET("/project/:id/company", h.handleGetCompanyByProject,
middleware.Auth(s.GetDB(), permissions.PermViewAllProjects),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createFileRoute, Link } from '@tanstack/react-router'
import { Stack } from '@layouts'
import { FiFileText, FiMessageSquare, FiDollarSign, FiTrendingUp, FiGlobe, FiLinkedin } from 'react-icons/fi'
import { useEffect, useState } from 'react'
import { CompanyResponse, getCompany } from '@/services/company'
import { CompanyResponse, getCompanyByProjectId } from '@/services/company'
import { TeamMember } from '@/types'
import { getTeamMembers } from '@/services/teams'
import { useAuth } from '@/contexts/AuthContext'
Expand Down Expand Up @@ -199,7 +199,7 @@ function RouteComponent() {
// First fetch project and company data since we need the company ID
const [projectData, companyData] = await Promise.all([
getProject(accessToken, projectId),
getCompany(accessToken)
getCompanyByProjectId(accessToken, projectId)
])

if (!companyData) {
Expand Down
29 changes: 28 additions & 1 deletion frontend/src/services/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ export async function getCompany(
return json as CompanyResponse;
}

export async function getCompanyByProjectId(
token: string,
projectId: string
): Promise<CompanyResponse | null> {
const url = getApiUrl(`/project/${projectId}/company`);
const res = await fetch(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
credentials: 'include',
});

if (res.status === HttpStatusCode.NOT_FOUND) {
return null;
}

const json = await res.json();
if (res.status !== HttpStatusCode.OK) {
throw new ApiError('Failed to get company', res.status, json);
}
return json as CompanyResponse;
}

export async function updateCompany(
token: string,
data: Partial<CompanyInformation>
Expand All @@ -85,7 +110,9 @@ export async function updateCompany(
...(data.stage && { stages: data.stage.map((s) => s.value) }),
...(data.website && { website: data.website }),
...(data.linkedin && { linkedin_url: data.linkedin }),
...(data.wallet_address !== undefined && { wallet_address: data.wallet_address }),
...(data.wallet_address !== undefined && {
wallet_address: data.wallet_address,
}),
};

const res = await fetch(url, {
Expand Down
Loading