rhythia-api 75.0.0 → 77.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
@@ -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(),
@@ -42,20 +43,47 @@ export async function POST(res: Response): Promise<NextResponse> {
42
43
  export async function handler(
43
44
  data: (typeof Schema)["input"]["_type"]
44
45
  ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
45
- let { data: profiles, error } = await supabase
46
- .from("profiles")
47
- .select("*")
48
- .eq("id", data.id);
46
+ let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
49
47
 
50
- console.log(profiles, error);
48
+ // Fetch by id
49
+ if (data.id !== undefined) {
50
+ let { data: queryData, error } = await supabase
51
+ .from("profiles")
52
+ .select("*")
53
+ .eq("id", data.id);
51
54
 
52
- if (!profiles?.length) {
53
- return NextResponse.json(
54
- {
55
- error: "User not found",
56
- },
57
- { status: 404 }
58
- );
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
+ }
86
+ profiles = queryData;
59
87
  }
60
88
 
61
89
  const user = profiles[0];
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": "75.0.0",
3
+ "version": "77.0.0",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "update": "bun ./scripts/update.ts",