Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revoke recurring grants #1446

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/boutique/backend/src/order/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,11 @@ export class OrderController implements IOrderController {
res: TypedResponse<CreateResponse>,
next: NextFunction
) => {
let order: Order
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get this right, but is this just a delete of the orderService in case the creation did not work due to having the OP client unauthorized?

If that is the case, Couldn't this just be a transaction that is rolled back, instead of a new specific delete call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I tried it out. First, I moved the instantBuy method into the transaction block, but it didn’t work. It seemed that the order created in the transaction block wasn’t fully completed, which caused some database errors in the instantBuy method. So, I had to implement it this way instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if you're certain about it.
However, maybe in this case the Order.transaction(){} blocks are no longer needed, just
try{
orderService.create()
}
catch{
orderService.delete()
}
?

const args = await validate(instantBuySchema, req.body)

const order = await Order.transaction(async (trx) => {
order = await Order.transaction(async (trx) => {
const newOrder = await this.orderService.create(
{ orderItems: args.products },
trx
Expand All @@ -241,11 +242,14 @@ export class OrderController implements IOrderController {
})
)
} catch (err) {
if (err instanceof OpenPaymentsClientError && err.status === 401)
if (err instanceof OpenPaymentsClientError && err.status === 401) {
next(
new Unauthorized('Instant-buy is not valid please initiate it again')
)
else next(err)
await Order.transaction(async (trx) => {
return await this.orderService.delete(order!.id, trx)
})
} else next(err)
}
}
}
8 changes: 8 additions & 0 deletions packages/boutique/backend/src/order/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface CreateParams {

export interface IOrderService {
create: (params: CreateParams, trx: TransactionOrKnex) => Promise<Order>
delete: (id: string, trx: TransactionOrKnex) => Promise<Order | undefined>
get: (id: string, userId?: string) => Promise<Order>
ensurePendingState: (id: string) => Promise<Order>
list: (userId: string) => Promise<Order[]>
Expand All @@ -35,6 +36,13 @@ export class OrderService implements IOrderService {
.returning('*')
}

public async delete(
id: string,
trx: TransactionOrKnex
): Promise<Order | undefined> {
return Order.query(trx).deleteById(id).returning('*').first()
}

public async get(id: string, userId?: string): Promise<Order> {
const row = Order.query().findById(id).withGraphFetched('orderItems')
if (userId) row.where('userId', '=', userId)
Expand Down
Loading