From 04dfbd75528cc1d241ed5fab3087aeea2eb01e53 Mon Sep 17 00:00:00 2001 From: AlexMendozaPrado Date: Mon, 3 Jun 2024 03:08:11 -0600 Subject: [PATCH] Growth support y Growth opportunities graph --- app/(base)/projects/[projectId]/page.tsx | 45 ++++++---------- db/schema.ts | 3 ++ services/sprintSurvey.ts | 68 +++++++++++++++++++++++- 3 files changed, 87 insertions(+), 29 deletions(-) diff --git a/app/(base)/projects/[projectId]/page.tsx b/app/(base)/projects/[projectId]/page.tsx index 49dcf9c..a1dd6d1 100644 --- a/app/(base)/projects/[projectId]/page.tsx +++ b/app/(base)/projects/[projectId]/page.tsx @@ -15,6 +15,7 @@ import { getUserRole } from "@/services/user"; import { getOverallStatistics, getDetailedProjectStatistics, + getGrowthData, // Importar el nuevo servicio } from "@/services/sprintSurvey"; import ChevronDownIcon from "@/components/icons/ChevronDownIcon"; @@ -56,6 +57,11 @@ const Project = ({ params }: { params: { projectId: string } }) => { queryKey: ["project-detailed-statistics", projectId], queryFn: () => getDetailedProjectStatistics(projectId), }); + + const { data: growthData, isLoading: isLoadingGrowthData } = useQuery({ + queryKey: ["project-growth-data", projectId], + queryFn: () => getGrowthData(projectId), // Consumir el nuevo servicio + }); const radarData = statistics ? [ { @@ -80,33 +86,15 @@ const Project = ({ params }: { params: { projectId: string } }) => { }, ] : []; - const areaData = [ - { - month: "Jan", - growthSupport: 89, - growthOportunities: 70, - }, - { - month: "Feb", - growthSupport: 76, - growthOportunities: 82, - }, - { - month: "Mar", - growthSupport: 95, - growthOportunities: 89, - }, - { - month: "Apr", - growthSupport: 72, - growthOportunities: 85, - }, - { - month: "May", - growthSupport: 83, - growthOportunities: 78, - }, - ]; + const areaData = growthData + ? growthData.growthSupportData.map((item, index) => ({ + month: item.month, + growthSupport: item.averageAnswer, + growthOportunities: + growthData.growthOpportunitiesData[index]?.averageAnswer || 0, + })) + : []; + const gaugeData = detailedStatistics ? [ { @@ -140,7 +128,8 @@ const Project = ({ params }: { params: { projectId: string } }) => { if ( isLoadingStatistics || isLoadingDetailedStatistics || - isLoadingProjectData + isLoadingProjectData || + isLoadingGrowthData // Agregar el estado de carga de los datos de crecimiento ) { return (
diff --git a/db/schema.ts b/db/schema.ts index aafc1ae..3148749 100644 --- a/db/schema.ts +++ b/db/schema.ts @@ -251,6 +251,9 @@ export const finalSurveyAnswer = pgTable( }), questionId: integer("question_id").references(() => question.id), answer: integer("answer"), + answeredAt: date("answered_at", { mode: "date" }).default( + sql`CURRENT_TIMESTAMP::date`, + ), // Nueva columna comment: text("comment"), }, // composite primary key on (userId, finalSurveyId) diff --git a/services/sprintSurvey.ts b/services/sprintSurvey.ts index 1335a82..2bf7c2f 100644 --- a/services/sprintSurvey.ts +++ b/services/sprintSurvey.ts @@ -9,7 +9,7 @@ import { projectMember, finalSurveyAnswer, } from "@/db/schema"; -import { eq, or, sql, and } from "drizzle-orm"; +import { eq, or, sql, and, inArray } from "drizzle-orm"; import { Questions, @@ -320,3 +320,69 @@ export async function getDetailedProjectStatistics(projectId: number) { resourcesSatisfaction, }; } + +export const getGrowthData = async (projectId: number) => { + const growthSupportQuestions = [27, 37, 38]; // Preguntas relacionadas con soporte de crecimiento + const growthOpportunitiesQuestions = [37, 38]; // Preguntas relacionadas con oportunidades de crecimiento + + // Obtener los datos de soporte de crecimiento + const growthSupportDataResults = await db + .select({ + month: sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`.as( + "month", + ), + averageAnswer: sql`AVG(${finalSurveyAnswer.answer})`.as("averageAnswer"), + }) + .from(finalSurveyAnswer) + .innerJoin( + projectMember, + eq(finalSurveyAnswer.userId, projectMember.userId), + ) + .where( + and( + eq(projectMember.projectId, projectId), + inArray(finalSurveyAnswer.questionId, growthSupportQuestions), + ), + ) + .groupBy(sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`) + .execute(); + + const growthSupportData = growthSupportDataResults.map((result) => ({ + month: result.month, + averageAnswer: result.averageAnswer, + })); + + // Obtener los datos de oportunidades de crecimiento + const growthOpportunitiesDataResults = await db + .select({ + month: sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`.as( + "month", + ), + averageAnswer: sql`AVG(${finalSurveyAnswer.answer})`.as("averageAnswer"), + }) + .from(finalSurveyAnswer) + .innerJoin( + projectMember, + eq(finalSurveyAnswer.userId, projectMember.userId), + ) + .where( + and( + eq(projectMember.projectId, projectId), + inArray(finalSurveyAnswer.questionId, growthOpportunitiesQuestions), + ), + ) + .groupBy(sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`) + .execute(); + + const growthOpportunitiesData = growthOpportunitiesDataResults.map( + (result) => ({ + month: result.month, + averageAnswer: result.averageAnswer, + }), + ); + + return { + growthSupportData, + growthOpportunitiesData, + }; +};