Skip to content

Commit

Permalink
final survey is now shown on the tasks and resource history part of t…
Browse files Browse the repository at this point in the history
…he pcp
  • Loading branch information
pedroalonsoms committed Jun 14, 2024
1 parent 28b7993 commit 97c42ee
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/(base)/pcp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ const PCPTasksDialogContent = ({ projectId }: { projectId: number }) => {
{tasksHistoryQuery.data.map((sprint) => (
<div key={sprint.id}>
{sprint.scheduledAt && (
<p className="py-1 text-lg font-medium">{`Sprint ${formatDate(sprint.scheduledAt)}`}</p>
<p className="py-1 text-lg font-medium">{`${sprint.type === "SPRINT" ? "Sprint" : "Final"} Survey ${formatDate(sprint.scheduledAt)}`}</p>
)}
{sprint.processed ? (
sprint.tasks.map((task) => {
Expand Down Expand Up @@ -649,7 +649,7 @@ const PCPResourcesDialogContent = ({ projectId }: { projectId: number }) => {
{resourcesHistoryQuery.data.map((sprint) => (
<div key={sprint.id}>
{sprint.scheduledAt && (
<p className="py-1 text-lg font-medium">{`Sprint ${formatDate(sprint.scheduledAt)}`}</p>
<p className="py-1 text-lg font-medium">{`${sprint.type === "SPRINT" ? "Sprint" : "Final"} Survey ${formatDate(sprint.scheduledAt)}`}</p>
)}
{sprint.processed ? (
sprint.resources.map((resource) => (
Expand Down
3 changes: 3 additions & 0 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ export const finalSurvey = pgTable("final_survey", {
isProcessing: boolean("is_processing").default(false).notNull(),
});

export type SelectFinalSurvey = typeof finalSurvey.$inferSelect;
export type InsertFinalSurvey = typeof finalSurvey.$inferInsert;

export const finalSurveyAnswer = pgTable(
"final_survey_answer",
{
Expand Down
143 changes: 136 additions & 7 deletions services/tasks-and-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
pipResource,
SelectPipResource,
rulerSurveyAnswers,
finalSurvey,
SelectFinalSurvey,
} from "@/db/schema";
import db from "@/db/drizzle";
import { eq, lte, and, desc, asc, isNotNull } from "drizzle-orm";
Expand Down Expand Up @@ -110,11 +112,73 @@ export async function getUserTasksHistory(projectId: number) {

// TODO: inform the frontend when the survey is pending for processing

if (sprintSurveysWithTasks.length === 0) {
const finalSurveys = await db
.select({ finalSurvey: finalSurvey, task: pipTask })
.from(finalSurvey)
.innerJoin(pipTask, eq(finalSurvey.id, pipTask.finalSurveyId))
.where(
and(
// final surveys that belong to that project
eq(finalSurvey.projectId, projectId),
// tasks that belong to that user
eq(pipTask.userId, userId),
// final surveys that have been already scheduled (i.e. the surveys have already been launched)
lte(finalSurvey.scheduledAt, new Date()),
),
);

interface SelectFinalSurveyWithTasks extends SelectFinalSurvey {
tasks: SelectPipTask[];
}

const finalSurveysWithTasks = finalSurveys.reduce((acc, row) => {
const { finalSurvey, task } = row;
let survey = acc.find((s) => s.id === finalSurvey.id);

if (!survey) {
survey = { ...finalSurvey, tasks: [] };
acc.push(survey);
}

survey.tasks.push(task);

return acc;
}, [] as SelectFinalSurveyWithTasks[]);

interface SurveysWithTasks {
id: number;
type: "SPRINT" | "FINAL";
processed: boolean | null;
scheduledAt: Date | null;
tasks: SelectPipTask[];
}
const surveys = [] as SurveysWithTasks[];

for (const sprintSurveyWithTasks of sprintSurveysWithTasks) {
surveys.push({
id: sprintSurveyWithTasks.id,
type: "SPRINT",
processed: sprintSurveyWithTasks.processed,
scheduledAt: sprintSurveyWithTasks.scheduledAt,
tasks: sprintSurveyWithTasks.tasks,
});
}

for (const finalSurveyWithTasks of finalSurveysWithTasks) {
surveys.push({
id: finalSurveyWithTasks.id,
type: "FINAL",
processed: finalSurveyWithTasks.processed,
scheduledAt: finalSurveyWithTasks.scheduledAt,
tasks: finalSurveyWithTasks.tasks,
});
}

if (surveys.length === 0) {
return "No task history available";
}

return sprintSurveysWithTasks;
return surveys;
}

export async function getUserResourcesForCurrentSprint(projectId: number) {
Expand Down Expand Up @@ -201,13 +265,80 @@ export async function getUserResourcesHistory(projectId: number) {
return acc;
}, [] as SelectSprintSurveyWithResources[]);

const finalSurveys = await db
.select({
resource: pipResource,
finalSurvey: finalSurvey,
})
.from(finalSurvey)
.innerJoin(userResource, eq(finalSurvey.id, userResource.finalSurveyId))
.innerJoin(pipResource, eq(userResource.resourceId, pipResource.id))
.where(
and(
// final surveys that belong to that project
eq(finalSurvey.projectId, projectId),
// final surveys that have been already scheduled (i.e. the surveys have already been launched)
lte(finalSurvey.scheduledAt, new Date()),
// resources that belong to the user
eq(userResource.userId, userId),
),
)
.orderBy(desc(finalSurvey.scheduledAt), asc(pipResource.title));

interface SelectFinalSurveyWithResources extends SelectFinalSurvey {
resources: SelectPipResource[];
}

const finalSurveysWithResources = finalSurveys.reduce((acc, row) => {
const { finalSurvey, resource } = row;
let survey = acc.find((s) => s.id === finalSurvey.id);

if (!survey) {
survey = { ...finalSurvey, resources: [] };
acc.push(survey);
}

survey.resources.push(resource);

return acc;
}, [] as SelectFinalSurveyWithResources[]);

// TODO: inform the frontend when the survey is pending for processing

if (sprintSurveysWithResources.length === 0) {
return "No resource history available";
interface SurveysWithResources {
id: number;
type: "SPRINT" | "FINAL";
processed: boolean | null;
scheduledAt: Date | null;
resources: SelectPipResource[];
}
const surveys = [] as SurveysWithResources[];

for (const sprintSurveyWithResources of sprintSurveysWithResources) {
surveys.push({
id: sprintSurveyWithResources.id,
type: "SPRINT",
processed: sprintSurveyWithResources.processed,
scheduledAt: sprintSurveyWithResources.scheduledAt,
resources: sprintSurveyWithResources.resources,
});
}

return sprintSurveysWithResources;
for (const finalSurveyWithResources of finalSurveysWithResources) {
surveys.push({
id: finalSurveyWithResources.id,
type: "FINAL",
processed: finalSurveyWithResources.processed,
scheduledAt: finalSurveyWithResources.scheduledAt,
resources: finalSurveyWithResources.resources,
});
}

if (surveys.length === 0) {
return "No task history available";
}

return surveys;
}

export async function updateTask({
Expand All @@ -221,8 +352,6 @@ export async function updateTask({
.update(pipTask)
.set({ status: newStatus })
.where(eq(pipTask.id, taskId));

console.log("pipTask", taskId, newStatus);
}

export async function rulerResources() {
Expand Down

0 comments on commit 97c42ee

Please sign in to comment.