Skip to content

Commit

Permalink
fix: postNewDay service
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Broudoux <[email protected]>
  • Loading branch information
abroudoux committed Oct 18, 2024
1 parent 75a4371 commit b4f8593
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/routes/api/days/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,35 @@ export const POST: RequestHandler = async ({ request }: RequestEvent) => {
try {
const now = getDate();
const id = uuidv4().toString();
// TODO => replace userId with the actual user id
const userId = "2e3ae305-0bae-4fc0-9b8b-890770cbbaf0";
const habits: Habit[] = (await request.json()).habits;
const habitsLength: number = habits.length;
const habitsCompleted: number = habits.filter((habit) => habit.isCompleted).length;
const percentage: number = (habitsCompleted / habitsLength) * 100;
const percentage: number = Math.round((habitsCompleted / habitsLength) * 100);

const newDay: Day = {
id: id,
userId: userId,
date: now,
habits: habits,
habitsCompleted: habitsCompleted,
percentage: percentage
percentage: percentage,
habitsNum: habits.length
};

//! DEBUG
if (process.env.NODE_ENV === "development") console.log("newDay from POST days:", newDay);

const dayCreated = await db.insert(daysTable).values(newDay).execute();

//! DEBUG
console.log("dayCreated from POST days:", dayCreated);
if (process.env.NODE_ENV === "development")
console.log("dayCreated from POST days:", dayCreated);

return json({ day: dayCreated, message: "Day successfully created" }, { status: 201 });
} catch (error: any) {
console.error("Error creating day:", error.message);
return json({ message: "Internal Server Error" }, { status: 500 });
} catch (error: unknown) {
console.error("Error creating day:", error instanceof Error ? error.message : error);
return json(
{ message: "Internal Server Error", error: error instanceof Error ? error.message : error },
{ status: 500 }
);
}
};
6 changes: 5 additions & 1 deletion src/utils/days.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function getDate() {

export async function manageDay() {
const response = await isNewDay(fetch);

//! DEBUG
if (process.env.NODE_ENV === "developement") console.log("Response {days.ts}", response);

const habits: Habit[] = await getAllHabits(fetch);

if (response.isNewDay) {
Expand All @@ -21,7 +25,7 @@ export async function manageDay() {
} else if (response.dayId) {
const dayUpdated: Day = await putDay(fetch, response.dayId, habits);

console.log(dayUpdated);
console.log("Day updated:", dayUpdated);

return dayUpdated;
}
Expand Down

0 comments on commit b4f8593

Please sign in to comment.