Skip to content

Commit

Permalink
fix stuff (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirAgassi authored Dec 12, 2024
2 parents aa6d3fb + d9b8281 commit 037d976
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 30 deletions.
21 changes: 20 additions & 1 deletion backend/.sqlc/queries/projects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 *;
24 changes: 13 additions & 11 deletions backend/db/models.go

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

43 changes: 43 additions & 0 deletions backend/internal/server/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
48 changes: 30 additions & 18 deletions frontend/src/services/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,7 @@ interface ProjectResponse {
CreatedAt: string;
UpdatedAt: string;
Company?: CompanyResponse;
Sections?: ProjectSection[];
Sections?: string; // Base64 encoded JSON string
}

interface ProjectFile {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -104,7 +112,6 @@ export async function createProject(
files: File[] = [],
links: ProjectLink[] = []
): Promise<ProjectResponse> {
// First upload all files
const uploadedFiles: ProjectFile[] = await Promise.all(
files.map(async (file) => {
const fileUrl = await uploadFile(file);
Expand All @@ -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);
Expand Down

0 comments on commit 037d976

Please sign in to comment.