forked from openstatusHQ/openstatus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.ts
More file actions
88 lines (84 loc) · 2.89 KB
/
import.ts
File metadata and controls
88 lines (84 loc) · 2.89 KB
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
import { and, db, eq } from "@openstatus/db";
import { page } from "@openstatus/db/src/schema";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { previewImport, runImport } from "../service/import";
import { createTRPCRouter, protectedProcedure } from "../trpc";
export const importRouter = createTRPCRouter({
preview: protectedProcedure
.input(
z.object({
provider: z.enum(["statuspage", "betterstack", "instatus"]),
apiKey: z.string().min(1),
statuspagePageId: z.string().nullish(),
betterstackStatusPageId: z.string().nullish(),
instatusPageId: z.string().nullish(),
pageId: z.number().optional(),
}),
)
.mutation(async (opts) => {
return previewImport({
provider: opts.input.provider,
apiKey: opts.input.apiKey,
statuspagePageId: opts.input.statuspagePageId ?? undefined,
betterstackStatusPageId:
opts.input.betterstackStatusPageId ?? undefined,
instatusPageId: opts.input.instatusPageId ?? undefined,
workspaceId: opts.ctx.workspace.id,
pageId: opts.input.pageId,
limits: opts.ctx.workspace.limits,
});
}),
run: protectedProcedure
.input(
z.object({
provider: z.enum(["statuspage", "betterstack", "instatus"]),
apiKey: z.string().min(1),
pageId: z.number().optional(),
statuspagePageId: z.string().nullish(),
betterstackStatusPageId: z.string().nullish(),
instatusPageId: z.string().nullish(),
options: z
.object({
includeStatusReports: z.boolean().default(true),
includeSubscribers: z.boolean().default(false),
includeComponents: z.boolean().default(true),
includeMonitors: z.boolean().default(true),
})
.optional(),
}),
)
.mutation(async (opts) => {
// If pageId provided, verify it belongs to workspace
if (opts.input.pageId) {
const existing = await db
.select()
.from(page)
.where(
and(
eq(page.id, opts.input.pageId),
eq(page.workspaceId, opts.ctx.workspace.id),
),
)
.get();
if (!existing) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Page not found or does not belong to this workspace",
});
}
}
return runImport({
provider: opts.input.provider,
apiKey: opts.input.apiKey,
statuspagePageId: opts.input.statuspagePageId ?? undefined,
betterstackStatusPageId:
opts.input.betterstackStatusPageId ?? undefined,
instatusPageId: opts.input.instatusPageId ?? undefined,
workspaceId: opts.ctx.workspace.id,
pageId: opts.input.pageId,
options: opts.input.options,
limits: opts.ctx.workspace.limits,
});
}),
});