-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathApiWaitpointListPresenter.server.ts
More file actions
132 lines (110 loc) · 3.88 KB
/
ApiWaitpointListPresenter.server.ts
File metadata and controls
132 lines (110 loc) · 3.88 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
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
import { type RuntimeEnvironmentType, WaitpointTokenStatus } from "@trigger.dev/core/v3";
import { type RunEngineVersion, type WaitpointResolver } from "@trigger.dev/database";
import { z } from "zod";
import { CoercedDate } from "~/utils/zod";
import { ServiceValidationError } from "~/v3/services/baseService.server";
import { BasePresenter } from "./basePresenter.server";
import { type WaitpointListOptions, WaitpointListPresenter } from "./WaitpointListPresenter.server";
export const ApiWaitpointListSearchParams = z.object({
"page[size]": z.coerce.number().int().positive().min(1).max(100).optional(),
"page[after]": z.string().optional(),
"page[before]": z.string().optional(),
"filter[status]": z
.string()
.optional()
.transform((value, ctx) => {
if (!value) {
return undefined;
}
const statuses = value.split(",");
const parsedStatuses = statuses.map((status) => WaitpointTokenStatus.safeParse(status));
if (parsedStatuses.some((result) => !result.success)) {
const invalidStatuses: string[] = [];
for (const [index, result] of parsedStatuses.entries()) {
if (!result.success) {
invalidStatuses.push(statuses[index]);
}
}
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Invalid status values: ${invalidStatuses.join(", ")}`,
});
return z.NEVER;
}
const $statuses = parsedStatuses
.map((result) => (result.success ? result.data : undefined))
.filter(Boolean);
return Array.from(new Set($statuses));
}),
"filter[idempotencyKey]": z.string().optional(),
"filter[tags]": z
.string()
.optional()
.transform((value) => {
if (!value) return undefined;
return value.split(",");
}),
"filter[createdAt][period]": z.string().optional(),
"filter[createdAt][from]": CoercedDate,
"filter[createdAt][to]": CoercedDate,
});
type ApiWaitpointListSearchParams = z.infer<typeof ApiWaitpointListSearchParams>;
export class ApiWaitpointListPresenter extends BasePresenter {
public async call(
environment: {
id: string;
type: RuntimeEnvironmentType;
project: {
id: string;
engine: RunEngineVersion;
};
},
resolver: WaitpointResolver,
searchParams: ApiWaitpointListSearchParams
) {
return this.trace("call", async (span) => {
const options: WaitpointListOptions = {
environment,
resolver,
};
if (searchParams["page[size]"]) {
options.pageSize = searchParams["page[size]"];
}
if (searchParams["page[after]"]) {
options.cursor = searchParams["page[after]"];
options.direction = "forward";
}
if (searchParams["page[before]"]) {
options.cursor = searchParams["page[before]"];
options.direction = "backward";
}
if (searchParams["filter[status]"]) {
options.statuses = searchParams["filter[status]"];
}
if (searchParams["filter[idempotencyKey]"]) {
options.idempotencyKey = searchParams["filter[idempotencyKey]"];
}
if (searchParams["filter[tags]"]) {
options.tags = searchParams["filter[tags]"];
}
if (searchParams["filter[createdAt][period]"]) {
options.period = searchParams["filter[createdAt][period]"];
}
if (searchParams["filter[createdAt][from]"]) {
options.from = searchParams["filter[createdAt][from]"].getTime();
}
if (searchParams["filter[createdAt][to]"]) {
options.to = searchParams["filter[createdAt][to]"].getTime();
}
const presenter = new WaitpointListPresenter();
const result = await presenter.call(options);
if (!result.success) {
throw new ServiceValidationError(result.error);
}
return {
data: result.tokens,
pagination: result.pagination,
};
});
}
}