Add isActive column to companies table and update service

This commit is contained in:
richardtekula
2025-11-25 10:01:04 +01:00
parent 043eeccb77
commit 9d5d42ee9f
7 changed files with 258 additions and 3 deletions

View File

@@ -0,0 +1,102 @@
import { db } from '../config/database.js';
import { companies, companyReminders } from '../db/schema.js';
import { eq, desc } from 'drizzle-orm';
import { NotFoundError, BadRequestError } from '../utils/errors.js';
const ensureCompanyExists = async (companyId) => {
const [company] = await db
.select({ id: companies.id })
.from(companies)
.where(eq(companies.id, companyId))
.limit(1);
if (!company) {
throw new NotFoundError('Firma nenájdená');
}
};
const getReminderById = async (reminderId) => {
const [reminder] = await db
.select()
.from(companyReminders)
.where(eq(companyReminders.id, reminderId))
.limit(1);
if (!reminder) {
throw new NotFoundError('Reminder nenájdený');
}
return reminder;
};
export const getRemindersByCompanyId = async (companyId) => {
await ensureCompanyExists(companyId);
const reminders = await db
.select()
.from(companyReminders)
.where(eq(companyReminders.companyId, companyId))
.orderBy(desc(companyReminders.createdAt));
return reminders;
};
export const createReminder = async (companyId, data) => {
await ensureCompanyExists(companyId);
const description = data.description?.trim();
if (!description) {
throw new BadRequestError('Popis pripomienky je povinný');
}
const [reminder] = await db
.insert(companyReminders)
.values({
companyId,
description,
isChecked: data.isChecked ?? false,
})
.returning();
return reminder;
};
export const updateReminder = async (companyId, reminderId, data) => {
const reminder = await getReminderById(reminderId);
if (reminder.companyId !== companyId) {
throw new NotFoundError('Reminder nenájdený');
}
const trimmedDescription = data.description !== undefined
? data.description.trim()
: reminder.description;
if (data.description !== undefined && !trimmedDescription) {
throw new BadRequestError('Popis pripomienky je povinný');
}
const [updatedReminder] = await db
.update(companyReminders)
.set({
description: trimmedDescription,
isChecked: data.isChecked !== undefined ? data.isChecked : reminder.isChecked,
updatedAt: new Date(),
})
.where(eq(companyReminders.id, reminderId))
.returning();
return updatedReminder;
};
export const deleteReminder = async (companyId, reminderId) => {
const reminder = await getReminderById(reminderId);
if (reminder.companyId !== companyId) {
throw new NotFoundError('Reminder nenájdený');
}
await db.delete(companyReminders).where(eq(companyReminders.id, reminderId));
return { success: true, message: 'Reminder bol odstránený' };
};

View File

@@ -1,5 +1,5 @@
import { db } from '../config/database.js';
import { companies, projects, todos, notes } from '../db/schema.js';
import { companies, projects, todos, notes, companyReminders } from '../db/schema.js';
import { eq, desc, ilike, or, and } from 'drizzle-orm';
import { NotFoundError, ConflictError } from '../utils/errors.js';
@@ -82,7 +82,7 @@ export const createCompany = async (userId, data) => {
export const updateCompany = async (companyId, data) => {
const company = await getCompanyById(companyId);
const { name, description, address, city, country, phone, email, website } = data;
const { name, description, address, city, country, phone, email, website, isActive } = data;
// If name is being changed, check for duplicates
if (name && name !== company.name) {
@@ -108,6 +108,7 @@ export const updateCompany = async (companyId, data) => {
phone: phone !== undefined ? phone : company.phone,
email: email !== undefined ? email : company.email,
website: website !== undefined ? website : company.website,
isActive: isActive !== undefined ? isActive : company.isActive,
updatedAt: new Date(),
})
.where(eq(companies.id, companyId))
@@ -154,10 +155,17 @@ export const getCompanyWithRelations = async (companyId) => {
.where(eq(notes.companyId, companyId))
.orderBy(desc(notes.createdAt));
const companyReminderList = await db
.select()
.from(companyReminders)
.where(eq(companyReminders.companyId, companyId))
.orderBy(desc(companyReminders.createdAt));
return {
...company,
projects: companyProjects,
todos: companyTodos,
notes: companyNotes,
reminders: companyReminderList,
};
};