Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/functions/api/events-team/team-members/user.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (66 sloc)
1.74 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function onRequestDelete(context: RequestContext) { | |
const { id } = context.data.body; | |
if ( | |
typeof id !== "string" || | |
id.search(/\D/) || | |
id.length > 19 || | |
id.length < 17 | |
) | |
return new Response('{"error":"Invalid ID"}', { | |
headers: { | |
"content-type": "application/json", | |
}, | |
status: 400, | |
}); | |
await context.env.DATA.delete(`etmember_${id}`); | |
await context.env.D1.prepare("DELETE FROM et_members WHERE id = ?;") | |
.bind(id) | |
.run(); | |
return new Response(null, { | |
status: 204, | |
}); | |
} | |
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(); | |
} |