99 "github.com/devmanishoffl/sabhyatam-orders/internal/model"
1010 "github.com/devmanishoffl/sabhyatam-orders/internal/store"
1111 "github.com/go-chi/chi/v5"
12+ "github.com/google/uuid"
1213)
1314
1415type Handler struct {
@@ -17,6 +18,11 @@ type Handler struct {
1718 cartClient * client.CartClient
1819}
1920
21+ func isValidUUID (id string ) bool {
22+ _ , err := uuid .Parse (id )
23+ return err == nil
24+ }
25+
2026func NewHandler (s * store.PGStore , pc * client.ProductClient , cc * client.CartClient ) * Handler {
2127 return & Handler {store : s , pclient : pc , cartClient : cc }
2228}
@@ -108,6 +114,13 @@ func (h *Handler) PrepareOrder(w http.ResponseWriter, r *http.Request) {
108114 return
109115 }
110116
117+ for _ , it := range orderItems {
118+ if err := h .pclient .ReserveStock (ctx , it .VariantID , it .Quantity ); err != nil {
119+ http .Error (w , "stock reservation failed" , http .StatusConflict )
120+ return
121+ }
122+ }
123+
111124 w .Header ().Set ("Content-Type" , "application/json" )
112125 json .NewEncoder (w ).Encode (map [string ]any {
113126 "order_id" : orderID ,
@@ -163,6 +176,12 @@ func (h *Handler) ConfirmOrder(w http.ResponseWriter, r *http.Request) {
163176// called ONLY by payments service
164177func (h * Handler ) MarkOrderPaid (w http.ResponseWriter , r * http.Request ) {
165178 orderID := chi .URLParam (r , "orderID" )
179+
180+ if ! isValidUUID (orderID ) {
181+ http .Error (w , "invalid order id" , http .StatusBadRequest )
182+ return
183+ }
184+
166185 if orderID == "" {
167186 http .Error (w , "order id required" , http .StatusBadRequest )
168187 return
@@ -189,7 +208,7 @@ func (h *Handler) MarkOrderPaid(w http.ResponseWriter, r *http.Request) {
189208
190209 // deduct stock
191210 for _ , it := range order .Items {
192- if err := h .pclient .DeductStock (ctx , it .VariantID , it .Quantity ); err != nil {
211+ if err := h .pclient .DeductReservedStock (ctx , it .VariantID , it .Quantity ); err != nil {
193212 http .Error (w , "stock deduction failed" , http .StatusBadGateway )
194213 return
195214 }
@@ -202,3 +221,29 @@ func (h *Handler) MarkOrderPaid(w http.ResponseWriter, r *http.Request) {
202221
203222 w .WriteHeader (http .StatusOK )
204223}
224+
225+ func (h * Handler ) GetOrderInternal (w http.ResponseWriter , r * http.Request ) {
226+ orderID := chi .URLParam (r , "orderID" )
227+ if orderID == "" {
228+ http .Error (w , "order id required" , http .StatusBadRequest )
229+ return
230+ }
231+
232+ if r .Header .Get ("X-INTERNAL-KEY" ) != os .Getenv ("INTERNAL_SERVICE_KEY" ) {
233+ http .Error (w , "unauthorized" , http .StatusUnauthorized )
234+ return
235+ }
236+
237+ order , err := h .store .GetOrder (r .Context (), orderID )
238+ if err != nil {
239+ http .Error (w , "order not found" , http .StatusNotFound )
240+ return
241+ }
242+
243+ _ = json .NewEncoder (w ).Encode (map [string ]any {
244+ "order_id" : order .ID ,
245+ "status" : order .Status ,
246+ "amount_cents" : order .TotalAmountCents ,
247+ "currency" : order .Currency ,
248+ })
249+ }
0 commit comments