Skip to content

Commit

Permalink
feat: update day if already exists in db
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Broudoux <[email protected]>
  • Loading branch information
abroudoux committed Oct 1, 2024
1 parent 5fd23b3 commit 0811097
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/routes/api/days/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ 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: {
"Content-Type": "application/json"
}
});
} 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 });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/days.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;
}
}
3 changes: 0 additions & 3 deletions src/utils/types/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ export interface ServiceResponse<T> {
error: string | null;
}

export type RegisterUser = Pick<User, "name" | "email" | "password">;
export type LoginUser = Pick<User, "email" | "password">;

export type HabitRequest = Pick<Habit, "name">;

export type IsNewDayResponse = {
Expand Down
56 changes: 56 additions & 0 deletions tests/services/days.services.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 0811097

Please sign in to comment.