From 917d72985f3534d8f60d5e3b091f4f2d95381fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pansk=C3=BD?= Date: Wed, 17 Apr 2024 16:44:03 +0200 Subject: [PATCH 1/4] fix order repo --- src/repositories/OrderRepository.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/repositories/OrderRepository.ts b/src/repositories/OrderRepository.ts index 64f2f91..0ea16cf 100644 --- a/src/repositories/OrderRepository.ts +++ b/src/repositories/OrderRepository.ts @@ -22,7 +22,7 @@ export default class OrderRepository implements IOrderRepository { .getDB() .run( ` - INSERT INTO booking ( + INSERT INTO \`order\` ( id, status, supplierReference @@ -43,7 +43,7 @@ export default class OrderRepository implements IOrderRepository { .getDB() .run( ` - UPDATE booking + UPDATE \`order\` SET status = ?, supplierReference = ?, data = ? @@ -62,7 +62,7 @@ export default class OrderRepository implements IOrderRepository { await this.getOrderById(getOrderSchema.id); public getOrderById = async (orderId: string): Promise => { - const result = (await DB.getInstance().getDB().get('SELECT * FROM order WHERE id = ?', orderId)) ?? null; + const result = (await DB.getInstance().getDB().get('SELECT * FROM \`order\` WHERE id = ?', orderId)) ?? null; if (result === null) { throw new InvalidOrderIdError(orderId); From 55c829d4280ceeb8385068b6f5aa62a65d1ad710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pansk=C3=BD?= Date: Wed, 17 Apr 2024 17:39:13 +0200 Subject: [PATCH 2/4] fix orders --- src/controllers/BookingController.ts | 46 +++++++++++++--------------- src/repositories/OrderRepository.ts | 3 +- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/controllers/BookingController.ts b/src/controllers/BookingController.ts index eeb4bf6..df6f6f8 100644 --- a/src/controllers/BookingController.ts +++ b/src/controllers/BookingController.ts @@ -64,34 +64,32 @@ export class BookingController implements IBookingController { ); this.checkRestrictions(bookingModel.optionModel, schema.unitItems); - if (capabilities.includes(CapabilityId.Cart)) { - let orderModel; - let primary; - - if (schema.orderId === undefined) { - orderModel = OrderModelFactory.createByBooking(bookingModel, schema); - await this.orderRepository.createOrder(orderModel); - primary = true; - } else { - orderModel = await this.orderRepository.getOrderById(schema.orderId); - - if (orderModel === null) { - throw new InvalidOrderIdError(schema.orderId); - } - - orderModel.bookingModels = [...orderModel.bookingModels, bookingModel]; - await this.orderRepository.updateOrder(orderModel); - primary = false; - } + let orderModel; + let primary; + + if (schema.orderId === undefined) { + orderModel = OrderModelFactory.createByBooking(bookingModel, schema); + await this.orderRepository.createOrder(orderModel); + primary = true; + } else { + orderModel = await this.orderRepository.getOrderById(schema.orderId); - const bookingCartModel = new BookingCartModel({ - orderId: orderModel.id, - primary, - }); + if (orderModel === null) { + throw new InvalidOrderIdError(schema.orderId); + } - bookingModel.bookingCartModel = bookingCartModel; + orderModel.bookingModels = [...orderModel.bookingModels, bookingModel]; + await this.orderRepository.updateOrder(orderModel); + primary = false; } + const bookingCartModel = new BookingCartModel({ + orderId: orderModel.id, + primary, + }); + + bookingModel.bookingCartModel = bookingCartModel; + await this.bookingRepository.createBooking(bookingModel); return this.bookingParser.parseModelToPOJOWithSpecificCapabilities(bookingModel, capabilities); diff --git a/src/repositories/OrderRepository.ts b/src/repositories/OrderRepository.ts index 0ea16cf..3193424 100644 --- a/src/repositories/OrderRepository.ts +++ b/src/repositories/OrderRepository.ts @@ -25,7 +25,7 @@ export default class OrderRepository implements IOrderRepository { INSERT INTO \`order\` ( id, status, - supplierReference + supplierReference, data ) VALUES (?, ?, ?, ?) `, @@ -67,6 +67,7 @@ export default class OrderRepository implements IOrderRepository { if (result === null) { throw new InvalidOrderIdError(orderId); } + const order = JSON.parse(result.data) as Order; this.handleExpiredOrder(order); From 6e43f1687519b16f0dc034ac21faa2988b4885a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pansk=C3=BD?= Date: Thu, 18 Apr 2024 13:40:24 +0200 Subject: [PATCH 3/4] fixes --- src/controllers/BookingController.ts | 9 ++++++++- src/factories/OrderModelFactory.ts | 2 +- src/repositories/OrderRepository.ts | 8 ++++---- src/router/middlewares.ts | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/controllers/BookingController.ts b/src/controllers/BookingController.ts index df6f6f8..a1b3229 100644 --- a/src/controllers/BookingController.ts +++ b/src/controllers/BookingController.ts @@ -85,6 +85,7 @@ export class BookingController implements IBookingController { const bookingCartModel = new BookingCartModel({ orderId: orderModel.id, + orderReference: bookingModel.resellerReference ?? orderModel.supplierReference, primary, }); @@ -92,7 +93,13 @@ export class BookingController implements IBookingController { await this.bookingRepository.createBooking(bookingModel); - return this.bookingParser.parseModelToPOJOWithSpecificCapabilities(bookingModel, capabilities); + const additionalCapabilities = capabilities; + + if (!capabilities.includes(CapabilityId.Cart)) { + additionalCapabilities.push(CapabilityId.Cart); + } + + return this.bookingParser.parseModelToPOJOWithSpecificCapabilities(bookingModel, additionalCapabilities); }; public confirmBooking = async (schema: ConfirmBookingSchema, capabilities: CapabilityId[]): Promise => { diff --git a/src/factories/OrderModelFactory.ts b/src/factories/OrderModelFactory.ts index d9de294..dbb4fdb 100644 --- a/src/factories/OrderModelFactory.ts +++ b/src/factories/OrderModelFactory.ts @@ -33,7 +33,7 @@ export abstract class OrderModelFactory { return this.orderModelGenerator.generateOrder({ orderData: { - id: bookingModel.bookingCartModel?.orderId ?? DataGenerator.generateUUID(), + id: DataGenerator.generateUUID(), supplierReference: bookingModel.supplierReference ?? DataGenerator.generateSupplierReference(), settlementMethod: 'settlementMethod', status, diff --git a/src/repositories/OrderRepository.ts b/src/repositories/OrderRepository.ts index 3193424..f8700db 100644 --- a/src/repositories/OrderRepository.ts +++ b/src/repositories/OrderRepository.ts @@ -73,7 +73,7 @@ export default class OrderRepository implements IOrderRepository { const orderModel = this.orderParser.parsePOJOToModel(order); const upToDateBookingModels = this.getUpToDateBookingModels(orderModel); - orderModel.bookingModels = upToDateBookingModels; + orderModel.bookingModels = await upToDateBookingModels; return orderModel; }; @@ -84,13 +84,13 @@ export default class OrderRepository implements IOrderRepository { } }; - private readonly getUpToDateBookingModels = (orderModel: OrderModel): BookingModel[] => { + private readonly getUpToDateBookingModels = async (orderModel: OrderModel): Promise => { const upToDateBookingModels: BookingModel[] = []; - orderModel.bookingModels.forEach(async (bookingModel) => { + await Promise.all(orderModel.bookingModels.map(async (bookingModel) => { const upToDateBookingModel = await this.BookingRepository.getBookingByUuid(bookingModel.uuid); upToDateBookingModels.push(upToDateBookingModel); - }); + })); return upToDateBookingModels; }; diff --git a/src/router/middlewares.ts b/src/router/middlewares.ts index 5045d10..8876d14 100644 --- a/src/router/middlewares.ts +++ b/src/router/middlewares.ts @@ -3,6 +3,6 @@ import { CapabilityId } from '@octocloud/types'; export async function parseCapabilities(ctx: Koa.Context, next: Koa.Next): Promise { const capabilities: string = ctx.get('Octo-Capabilities') ?? ''; - ctx.capabilities = capabilities.split(',') as CapabilityId[]; + ctx.capabilities = capabilities.replaceAll(' ', '').split(',') as CapabilityId[]; await next(); } From 4bbe7d7fec76ba0296cb0b954a417dea5bc9a3b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pansk=C3=BD?= Date: Thu, 18 Apr 2024 13:42:36 +0200 Subject: [PATCH 4/4] format --- src/repositories/OrderRepository.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/repositories/OrderRepository.ts b/src/repositories/OrderRepository.ts index f8700db..002562a 100644 --- a/src/repositories/OrderRepository.ts +++ b/src/repositories/OrderRepository.ts @@ -62,7 +62,7 @@ export default class OrderRepository implements IOrderRepository { await this.getOrderById(getOrderSchema.id); public getOrderById = async (orderId: string): Promise => { - const result = (await DB.getInstance().getDB().get('SELECT * FROM \`order\` WHERE id = ?', orderId)) ?? null; + const result = (await DB.getInstance().getDB().get('SELECT * FROM `order` WHERE id = ?', orderId)) ?? null; if (result === null) { throw new InvalidOrderIdError(orderId); @@ -87,10 +87,12 @@ export default class OrderRepository implements IOrderRepository { private readonly getUpToDateBookingModels = async (orderModel: OrderModel): Promise => { const upToDateBookingModels: BookingModel[] = []; - await Promise.all(orderModel.bookingModels.map(async (bookingModel) => { - const upToDateBookingModel = await this.BookingRepository.getBookingByUuid(bookingModel.uuid); - upToDateBookingModels.push(upToDateBookingModel); - })); + await Promise.all( + orderModel.bookingModels.map(async (bookingModel) => { + const upToDateBookingModel = await this.BookingRepository.getBookingByUuid(bookingModel.uuid); + upToDateBookingModels.push(upToDateBookingModel); + }), + ); return upToDateBookingModels; };