-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathadmin.api.v1.platform-notifications.ts
More file actions
51 lines (42 loc) · 1.75 KB
/
Copy pathadmin.api.v1.platform-notifications.ts
File metadata and controls
51 lines (42 loc) · 1.75 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
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
import { err, ok, type Result } from "neverthrow";
import { logger } from "~/services/logger.server";
import { authenticateAdminRequest } from "~/services/personalAccessToken.server";
import {
createPlatformNotification,
type CreatePlatformNotificationInput,
} from "~/services/platformNotifications.server";
type AdminUser = { id: string; admin: boolean };
type AuthError = { status: number; message: string };
async function authenticateAdmin(request: Request): Promise<Result<AdminUser, AuthError>> {
const result = await authenticateAdminRequest(request);
return result.ok
? ok({ id: result.user.id, admin: result.user.admin })
: err({ status: result.status, message: result.message });
}
export async function action({ request }: ActionFunctionArgs) {
if (request.method !== "POST") {
return json({ error: "Method not allowed" }, { status: 405 });
}
const authResult = await authenticateAdmin(request);
if (authResult.isErr()) {
const { status, message } = authResult.error;
return json({ error: message }, { status });
}
let body: unknown;
try {
body = await request.json();
} catch {
return json({ error: "Invalid JSON body" }, { status: 400 });
}
const result = await createPlatformNotification(body as CreatePlatformNotificationInput);
if (result.isErr()) {
const error = result.error;
if (error.type === "validation") {
return json({ error: "Validation failed", details: error.issues }, { status: 400 });
}
logger.error("Failed to create platform notification", { error });
return json({ error: "Something went wrong, please try again." }, { status: 500 });
}
return json(result.value, { status: 201 });
}