Skip to content

Commit

Permalink
feat(friends): implement POST /user/friends endpoint for sending fr…
Browse files Browse the repository at this point in the history
…iend requests, and moved types file
  • Loading branch information
Likqez committed Dec 2, 2024
1 parent 3a824d2 commit 9479e62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server/api/v1/user/friends/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {serverSupabaseServiceRole, serverSupabaseUser} from "#supabase/server";
import type {FriendError, GetFriendParam, GetFriendsResponse} from "~/types/api/responses/user.friends.response";
import type {FriendError, GetFriendParam, GetFriendsResponse} from "~/types/api/user.friends";

// Not relative to userid because you should always only be able to see your own friends
// User id can be grabbed from the access token
Expand Down
38 changes: 37 additions & 1 deletion server/api/v1/user/friends/index.post.ts
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 {};
});

0 comments on commit 9479e62

Please sign in to comment.