diff --git a/docker-compose.yaml b/docker-compose.yaml index 539cb23..00e4169 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,6 +1,7 @@ version: "3" services: postgres: + container_name: better-postgres-db image: postgres:latest environment: POSTGRES_USER: postgres diff --git a/src/lib/db/server/drizzle/0000_typical_thunderbird.sql b/src/lib/db/server/drizzle/0000_amusing_inhumans.sql similarity index 100% rename from src/lib/db/server/drizzle/0000_typical_thunderbird.sql rename to src/lib/db/server/drizzle/0000_amusing_inhumans.sql diff --git a/src/lib/db/server/drizzle/meta/0000_snapshot.json b/src/lib/db/server/drizzle/meta/0000_snapshot.json index f909ae6..82fff8d 100644 --- a/src/lib/db/server/drizzle/meta/0000_snapshot.json +++ b/src/lib/db/server/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "3deb0748-dcc5-4ba5-b388-0090c3e758fb", + "id": "30af2a04-ee69-4f27-8365-23a5f178ad37", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", @@ -53,7 +53,10 @@ "indexes": {}, "foreignKeys": {}, "compositePrimaryKeys": {}, - "uniqueConstraints": {} + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false }, "public.habits": { "name": "habits", @@ -83,12 +86,18 @@ "indexes": {}, "foreignKeys": {}, "compositePrimaryKeys": {}, - "uniqueConstraints": {} + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false } }, "enums": {}, "schemas": {}, "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, "_meta": { "columns": {}, "schemas": {}, diff --git a/src/lib/db/server/drizzle/meta/_journal.json b/src/lib/db/server/drizzle/meta/_journal.json index 3d8600d..94e7171 100644 --- a/src/lib/db/server/drizzle/meta/_journal.json +++ b/src/lib/db/server/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "7", - "when": 1728927379133, - "tag": "0000_typical_thunderbird", + "when": 1730385429752, + "tag": "0000_amusing_inhumans", "breakpoints": true } ] diff --git a/src/services/habits.services.ts b/src/services/habits.services.ts index be56691..371447a 100644 --- a/src/services/habits.services.ts +++ b/src/services/habits.services.ts @@ -90,6 +90,29 @@ export async function toggleHabitStatus(fetch: typeof global.fetch, id: string): } } +export async function toggleAllHabitsStatus( + fetch: typeof global.fetch, + habits: Habit[] +): Promise { + try { + //! DEBUG + if (process.env.NODE_ENV === "development") + console.log("Habits {toggleAllHabitsStatus service}:", habits); + + let habitsUpdated: Habit[] = []; + + for (const habit of habits) { + let habitUpdated: Habit = await toggleHabitStatus(fetch, habit.id); + habitUpdated = { ...habit, ...habitUpdated }; + } + + return habitsUpdated; + } catch (error: unknown) { + console.error("Error {toggleAllHabitsStatus}:", error instanceof Error ? error.message : error); + throw error instanceof Error ? error : new Error("An unexpected error occurred"); + } +} + export async function deleteHabit(fetch: typeof global.fetch, id: string): Promise { try { const response = await fetch(`/api/habits/${id}`, { diff --git a/src/utils/days.ts b/src/utils/days.ts index 596b9fc..e71c1ff 100644 --- a/src/utils/days.ts +++ b/src/utils/days.ts @@ -1,5 +1,5 @@ import { isNewDay, postNewDay, putDay } from "$services/days.services"; -import { getAllHabits } from "$services/habits.services"; +import { getAllHabits, toggleAllHabitsStatus } from "$services/habits.services"; import type { Day, Habit } from "$utils/types/entities"; export function getDate() { @@ -16,17 +16,15 @@ export async function manageDay() { if (response.isNewDay) { const dayCreated: Day = await postNewDay(fetch, habits); + const habitsUpdated: Habit[] = await toggleAllHabitsStatus(fetch, habits); - console.log("New day created:", dayCreated); - - for (const habit of habits) habit.isCompleted = false; - - return dayCreated; + //! DEBUG + if (process.env.NODE_ENV === "development") console.log("New day created:", dayCreated); + if (process.env.NODE_ENV === "development") console.log("Habits updated:", habitsUpdated); } else if (response.dayId) { const dayUpdated: Day = await putDay(fetch, response.dayId, habits); - console.log("Day updated:", dayUpdated); - - return dayUpdated; + //! DEBUG + if (process.env.NODE_ENV === "development") console.log("Day updated:", dayUpdated); } }