rhythia-api 76.0.0 → 78.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.
@@ -31,10 +31,28 @@ export async function handler(
31
31
  data: (typeof Schema)["input"]["_type"]
32
32
  ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
33
33
  const user = (await supabase.auth.getUser(data.session)).data.user!;
34
+ let userData: Database["public"]["Tables"]["profiles"]["Update"];
35
+
36
+ // Find user's entry
37
+ {
38
+ let { data: queryUserData, error } = await supabase
39
+ .from("profiles")
40
+ .select("*")
41
+ .eq("uid", user.id);
42
+
43
+ if (!queryUserData?.length) {
44
+ return NextResponse.json(
45
+ {
46
+ error: "User cannot be retrieved from session",
47
+ },
48
+ { status: 404 }
49
+ );
50
+ }
51
+ userData = queryUserData[0];
52
+ }
34
53
 
35
54
  const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
36
- uid: user.id,
37
- flag: "",
55
+ id: userData.id,
38
56
  ...data.data,
39
57
  };
40
58
 
@@ -43,5 +61,14 @@ export async function handler(
43
61
  .upsert(upsertPayload)
44
62
  .select();
45
63
 
64
+ if (upsertResult.status == 409) {
65
+ return NextResponse.json(
66
+ {
67
+ error: "Can't update, username might be used by someone else!",
68
+ },
69
+ { status: 404 }
70
+ );
71
+ }
72
+
46
73
  return NextResponse.json({});
47
74
  }
@@ -0,0 +1,68 @@
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { protectedApi, validUser } from "../utils/requestUtils";
4
+ import { supabase } from "../utils/supabase";
5
+
6
+ export const Schema = {
7
+ input: z.object({
8
+ session: z.string(),
9
+ page: z.number().optional(),
10
+ }),
11
+ output: z.object({
12
+ error: z.string().optional(),
13
+ total: z.number().optional(),
14
+ leaderboard: z
15
+ .array(
16
+ z.object({
17
+ flag: z.string().nullable(),
18
+ id: z.number(),
19
+ username: z.string().nullable(),
20
+ play_count: z.number().nullable(),
21
+ skill_points: z.number().nullable(),
22
+ total_score: z.number().nullable(),
23
+ })
24
+ )
25
+ .optional(),
26
+ }),
27
+ };
28
+
29
+ export async function POST(res: Response): Promise<NextResponse> {
30
+ return protectedApi({
31
+ response: res,
32
+ schema: Schema,
33
+ authorization: validUser,
34
+ activity: handler,
35
+ });
36
+ }
37
+
38
+ export async function handler(
39
+ data: (typeof Schema)["input"]["_type"]
40
+ ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
41
+ const range = [0, 100];
42
+ if (data.page) {
43
+ range[0] = 100 * data.page;
44
+ range[1] = range[0] + 100;
45
+ }
46
+
47
+ const countQuery = await supabase
48
+ .from("profiles")
49
+ .select("*", { count: "exact", head: true });
50
+
51
+ let { data: queryData, error } = await supabase
52
+ .from("profiles")
53
+ .select("*")
54
+ .order("skill_points", { ascending: false })
55
+ .range(range[0], range[1]);
56
+
57
+ return NextResponse.json({
58
+ total: countQuery.count || 0,
59
+ leaderboard: queryData?.map((user) => ({
60
+ flag: user.flag,
61
+ id: user.id,
62
+ play_count: user.play_count,
63
+ skill_points: user.skill_points,
64
+ total_score: user.total_score,
65
+ username: user.username,
66
+ })),
67
+ });
68
+ }
package/api/getProfile.ts CHANGED
@@ -26,6 +26,7 @@ export const Schema = {
26
26
  skill_points: z.number().nullable(),
27
27
  squares_hit: z.number().nullable(),
28
28
  total_score: z.number().nullable(),
29
+ position: z.number().nullable(),
29
30
  })
30
31
  .optional(),
31
32
  }),
@@ -46,7 +47,7 @@ export async function handler(
46
47
  let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
47
48
 
48
49
  // Fetch by id
49
- if (data.id) {
50
+ if (data.id !== undefined) {
50
51
  let { data: queryData, error } = await supabase
51
52
  .from("profiles")
52
53
  .select("*")
@@ -83,12 +84,21 @@ export async function handler(
83
84
  { status: 404 }
84
85
  );
85
86
  }
87
+ profiles = queryData;
86
88
  }
87
89
 
88
90
  const user = profiles[0];
91
+
92
+ // Query to count how many players have more skill points than the specific player
93
+ const { count: playersWithMorePoints, error: rankError } = await supabase
94
+ .from("profiles")
95
+ .select("*", { count: "exact", head: true })
96
+ .gt("skill_points", user.skill_points);
97
+
89
98
  return NextResponse.json({
90
99
  user: {
91
100
  ...user,
101
+ position: (playersWithMorePoints || 0) + 1,
92
102
  },
93
103
  });
94
104
  }
package/index.ts CHANGED
@@ -5,6 +5,11 @@ import { Schema as EditProfile } from "./api/editProfile"
5
5
  export { Schema as SchemaEditProfile } from "./api/editProfile"
6
6
  export const editProfile = handleApi({url:"/api/editProfile",...EditProfile})
7
7
 
8
+ // ./api/getLeaderboard.ts API
9
+ import { Schema as GetLeaderboard } from "./api/getLeaderboard"
10
+ export { Schema as SchemaGetLeaderboard } from "./api/getLeaderboard"
11
+ export const getLeaderboard = handleApi({url:"/api/getLeaderboard",...GetLeaderboard})
12
+
8
13
  // ./api/getProfile.ts API
9
14
  import { Schema as GetProfile } from "./api/getProfile"
10
15
  export { Schema as SchemaGetProfile } from "./api/getProfile"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rhythia-api",
3
- "version": "76.0.0",
3
+ "version": "78.0.0",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "update": "bun ./scripts/update.ts",