-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroups.ts
More file actions
165 lines (142 loc) · 4.11 KB
/
Copy pathgroups.ts
File metadata and controls
165 lines (142 loc) · 4.11 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { SupabaseClient } from "@supabase/supabase-js"
export type GroupMemberWithProfile = {
group_id: string
user_id: string
role: "admin" | "coach" | "athlete"
target_time_sec: number | null
created_at: string
profiles: {
display_name: string | null
avatar_url: string | null
email: string
} | null
}
export type GroupMember = Omit<GroupMemberWithProfile, "profiles">
export type Group = {
id: string
name: string
description: string | null
target_event_name: string
target_event_date: string
target_distance_km: number
invite_code: string
created_by: string
created_at: string
}
export async function getGroupById(supabase: SupabaseClient, groupId: string): Promise<Group | null> {
const { data, error } = await supabase
.from("groups")
.select("*")
.eq("id", groupId)
.maybeSingle()
if (error) {
console.error("Error fetching group:", error)
return null
}
return data
}
export async function getGroupMember(
supabase: SupabaseClient,
groupId: string,
userId: string
): Promise<GroupMember | null> {
const { data, error } = await supabase
.from("group_members")
.select("group_id, user_id, role, target_time_sec, created_at")
.eq("group_id", groupId)
.eq("user_id", userId)
.maybeSingle()
if (error) {
console.error("Error fetching group member:", error)
return null
}
return data
}
export async function getGroupMembers(supabase: SupabaseClient, groupId: string): Promise<GroupMemberWithProfile[]> {
const { data, error } = await supabase
.from("group_members")
.select(`
group_id,
user_id,
role,
target_time_sec,
created_at,
profiles (
display_name,
avatar_url,
email
)
`)
.eq("group_id", groupId)
if (error) {
console.error("Error fetching group members:", error)
return []
}
return (data as any) || []
}
export async function getUserGroups(supabase: SupabaseClient, userId: string): Promise<Group[]> {
const { data, error } = await supabase
.from("group_members")
.select("groups (*)")
.eq("user_id", userId)
if (error) {
console.error("Error fetching user groups:", error)
return []
}
return (data?.map((row: any) => row.groups).filter(Boolean) as Group[]) || []
}
export async function getGroupActivities(
supabase: SupabaseClient,
groupId: string,
daysAgo: number = 28
): Promise<any[]> {
// 1. Obtenir les IDs des membres du groupe
const { data: members, error: membersError } = await supabase
.from("group_members")
.select("user_id")
.eq("group_id", groupId)
if (membersError || !members) {
console.error("Error fetching group member IDs:", membersError)
return []
}
const userIds = members.map((m) => m.user_id)
if (userIds.length === 0) return []
// 2. Obtenir les activités récentes de ces membres
const cutoff = new Date()
cutoff.setDate(cutoff.getDate() - daysAgo)
const { data: activities, error: activitiesError } = await supabase
.from("activities")
.select("*, profiles:user_id(display_name, avatar_url)")
.in("user_id", userIds)
.gte("start_date", cutoff.toISOString())
.order("start_date", { ascending: false })
if (activitiesError) {
console.error("Error fetching group activities:", activitiesError)
return []
}
return activities || []
}
export async function getGroupPlannedSessions(supabase: SupabaseClient, groupId: string): Promise<any[]> {
const { data, error } = await supabase
.from("group_planned_sessions")
.select("*")
.eq("group_id", groupId)
.order("planned_date", { ascending: true })
if (error) {
console.error("Error fetching group planned sessions:", error)
return []
}
return data || []
}
export async function getGroupTrainingBlocks(supabase: SupabaseClient, groupId: string): Promise<any[]> {
const { data, error } = await supabase
.from("group_training_blocks")
.select("*")
.eq("group_id", groupId)
.order("start_date", { ascending: true })
if (error) {
console.error("Error fetching group training blocks:", error)
return []
}
return data || []
}