rhythia-api 98.0.0 → 99.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.
package/.prettierrc.json CHANGED
@@ -1,6 +1,6 @@
1
- {
2
- "trailingComma": "es5",
3
- "tabWidth": 2,
4
- "semi": true,
5
- "singleQuote": false
6
- }
1
+ {
2
+ "trailingComma": "es5",
3
+ "tabWidth": 2,
4
+ "semi": true,
5
+ "singleQuote": false
6
+ }
@@ -1,74 +1,74 @@
1
- import { NextResponse } from "next/server";
2
- import z from "zod";
3
- import { Database } from "../types/database";
4
- import { protectedApi, validUser } from "../utils/requestUtils";
5
- import { supabase } from "../utils/supabase";
6
-
7
- export const Schema = {
8
- input: z.strictObject({
9
- session: z.string(),
10
- data: z.object({
11
- avatar_url: z.string().optional(),
12
- about_me: z.string().optional(),
13
- username: z.string().optional(),
14
- }),
15
- }),
16
- output: z.object({
17
- error: z.string().optional(),
18
- }),
19
- };
20
-
21
- export async function POST(request: Request): Promise<NextResponse> {
22
- return protectedApi({
23
- request,
24
- schema: Schema,
25
- authorization: validUser,
26
- activity: handler,
27
- });
28
- }
29
-
30
- export async function handler(
31
- data: (typeof Schema)["input"]["_type"]
32
- ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
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
- }
53
-
54
- const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
55
- id: userData.id,
56
- ...data.data,
57
- };
58
-
59
- const upsertResult = await supabase
60
- .from("profiles")
61
- .upsert(upsertPayload)
62
- .select();
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
-
73
- return NextResponse.json({});
74
- }
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { Database } from "../types/database";
4
+ import { protectedApi, validUser } from "../utils/requestUtils";
5
+ import { supabase } from "../utils/supabase";
6
+
7
+ export const Schema = {
8
+ input: z.strictObject({
9
+ session: z.string(),
10
+ data: z.object({
11
+ avatar_url: z.string().optional(),
12
+ about_me: z.string().optional(),
13
+ username: z.string().optional(),
14
+ }),
15
+ }),
16
+ output: z.object({
17
+ error: z.string().optional(),
18
+ }),
19
+ };
20
+
21
+ export async function POST(request: Request): Promise<NextResponse> {
22
+ return protectedApi({
23
+ request,
24
+ schema: Schema,
25
+ authorization: validUser,
26
+ activity: handler,
27
+ });
28
+ }
29
+
30
+ export async function handler(
31
+ data: (typeof Schema)["input"]["_type"]
32
+ ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
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
+ }
53
+
54
+ const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
55
+ id: userData.id,
56
+ ...data.data,
57
+ };
58
+
59
+ const upsertResult = await supabase
60
+ .from("profiles")
61
+ .upsert(upsertPayload)
62
+ .select();
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
+
73
+ return NextResponse.json({});
74
+ }
@@ -1,99 +1,99 @@
1
- import { NextResponse } from "next/server";
2
- import z from "zod";
3
- import { getUser, protectedApi } from "../utils/requestUtils";
4
- import { supabase } from "../utils/supabase";
5
-
6
- export const Schema = {
7
- input: z.strictObject({
8
- session: z.string(),
9
- page: z.number().default(1),
10
- }),
11
- output: z.object({
12
- error: z.string().optional(),
13
- total: z.number(),
14
- viewPerPage: z.number(),
15
- currentPage: z.number(),
16
- userPosition: z.number(),
17
- leaderboard: z
18
- .array(
19
- z.object({
20
- flag: z.string().nullable(),
21
- id: z.number(),
22
- username: z.string().nullable(),
23
- play_count: z.number().nullable(),
24
- skill_points: z.number().nullable(),
25
- total_score: z.number().nullable(),
26
- })
27
- )
28
- .optional(),
29
- }),
30
- };
31
-
32
- export async function POST(request: Request): Promise<NextResponse> {
33
- return protectedApi({
34
- request,
35
- schema: Schema,
36
- authorization: () => {},
37
- activity: handler,
38
- });
39
- }
40
-
41
- export async function handler(
42
- data: (typeof Schema)["input"]["_type"]
43
- ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
44
- const result = await getLeaderboard(data.page, data.session);
45
- return NextResponse.json(result);
46
- }
47
-
48
- const VIEW_PER_PAGE = 100;
49
-
50
- export async function getLeaderboard(page = 1, session) {
51
- const getUserData = await getUser(session);
52
-
53
- let leaderPosition = 0;
54
-
55
- if (getUserData) {
56
- let { data: queryData, error } = await supabase
57
- .from("profiles")
58
- .select("*")
59
- .eq("uid", getUserData.data.user.id)
60
- .single();
61
-
62
- if (queryData) {
63
- const { count: playersWithMorePoints, error: rankError } = await supabase
64
- .from("profiles")
65
- .select("*", { count: "exact", head: true })
66
- .gt("skill_points", queryData.skill_points);
67
-
68
- leaderPosition = (playersWithMorePoints || 0) + 1;
69
- }
70
- }
71
-
72
- const startPage = (page - 1) * VIEW_PER_PAGE;
73
- const endPage = startPage + VIEW_PER_PAGE - 1;
74
- console.log(startPage, endPage);
75
- const countQuery = await supabase
76
- .from("profiles")
77
- .select("*", { count: "exact", head: true });
78
-
79
- let { data: queryData, error } = await supabase
80
- .from("profiles")
81
- .select("*")
82
- .order("skill_points", { ascending: false })
83
- .range(startPage, endPage);
84
-
85
- return {
86
- total: countQuery.count || 0,
87
- viewPerPage: VIEW_PER_PAGE,
88
- currentPage: page,
89
- userPosition: leaderPosition,
90
- leaderboard: queryData?.map((user) => ({
91
- flag: user.flag,
92
- id: user.id,
93
- play_count: user.play_count,
94
- skill_points: user.skill_points,
95
- total_score: user.total_score,
96
- username: user.username,
97
- })),
98
- };
99
- }
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { getUser, protectedApi } from "../utils/requestUtils";
4
+ import { supabase } from "../utils/supabase";
5
+
6
+ export const Schema = {
7
+ input: z.strictObject({
8
+ session: z.string(),
9
+ page: z.number().default(1),
10
+ }),
11
+ output: z.object({
12
+ error: z.string().optional(),
13
+ total: z.number(),
14
+ viewPerPage: z.number(),
15
+ currentPage: z.number(),
16
+ userPosition: z.number(),
17
+ leaderboard: z
18
+ .array(
19
+ z.object({
20
+ flag: z.string().nullable(),
21
+ id: z.number(),
22
+ username: z.string().nullable(),
23
+ play_count: z.number().nullable(),
24
+ skill_points: z.number().nullable(),
25
+ total_score: z.number().nullable(),
26
+ })
27
+ )
28
+ .optional(),
29
+ }),
30
+ };
31
+
32
+ export async function POST(request: Request): Promise<NextResponse> {
33
+ return protectedApi({
34
+ request,
35
+ schema: Schema,
36
+ authorization: () => {},
37
+ activity: handler,
38
+ });
39
+ }
40
+
41
+ export async function handler(
42
+ data: (typeof Schema)["input"]["_type"]
43
+ ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
44
+ const result = await getLeaderboard(data.page, data.session);
45
+ return NextResponse.json(result);
46
+ }
47
+
48
+ const VIEW_PER_PAGE = 100;
49
+
50
+ export async function getLeaderboard(page = 1, session: string) {
51
+ const getUserData = await getUser({ session });
52
+
53
+ let leaderPosition = 0;
54
+
55
+ if (getUserData) {
56
+ let { data: queryData, error } = await supabase
57
+ .from("profiles")
58
+ .select("*")
59
+ .eq("uid", getUserData.data.user.id)
60
+ .single();
61
+
62
+ if (queryData) {
63
+ const { count: playersWithMorePoints, error: rankError } = await supabase
64
+ .from("profiles")
65
+ .select("*", { count: "exact", head: true })
66
+ .gt("skill_points", queryData.skill_points);
67
+
68
+ leaderPosition = (playersWithMorePoints || 0) + 1;
69
+ }
70
+ }
71
+
72
+ const startPage = (page - 1) * VIEW_PER_PAGE;
73
+ const endPage = startPage + VIEW_PER_PAGE - 1;
74
+ console.log(startPage, endPage);
75
+ const countQuery = await supabase
76
+ .from("profiles")
77
+ .select("*", { count: "exact", head: true });
78
+
79
+ let { data: queryData, error } = await supabase
80
+ .from("profiles")
81
+ .select("*")
82
+ .order("skill_points", { ascending: false })
83
+ .range(startPage, endPage);
84
+
85
+ return {
86
+ total: countQuery.count || 0,
87
+ viewPerPage: VIEW_PER_PAGE,
88
+ currentPage: page,
89
+ userPosition: leaderPosition,
90
+ leaderboard: queryData?.map((user) => ({
91
+ flag: user.flag,
92
+ id: user.id,
93
+ play_count: user.play_count,
94
+ skill_points: user.skill_points,
95
+ total_score: user.total_score,
96
+ username: user.username,
97
+ })),
98
+ };
99
+ }
package/api/getProfile.ts CHANGED
@@ -1,115 +1,117 @@
1
- import { geolocation } from "@vercel/edge";
2
- import { NextResponse } from "next/server";
3
- import z from "zod";
4
- import { Database } from "../types/database";
5
- import { protectedApi, validUser } from "../utils/requestUtils";
6
- import { supabase } from "../utils/supabase";
7
-
8
- export const Schema = {
9
- input: z.strictObject({
10
- session: z.string(),
11
- id: z.number().optional(),
12
- }),
13
- output: z.object({
14
- error: z.string().optional(),
15
- user: z
16
- .object({
17
- about_me: z.string().nullable(),
18
- avatar_url: z.string().nullable(),
19
- badges: z.any().nullable(),
20
- created_at: z.number().nullable(),
21
- flag: z.string().nullable(),
22
- id: z.number(),
23
- uid: z.string().nullable(),
24
- username: z.string().nullable(),
25
- verified: z.boolean().nullable(),
26
- play_count: z.number().nullable(),
27
- skill_points: z.number().nullable(),
28
- squares_hit: z.number().nullable(),
29
- total_score: z.number().nullable(),
30
- position: z.number().nullable(),
31
- })
32
- .optional(),
33
- }),
34
- };
35
-
36
- export async function POST(request: Request): Promise<NextResponse> {
37
- return protectedApi({
38
- request,
39
- schema: Schema,
40
- authorization: validUser,
41
- activity: handler,
42
- });
43
- }
44
-
45
- export async function handler(
46
- data: (typeof Schema)["input"]["_type"],
47
- req: Request
48
- ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
49
- let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
50
-
51
- // Fetch by id
52
- if (data.id !== undefined) {
53
- let { data: queryData, error } = await supabase
54
- .from("profiles")
55
- .select("*")
56
- .eq("id", data.id);
57
-
58
- console.log(profiles, error);
59
-
60
- if (!queryData?.length) {
61
- return NextResponse.json(
62
- {
63
- error: "User not found",
64
- },
65
- { status: 404 }
66
- );
67
- }
68
-
69
- profiles = queryData;
70
- } else {
71
- // Fetch by session id
72
- const user = (await supabase.auth.getUser(data.session)).data.user!;
73
-
74
- let { data: queryData, error } = await supabase
75
- .from("profiles")
76
- .select("*")
77
- .eq("uid", user.id);
78
-
79
- console.log(profiles, error);
80
- if (!queryData?.length) {
81
- const geo = geolocation(req);
82
- const data = await supabase
83
- .from("profiles")
84
- .upsert({
85
- uid: user.id,
86
- about_me: "",
87
- avatar_url: user.user_metadata.avatar_url,
88
- badges: ["Early Bird"],
89
- username: user.user_metadata.full_name,
90
- flag: (geo.country || "US").toUpperCase(),
91
- created_at: Date.now(),
92
- })
93
- .select();
94
-
95
- profiles = data.data!;
96
- } else {
97
- profiles = queryData;
98
- }
99
- }
100
-
101
- const user = profiles[0];
102
-
103
- // Query to count how many players have more skill points than the specific player
104
- const { count: playersWithMorePoints, error: rankError } = await supabase
105
- .from("profiles")
106
- .select("*", { count: "exact", head: true })
107
- .gt("skill_points", user.skill_points);
108
-
109
- return NextResponse.json({
110
- user: {
111
- ...user,
112
- position: (playersWithMorePoints || 0) + 1,
113
- },
114
- });
115
- }
1
+ import { geolocation } from "@vercel/edge";
2
+ import { NextResponse } from "next/server";
3
+ import z from "zod";
4
+ import { Database } from "../types/database";
5
+ import { protectedApi, validUser } from "../utils/requestUtils";
6
+ import { supabase } from "../utils/supabase";
7
+
8
+ export const Schema = {
9
+ input: z.strictObject({
10
+ session: z.string(),
11
+ id: z.number().optional(),
12
+ }),
13
+ output: z.object({
14
+ error: z.string().optional(),
15
+ user: z
16
+ .object({
17
+ about_me: z.string().nullable(),
18
+ avatar_url: z.string().nullable(),
19
+ badges: z.any().nullable(),
20
+ created_at: z.number().nullable(),
21
+ flag: z.string().nullable(),
22
+ id: z.number(),
23
+ uid: z.string().nullable(),
24
+ username: z.string().nullable(),
25
+ verified: z.boolean().nullable(),
26
+ play_count: z.number().nullable(),
27
+ skill_points: z.number().nullable(),
28
+ squares_hit: z.number().nullable(),
29
+ total_score: z.number().nullable(),
30
+ position: z.number().nullable(),
31
+ })
32
+ .optional(),
33
+ }),
34
+ };
35
+
36
+ export async function POST(request: Request): Promise<NextResponse> {
37
+ return protectedApi({
38
+ request,
39
+ schema: Schema,
40
+ authorization: () => {},
41
+ activity: handler,
42
+ });
43
+ }
44
+
45
+ export async function handler(
46
+ data: (typeof Schema)["input"]["_type"],
47
+ req: Request
48
+ ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
49
+ let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
50
+
51
+ // Fetch by id
52
+ if (data.id !== undefined) {
53
+ let { data: queryData, error } = await supabase
54
+ .from("profiles")
55
+ .select("*")
56
+ .eq("id", data.id);
57
+
58
+ console.log(profiles, error);
59
+
60
+ if (!queryData?.length) {
61
+ return NextResponse.json(
62
+ {
63
+ error: "User not found",
64
+ },
65
+ { status: 404 }
66
+ );
67
+ }
68
+
69
+ profiles = queryData;
70
+ } else {
71
+ // Fetch by session id
72
+ const user = (await supabase.auth.getUser(data.session)).data.user;
73
+
74
+ if (user) {
75
+ let { data: queryData, error } = await supabase
76
+ .from("profiles")
77
+ .select("*")
78
+ .eq("uid", user.id);
79
+
80
+ console.log(profiles, error);
81
+ if (!queryData?.length) {
82
+ const geo = geolocation(req);
83
+ const data = await supabase
84
+ .from("profiles")
85
+ .upsert({
86
+ uid: user.id,
87
+ about_me: "",
88
+ avatar_url: user.user_metadata.avatar_url,
89
+ badges: ["Early Bird"],
90
+ username: user.user_metadata.full_name,
91
+ flag: (geo.country || "US").toUpperCase(),
92
+ created_at: Date.now(),
93
+ })
94
+ .select();
95
+
96
+ profiles = data.data!;
97
+ } else {
98
+ profiles = queryData;
99
+ }
100
+ }
101
+ }
102
+
103
+ const user = profiles[0];
104
+
105
+ // Query to count how many players have more skill points than the specific player
106
+ const { count: playersWithMorePoints, error: rankError } = await supabase
107
+ .from("profiles")
108
+ .select("*", { count: "exact", head: true })
109
+ .gt("skill_points", user.skill_points);
110
+
111
+ return NextResponse.json({
112
+ user: {
113
+ ...user,
114
+ position: (playersWithMorePoints || 0) + 1,
115
+ },
116
+ });
117
+ }