From d9b8281236b285137cd96307a7f1138411024c35 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Wed, 11 Dec 2024 19:21:30 -0500 Subject: [PATCH] fix stuff --- backend/.sqlc/queries/projects.sql | 21 ++++++++++++- backend/db/models.go | 24 ++++++++------- backend/internal/server/projects.go | 43 ++++++++++++++++++++++++++ frontend/src/services/project.ts | 48 ++++++++++++++++++----------- 4 files changed, 106 insertions(+), 30 deletions(-) diff --git a/backend/.sqlc/queries/projects.sql b/backend/.sqlc/queries/projects.sql index 4a64ab9d..2e8c92dc 100644 --- a/backend/.sqlc/queries/projects.sql +++ b/backend/.sqlc/queries/projects.sql @@ -178,4 +178,23 @@ LEFT JOIN companies c ON p.company_id = c.id LEFT JOIN project_sections ps ON ps.project_id = p.id LEFT JOIN project_files pf ON pf.project_id = p.id WHERE p.id = $1 -GROUP BY p.id, c.id; \ No newline at end of file +GROUP BY p.id, c.id; + +-- name: CreateProjectSection :one +INSERT INTO project_sections ( + project_id, + title +) VALUES ( + $1, $2 +) +RETURNING *; + +-- name: CreateProjectQuestion :one +INSERT INTO project_questions ( + section_id, + question_text, + answer_text +) VALUES ( + $1, $2, $3 +) +RETURNING *; \ No newline at end of file diff --git a/backend/db/models.go b/backend/db/models.go index d2be74c9..57b7e5cf 100644 --- a/backend/db/models.go +++ b/backend/db/models.go @@ -196,22 +196,24 @@ type ProjectLink struct { } type ProjectQuestion struct { - ID string - SectionID string - QuestionText string - AnswerText string - CreatedAt pgtype.Timestamp - UpdatedAt pgtype.Timestamp + Question string `json:"question"` + Answer string `json:"answer"` } type ProjectSection struct { - ID string - ProjectID string - Title string - CreatedAt pgtype.Timestamp - UpdatedAt pgtype.Timestamp + Title string `json:"title"` + Questions []ProjectQuestion `json:"questions"` } +type CreateProjectRequest struct { + CompanyID string `json:"company_id"` + Title string `json:"title"` + Description string `json:"description"` + Status string `json:"status"` + Files []ProjectFile `json:"files"` + Links []ProjectLink `json:"links"` + Sections []ProjectSection `json:"sections"` +} type ProjectTag struct { ID string ProjectID string diff --git a/backend/internal/server/projects.go b/backend/internal/server/projects.go index a428418f..f2ef67e3 100644 --- a/backend/internal/server/projects.go +++ b/backend/internal/server/projects.go @@ -15,6 +15,22 @@ import ( "github.com/labstack/echo/v4" ) +type CreateProjectRequest struct { + CompanyID string `json:"company_id"` + Title string `json:"title"` + Description *string `json:"description"` + Status string `json:"status"` + Files []ProjectFile `json:"files"` + Links []ProjectLink `json:"links"` + Sections []struct { + Title string `json:"title"` + Questions []struct { + Question string `json:"question"` + Answer string `json:"answer"` + } `json:"questions"` + } `json:"sections"` +} + func (s *Server) handleCreateProject(c echo.Context) error { var req *CreateProjectRequest req, ok := c.Get(mw.REQUEST_BODY_KEY).(*CreateProjectRequest) @@ -76,6 +92,33 @@ func (s *Server) handleCreateProject(c echo.Context) error { } } + // Create sections and questions + for _, section := range req.Sections { + sectionParams := db.CreateProjectSectionParams{ + ProjectID: project.ID, + Title: section.Title, + } + + projectSection, err := qtx.CreateProjectSection(context.Background(), sectionParams) + if err != nil { + return handleDBError(err, "create", "project section") + } + + // Create questions for this section + for _, q := range section.Questions { + questionParams := db.CreateProjectQuestionParams{ + SectionID: projectSection.ID, + QuestionText: q.Question, + AnswerText: q.Answer, + } + + _, err := qtx.CreateProjectQuestion(context.Background(), questionParams) + if err != nil { + return handleDBError(err, "create", "project question") + } + } + } + // Commit transaction if err := tx.Commit(context.Background()); err != nil { return handleDBError(err, "commit", "transaction") diff --git a/frontend/src/services/project.ts b/frontend/src/services/project.ts index e5f51829..08f63112 100644 --- a/frontend/src/services/project.ts +++ b/frontend/src/services/project.ts @@ -3,6 +3,7 @@ import { ApiError } from './errors'; import type { FormData } from '@/types'; import { uploadFile } from './storage'; import { fetchWithAuth } from './auth'; +import { projectFormSchema } from '@/config/forms/project'; interface CompanyResponse { ID: string; @@ -22,7 +23,7 @@ interface ProjectResponse { CreatedAt: string; UpdatedAt: string; Company?: CompanyResponse; - Sections?: ProjectSection[]; + Sections?: string; // Base64 encoded JSON string } interface ProjectFile { @@ -66,15 +67,22 @@ export interface Project { const transformProject = (project: ProjectResponse): Project => { let sections: ProjectSection[] = []; - if (project.Sections && Array.isArray(project.Sections)) { - sections = project.Sections.map(s => ({ - id: s.id, - title: s.title || '', - questions: s.questions?.map((q: any) => ({ - question: q.question || '', - answer: q.answer || '' - })) || [] - })); + if (project.Sections) { + try { + const decodedSections = JSON.parse(atob(project.Sections)); + if (Array.isArray(decodedSections)) { + sections = decodedSections.map(s => ({ + id: s.id || '', + title: s.title || '', + questions: s.questions?.map((q: any) => ({ + question: q.question || '', + answer: q.answer || '' + })) || [] + })); + } + } catch (error) { + console.error('Error decoding sections:', error); + } } return { @@ -104,7 +112,6 @@ export async function createProject( files: File[] = [], links: ProjectLink[] = [] ): Promise { - // First upload all files const uploadedFiles: ProjectFile[] = await Promise.all( files.map(async (file) => { const fileUrl = await uploadFile(file); @@ -115,23 +122,28 @@ export async function createProject( }) ); - const url = getApiUrl('/projects'); - - const sanitizedLinks = links.map(link => ({ - LinkType: link.LinkType, - URL: link.URL + // Get all sections from the schema (excluding document upload section) + const sections = projectFormSchema[0].sections.map(section => ({ + title: section.title, + questions: section.fields.map(field => ({ + question: field.label, + answer: formData[field.id] || '' // Use the field ID to get the answer from formData + })) })); + const url = getApiUrl('/projects'); + console.log('Sections:', sections); const body = { company_id: companyId, title: formData.companyName, description: formData.description, status: 'in_review', files: uploadedFiles, - links: sanitizedLinks.map(link => ({ + links: links.map(link => ({ link_type: link.LinkType.toLowerCase(), url: link.URL - })) + })), + sections: btoa(JSON.stringify(sections)) // Base64 encode sections }; console.log('Request body:', body);