Skip to content

Commit

Permalink
feat(manageDay - habitsServices): toggle all habits status when newDa…
Browse files Browse the repository at this point in the history
…y -> true

Signed-off-by: Arthur Broudoux <[email protected]>
  • Loading branch information
abroudoux committed Oct 31, 2024
1 parent 81d8848 commit e61cee0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: "3"
services:
postgres:
container_name: better-postgres-db
image: postgres:latest
environment:
POSTGRES_USER: postgres
Expand Down
15 changes: 12 additions & 3 deletions src/lib/db/server/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -53,7 +53,10 @@
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.habits": {
"name": "habits",
Expand Down Expand Up @@ -83,12 +86,18 @@
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
Expand Down
4 changes: 2 additions & 2 deletions src/lib/db/server/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1728927379133,
"tag": "0000_typical_thunderbird",
"when": 1730385429752,
"tag": "0000_amusing_inhumans",
"breakpoints": true
}
]
Expand Down
23 changes: 23 additions & 0 deletions src/services/habits.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Habit[]> {
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<Habit> {
try {
const response = await fetch(`/api/habits/${id}`, {
Expand Down
16 changes: 7 additions & 9 deletions src/utils/days.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
}

0 comments on commit e61cee0

Please sign in to comment.