1
1
import supabase from '@/supabase' ;
2
2
import { Member } from '@/types/Member' ;
3
+ interface addGroupShedule {
4
+ title : string ;
5
+ description : string ;
6
+ startDate : string ;
7
+ endDate : string ;
8
+ memo : string ;
9
+ newMemberList : Member [ ] ;
10
+ }
3
11
4
- export const getOneGroupSchedule = async ( scheduleId : string ) => {
5
- const { data, error } = await supabase . from ( 'group_schedules' ) . select ( '*' ) . eq ( 'id' , + scheduleId ) ;
12
+ export const addGroupScheduleFetch = async ( { startDate, endDate, newMemberList, ...props } : addGroupShedule ) => {
13
+ // νμ¬ λ‘κ·ΈμΈλ μ¬μ©μλ₯Ό κ°μ Έμ¨λ€.
14
+ const { data : user , error : userError } = await supabase . auth . getUser ( ) ;
6
15
7
- if ( error ) {
8
- throw error ;
16
+ if ( userError ) {
17
+ throw userError ;
9
18
}
10
19
11
- if ( ! data ) {
12
- throw new Error ( 'λ°μ΄ν°λ₯Ό μ°Ύμ μ μμ΅λλ€.' ) ;
20
+ // κ·Έλ£Ήμ μμ±νλ€.
21
+ const { data : groupInfo , error : groupInfoError } = await supabase
22
+ . from ( 'groups' )
23
+ . insert ( {
24
+ name : props . title ,
25
+ open : true ,
26
+ start_date : startDate ,
27
+ end_date : endDate ,
28
+ } )
29
+ . select ( )
30
+ . single ( ) ;
31
+
32
+ if ( ! groupInfo || groupInfoError ) {
33
+ throw groupInfoError ;
13
34
}
14
35
15
- return data [ 0 ] ;
36
+ // κ·Έλ£Ήμ λ©€λ²λ₯Ό μΆκ°νλ€.
37
+ const newRelationSchema = newMemberList . map ( ( member ) => ( {
38
+ group_id : groupInfo . id ,
39
+ user_id : member . id ,
40
+ } ) ) ;
41
+ newRelationSchema . push ( {
42
+ group_id : groupInfo . id ,
43
+ user_id : user . user . id ,
44
+ } ) ;
45
+ const { error : groupMemberError } = await supabase . from ( 'group_user_ralations' ) . insert ( newRelationSchema ) ;
46
+ if ( groupMemberError ) {
47
+ throw groupMemberError ;
48
+ }
49
+ // κ·Έλ£Ή μΌμ μ μμ±νλ€.
50
+ const { data : groupScheduleData , error : groupScheduleError } = await supabase
51
+ . from ( 'group_schedules' )
52
+ . insert ( { ...props , start_date : startDate , end_date : endDate , group_id : groupInfo . id } )
53
+ . select ( )
54
+ . single ( ) ;
55
+
56
+ if ( groupScheduleError ) {
57
+ throw groupScheduleError ;
58
+ }
59
+ return groupScheduleData ;
16
60
} ;
17
61
18
62
interface UpdateGroupSchedule {
@@ -24,6 +68,25 @@ interface UpdateGroupSchedule {
24
68
scheduleId : string ;
25
69
}
26
70
71
+ interface UpdateGroupScheduleMember {
72
+ updatedMemberList : Member [ ] ;
73
+ groupId : string ;
74
+ }
75
+
76
+ export const getOneGroupSchedule = async ( scheduleId : string ) => {
77
+ const { data, error } = await supabase . from ( 'group_schedules' ) . select ( '*' ) . eq ( 'id' , + scheduleId ) ;
78
+
79
+ if ( error ) {
80
+ throw error ;
81
+ }
82
+
83
+ if ( ! data ) {
84
+ throw new Error ( 'λ°μ΄ν°λ₯Ό μ°Ύμ μ μμ΅λλ€.' ) ;
85
+ }
86
+
87
+ return data [ 0 ] ;
88
+ } ;
89
+
27
90
export const updateGroupSchedule = async ( {
28
91
name,
29
92
description,
@@ -50,11 +113,6 @@ export const updateGroupSchedule = async ({
50
113
return data ;
51
114
} ;
52
115
53
- interface UpdateGroupScheduleMember {
54
- updatedMemberList : Member [ ] ;
55
- groupId : string ;
56
- }
57
-
58
116
export const updateGroupScheduleMember = async ( { updatedMemberList, groupId } : UpdateGroupScheduleMember ) => {
59
117
const { error : deleteError } = await supabase . from ( 'group_user_ralations' ) . delete ( ) . eq ( 'group_id' , groupId ) ;
60
118
if ( deleteError ) {
0 commit comments