Add dueDate to reminders, remove reminder from notes

Schema changes:
- Added dueDate field to companyReminders table
- Removed reminderDate and reminderSent from notes table

Backend changes:
- Updated company-reminder.service with dueDate handling
- Added getUpcomingReminders function for dashboard
- Simplified note.service (removed reminder logic)
- Updated validators and routes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
richardtekula
2025-12-01 11:21:54 +01:00
parent 947d1d9b99
commit ffaf916f5e
6 changed files with 82 additions and 113 deletions

View File

@@ -1,11 +1,11 @@
import { db } from '../config/database.js';
import { companies, companyReminders } from '../db/schema.js';
import { eq, desc, sql } from 'drizzle-orm';
import { eq, desc, sql, and, lte, gte, isNull, or } from 'drizzle-orm';
import { NotFoundError, BadRequestError } from '../utils/errors.js';
const ensureCompanyExists = async (companyId) => {
const [company] = await db
.select({ id: companies.id })
.select({ id: companies.id, name: companies.name })
.from(companies)
.where(eq(companies.id, companyId))
.limit(1);
@@ -13,6 +13,8 @@ const ensureCompanyExists = async (companyId) => {
if (!company) {
throw new NotFoundError('Firma nenájdená');
}
return company;
};
const getReminderById = async (reminderId) => {
@@ -36,7 +38,7 @@ export const getRemindersByCompanyId = async (companyId) => {
.select()
.from(companyReminders)
.where(eq(companyReminders.companyId, companyId))
.orderBy(desc(companyReminders.createdAt));
.orderBy(companyReminders.dueDate, desc(companyReminders.createdAt));
return reminders;
};
@@ -54,6 +56,7 @@ export const createReminder = async (companyId, data) => {
.values({
companyId,
description,
dueDate: data.dueDate ? new Date(data.dueDate) : null,
isChecked: data.isChecked ?? false,
})
.returning();
@@ -80,6 +83,7 @@ export const updateReminder = async (companyId, reminderId, data) => {
.update(companyReminders)
.set({
description: trimmedDescription,
dueDate: data.dueDate !== undefined ? (data.dueDate ? new Date(data.dueDate) : null) : reminder.dueDate,
isChecked: data.isChecked !== undefined ? data.isChecked : reminder.isChecked,
updatedAt: new Date(),
})
@@ -131,3 +135,37 @@ export const getReminderCountsByCompany = async () => {
return rows;
};
/**
* Get upcoming reminders for dashboard
* Returns reminders due within the next 5 days that are not checked
* Includes company name for display
*/
export const getUpcomingReminders = async () => {
const now = new Date();
const fiveDaysFromNow = new Date();
fiveDaysFromNow.setDate(fiveDaysFromNow.getDate() + 5);
const reminders = await db
.select({
id: companyReminders.id,
description: companyReminders.description,
dueDate: companyReminders.dueDate,
isChecked: companyReminders.isChecked,
companyId: companyReminders.companyId,
companyName: companies.name,
createdAt: companyReminders.createdAt,
})
.from(companyReminders)
.leftJoin(companies, eq(companyReminders.companyId, companies.id))
.where(
and(
eq(companyReminders.isChecked, false),
lte(companyReminders.dueDate, fiveDaysFromNow),
gte(companyReminders.dueDate, now)
)
)
.orderBy(companyReminders.dueDate);
return reminders;
};