File tree Expand file tree Collapse file tree
packages/boutique/backend/src/order Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -222,10 +222,11 @@ export class OrderController implements IOrderController {
222222 res : TypedResponse < CreateResponse > ,
223223 next : NextFunction
224224 ) => {
225+ let order : Order
225226 try {
226227 const args = await validate ( instantBuySchema , req . body )
227228
228- const order = await Order . transaction ( async ( trx ) => {
229+ order = await Order . transaction ( async ( trx ) => {
229230 const newOrder = await this . orderService . create (
230231 { orderItems : args . products } ,
231232 trx
@@ -241,11 +242,14 @@ export class OrderController implements IOrderController {
241242 } )
242243 )
243244 } catch ( err ) {
244- if ( err instanceof OpenPaymentsClientError && err . status === 401 )
245+ if ( err instanceof OpenPaymentsClientError && err . status === 401 ) {
245246 next (
246247 new Unauthorized ( 'Instant-buy is not valid please initiate it again' )
247248 )
248- else next ( err )
249+ await Order . transaction ( async ( trx ) => {
250+ return await this . orderService . delete ( order ! . id , trx )
251+ } )
252+ } else next ( err )
249253 }
250254 }
251255}
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ interface CreateParams {
1313
1414export interface IOrderService {
1515 create : ( params : CreateParams , trx : TransactionOrKnex ) => Promise < Order >
16+ delete : ( id : string , trx : TransactionOrKnex ) => Promise < Order | undefined >
1617 get : ( id : string , userId ?: string ) => Promise < Order >
1718 ensurePendingState : ( id : string ) => Promise < Order >
1819 list : ( userId : string ) => Promise < Order [ ] >
@@ -35,6 +36,13 @@ export class OrderService implements IOrderService {
3536 . returning ( '*' )
3637 }
3738
39+ public async delete (
40+ id : string ,
41+ trx : TransactionOrKnex
42+ ) : Promise < Order | undefined > {
43+ return Order . query ( trx ) . deleteById ( id ) . returning ( '*' ) . first ( )
44+ }
45+
3846 public async get ( id : string , userId ?: string ) : Promise < Order > {
3947 const row = Order . query ( ) . findById ( id ) . withGraphFetched ( 'orderItems' )
4048 if ( userId ) row . where ( 'userId' , '=' , userId )
You can’t perform that action at this time.
0 commit comments