-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathroute.ts
More file actions
106 lines (96 loc) · 3.37 KB
/
Copy pathroute.ts
File metadata and controls
106 lines (96 loc) · 3.37 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
import { NextResponse, NextRequest } from "next/server";
import { createClient } from "~/utils/supabase/server";
import { getOrCreateEntity, ItemValidator } from "~/utils/supabase/dbUtils";
import { asPostgrestFailure } from "@repo/database/lib/contextFunctions";
import {
createApiResponse,
handleRouteError,
defaultOptionsHandler,
} from "~/utils/supabase/apiUtils";
import { type TablesInsert, Constants } from "@repo/database/dbTypes";
// eslint-disable-next-line @typescript-eslint/naming-convention
const { AgentType, Platform } = Constants.public.Enums;
type PlatformAccountDataInput = TablesInsert<"PlatformAccount">;
const accountValidator: ItemValidator<PlatformAccountDataInput> = (
account: any,
) => {
if (!account || typeof account !== "object")
return "Invalid request body: expected a JSON object.";
/* eslint-disable @typescript-eslint/naming-convention */
const {
name,
platform,
account_local_id,
write_permission,
active,
agent_type,
metadata,
dg_account,
} = account;
/* eslint-enable @typescript-eslint/naming-convention */
if (!name || typeof name !== "string" || name.trim() === "")
return "Missing or invalid name";
// This is not dry, to be rewritten with Drizzle/Zed.
if (!Platform.includes(platform)) return "Missing or invalid platform";
if (agent_type !== undefined && !AgentType.includes(agent_type))
return "Invalid agent_type";
if (write_permission !== undefined && typeof write_permission != "boolean")
return "write_permission must be boolean";
if (active !== undefined && typeof active != "boolean")
return "active must be boolean";
if (metadata !== undefined) {
if (typeof metadata != "string") return "metadata should be a JSON string";
else
try {
JSON.parse(metadata);
} catch (error) {
return "metadata should be a JSON string";
}
}
if (
!account_local_id ||
typeof account_local_id != "string" ||
account_local_id.trim() === ""
)
return "Missing or invalid account_local_id";
if (dg_account != undefined) {
if (typeof dg_account != "string")
return "dg_account should be a UUID string";
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(dg_account)) return "dg_account must be a valid UUID";
}
const keys = [
"name",
"platform",
"account_local_id",
"write_permission",
"active",
"agent_type",
"metadata",
"dg_account",
];
if (!Object.keys(account).every((key) => keys.includes(key)))
return "Invalid account object: extra keys";
return null;
};
export const POST = async (request: NextRequest): Promise<NextResponse> => {
const supabasePromise = createClient();
try {
const body = await request.json();
const error = accountValidator(body);
if (error !== null)
return createApiResponse(request, asPostgrestFailure(error, "invalid"));
const supabase = await supabasePromise;
const result = await getOrCreateEntity<"PlatformAccount">({
supabase,
tableName: "PlatformAccount",
insertData: body as PlatformAccountDataInput,
uniqueOn: ["account_local_id", "platform"],
});
return createApiResponse(request, result);
} catch (e: unknown) {
return handleRouteError(request, e, "/api/supabase/platfor-account");
}
};
export const OPTIONS = defaultOptionsHandler;