Skip to content

Instantly share code, notes, and snippets.

@diasjuniorr
Created December 23, 2023 23:35
Show Gist options
  • Save diasjuniorr/4256934a7b7512b92d0de03c0e75760e to your computer and use it in GitHub Desktop.
Save diasjuniorr/4256934a7b7512b92d0de03c0e75760e to your computer and use it in GitHub Desktop.
serve(async (req: Request): Promise<Response> => {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders });
}
return await handleRequest(req);
});
const handleRequest = async (req: Request): Promise<Response> => {
try {
const supabaseClient = createNewSupabaseClient(
Deno.env.get("SUPABASE_URL") ?? "",
Deno.env.get("SUPABASE_ANON_KEY") ?? "",
req.headers.get("Authorization")!
);
const {
data: { user }
} = await supabaseClient.auth.getUser();
if (!user) throwCustomError(USER_NOT_FOUND_ERROR);
authorizeUser(user, USER_ROLES.ADMIN);
const { id_pericia } = await parseRequest(req);
const { data, error } = await supabaseClient
.from("pericia_notes")
.select(`id, note, id_pericia, createdAt:created_at, updatedAt:updated_at, deletedAt:deleted_at, deleted`)
.eq("id_pericia", id_pericia)
.eq("deleted", false);
if (error) throwInternalCustomError({ message: error.message, toast_message: ERROR_FETCHING_PERICIA_NOTES_TOAST });
return new Response(JSON.stringify({ notes: data }), {
headers: { "Content-Type": "application/json", ...corsHeaders },
status: 200
});
} catch (error) {
return handleErrorResponse(error);
}
};
const parseRequest = async (req: Request): Promise<{ id_pericia: string }> => {
const { id_pericia }: ListPericiaNotesRequest = await req.json();
if (!id_pericia) throwCustomError(newMissingParametersError("id_pericia is required"));
return { id_pericia };
};
interface ListPericiaNotesRequest {
id_pericia: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment