Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ET member creation endpoint
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent 514b97c commit 8ada65b
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions functions/api/events-team/team-members/user.ts
@@ -0,0 +1,49 @@
export async function onRequestPost(context: RequestContext) {
const { id, name } = context.data.body;

if (
typeof id !== "string" ||
id.search(/\D/) ||
id.length > 19 ||
id.length < 17
)
return new Response('{"error":"Invalid user ID"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});

if (typeof name !== "string" || !name.length || name.length > 32)
return new Response('{"error":"Invalid name"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});

if (await context.env.DATA.get(`etmember_${id}`))
return new Response('{"error":"User is already a member"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});

const createdAt = Date.now();
const addingUser = context.data.current_user.id;

await context.env.DATA.put(
`etmember_${id}`,
JSON.stringify({
created_at: createdAt,
created_by: addingUser,
name,
}),
);
await context.env.D1.prepare(
"INSERT INTO et_members (created_at, created_by, id, name) VALUES (?, ?, ?, ?);",
)
.bind(createdAt, addingUser, id, name)
.run();
}

0 comments on commit 8ada65b

Please sign in to comment.