-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(friends): implement POST
/user/friends
endpoint for sending fr…
…iend requests, and moved types file
- Loading branch information
Showing
2 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
// send friend invite | ||
// send friend invite | ||
import {z} from 'zod' | ||
import {serverSupabaseServiceRole, serverSupabaseUser} from "#supabase/server"; | ||
import type {SendFriendRequestParam} from "~/types/api/user.friends"; | ||
|
||
const userSchema = z.object({ | ||
receiver_id: z.string().uuid() | ||
}) | ||
|
||
export default defineEventHandler(async (event) => { | ||
// validate post-request body | ||
const result = await readValidatedBody(event, body => userSchema.safeParse(body)) | ||
if (!result.success) { | ||
setResponseStatus(event, 400); | ||
return {error: result.error.issues}; | ||
} | ||
|
||
// Require user to be authenticated | ||
const user = await serverSupabaseUser(event); | ||
if (!user?.id) { | ||
setResponseStatus(event, 401); | ||
return {error: 'unauthenticated'}; | ||
} | ||
|
||
// Send request | ||
const client = serverSupabaseServiceRole(event); | ||
const param: SendFriendRequestParam = {sender_id: user.id, receiver_id: result.data.receiver_id}; | ||
const {error} = await client.rpc('send_friend_request', param as never); | ||
|
||
// Handle errors | ||
if (error) { | ||
setResponseStatus(event, 500); | ||
return {error: error.message}; | ||
} | ||
|
||
return {}; | ||
}); |