rhythia-api 74.0.0 → 76.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/api/getProfile.ts +46 -15
  2. package/package.json +1 -1
package/api/getProfile.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import { NextResponse } from "next/server";
2
2
  import z from "zod";
3
+ import { Database } from "../types/database";
3
4
  import { protectedApi, validUser } from "../utils/requestUtils";
4
5
  import { supabase } from "../utils/supabase";
5
6
 
6
7
  export const Schema = {
7
8
  input: z.object({
8
9
  session: z.string(),
9
- id: z.number(),
10
+ id: z.number().optional(),
10
11
  }),
11
12
  output: z.object({
12
13
  error: z.string().optional(),
@@ -21,6 +22,10 @@ export const Schema = {
21
22
  uid: z.string().nullable(),
22
23
  username: z.string().nullable(),
23
24
  verified: z.boolean().nullable(),
25
+ play_count: z.number().nullable(),
26
+ skill_points: z.number().nullable(),
27
+ squares_hit: z.number().nullable(),
28
+ total_score: z.number().nullable(),
24
29
  })
25
30
  .optional(),
26
31
  }),
@@ -38,20 +43,46 @@ export async function POST(res: Response): Promise<NextResponse> {
38
43
  export async function handler(
39
44
  data: (typeof Schema)["input"]["_type"]
40
45
  ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
41
- let { data: profiles, error } = await supabase
42
- .from("profiles")
43
- .select("*")
44
- .eq("id", data.id);
45
-
46
- console.log(profiles, error);
47
-
48
- if (!profiles?.length) {
49
- return NextResponse.json(
50
- {
51
- error: "User not found",
52
- },
53
- { status: 404 }
54
- );
46
+ let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
47
+
48
+ // Fetch by id
49
+ if (data.id) {
50
+ let { data: queryData, error } = await supabase
51
+ .from("profiles")
52
+ .select("*")
53
+ .eq("id", data.id);
54
+
55
+ console.log(profiles, error);
56
+
57
+ if (!queryData?.length) {
58
+ return NextResponse.json(
59
+ {
60
+ error: "User not found",
61
+ },
62
+ { status: 404 }
63
+ );
64
+ }
65
+
66
+ profiles = queryData;
67
+ } else {
68
+ // Fetch by session id
69
+ const user = (await supabase.auth.getUser(data.session)).data.user!;
70
+
71
+ let { data: queryData, error } = await supabase
72
+ .from("profiles")
73
+ .select("*")
74
+ .eq("uid", user.id);
75
+
76
+ console.log(profiles, error);
77
+
78
+ if (!queryData?.length) {
79
+ return NextResponse.json(
80
+ {
81
+ error: "User cannot be retrieved from session",
82
+ },
83
+ { status: 404 }
84
+ );
85
+ }
55
86
  }
56
87
 
57
88
  const user = profiles[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rhythia-api",
3
- "version": "74.0.0",
3
+ "version": "76.0.0",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "update": "bun ./scripts/update.ts",