File size: 13,537 Bytes
57a889c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
import { z } from 'zod';
import { isDemoUser, getCurrentUser } from '../../services/authService';
import {
  getOwnPlan, getActivePlan, getActivePlanId, getPlanData,
  updatePlan, setUserColor,
  sendInvite as sendVacayInvite, acceptInvite, declineInvite, cancelInvite, dissolvePlan,
  getAvailableUsers,
  listYears, addYear, deleteYear,
  getEntries as getVacayEntries, toggleEntry, toggleCompanyHoliday,
  getStats as getVacayStats, updateStats as updateVacayStats,
  addHolidayCalendar, updateHolidayCalendar, deleteHolidayCalendar,
  getCountries as getHolidayCountries, getHolidays,
} from '../../services/vacayService';
import { isAddonEnabled } from '../../services/adminService';
import { ADDON_IDS } from '../../addons';
import {
  TOOL_ANNOTATIONS_READONLY, TOOL_ANNOTATIONS_WRITE,
  TOOL_ANNOTATIONS_DELETE, TOOL_ANNOTATIONS_NON_IDEMPOTENT,
  demoDenied, ok,
} from './_shared';
import { canRead, canWrite } from '../scopes';

export function registerVacayTools(server: McpServer, userId: number, scopes: string[] | null): void {
  const R = canRead(scopes, 'vacay');
  const W = canWrite(scopes, 'vacay');

  if (isAddonEnabled(ADDON_IDS.VACAY)) {
    if (R) server.registerTool(
      'get_vacay_plan',
      {
        description: "Get the current user's active vacation plan (own or joined).",
        inputSchema: {},
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async () => {
        const plan = getPlanData(userId);
        return ok({ plan });
      }
    );

    if (W) server.registerTool(
      'update_vacay_plan',
      {
        description: 'Update vacation plan settings (weekends blocking, holidays, carry-over).',
        inputSchema: {
          block_weekends: z.boolean().optional(),
          holidays_enabled: z.boolean().optional(),
          holidays_region: z.string().nullable().optional(),
          company_holidays_enabled: z.boolean().optional(),
          carry_over_enabled: z.boolean().optional(),
        },
        annotations: TOOL_ANNOTATIONS_WRITE,
      },
      async ({ block_weekends, holidays_enabled, holidays_region, company_holidays_enabled, carry_over_enabled }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        await updatePlan(planId, { block_weekends, holidays_enabled, holidays_region, company_holidays_enabled, carry_over_enabled }, undefined);
        return ok({ success: true });
      }
    );

    if (W) server.registerTool(
      'set_vacay_color',
      {
        description: "Set the current user's color in the vacation plan calendar.",
        inputSchema: {
          color: z.string().describe('Hex color e.g. #6366f1'),
        },
        annotations: TOOL_ANNOTATIONS_WRITE,
      },
      async ({ color }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        setUserColor(userId, planId, color, undefined);
        return ok({ success: true });
      }
    );

    if (R) server.registerTool(
      'get_available_vacay_users',
      {
        description: 'List users who can be invited to the current vacation plan.',
        inputSchema: {},
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async () => {
        const planId = getActivePlanId(userId);
        const users = getAvailableUsers(userId, planId);
        return ok({ users });
      }
    );

    if (W) server.registerTool(
      'send_vacay_invite',
      {
        description: 'Invite a user to join the vacation plan by their user ID.',
        inputSchema: {
          targetUserId: z.number().int().positive(),
        },
        annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
      },
      async ({ targetUserId }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const me = getCurrentUser(userId);
        if (!me) return { content: [{ type: 'text' as const, text: 'User not found.' }], isError: true };
        const result = sendVacayInvite(planId, userId, me.username, me.email, targetUserId);
        if (result.error) return { content: [{ type: 'text' as const, text: result.error }], isError: true };
        return ok({ success: true });
      }
    );

    if (W) server.registerTool(
      'accept_vacay_invite',
      {
        description: 'Accept a pending invitation to join another user\'s vacation plan.',
        inputSchema: {
          planId: z.number().int().positive(),
        },
        annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
      },
      async ({ planId }) => {
        if (isDemoUser(userId)) return demoDenied();
        const result = acceptInvite(userId, planId, undefined);
        if (result.error) return { content: [{ type: 'text' as const, text: result.error }], isError: true };
        return ok({ success: true });
      }
    );

    if (W) server.registerTool(
      'decline_vacay_invite',
      {
        description: 'Decline a pending vacation plan invitation.',
        inputSchema: {
          planId: z.number().int().positive(),
        },
        annotations: TOOL_ANNOTATIONS_WRITE,
      },
      async ({ planId }) => {
        declineInvite(userId, planId, undefined);
        return ok({ success: true });
      }
    );

    if (W) server.registerTool(
      'cancel_vacay_invite',
      {
        description: 'Cancel an outgoing invitation (owner cancels invite they sent).',
        inputSchema: {
          targetUserId: z.number().int().positive(),
        },
        annotations: TOOL_ANNOTATIONS_WRITE,
      },
      async ({ targetUserId }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        cancelInvite(planId, targetUserId);
        return ok({ success: true });
      }
    );

    if (W) server.registerTool(
      'dissolve_vacay_plan',
      {
        description: 'Dissolve the shared plan — all members are removed and everyone returns to their own individual plan.',
        inputSchema: {},
        annotations: TOOL_ANNOTATIONS_DELETE,
      },
      async () => {
        if (isDemoUser(userId)) return demoDenied();
        dissolvePlan(userId, undefined);
        return ok({ success: true });
      }
    );

    if (R) server.registerTool(
      'list_vacay_years',
      {
        description: 'List calendar years tracked in the current vacation plan.',
        inputSchema: {},
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async () => {
        const planId = getActivePlanId(userId);
        const years = listYears(planId);
        return ok({ years });
      }
    );

    if (W) server.registerTool(
      'add_vacay_year',
      {
        description: 'Add a calendar year to the vacation plan.',
        inputSchema: {
          year: z.number().int().min(2000).max(2100),
        },
        annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
      },
      async ({ year }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const years = addYear(planId, year, undefined);
        return ok({ years });
      }
    );

    if (W) server.registerTool(
      'delete_vacay_year',
      {
        description: 'Remove a calendar year from the vacation plan.',
        inputSchema: {
          year: z.number().int(),
        },
        annotations: TOOL_ANNOTATIONS_DELETE,
      },
      async ({ year }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const years = deleteYear(planId, year, undefined);
        return ok({ years });
      }
    );

    if (R) server.registerTool(
      'get_vacay_entries',
      {
        description: 'Get all vacation day entries for a plan and year.',
        inputSchema: {
          year: z.number().int(),
        },
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async ({ year }) => {
        const planId = getActivePlanId(userId);
        const entries = getVacayEntries(planId, String(year));
        return ok({ entries });
      }
    );

    if (W) server.registerTool(
      'toggle_vacay_entry',
      {
        description: 'Toggle a day on or off as a vacation day for the current user.',
        inputSchema: {
          date: z.string().describe('ISO date YYYY-MM-DD'),
        },
        annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
      },
      async ({ date }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const result = toggleEntry(userId, planId, date, undefined);
        return ok(result);
      }
    );

    if (W) server.registerTool(
      'toggle_company_holiday',
      {
        description: 'Toggle a date as a company holiday for the whole plan.',
        inputSchema: {
          date: z.string(),
          note: z.string().optional(),
        },
        annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
      },
      async ({ date, note }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const result = toggleCompanyHoliday(planId, date, note, undefined);
        return ok(result);
      }
    );

    if (R) server.registerTool(
      'get_vacay_stats',
      {
        description: 'Get vacation statistics for a specific year (days used, remaining, carried over).',
        inputSchema: {
          year: z.number().int(),
        },
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async ({ year }) => {
        const planId = getActivePlanId(userId);
        const stats = getVacayStats(planId, year);
        return ok({ stats });
      }
    );

    if (W) server.registerTool(
      'update_vacay_stats',
      {
        description: 'Update the vacation day allowance for a specific user and year.',
        inputSchema: {
          year: z.number().int(),
          vacationDays: z.number().int().min(0),
        },
        annotations: TOOL_ANNOTATIONS_WRITE,
      },
      async ({ year, vacationDays }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        updateVacayStats(userId, planId, year, vacationDays, undefined);
        return ok({ success: true });
      }
    );

    if (W) server.registerTool(
      'add_holiday_calendar',
      {
        description: 'Add a public holiday calendar (by region code) to the vacation plan.',
        inputSchema: {
          region: z.string().describe('Country/region code e.g. US, GB, DE'),
          label: z.string().nullable().optional(),
          color: z.string().optional(),
          sortOrder: z.number().int().optional(),
        },
        annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
      },
      async ({ region, label, color, sortOrder }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const calendar = addHolidayCalendar(planId, region, label ?? null, color, sortOrder, undefined);
        return ok({ calendar });
      }
    );

    if (W) server.registerTool(
      'update_holiday_calendar',
      {
        description: 'Update label or color for a holiday calendar.',
        inputSchema: {
          calendarId: z.number().int().positive(),
          label: z.string().nullable().optional(),
          color: z.string().optional(),
        },
        annotations: TOOL_ANNOTATIONS_WRITE,
      },
      async ({ calendarId, label, color }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        const cal = updateHolidayCalendar(calendarId, planId, { label, color }, undefined);
        if (!cal) return { content: [{ type: 'text' as const, text: 'Holiday calendar not found.' }], isError: true };
        return ok({ calendar: cal });
      }
    );

    if (W) server.registerTool(
      'delete_holiday_calendar',
      {
        description: 'Remove a holiday calendar from the vacation plan.',
        inputSchema: {
          calendarId: z.number().int().positive(),
        },
        annotations: TOOL_ANNOTATIONS_DELETE,
      },
      async ({ calendarId }) => {
        if (isDemoUser(userId)) return demoDenied();
        const planId = getActivePlanId(userId);
        deleteHolidayCalendar(calendarId, planId, undefined);
        return ok({ success: true });
      }
    );

    if (R) server.registerTool(
      'list_holiday_countries',
      {
        description: 'List countries available for public holiday calendars.',
        inputSchema: {},
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async () => {
        const result = await getHolidayCountries();
        if (result.error) return { content: [{ type: 'text' as const, text: result.error }], isError: true };
        return ok({ countries: result.data });
      }
    );

    if (R) server.registerTool(
      'list_holidays',
      {
        description: 'List public holidays for a country and year.',
        inputSchema: {
          country: z.string().describe('ISO 3166-1 alpha-2 code'),
          year: z.number().int(),
        },
        annotations: TOOL_ANNOTATIONS_READONLY,
      },
      async ({ country, year }) => {
        const result = await getHolidays(String(year), country);
        if (result.error) return { content: [{ type: 'text' as const, text: result.error }], isError: true };
        return ok({ holidays: result.data });
      }
    );
  }
}