mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
feat(vacay): add manual school holiday calendars
This commit is contained in:
@@ -4534,6 +4534,26 @@ function runMigrations(db: Database.Database): void {
|
||||
db.exec('ALTER TABLE places ADD COLUMN fill_percent INTEGER');
|
||||
}
|
||||
},
|
||||
() => {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS school_holiday_countries (
|
||||
code TEXT PRIMARY KEY, name TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS school_holiday_regions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
country TEXT NOT NULL REFERENCES school_holiday_countries(code),
|
||||
name TEXT NOT NULL COLLATE NOCASE, revision INTEGER NOT NULL DEFAULT 1,
|
||||
UNIQUE(country, name)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS school_holiday_periods (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
region_id INTEGER NOT NULL REFERENCES school_holiday_regions(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL, start_date TEXT NOT NULL, end_date TEXT NOT NULL,
|
||||
CHECK (end_date >= start_date)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_school_holiday_periods_region ON school_holiday_periods(region_id);
|
||||
`);
|
||||
},
|
||||
];
|
||||
|
||||
if (currentVersion < migrations.length) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import { CategoriesModule } from './categories/categories.module';
|
||||
import { TagsModule } from './tags/tags.module';
|
||||
import { NotificationsModule } from './notifications/notifications.module';
|
||||
import { AtlasModule } from './atlas/atlas.module';
|
||||
import { SchoolHolidaysModule } from './school-holidays/school-holidays.module';
|
||||
import { VacayModule } from './vacay/vacay.module';
|
||||
import { PackingModule } from './packing/packing.module';
|
||||
import { BudgetModule } from './budget/budget.module';
|
||||
@@ -79,7 +80,7 @@ import { RealtimeGatewayModule } from './realtime/realtime-gateway.module';
|
||||
* migrated.
|
||||
*/
|
||||
@Module({
|
||||
imports: [AppConfigModule, DatabaseModule, RealtimeModule, RealtimeGatewayModule, SchedulingModule, McpModule.forRoot({ accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess }), HealthModule, PlatformModule, McpTransportModule, WeatherModule, PublicApiModule, HelpModule, AirportsModule, ConfigModule, SystemNoticesModule, GeoModule, MapsModule, PlaceEnrichmentModule, PlaceShadowModule, RouteUsageModule, CategoriesModule, TagsModule, NotificationsModule, AtlasModule, VacayModule, PackingModule, TodoModule, BudgetModule, ReservationsModule, DaysModule, DayNotesModule, AccommodationsModule, AssignmentsModule, PlacesModule, RoadtripModule, TripsModule, CollabModule, FilesModule, PhotosModule, MemoriesModule, AirtrailModule, JourneyModule, CollectionsModule, ShareModule, TripInviteModule, TransitModule, FeedsModule, SettingsModule, StorageModule, BackupModule, AuthModule, OidcModule, OauthModule, AdminModule, AddonsModule, AuditModule, PermissionsModule, PluginsModule, BookingImportModule, ReservationImportModule, LlmParseModule, ManagedExtModule],
|
||||
imports: [AppConfigModule, DatabaseModule, RealtimeModule, RealtimeGatewayModule, SchedulingModule, McpModule.forRoot({ accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess }), HealthModule, PlatformModule, McpTransportModule, WeatherModule, PublicApiModule, HelpModule, AirportsModule, ConfigModule, SystemNoticesModule, GeoModule, MapsModule, PlaceEnrichmentModule, PlaceShadowModule, RouteUsageModule, CategoriesModule, TagsModule, NotificationsModule, AtlasModule, VacayModule, SchoolHolidaysModule, PackingModule, TodoModule, BudgetModule, ReservationsModule, DaysModule, DayNotesModule, AccommodationsModule, AssignmentsModule, PlacesModule, RoadtripModule, TripsModule, CollabModule, FilesModule, PhotosModule, MemoriesModule, AirtrailModule, JourneyModule, CollectionsModule, ShareModule, TripInviteModule, TransitModule, FeedsModule, SettingsModule, StorageModule, BackupModule, AuthModule, OidcModule, OauthModule, AdminModule, AddonsModule, AuditModule, PermissionsModule, PluginsModule, BookingImportModule, ReservationImportModule, LlmParseModule, ManagedExtModule],
|
||||
providers: [
|
||||
// Default-deny: a route is authenticated unless it carries @Public() or
|
||||
// @OptionalAuth(), or declares its own @UseGuards chain. Protection used to
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, UseGuards, BadRequestException } from '@nestjs/common';
|
||||
import { createZodDto } from 'nestjs-zod';
|
||||
import { schoolHolidayCountryRequestSchema, schoolHolidayRegionRequestSchema } from '@trek/shared';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { AdminGuard } from '../auth/admin.guard';
|
||||
import { SchoolHolidaysService } from './school-holidays.service';
|
||||
|
||||
export class SchoolHolidayCountryDto extends createZodDto(schoolHolidayCountryRequestSchema) {}
|
||||
export class SchoolHolidayRegionDto extends createZodDto(schoolHolidayRegionRequestSchema) {}
|
||||
|
||||
@Controller('api/school-holiday-catalog')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class SchoolHolidaysController {
|
||||
constructor(private readonly holidays: SchoolHolidaysService) {}
|
||||
|
||||
@Get()
|
||||
catalog() { return this.holidays.catalog(); }
|
||||
|
||||
@Get('regions/:id')
|
||||
region(@Param('id', ParseIntPipe) id: number) { return this.holidays.region(id); }
|
||||
|
||||
@Get('regions/:id/holidays/:year')
|
||||
forYear(@Param('id', ParseIntPipe) id: number, @Param('year') year: string) {
|
||||
if (!/^\d{4}$/.test(year)) throw new BadRequestException('Invalid year');
|
||||
return this.holidays.holidays(id, year);
|
||||
}
|
||||
|
||||
@Post('countries')
|
||||
@UseGuards(AdminGuard)
|
||||
createCountry(@Body() body: SchoolHolidayCountryDto) { return this.holidays.createCountry(body); }
|
||||
|
||||
@Delete('countries/:code')
|
||||
@UseGuards(AdminGuard)
|
||||
deleteCountry(@Param('code') code: string) { return this.holidays.deleteCountry(code); }
|
||||
|
||||
@Post('countries/:code/regions')
|
||||
@UseGuards(AdminGuard)
|
||||
createRegion(@Param('code') code: string, @Body() body: SchoolHolidayRegionDto) { return this.holidays.createRegion(code, body); }
|
||||
|
||||
@Put('regions/:id')
|
||||
@UseGuards(AdminGuard)
|
||||
updateRegion(@Param('id', ParseIntPipe) id: number, @Body() body: SchoolHolidayRegionDto) { return this.holidays.updateRegion(id, body); }
|
||||
|
||||
@Delete('regions/:id')
|
||||
@UseGuards(AdminGuard)
|
||||
deleteRegion(@Param('id', ParseIntPipe) id: number, @Query('revision', ParseIntPipe) revision: number) { return this.holidays.deleteRegion(id, revision); }
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { HttpException } from '@nestjs/common';
|
||||
import { z } from 'zod';
|
||||
import { idSchema, schoolHolidayCountryRequestSchema, schoolHolidayRegionRequestSchema, type SchoolHolidayCountryRequest, type SchoolHolidayRegionRequest } from '@trek/shared';
|
||||
import { McpController, Tool, type McpContext, TOOL_ANNOTATIONS_READONLY, TOOL_ANNOTATIONS_NON_IDEMPOTENT, TOOL_ANNOTATIONS_WRITE, TOOL_ANNOTATIONS_DELETE, errorResult, ok } from '../../nest-mcp';
|
||||
import { adminRequired } from '../../mcp/tools/_shared';
|
||||
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
|
||||
import { SchoolHolidaysService } from './school-holidays.service';
|
||||
|
||||
@McpController()
|
||||
export class SchoolHolidaysMcp {
|
||||
constructor(private readonly holidays: SchoolHolidaysService, private readonly guards: McpToolGuardsService) {}
|
||||
|
||||
private adminWrite(ctx: McpContext, write: () => unknown) {
|
||||
if (!this.guards.isAdminUser(ctx.userId)) return adminRequired();
|
||||
try { return ok(write()); }
|
||||
catch (error) {
|
||||
if (error instanceof HttpException || error instanceof z.ZodError) return errorResult(error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'list_manual_school_holiday_regions',
|
||||
description: 'List the global manual school holiday catalog. Use regions[].code verbatim as the region with add_holiday_calendar and type school_holiday. These calendars require no external API.',
|
||||
inputSchema: {}, annotations: TOOL_ANNOTATIONS_READONLY,
|
||||
access: { group: 'vacay', mode: 'read' },
|
||||
})
|
||||
catalog() { return ok(this.holidays.catalog()); }
|
||||
|
||||
@Tool({
|
||||
name: 'get_manual_school_holiday_region',
|
||||
description: 'Read a manual school region, its current revision and all named holiday periods. Read before updating: updates replace the complete list of periods, and the current revision is required.',
|
||||
inputSchema: { regionId: idSchema }, annotations: TOOL_ANNOTATIONS_READONLY,
|
||||
access: { group: 'vacay', mode: 'read' },
|
||||
})
|
||||
region({ regionId }: { regionId: number }) { return ok(this.holidays.region(regionId)); }
|
||||
|
||||
@Tool({
|
||||
name: 'list_manual_school_holidays',
|
||||
description: 'Read named school breaks for a manual region and year, including breaks spanning a year boundary. Both boundary dates are included.',
|
||||
inputSchema: { regionId: idSchema, year: z.number().int().min(1000).max(9999) },
|
||||
annotations: TOOL_ANNOTATIONS_READONLY, access: { group: 'vacay', mode: 'read' },
|
||||
})
|
||||
forYear({ regionId, year }: { regionId: number; year: number }) { return ok({ holidays: this.holidays.holidays(regionId, String(year)) }); }
|
||||
|
||||
@Tool({
|
||||
name: 'create_manual_school_holiday_country',
|
||||
description: 'Admin only. Create a country in the instance-wide manual school holiday catalog with its two-letter uppercase country code and display name. Check the catalog first to avoid duplicates.',
|
||||
inputSchema: schoolHolidayCountryRequestSchema.shape, annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
|
||||
access: { group: 'vacay', mode: 'write' },
|
||||
})
|
||||
createCountry(body: SchoolHolidayCountryRequest, ctx: McpContext) {
|
||||
return this.adminWrite(ctx, () => this.holidays.createCountry(schoolHolidayCountryRequestSchema.parse(body)));
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'create_manual_school_holiday_region',
|
||||
description: 'Admin only. Create a region or school district in an existing manual country, with named holiday periods. Use revision 0 for creation. Dates are inclusive YYYY-MM-DD. All users can select this region.',
|
||||
inputSchema: { country: schoolHolidayCountryRequestSchema.shape.code, ...schoolHolidayRegionRequestSchema.shape },
|
||||
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT, access: { group: 'vacay', mode: 'write' },
|
||||
})
|
||||
createRegion({ country, ...body }: SchoolHolidayRegionRequest & { country: string }, ctx: McpContext) {
|
||||
return this.adminWrite(ctx, () => this.holidays.createRegion(country, schoolHolidayRegionRequestSchema.parse(body)));
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'update_manual_school_holiday_region',
|
||||
description: 'Admin only. Rename a global school region and replace its complete list of holiday periods. First read get_manual_school_holiday_region and preserve periods that should remain. Supply its current revision; stale updates are rejected. Changes affect every calendar using this region.',
|
||||
inputSchema: { regionId: idSchema, ...schoolHolidayRegionRequestSchema.shape },
|
||||
annotations: TOOL_ANNOTATIONS_WRITE, access: { group: 'vacay', mode: 'write' },
|
||||
})
|
||||
updateRegion({ regionId, ...body }: SchoolHolidayRegionRequest & { regionId: number }, ctx: McpContext) {
|
||||
return this.adminWrite(ctx, () => this.holidays.updateRegion(regionId, schoolHolidayRegionRequestSchema.parse(body)));
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'delete_manual_school_holiday_region',
|
||||
description: 'Admin only. Delete a global manual school region and its periods using the current revision. Regions selected by a vacation calendar cannot be deleted.',
|
||||
inputSchema: { regionId: idSchema, revision: z.number().int().positive() },
|
||||
annotations: TOOL_ANNOTATIONS_DELETE, access: { group: 'vacay', mode: 'write' },
|
||||
})
|
||||
deleteRegion({ regionId, revision }: { regionId: number; revision: number }, ctx: McpContext) {
|
||||
return this.adminWrite(ctx, () => this.holidays.deleteRegion(regionId, revision));
|
||||
}
|
||||
|
||||
@Tool({
|
||||
name: 'delete_manual_school_holiday_country',
|
||||
description: 'Admin only. Delete an empty country from the global manual school holiday catalog. Remove its unused regions first.',
|
||||
inputSchema: { code: schoolHolidayCountryRequestSchema.shape.code },
|
||||
annotations: TOOL_ANNOTATIONS_DELETE, access: { group: 'vacay', mode: 'write' },
|
||||
})
|
||||
deleteCountry({ code }: { code: string }, ctx: McpContext) {
|
||||
return this.adminWrite(ctx, () => this.holidays.deleteCountry(code));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { McpSharedModule } from '../mcp-shared/mcp-shared.module';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SchoolHolidaysController } from './school-holidays.controller';
|
||||
import { SchoolHolidaysService } from './school-holidays.service';
|
||||
import { SchoolHolidaysMcp } from './school-holidays.mcp';
|
||||
|
||||
@Module({ imports: [McpSharedModule], controllers: [SchoolHolidaysController], providers: [SchoolHolidaysService, SchoolHolidaysMcp] })
|
||||
export class SchoolHolidaysModule {}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import type { SchoolHolidayCatalog, SchoolHolidayCountryRequest, SchoolHolidayPeriod, SchoolHolidayRegion, SchoolHolidayRegionDetail, SchoolHolidayRegionRequest } from '@trek/shared';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
|
||||
@Injectable()
|
||||
export class SchoolHolidaysService {
|
||||
constructor(private readonly db: DatabaseService) {}
|
||||
|
||||
catalog(): SchoolHolidayCatalog {
|
||||
return {
|
||||
countries: this.db.all<SchoolHolidayCountryRequest>('SELECT code, name FROM school_holiday_countries ORDER BY name, code'),
|
||||
regions: this.db.all<SchoolHolidayRegion>("SELECT *, country || '-MANUAL-' || id AS code FROM school_holiday_regions ORDER BY name, id"),
|
||||
};
|
||||
}
|
||||
|
||||
country(code: string): SchoolHolidayCountryRequest {
|
||||
const country = this.db.get<SchoolHolidayCountryRequest>('SELECT code, name FROM school_holiday_countries WHERE code = ?', code);
|
||||
if (!country) throw new NotFoundException('Country not found');
|
||||
return country;
|
||||
}
|
||||
|
||||
createCountry(country: SchoolHolidayCountryRequest) {
|
||||
const inserted = this.db.run('INSERT OR IGNORE INTO school_holiday_countries (code, name) VALUES (?, ?)', country.code, country.name);
|
||||
if (!inserted.changes) throw new ConflictException('Country already exists');
|
||||
return country;
|
||||
}
|
||||
|
||||
deleteCountry(code: string) {
|
||||
return this.db.transaction(() => {
|
||||
this.country(code);
|
||||
if (this.db.get('SELECT id FROM school_holiday_regions WHERE country = ? LIMIT 1', code)) {
|
||||
throw new ConflictException('Remove the regions before deleting this country');
|
||||
}
|
||||
this.db.run('DELETE FROM school_holiday_countries WHERE code = ?', code);
|
||||
return { success: true };
|
||||
});
|
||||
}
|
||||
|
||||
region(id: number): SchoolHolidayRegionDetail {
|
||||
const region = this.db.get<SchoolHolidayRegion>("SELECT *, country || '-MANUAL-' || id AS code FROM school_holiday_regions WHERE id = ?", id);
|
||||
if (!region) throw new NotFoundException('Region not found');
|
||||
return { ...region, holidays: this.db.all<SchoolHolidayPeriod>('SELECT name, start_date AS startDate, end_date AS endDate FROM school_holiday_periods WHERE region_id = ? ORDER BY start_date, end_date, name', id) };
|
||||
}
|
||||
|
||||
private checkName(country: string, name: string, id: number) {
|
||||
if (this.db.get('SELECT id FROM school_holiday_regions WHERE country = ? AND name = ? COLLATE NOCASE AND id != ?', country, name, id)) {
|
||||
throw new ConflictException('A region with this name already exists');
|
||||
}
|
||||
}
|
||||
|
||||
private writePeriods(id: number, holidays: SchoolHolidayPeriod[]) {
|
||||
this.db.run('DELETE FROM school_holiday_periods WHERE region_id = ?', id);
|
||||
const insert = this.db.prepare('INSERT INTO school_holiday_periods (region_id, name, start_date, end_date) VALUES (?, ?, ?, ?)');
|
||||
for (const holiday of holidays) insert.run(id, holiday.name, holiday.startDate, holiday.endDate);
|
||||
}
|
||||
|
||||
createRegion(country: string, body: SchoolHolidayRegionRequest) {
|
||||
return this.db.transaction(() => {
|
||||
this.country(country);
|
||||
this.checkName(country, body.name, 0);
|
||||
if (body.revision !== 0) throw new ConflictException('New regions must have revision zero');
|
||||
const inserted = this.db.run('INSERT INTO school_holiday_regions (country, name) VALUES (?, ?)', country, body.name);
|
||||
const id = Number(inserted.lastInsertRowid);
|
||||
this.writePeriods(id, body.holidays);
|
||||
return this.region(id);
|
||||
});
|
||||
}
|
||||
|
||||
updateRegion(id: number, body: SchoolHolidayRegionRequest) {
|
||||
return this.db.transaction(() => {
|
||||
const region = this.region(id);
|
||||
this.checkName(region.country, body.name, id);
|
||||
const updated = this.db.run('UPDATE school_holiday_regions SET name = ?, revision = revision + 1 WHERE id = ? AND revision = ?', body.name, id, body.revision);
|
||||
if (!updated.changes) throw new ConflictException('This region changed. Reopen it before saving again.');
|
||||
this.writePeriods(id, body.holidays);
|
||||
return this.region(id);
|
||||
});
|
||||
}
|
||||
|
||||
deleteRegion(id: number, revision: number) {
|
||||
return this.db.transaction(() => {
|
||||
const region = this.region(id);
|
||||
if (region.revision !== revision) throw new ConflictException('This region changed. Reload before deleting it.');
|
||||
if (this.db.get("SELECT id FROM vacay_holiday_calendars WHERE type = 'school_holiday' AND region = ? LIMIT 1", region.code)) {
|
||||
throw new ConflictException('This region is used by vacation calendars and cannot be deleted');
|
||||
}
|
||||
this.db.run('DELETE FROM school_holiday_periods WHERE region_id = ?', id);
|
||||
this.db.run('DELETE FROM school_holiday_regions WHERE id = ?', id);
|
||||
return { success: true };
|
||||
});
|
||||
}
|
||||
|
||||
holidays(id: number, year: string): SchoolHolidayPeriod[] {
|
||||
return this.region(id).holidays.filter(holiday => holiday.startDate <= `${year}-12-31` && holiday.endDate >= `${year}-01-01`);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, BadRequestException } from '@nestjs/common';
|
||||
import { manualSchoolRegionId } from '@trek/shared';
|
||||
import { RealtimeService } from '../realtime/realtime.service';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { NotificationsService } from '../notifications/notifications.service';
|
||||
@@ -578,6 +579,7 @@ export class VacayService {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
addHolidayCalendar(planId: number, region: string, label: string | null, color: string | undefined, sortOrder: number | undefined, socketId: string | undefined, type: 'public_holiday' | 'school_holiday' = 'public_holiday') {
|
||||
this.validateManualRegion(region, type);
|
||||
const result = this.db.run(
|
||||
'INSERT INTO vacay_holiday_calendars (plan_id, type, region, label, color, sort_order) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
planId, type, region, label || null, color || (type === 'school_holiday' ? '#a5f3fc' : '#fecaca'), sortOrder ?? 0
|
||||
@@ -595,6 +597,7 @@ export class VacayService {
|
||||
): VacayHolidayCalendar | null {
|
||||
const cal = this.db.get<VacayHolidayCalendar>('SELECT * FROM vacay_holiday_calendars WHERE id = ? AND plan_id = ?', calId, planId);
|
||||
if (!cal) return null;
|
||||
this.validateManualRegion(body.region ?? cal.region, body.type ?? cal.type);
|
||||
const { region, label, color, sort_order, type } = body;
|
||||
const updates: string[] = [];
|
||||
const params: (string | number | null)[] = [];
|
||||
@@ -620,6 +623,14 @@ export class VacayService {
|
||||
return true;
|
||||
}
|
||||
|
||||
private validateManualRegion(code: string, type: string) {
|
||||
if (!code.includes('-MANUAL-')) return;
|
||||
const id = manualSchoolRegionId(code);
|
||||
if (type !== 'school_holiday' || !id || !this.db.get('SELECT id FROM school_holiday_regions WHERE id = ? AND country = ?', id, code.slice(0, 2))) {
|
||||
throw new BadRequestException('Unknown manual school holiday region');
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// User colors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user