1
+ import getLogbookType from "../utils/get-logbook-type.js" ;
2
+ import getTable from "../utils/get-table.js" ;
3
+ import insertTable from "../utils/insert-table.js" ;
4
+ import parseUserID from "../utils/parse-user-id.js" ;
5
+
1
6
export async function createLogbook ( req ) {
2
- try {
3
- const supabase = req . supabase ;
4
- const { type } = req . body ;
5
- const { data, error } = await supabase . from ( "logbooks" ) . insert ( { type : type } ) . select ( ) ;
6
- if ( error ) {
7
- throw new Error ( error . message ) ;
8
- }
9
- return data [ 0 ] ;
10
- } catch ( error ) {
11
- throw new Error ( error . message ) ;
12
- }
7
+ const supabase = req . supabase ;
8
+ const body = req . body
9
+ return insertTable ( supabase , "logbooks" , body ) ;
13
10
}
14
11
15
12
export async function getUserLogbooks ( req ) {
16
- try {
17
- const supabase = req . supabase ;
18
- const token = req . header ( "Authorization" ) ?. split ( " " ) [ 1 ] ;
19
- const userID = parseUserID ( token ) ;
20
- const { data, error } = await supabase . from ( "logbooks" ) . select ( ) . eq ( "user_id" , userID ) ;
21
- if ( error ) {
22
- throw new Error ( error . message ) ;
23
- }
24
- return data ;
25
- } catch ( error ) {
26
- throw new Error ( error . message ) ;
27
- }
28
- }
29
-
30
- function parseUserID ( token ) {
31
- const parts = token . split ( "." ) ;
32
- const decodedPayload = JSON . parse ( atob ( parts [ 1 ] ) ) ;
33
- const userID = decodedPayload [ "sub" ] ;
34
- return userID ;
13
+ const supabase = req . supabase ;
14
+ const token = req . header ( "Authorization" ) ?. split ( " " ) [ 1 ] ;
15
+ const userID = parseUserID ( token ) ;
16
+ return getTable ( supabase , "logbooks" , "user_id" , userID , "collection" ) ;
35
17
}
36
18
37
19
export async function getLogbook ( req ) {
38
- try {
39
- const supabase = req . supabase ;
40
- const { logbookID } = req . params ;
41
- const { data, error } = await supabase . from ( "logbooks" ) . select ( ) . eq ( "id" , logbookID ) ;
42
- if ( error ) {
43
- throw new Error ( error . message ) ;
44
- }
45
- return data [ 0 ] ;
46
- } catch ( error ) {
47
- throw new Error ( error . message ) ;
48
- }
20
+ const supabase = req . supabase ;
21
+ const { logbookID } = req . params ;
22
+ return getTable ( supabase , "logbooks" , "id" , logbookID , "resource" ) ;
49
23
}
50
24
51
25
export async function createLog ( req ) {
52
26
try {
53
27
const supabase = req . supabase ;
54
28
const { logbookID } = req . params ;
55
- const { type } = req . body ;
29
+ let body = req . body
30
+ body [ 'logbook_id' ] = logbookID
56
31
const logbookType = await getLogbookType ( logbookID , supabase ) ;
57
- if ( type !== logbookType ) {
58
- throw new Error ( "Error: Log does not match logbook type" ) ;
32
+ if ( body [ ' type' ] !== logbookType ) {
33
+ throw new Error ( `log type ' ${ body [ 'type' ] } ' does not match logbook type ' ${ logbookType } '` ) ;
59
34
}
60
- let data ;
61
- switch ( type ) {
35
+ switch ( body [ 'type' ] ) {
62
36
case "adult_cardiac_logs" :
63
- data = createAdultCardiacLog ( req , supabase ) ;
64
- break ;
37
+ return insertTable ( supabase , "adult_cardiac_logs" , body )
65
38
default :
66
- break ;
39
+ throw new Error ( `log and logbook type ' ${ type } ' are invalid` ) ;
67
40
}
68
- return data ;
69
41
} catch ( error ) {
70
- throw new Error ( error . message ) ;
71
- }
72
- }
73
-
74
- async function createAdultCardiacLog ( req , supabase ) {
75
- const { logbookID } = req . params ;
76
- const {
77
- type,
78
- caseNo,
79
- paitentID,
80
- surgeon,
81
- age,
82
- gender,
83
- orDate,
84
- reason,
85
- hpi,
86
- socialEtOH,
87
- socialDrugs,
88
- socialSmoking,
89
- socialMeds,
90
- socialAllergies,
91
- phmxHTN,
92
- phmxDMII,
93
- phmxDLP,
94
- phmxCVA,
95
- examWeight,
96
- examHeight,
97
- examBMI,
98
- examVeins,
99
- examAllenTest,
100
- examPulsesSixtyTwo,
101
- examPulsesSixtyFive,
102
- examPulsesSixtyEight,
103
- examPulsesSeventy,
104
- cathLink,
105
- invxEchoEF,
106
- invxEchoRVFx,
107
- invxWMA,
108
- invxAorta,
109
- invxValves,
110
- invxCXR,
111
- invxHb,
112
- invxW,
113
- invxPit,
114
- invxCr,
115
- ctLink,
116
- surgicalPlan,
117
- surgicalPlanFirstOperator,
118
- surgicalPlanIssueOR,
119
- surgicalPlanIssuePost,
120
- surgicalPlanaFlagForFU,
121
- operativeNotesCPBh,
122
- operativeNotesCPBm,
123
- operativeNotesXCh,
124
- operativeNotesXCm,
125
- operativeNotesCAh,
126
- operativeNotesCAm,
127
- myRole,
128
- postOperativeCourse,
129
- learningPointsKeyLessons,
130
- } = req . body ;
131
- const { data, error } = await supabase
132
- . from ( "adult_cardiac_logs" )
133
- . insert ( {
134
- logbook_id : logbookID ,
135
- type : type ,
136
- case_no : caseNo ,
137
- paitent_id : paitentID ,
138
- surgeon : surgeon ,
139
- age : age ,
140
- gender : gender ,
141
- or_date : orDate ,
142
- reason : reason ,
143
- hpi : hpi ,
144
- social_etoh : socialEtOH ,
145
- social_drugs : socialDrugs ,
146
- social_smoking : socialSmoking ,
147
- social_meds : socialMeds ,
148
- social_allergies : socialAllergies ,
149
- pmhx_htn : phmxHTN ,
150
- pmhx_dmii : phmxDMII ,
151
- pmhx_dlp : phmxDLP ,
152
- pmhx_cva : phmxCVA ,
153
- exam_weight : examWeight ,
154
- exam_height : examHeight ,
155
- exam_bmi : examBMI ,
156
- exam_veins : examVeins ,
157
- exam_allen_test : examAllenTest ,
158
- exam_pulses_sixtytwo : examPulsesSixtyTwo ,
159
- exam_pulses_sixtyfive : examPulsesSixtyFive ,
160
- exam_pulses_sixtyeight : examPulsesSixtyEight ,
161
- exam_pulses_seventy : examPulsesSeventy ,
162
- cath : cathLink ,
163
- invx_echo_ef : invxEchoEF ,
164
- invx_echo_rvfx : invxEchoRVFx ,
165
- invx_wma : invxWMA ,
166
- invx_aorta : invxAorta ,
167
- invx_valves : invxValves ,
168
- invx_cxr : invxCXR ,
169
- invx_hb : invxHb ,
170
- invx_w : invxW ,
171
- invx_pit : invxPit ,
172
- invx_cr : invxCr ,
173
- ct : ctLink ,
174
- surgical_plan : surgicalPlan ,
175
- surgical_plan_first_operator : surgicalPlanFirstOperator ,
176
- surgical_plan_issue_or : surgicalPlanIssueOR ,
177
- surgical_plan_issue_post : surgicalPlanIssuePost ,
178
- surgical_plan_flag_for_fu : surgicalPlanaFlagForFU ,
179
- operative_notes_cpb_h : operativeNotesCPBh ,
180
- operative_notes_cpb_m : operativeNotesCPBm ,
181
- operative_notes_xc_h : operativeNotesXCh ,
182
- operative_notes_xc_m : operativeNotesXCm ,
183
- operative_notes_ca_h : operativeNotesCAh ,
184
- operative_notes_ca_m : operativeNotesCAm ,
185
- my_role : myRole ,
186
- post_operative_course : postOperativeCourse ,
187
- learning_points_key_lessons : learningPointsKeyLessons ,
188
- } )
189
- . select ( ) ;
190
- if ( error ) {
191
- throw new Error ( error . message ) ;
192
- }
193
- return data [ 0 ] ;
194
- }
195
-
196
- async function getLogbookType ( logbookID , supabase ) {
197
- const { data, error } = await supabase . from ( "logbooks" ) . select ( ) . eq ( "id" , logbookID ) ;
198
- if ( error ) {
199
- throw new Error ( error . message ) ;
42
+ return { error : error . message } ;
200
43
}
201
- return data [ 0 ] [ "type" ] ;
202
44
}
203
45
204
46
export async function getLogbookLogs ( req ) {
205
- try {
206
- const supabase = req . supabase ;
207
- const { logbookID } = req . params ;
208
- const logbookType = await getLogbookType ( logbookID , supabase ) ;
209
- const { data, error } = await supabase . from ( logbookType ) . select ( ) ;
210
- if ( error ) {
211
- throw new Error ( error . message ) ;
212
- }
213
- return data ;
214
- } catch ( error ) {
215
- console . error ( error . message ) ;
216
- return { error : error . message }
217
- }
47
+ const supabase = req . supabase ;
48
+ const { logbookID } = req . params ;
49
+ const logbookType = await getLogbookType ( logbookID , supabase ) ;
50
+ return getTable ( supabase , logbookType , "logbook_id" , logbookID , "collection" ) ;
218
51
}
219
52
220
53
export async function getLog ( req ) {
221
- try {
222
- const supabase = req . supabase ;
223
- const { logbookID, logID } = req . params ;
224
- const logbookType = await getLogbookType ( logbookID , supabase ) ;
225
- const { data, error } = await supabase . from ( logbookType ) . select ( ) . eq ( "id" , logID ) ;
226
- if ( error ) {
227
- throw new Error ( error . message ) ;
228
- }
229
- return data [ 0 ] ;
230
- } catch ( error ) {
231
- console . error ( error . message ) ;
232
- return { error : error . message }
233
- }
234
- }
54
+ const supabase = req . supabase ;
55
+ const { logbookID, logID } = req . params ;
56
+ const logbookType = await getLogbookType ( logbookID , supabase ) ;
57
+ return getTable ( supabase , logbookType , "id" , logID , "resource" ) ;
58
+ }
0 commit comments