diff --git a/src/lib/server/services/appointment-service.ts b/src/lib/server/services/appointment-service.ts index 9606414..bbdbe7f 100644 --- a/src/lib/server/services/appointment-service.ts +++ b/src/lib/server/services/appointment-service.ts @@ -77,6 +77,14 @@ export class AppointmentService { } } + async hasAppointments(): Promise { + const log = logger.setContext("AppointmentService"); + log.debug("Checking if tenant has appointments", { tenantId: this.tenantId }); + const db = await this.getDb(); + const result = await db.select().from(tenantSchema.appointment).limit(1); + return result.length > 0; + } + /** * Send appointment notification email to client * @param appointmentId - The appointment ID diff --git a/src/lib/server/services/user-service.ts b/src/lib/server/services/user-service.ts index 42e9957..004f1e3 100644 --- a/src/lib/server/services/user-service.ts +++ b/src/lib/server/services/user-service.ts @@ -19,6 +19,7 @@ import { TenantAdminService } from "./tenant-admin-service"; import { InviteService } from "./invite-service"; import type { PgTransaction } from "drizzle-orm/pg-core"; import type { PostgresJsQueryResultHKT } from "drizzle-orm/postgres-js"; +import { AppointmentService } from "./appointment-service"; export type InsertUser = InferInsertModel; export type InsertUserPasskey = InferInsertModel; @@ -140,6 +141,15 @@ export class UserService { if (userData.role === "GLOBAL_ADMIN") { userDataForDb.confirmationState = "ACCESS_GRANTED"; // Admin account is active immediately after email confirmation userDataForDb.isActive = true; + } else if (userData.tenantId) { + const appointmentService = await AppointmentService.forTenant(userData.tenantId); + const hasAppointments = await appointmentService.hasAppointments(); + if (!hasAppointments) { + log.debug("No appointments found for tenant, granting immediate access to user", { + tenantId: userData.tenantId, + }); + userDataForDb.confirmationState = "ACCESS_GRANTED"; + } } // Handle passphrase or generate recovery passphrase