Skip to content

Commit

Permalink
refactor/db-changes (#73)
Browse files Browse the repository at this point in the history
* refactor(db): room status handling is now updated to work with new database schema

* refactor(backend): adding host as participant when creating the room and improved code

* chore(backend): removed useless import

* fix: import correct Alert component and fixed eslint issues

* refactor(backend): removed useless array in insert method of supabase client
  • Loading branch information
MAXOUXAX authored Jan 28, 2024
1 parent cb68854 commit 2a462a6
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 142 deletions.
114 changes: 52 additions & 62 deletions backend/src/room.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { adminSupabase } from "./server";
import createClient from "./lib/supabase";
import { PostgrestError } from "@supabase/supabase-js";

export async function getUserFromRequest(
request: FastifyRequest,
response: FastifyReply,
response: FastifyReply
) {
const supabase = createClient({ request, response });

Expand All @@ -23,6 +22,10 @@ export async function getUserProfileIdFromAccountId(accId: string) {
return { data: data.user_profile_id, error: null };
}

function unauthorizedResponse(response: FastifyReply) {
return response.code(401).send("User not logged in");
}

export async function createRoom(
name: string,
code: string,
Expand All @@ -32,16 +35,19 @@ export async function createRoom(
maxMusicPerUserDuration: number,
serviceId: string,
req: FastifyRequest,
rep: FastifyReply,
rep: FastifyReply
) {
const supabase = adminSupabase;

const user = await getUserFromRequest(req, rep);
if (!user.data.user) {
return rep.code(401).send("User not logged in");
}
const hostUserProfileId =
(await getUserProfileIdFromAccountId(user.data.user.id)).data || null;

// If the user is not logged in
if (!user.data.user) return unauthorizedResponse(rep);

// If we didn't manage to get the user profile id from the account id
const { data: hostUserProfileId } = await getUserProfileIdFromAccountId(
user.data.user.id
);
if (!hostUserProfileId) return unauthorizedResponse(rep);

const roomConfigRes = await supabase
.from("room_configurations")
Expand All @@ -54,63 +60,47 @@ export async function createRoom(
.select("id")
.single();

if (roomConfigRes.error) {
// If we didn't manage to create the room configuration
if (roomConfigRes.error)
return rep.code(roomConfigRes.status).send(roomConfigRes.error);
}

const configurationId = roomConfigRes.data.id;

const roomRes = await supabase
.from("active_rooms")
.insert([
{
name: name,
code: code,
configuration_id: configurationId,
host_user_profile_id: hostUserProfileId,
service_id: serviceId,
},
])
.select("id");

if (roomRes.error) {
return rep.code(roomRes.status).send(roomRes.error);
} else {
// TODO use roomid here (send)
return rep.code(201).send("Room created");
}
const { error, data, status } = await supabase
.from("rooms")
.insert({
name: name,
code: code,
configuration_id: configurationId,
host_user_profile_id: hostUserProfileId,
service_id: serviceId,
})
.select("id")
.single();

// If we didn't manage to create the room
if (error || !data) return rep.code(status).send(error);

const { error: roomUserError } = await supabase.from("room_users").insert({
room_id: data.id,
profile_id: hostUserProfileId,
});

// If we didn't manage to add the host to the participants
if (error) return rep.code(500).send(roomUserError);

const response = {
error: null,
data: {
room_id: data.id,
},
};

// If we managed to create the room
return rep.code(201).send(response);
}

export function endRoom(roomId: string) {
let room: any = null;
const supabase = adminSupabase;

supabase
.from("active_rooms")
.delete()
.eq("id", roomId)
.select("*")
.then((res) => {
if (res.error) {
return res.error;
} else {
room = res.data[0];
console.log("Room ended");
}
});

supabase
.from("rooms")
.insert([
{
...room,
ended_at: new Date(),
},
])
.then((res) => {
if (res.error) {
return res.error;
} else {
console.log("Room added to rooms");
}
});
// TODO: Properly end room
// This will set the join code of this room to null, and set is_active to false
}
5 changes: 2 additions & 3 deletions backend/src/route/RoomPOST.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { createRoom } from "../room";
import * as repl from "repl";

interface BodyParams {
name: string;
Expand Down Expand Up @@ -34,7 +33,7 @@ function extractFromRequest(req: FastifyRequest): BodyParams {

export default async function RoomPOST(
req: FastifyRequest,
reply: FastifyReply,
reply: FastifyReply
) {
const roomOptions = extractFromRequest(req);

Expand All @@ -47,6 +46,6 @@ export default async function RoomPOST(
roomOptions.maxMusicDuration,
roomOptions.service,
req,
reply,
reply
);
}
2 changes: 0 additions & 2 deletions commons/database-types-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Database } from "./database-types";
// prettier-ignore
export type ActiveRoom = Database["public"]["Tables"]["active_rooms"]["Row"];
// prettier-ignore
export type BoundService = Database["public"]["Tables"]["bound_services"]["Row"];
// prettier-ignore
export type FriendList = Database["public"]["Tables"]["friends_list"]["Row"];
Expand Down
66 changes: 9 additions & 57 deletions commons/database-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,6 @@ export type Json =
export interface Database {
public: {
Tables: {
active_rooms: {
Row: {
code: string;
configuration_id: string | null;
created_at: string;
host_user_profile_id: string | null;
id: string;
name: string;
service_id: string | null;
};
Insert: {
code: string;
configuration_id?: string | null;
created_at?: string;
host_user_profile_id?: string | null;
id?: string;
name: string;
service_id?: string | null;
};
Update: {
code?: string;
configuration_id?: string | null;
created_at?: string;
host_user_profile_id?: string | null;
id?: string;
name?: string;
service_id?: string | null;
};
Relationships: [
{
foreignKeyName: "active_rooms_configuration_id_fkey";
columns: ["configuration_id"];
isOneToOne: false;
referencedRelation: "room_configurations";
referencedColumns: ["id"];
},
{
foreignKeyName: "active_rooms_host_user_profile_id_fkey";
columns: ["host_user_profile_id"];
isOneToOne: false;
referencedRelation: "user_profile";
referencedColumns: ["user_profile_id"];
},
{
foreignKeyName: "active_rooms_service_id_fkey";
columns: ["service_id"];
isOneToOne: false;
referencedRelation: "streaming_services";
referencedColumns: ["service_id"];
}
];
};
bound_services: {
Row: {
access_token: string | null;
Expand Down Expand Up @@ -280,37 +228,40 @@ export interface Database {
{
foreignKeyName: "room_users_room_id_fkey";
columns: ["room_id"];
isOneToOne: true;
isOneToOne: false;
referencedRelation: "rooms";
referencedColumns: ["id"];
}
];
};
rooms: {
Row: {
code: string;
configuration_id: string | null;
created_at: string;
ended_at: string | null;
host_user_profile_id: string | null;
id: string;
is_active: boolean;
name: string;
service_id: string | null;
};
Insert: {
code: string;
configuration_id?: string | null;
created_at: string;
ended_at?: string | null;
created_at?: string;
host_user_profile_id?: string | null;
id?: string;
is_active?: boolean;
name: string;
service_id?: string | null;
};
Update: {
code?: string;
configuration_id?: string | null;
created_at?: string;
ended_at?: string | null;
host_user_profile_id?: string | null;
id?: string;
is_active?: boolean;
name?: string;
service_id?: string | null;
};
Expand Down Expand Up @@ -506,3 +457,4 @@ export type Enums<
: PublicEnumNameOrOptions extends keyof Database["public"]["Enums"]
? Database["public"]["Enums"][PublicEnumNameOrOptions]
: never;

3 changes: 2 additions & 1 deletion expo/app/(tabs)/rooms/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export default function RoomPage() {
useEffect(() => {
const fetchData = async () => {
const { data, error } = await supabase
.from("active_rooms")
.from("rooms")
.select("*")
.eq("id", id)
.eq("is_active", true)
.single();
if (error) {
Alert.alert(
Expand Down
Loading

0 comments on commit 2a462a6

Please sign in to comment.