mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-18 04:29:40 +02:00
fix(types): resolve TypeScript errors across multiple packages
- bot-services: Add registerAsync to AI, Calendar, Clock, Todo modules - bot-services: Add convenience methods to ClockService for bot handlers - bot-services: Make CreateEventInput.endTime optional with sensible defaults - bot-services: Fix empty interface ESLint errors (use type aliases) - questions-backend: Add missing schema columns (isDefault, sortOrder, deletedAt) - questions-backend: Fix or() return type handling in question service - questions-web: Add guard for undefined question ID in route params - skilltree-web: Fix DBSchema type by not extending idb interface directly - calendar-web: Fix Check icon prop (use weight instead of strokeWidth) - matrix-mana-bot: Update clock handler to use new service methods Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
91143a497b
commit
1733580d05
14 changed files with 314 additions and 37 deletions
|
|
@ -1,8 +1,13 @@
|
|||
import { Module, DynamicModule } from '@nestjs/common';
|
||||
import { Module, DynamicModule, Provider, Type, ModuleMetadata } from '@nestjs/common';
|
||||
import { AiService } from './ai.service';
|
||||
import { AiServiceConfig } from './types';
|
||||
|
||||
export interface AiModuleOptions extends Partial<AiServiceConfig> {}
|
||||
export type AiModuleOptions = Partial<AiServiceConfig>;
|
||||
|
||||
export interface AiModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
||||
useFactory: (...args: unknown[]) => Promise<AiModuleOptions> | AiModuleOptions;
|
||||
inject?: (Type<unknown> | string | symbol)[];
|
||||
}
|
||||
|
||||
@Module({})
|
||||
export class AiModule {
|
||||
|
|
@ -42,4 +47,29 @@ export class AiModule {
|
|||
exports: [AiService],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register asynchronously with factory function
|
||||
*/
|
||||
static registerAsync(options: AiModuleAsyncOptions): DynamicModule {
|
||||
const configProvider: Provider = {
|
||||
provide: 'AI_SERVICE_CONFIG',
|
||||
useFactory: options.useFactory,
|
||||
inject: options.inject || [],
|
||||
};
|
||||
|
||||
return {
|
||||
module: AiModule,
|
||||
imports: options.imports || [],
|
||||
providers: [
|
||||
configProvider,
|
||||
{
|
||||
provide: AiService,
|
||||
useFactory: (config: Partial<AiServiceConfig>) => new AiService(config),
|
||||
inject: ['AI_SERVICE_CONFIG'],
|
||||
},
|
||||
],
|
||||
exports: [AiService],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Module, DynamicModule } from '@nestjs/common';
|
||||
import { Module, DynamicModule, Provider, Type, ModuleMetadata } from '@nestjs/common';
|
||||
import { CalendarService, CALENDAR_STORAGE_PROVIDER } from './calendar.service';
|
||||
import { StorageProvider } from '../shared/types';
|
||||
import { FileStorageProvider } from '../shared/storage';
|
||||
|
|
@ -9,6 +9,11 @@ export interface CalendarModuleOptions {
|
|||
storageProvider?: StorageProvider<CalendarData>;
|
||||
}
|
||||
|
||||
export interface CalendarModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
||||
useFactory: (...args: unknown[]) => Promise<CalendarModuleOptions> | CalendarModuleOptions;
|
||||
inject?: (Type<unknown> | string | symbol)[];
|
||||
}
|
||||
|
||||
@Module({})
|
||||
export class CalendarModule {
|
||||
/**
|
||||
|
|
@ -24,7 +29,8 @@ export class CalendarModule {
|
|||
{
|
||||
provide: CALENDAR_STORAGE_PROVIDER,
|
||||
useValue:
|
||||
options?.storageProvider ?? new FileStorageProvider<CalendarData>(storagePath, defaultData),
|
||||
options?.storageProvider ??
|
||||
new FileStorageProvider<CalendarData>(storagePath, defaultData),
|
||||
},
|
||||
CalendarService,
|
||||
],
|
||||
|
|
@ -48,4 +54,30 @@ export class CalendarModule {
|
|||
exports: [CalendarService],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register asynchronously with factory function
|
||||
*/
|
||||
static registerAsync(options: CalendarModuleAsyncOptions): DynamicModule {
|
||||
const storageProvider: Provider = {
|
||||
provide: CALENDAR_STORAGE_PROVIDER,
|
||||
useFactory: async (...args: unknown[]) => {
|
||||
const moduleOptions = await options.useFactory(...args);
|
||||
const storagePath = moduleOptions?.storagePath ?? './data/calendar-data.json';
|
||||
const defaultData: CalendarData = { events: [], calendars: [] };
|
||||
return (
|
||||
moduleOptions?.storageProvider ??
|
||||
new FileStorageProvider<CalendarData>(storagePath, defaultData)
|
||||
);
|
||||
},
|
||||
inject: options.inject || [],
|
||||
};
|
||||
|
||||
return {
|
||||
module: CalendarModule,
|
||||
imports: options.imports || [],
|
||||
providers: [storageProvider, CalendarService],
|
||||
exports: [CalendarService],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,10 @@ export class CalendarService implements OnModuleInit {
|
|||
) {
|
||||
this.storage =
|
||||
storage ||
|
||||
new FileStorageProvider<CalendarData>('./data/calendar-data.json', { events: [], calendars: [] });
|
||||
new FileStorageProvider<CalendarData>('./data/calendar-data.json', {
|
||||
events: [],
|
||||
calendars: [],
|
||||
});
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
|
|
@ -46,7 +49,9 @@ export class CalendarService implements OnModuleInit {
|
|||
private async loadData(): Promise<void> {
|
||||
try {
|
||||
this.data = await this.storage.load();
|
||||
this.logger.log(`Loaded ${this.data.events.length} events, ${this.data.calendars.length} calendars`);
|
||||
this.logger.log(
|
||||
`Loaded ${this.data.events.length} events, ${this.data.calendars.length} calendars`
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to load calendar data:', error);
|
||||
this.data = { events: [], calendars: [] };
|
||||
|
|
@ -81,6 +86,19 @@ export class CalendarService implements OnModuleInit {
|
|||
async createEvent(userId: string, input: CreateEventInput): Promise<CalendarEvent> {
|
||||
const calendar = this.ensureDefaultCalendar(userId);
|
||||
|
||||
// Calculate endTime if not provided
|
||||
let endTime: Date;
|
||||
if (input.endTime) {
|
||||
endTime = input.endTime;
|
||||
} else if (input.isAllDay) {
|
||||
// For all-day events, end at end of the same day
|
||||
endTime = new Date(input.startTime);
|
||||
endTime.setHours(23, 59, 59, 999);
|
||||
} else {
|
||||
// Default to 1 hour after start
|
||||
endTime = new Date(input.startTime.getTime() + 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
const event: CalendarEvent = {
|
||||
id: generateId(),
|
||||
userId,
|
||||
|
|
@ -88,7 +106,7 @@ export class CalendarService implements OnModuleInit {
|
|||
description: input.description ?? null,
|
||||
location: input.location ?? null,
|
||||
startTime: input.startTime.toISOString(),
|
||||
endTime: input.endTime.toISOString(),
|
||||
endTime: endTime.toISOString(),
|
||||
isAllDay: input.isAllDay ?? false,
|
||||
calendarId: input.calendarId ?? calendar.id,
|
||||
calendarName: calendar.name,
|
||||
|
|
@ -101,7 +119,11 @@ export class CalendarService implements OnModuleInit {
|
|||
return event;
|
||||
}
|
||||
|
||||
async updateEvent(userId: string, eventId: string, input: UpdateEventInput): Promise<CalendarEvent | null> {
|
||||
async updateEvent(
|
||||
userId: string,
|
||||
eventId: string,
|
||||
input: UpdateEventInput
|
||||
): Promise<CalendarEvent | null> {
|
||||
const event = this.data.events.find((e) => e.id === eventId && e.userId === userId);
|
||||
if (!event) return null;
|
||||
|
||||
|
|
@ -197,7 +219,7 @@ export class CalendarService implements OnModuleInit {
|
|||
return this.getEventsInRange(userId, today, weekEnd);
|
||||
}
|
||||
|
||||
async getUpcomingEvents(userId: string, days: number = 7): Promise<CalendarEvent[]> {
|
||||
async getUpcomingEvents(userId: string, days = 7): Promise<CalendarEvent[]> {
|
||||
const now = new Date();
|
||||
const endDate = addDays(now, days);
|
||||
return this.getEventsInRange(userId, now, endDate);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { UserEntity } from '../shared/types';
|
||||
import { type UserEntity } from '../shared/types';
|
||||
|
||||
/**
|
||||
* Calendar event entity
|
||||
|
|
@ -38,7 +38,7 @@ export interface CalendarData {
|
|||
export interface CreateEventInput {
|
||||
title: string;
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
endTime?: Date; // Optional - defaults to startTime + 1 hour, or end of day for all-day events
|
||||
description?: string | null;
|
||||
location?: string | null;
|
||||
isAllDay?: boolean;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
import { Module, DynamicModule } from '@nestjs/common';
|
||||
import { Module, DynamicModule, Provider, Type, ModuleMetadata } from '@nestjs/common';
|
||||
import { ClockService } from './clock.service';
|
||||
import { ClockServiceConfig } from './types';
|
||||
|
||||
export interface ClockModuleOptions extends Partial<ClockServiceConfig> {}
|
||||
export type ClockModuleOptions = Partial<ClockServiceConfig>;
|
||||
|
||||
export interface ClockModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
||||
useFactory: (...args: unknown[]) => Promise<ClockModuleOptions> | ClockModuleOptions;
|
||||
inject?: (Type<unknown> | string | symbol)[];
|
||||
}
|
||||
|
||||
@Module({})
|
||||
export class ClockModule {
|
||||
|
|
@ -42,4 +47,29 @@ export class ClockModule {
|
|||
exports: [ClockService],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register asynchronously with factory function
|
||||
*/
|
||||
static registerAsync(options: ClockModuleAsyncOptions): DynamicModule {
|
||||
const configProvider: Provider = {
|
||||
provide: 'CLOCK_SERVICE_CONFIG',
|
||||
useFactory: options.useFactory,
|
||||
inject: options.inject || [],
|
||||
};
|
||||
|
||||
return {
|
||||
module: ClockModule,
|
||||
imports: options.imports || [],
|
||||
providers: [
|
||||
configProvider,
|
||||
{
|
||||
provide: ClockService,
|
||||
useFactory: (config: Partial<ClockServiceConfig>) => new ClockService(config),
|
||||
inject: ['CLOCK_SERVICE_CONFIG'],
|
||||
},
|
||||
],
|
||||
exports: [ClockService],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -267,4 +267,116 @@ export class ClockService {
|
|||
|
||||
return parts.join(' ');
|
||||
}
|
||||
|
||||
// ===== Convenience Methods for Bot Handlers =====
|
||||
|
||||
/**
|
||||
* Start a timer from natural language input
|
||||
* Parses duration and optional label from input like "25m Pomodoro"
|
||||
*/
|
||||
async startTimerForUser(userId: string, input: string): Promise<Timer & { name?: string }> {
|
||||
const token = this.getUserToken(userId);
|
||||
if (!token) {
|
||||
throw new Error('Nicht authentifiziert. Bitte zuerst anmelden.');
|
||||
}
|
||||
|
||||
// Parse duration from input
|
||||
const durationSeconds = this.parseDuration(input);
|
||||
if (!durationSeconds) {
|
||||
throw new Error('Ungültiges Dauer-Format. Beispiele: 25m, 1h30m, 90s');
|
||||
}
|
||||
|
||||
// Extract label (everything after duration pattern)
|
||||
const label = input.replace(/\d+\s*[hms]?(?:in)?/gi, '').trim() || null;
|
||||
|
||||
const timer = await this.createTimer({ durationSeconds, label }, token);
|
||||
// Start the timer immediately
|
||||
const started = await this.startTimer(timer.id, token);
|
||||
return { ...started, name: started.label ?? undefined };
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the running timer for a user
|
||||
*/
|
||||
async stopTimerForUser(userId: string, timerName?: string): Promise<Timer & { name?: string }> {
|
||||
const token = this.getUserToken(userId);
|
||||
if (!token) {
|
||||
throw new Error('Nicht authentifiziert. Bitte zuerst anmelden.');
|
||||
}
|
||||
|
||||
const timers = await this.getTimers(token);
|
||||
let timer: Timer | undefined;
|
||||
|
||||
if (timerName) {
|
||||
timer = timers.find(
|
||||
(t) =>
|
||||
(t.status === 'running' || t.status === 'paused') &&
|
||||
t.label?.toLowerCase().includes(timerName.toLowerCase())
|
||||
);
|
||||
} else {
|
||||
timer = timers.find((t) => t.status === 'running' || t.status === 'paused');
|
||||
}
|
||||
|
||||
if (!timer) {
|
||||
throw new Error('Kein aktiver Timer gefunden.');
|
||||
}
|
||||
|
||||
await this.deleteTimer(timer.id, token);
|
||||
return { ...timer, name: timer.label ?? undefined };
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an alarm from natural language input
|
||||
* Parses time and optional label from input like "14:30 Meeting"
|
||||
*/
|
||||
async setAlarmForUser(userId: string, input: string): Promise<Alarm & { name?: string }> {
|
||||
const token = this.getUserToken(userId);
|
||||
if (!token) {
|
||||
throw new Error('Nicht authentifiziert. Bitte zuerst anmelden.');
|
||||
}
|
||||
|
||||
const time = this.parseAlarmTime(input);
|
||||
if (!time) {
|
||||
throw new Error('Ungültiges Zeit-Format. Beispiele: 14:30, 9:00, 14 Uhr 30');
|
||||
}
|
||||
|
||||
// Extract label (everything after time pattern)
|
||||
const label =
|
||||
input
|
||||
.replace(/\d{1,2}:\d{2}(:\d{2})?/g, '')
|
||||
.replace(/\d{1,2}\s*uhr(\s*\d{1,2})?/gi, '')
|
||||
.trim() || null;
|
||||
|
||||
const alarm = await this.createAlarm({ time, label }, token);
|
||||
return { ...alarm, name: alarm.label ?? undefined };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get time for a specific city/timezone
|
||||
*/
|
||||
async getWorldClockTime(city: string): Promise<{ city: string; time: string; date: string }> {
|
||||
// Search for timezone
|
||||
const results = await this.searchTimezones(city);
|
||||
if (results.length === 0) {
|
||||
throw new Error(`Stadt "${city}" nicht gefunden.`);
|
||||
}
|
||||
|
||||
const tz = results[0];
|
||||
const now = new Date();
|
||||
|
||||
const time = now.toLocaleTimeString('de-DE', {
|
||||
timeZone: tz.timezone,
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
});
|
||||
|
||||
const date = now.toLocaleDateString('de-DE', {
|
||||
timeZone: tz.timezone,
|
||||
weekday: 'long',
|
||||
day: 'numeric',
|
||||
month: 'long',
|
||||
});
|
||||
|
||||
return { city: tz.city, time, date };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Module, DynamicModule } from '@nestjs/common';
|
||||
import { Module, DynamicModule, Provider, Type, ModuleMetadata } from '@nestjs/common';
|
||||
import { TodoService, TODO_STORAGE_PROVIDER } from './todo.service';
|
||||
import { StorageProvider } from '../shared/types';
|
||||
import { FileStorageProvider } from '../shared/storage';
|
||||
|
|
@ -9,6 +9,11 @@ export interface TodoModuleOptions {
|
|||
storageProvider?: StorageProvider<TodoData>;
|
||||
}
|
||||
|
||||
export interface TodoModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
||||
useFactory: (...args: unknown[]) => Promise<TodoModuleOptions> | TodoModuleOptions;
|
||||
inject?: (Type<unknown> | string | symbol)[];
|
||||
}
|
||||
|
||||
@Module({})
|
||||
export class TodoModule {
|
||||
/**
|
||||
|
|
@ -23,7 +28,8 @@ export class TodoModule {
|
|||
providers: [
|
||||
{
|
||||
provide: TODO_STORAGE_PROVIDER,
|
||||
useValue: options?.storageProvider ?? new FileStorageProvider<TodoData>(storagePath, defaultData),
|
||||
useValue:
|
||||
options?.storageProvider ?? new FileStorageProvider<TodoData>(storagePath, defaultData),
|
||||
},
|
||||
TodoService,
|
||||
],
|
||||
|
|
@ -47,4 +53,30 @@ export class TodoModule {
|
|||
exports: [TodoService],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register asynchronously with factory function
|
||||
*/
|
||||
static registerAsync(options: TodoModuleAsyncOptions): DynamicModule {
|
||||
const storageProvider: Provider = {
|
||||
provide: TODO_STORAGE_PROVIDER,
|
||||
useFactory: async (...args: unknown[]) => {
|
||||
const moduleOptions = await options.useFactory(...args);
|
||||
const storagePath = moduleOptions?.storagePath ?? './data/todo-data.json';
|
||||
const defaultData: TodoData = { tasks: [], projects: [] };
|
||||
return (
|
||||
moduleOptions?.storageProvider ??
|
||||
new FileStorageProvider<TodoData>(storagePath, defaultData)
|
||||
);
|
||||
},
|
||||
inject: options.inject || [],
|
||||
};
|
||||
|
||||
return {
|
||||
module: TodoModule,
|
||||
imports: options.imports || [],
|
||||
providers: [storageProvider, TodoService],
|
||||
exports: [TodoService],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue