-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupabaseService.js
102 lines (83 loc) · 2.96 KB
/
supabaseService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const supabase = require('@supabase/supabase-js').createClient(process.env.SUPABASE_URL, process.env.SUPABASE_ANON_PSW, {
auth: {
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true
},
});
class SupabaseService {
init = async () => {
const { data, error } = await supabase.auth.signInWithPassword({
email: process.env.SUPABASE_MAIL,
password: process.env.SUPABASE_PSW,
})
if (error && error !== null) console.error(error);
}
create_game = async (players, rules, room) => {
const players_ids = await this.get_players_id(players.map((player) => player.email));
const game = {
name: room,
max_time: rules.max_time,
max_players: rules.max_players,
};
if (!players_ids) {
throw Error('error on getting the players');
}
for (let index = 0; index < players.length; index++) {
game[`player${index}`] = players_ids[index];
game[`player${index}_points`] = players[index].points;
}
const { data, error } = await supabase
.from('games')
.insert(game).select(`
id,
name,
max_time,
player0(email),
player0_points,
player1(email),
player1_points,
player2(email),
player2_points,
player3(email),
player3_points
`);
if (error && error !== null) {
console.error(error)
throw Error(error);
};
console.log(`Inserting the game: ${room}`);
return data;
}
get_players_id = async (players) => {
const { data, error } = await supabase
.from('profiles')
.select('id')
.in('email', players);
if (error && error !== null) {
console.error(error)
throw Error(error);
};
return data.map((player) => player.id);
}
check_friendship = async (admin, player) => {
const players_ids = await this.get_players_id([admin, player]);
if (!players_ids) {
throw Error('error on getting the players');
}
// for (let index = 0; index < players.length; index++) {
// game[`player${index}`] = players_ids[index].id;
// game[`player${index}_points`] = players[index].points;
// }
const { data, error } = await supabase
.from('friends')
.select('*', { count: 'estimated', head: true }).in('requester', players_ids).in('followed', players_ids).eq('is_confirmed', true);
if (error && error !== null) {
console.error(error)
throw Error(error);
};
console.log(`Check friendship of ${admin} & ${player}: ${data.count >= 0}`);
return data.count >= 0;
}
}
module.exports = SupabaseService;