@@ -9,6 +9,10 @@ import { PaymentMethod } from "@prisma/client";
9
9
10
10
import { redirect } from "next/navigation" ;
11
11
12
+ import { Knock } from "@knocklabs/node" ;
13
+
14
+ const knock = new Knock ( process . env . KNOCK_SECRET_KEY ) ;
15
+
12
16
//TODO: When paying with a card and calling an API, the paid value should be set to true upon success card payment
13
17
14
18
type AddBookingArgs = {
@@ -94,5 +98,50 @@ export async function addBookings({ bookings, payMethod }: AddBookingArgs) {
94
98
data : modifiedBookings ,
95
99
} ) ;
96
100
101
+ // Notifications object
102
+ const recipients : {
103
+ id : string ;
104
+ name : string ;
105
+ email : string ;
106
+ } [ ] = [ ] ;
107
+
108
+ // Find instructors where instructor.id is found in instructorIds[]
109
+ // then find users associtated with each instructor for you to be able to show notification to the user
110
+ const instructors = await prisma . instructor . findMany ( {
111
+ where : {
112
+ id : { in : Array . from ( instructorIds ) } , // Convert Set to array for prisma query
113
+ } ,
114
+ include : {
115
+ user : {
116
+ select : {
117
+ id : true ,
118
+ name : true ,
119
+ email : true ,
120
+ } ,
121
+ } ,
122
+ } ,
123
+ } ) ;
124
+ // Loop through each instructor and prepare recipient data
125
+ for ( const instructor of instructors ) {
126
+ recipients . push ( {
127
+ id : instructor . user . id ,
128
+ name : instructor . user . name as string ,
129
+ email : instructor . user . email as string ,
130
+ } ) ;
131
+ }
132
+
133
+ if ( recipients . length > 0 ) {
134
+ await knock . workflows . trigger ( "booking-placed" , {
135
+ actor : {
136
+ id : userId ,
137
+ name : session ?. user ?. name ?? "Anonymous" ,
138
+ email : session ?. user ?. email ,
139
+ collection : "users" ,
140
+ } ,
141
+ recipients,
142
+ data : { } ,
143
+ } ) ;
144
+ }
145
+
97
146
redirect ( "/user/bookings" ) ;
98
147
}
0 commit comments