Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uncomplete habit regardless time #15

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,39 @@ export class MemoryHabitCompletionDateRepository
}

async findByHabitIdAndDate(habitId: string, completedDate: string) {
return this.habitCompletionDates.find(
habit =>
habit.habitId === habitId && habit.completedDate === completedDate,
);
const completeDateWithoutHour = new Date(completedDate)
.toISOString()
.substring(0, 10);

return this.habitCompletionDates.find(habit => {
const storeCompletedDateWithoutHour = new Date(habit.completedDate)
.toISOString()
.substring(0, 10);

return (
habit.habitId === habitId &&
completeDateWithoutHour === storeCompletedDateWithoutHour
);
});
}

async list() {
return this.habitCompletionDates;
}

async delete(habitId: string, completedDate: string) {
this.habitCompletionDates = this.habitCompletionDates.filter(
habit =>
!(habit.habitId === habitId && habit.completedDate === completedDate),
);
const completedDateWithoutHour = new Date(completedDate)
.toISOString()
.substring(0, 10);

this.habitCompletionDates = this.habitCompletionDates.filter(habit => {
const storedCompletedDateWithouHour = new Date(habit.completedDate)
.toISOString()
.substring(0, 10);
return !(
habit.habitId === habitId &&
storedCompletedDateWithouHour === completedDateWithoutHour
);
});
}
}
6 changes: 3 additions & 3 deletions src/adapters/database/postgres/PgHabitCompDateRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class PgHabitCompDateRepository
async findByHabitIdAndDate(habitId: string, completedDate: string) {
const [habit] = (
await pool.query<PgHabitCompletionDate>(
`SELECT * FROM habit_completion_dates
WHERE habit_id = $1 AND completed_date = $2`,
`SELECT *, completed_date AT TIME ZONE 'UTC' as completed_date FROM habit_completion_dates
WHERE habit_id = $1 AND DATE(completed_date) = $2`,
[habitId, completedDate],
)
).rows;
Expand All @@ -51,7 +51,7 @@ export class PgHabitCompDateRepository
async delete(habitId: string, completedDate: string) {
await pool.query(
`DELETE FROM habit_completion_dates
WHERE habit_id = $1 AND completed_date = $2`,
WHERE habit_id = $1 AND DATE(completed_date) = $2`,
[habitId, completedDate],
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/application/tests/HabitCompletionDateUseCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ describe('Complete a Habit', () => {
habitCompletionDateUseCase.execute(habitId, completedDate),
).rejects.toEqual({ statusCode: 400, message: 'habit not found' });
});

it('should be possible to uncomplete a habit regardless of the time.', async () => {
const createdHabit = await createHabitUseCase.execute('run');
const habitCompletionDate = await habitCompletionDateUseCase.execute(
createdHabit.id,
completedDate,
);
expect(habitCompletionDate?.habitId).toBe(createdHabit.id);
expect(habitCompletionDate?.completedDate).toBe(completedDate);
});
});
2 changes: 1 addition & 1 deletion src/domain/entities/HabitCompletionDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface HabitCompletionDate {
id: string;
habitId: string;
completedDate?: string;
completedDate: string;
}
10 changes: 10 additions & 0 deletions src/infra/web/routes/e2e/habitCompDate.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ describe('Habit Complete Date Controllers', () => {
expect(response.status).toBe(400);
expect(response.body).toBe('habit not found');
});

it('POST (should be able to uncomplete a habit regardless of the time)', async () => {
const response = await request(app).post('/').send({
habitId: '4567',
completedDate: '2000-02-03T23:59:59.000Z',
});
const completedHabits = await memoryHabitCompDateRepository.list();
expect(completedHabits).toHaveLength(1);
expect(response.status).toBe(200);
});
});
Loading