From 0811097e569baf231fad5f3757f238976103e89c Mon Sep 17 00:00:00 2001 From: arthur Date: Tue, 1 Oct 2024 17:01:24 +0200 Subject: [PATCH] feat: update day if already exists in db Signed-off-by: Arthur Broudoux --- src/routes/api/days/[id]/+server.ts | 10 +++- .../{day.services.ts => days.services.ts} | 1 - src/utils/days.ts | 3 +- src/utils/types/services.ts | 3 - tests/services/days.services.test.ts | 56 +++++++++++++++++++ 5 files changed, 64 insertions(+), 9 deletions(-) rename src/services/{day.services.ts => days.services.ts} (97%) create mode 100644 tests/services/days.services.test.ts diff --git a/src/routes/api/days/[id]/+server.ts b/src/routes/api/days/[id]/+server.ts index 84fe99f..859a4ba 100644 --- a/src/routes/api/days/[id]/+server.ts +++ b/src/routes/api/days/[id]/+server.ts @@ -63,12 +63,16 @@ export const PUT: RequestHandler = async ({ request, params }: RequestEvent) => const habitsCompleted: number = habits.filter((habit) => habit.isCompleted).length; const percentage: number = (habitsCompleted / habitsLength) * 100; - const updatedDay = await db + await db .update(daysTable) .set({ habits: habitsLength, habitsCompleted, percentage }) .where(eq(daysTable.id, params.id)) .execute(); + const updatedDay = await db.query.daysTable.findFirst({ + where: eq(daysTable.id, params.id) + }); + return new Response(JSON.stringify(updatedDay), { status: 200, headers: { @@ -76,7 +80,7 @@ export const PUT: RequestHandler = async ({ request, params }: RequestEvent) => } }); } catch (error: any) { - console.error("Error fetching habit:", error); - return json({ message: "Iternal Server Error" }, { status: 500 }); + console.error("Error updating day:", error); + return json({ message: "Internal Server Error" }, { status: 500 }); } }; diff --git a/src/services/day.services.ts b/src/services/days.services.ts similarity index 97% rename from src/services/day.services.ts rename to src/services/days.services.ts index ed840bb..27aab4c 100644 --- a/src/services/day.services.ts +++ b/src/services/days.services.ts @@ -62,7 +62,6 @@ export async function editDay( if (!response.ok) throw new Error("Failed to edit day"); const editedDay = await response.json(); - console.log("Day edited:", editedDay); return editedDay as Day; } catch (error: any) { diff --git a/src/utils/days.ts b/src/utils/days.ts index 4d14add..8e633de 100644 --- a/src/utils/days.ts +++ b/src/utils/days.ts @@ -1,4 +1,4 @@ -import { isNewDay, createNewDay, editDay } from "$services/day.services"; +import { isNewDay, createNewDay, editDay } from "$services/days.services"; import { getAllHabits } from "$services/habit.services"; import type { Habit } from "$utils/types/entities"; @@ -15,7 +15,6 @@ export async function manageDay() { return newDay; } else if (response.dayId) { const editedDay = await editDay(fetch, response.dayId, habits); - console.log(editedDay); return editedDay; } } diff --git a/src/utils/types/services.ts b/src/utils/types/services.ts index f338643..8050887 100644 --- a/src/utils/types/services.ts +++ b/src/utils/types/services.ts @@ -5,9 +5,6 @@ export interface ServiceResponse { error: string | null; } -export type RegisterUser = Pick; -export type LoginUser = Pick; - export type HabitRequest = Pick; export type IsNewDayResponse = { diff --git a/tests/services/days.services.test.ts b/tests/services/days.services.test.ts new file mode 100644 index 0000000..fc96489 --- /dev/null +++ b/tests/services/days.services.test.ts @@ -0,0 +1,56 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; + +import { isNewDay, createNewDay } from "$services/days.services"; + +const mockFetch = vi.fn(); +global.fetch = mockFetch; + +describe("Day Services", () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + describe.skip("isNewDay", () => { + it("should return isNewDay and dayId", async () => { + const isNewDay = true; + const dayId = "1"; + const mockResponse = { isNewDay, today: { id: dayId } }; + + mockFetch.mockResolvedValueOnce({ + ok: true, + json: () => Promise.resolve(mockResponse), + status: 200 + }); + + const result = await isNewDay(fetch); + + expect(result).toEqual({ isNewDay, dayId }); + expect(mockFetch).toHaveBeenCalledWith("/api/days"); + expect(mockFetch).toHaveBeenCalledTimes(1); + }); + }); + + describe("createNewDay", () => { + it("should return new day", async () => { + const habits = [ + { id: "1", userId: "1", isCompleted: false, name: "Drink water", points: 10 }, + { id: "2", userId: "1", isCompleted: false, name: "Read a book", points: 10 } + ]; + const newDay = { id: "1", userId: "1", habits }; + + const mockResponse = newDay; + + mockFetch.mockResolvedValueOnce({ + ok: true, + json: () => Promise.resolve(mockResponse), + status: 200 + }); + + const result = await createNewDay(fetch, habits); + + expect(result).toEqual(newDay); + expect(mockFetch).toHaveBeenCalledWith("/api/days"); + expect(mockFetch).toHaveBeenCalledTimes(1); + }); + }); +});