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

Order fixes #17

Merged
merged 5 commits into from
Apr 18, 2024
Merged
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
55 changes: 30 additions & 25 deletions src/controllers/BookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,42 @@ 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;

const bookingCartModel = new BookingCartModel({
orderId: orderModel.id,
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);

bookingModel.bookingCartModel = bookingCartModel;
if (orderModel === null) {
throw new InvalidOrderIdError(schema.orderId);
}

orderModel.bookingModels = [...orderModel.bookingModels, bookingModel];
await this.orderRepository.updateOrder(orderModel);
primary = false;
}

const bookingCartModel = new BookingCartModel({
orderId: orderModel.id,
orderReference: bookingModel.resellerReference ?? orderModel.supplierReference,
primary,
});

bookingModel.bookingCartModel = bookingCartModel;

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<Booking> => {
Expand Down
2 changes: 1 addition & 1 deletion src/factories/OrderModelFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 13 additions & 10 deletions src/repositories/OrderRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default class OrderRepository implements IOrderRepository {
.getDB()
.run(
`
INSERT INTO booking (
INSERT INTO \`order\` (
id,
status,
supplierReference
supplierReference,
data
) VALUES (?, ?, ?, ?)
`,
Expand All @@ -43,7 +43,7 @@ export default class OrderRepository implements IOrderRepository {
.getDB()
.run(
`
UPDATE booking
UPDATE \`order\`
SET status = ?,
supplierReference = ?,
data = ?
Expand All @@ -62,17 +62,18 @@ export default class OrderRepository implements IOrderRepository {
await this.getOrderById(getOrderSchema.id);

public getOrderById = async (orderId: string): Promise<OrderModel> => {
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);
}

const order = JSON.parse(result.data) as Order;
this.handleExpiredOrder(order);

const orderModel = this.orderParser.parsePOJOToModel(order);
const upToDateBookingModels = this.getUpToDateBookingModels(orderModel);
orderModel.bookingModels = upToDateBookingModels;
orderModel.bookingModels = await upToDateBookingModels;

return orderModel;
};
Expand All @@ -83,13 +84,15 @@ export default class OrderRepository implements IOrderRepository {
}
};

private readonly getUpToDateBookingModels = (orderModel: OrderModel): BookingModel[] => {
private readonly getUpToDateBookingModels = async (orderModel: OrderModel): Promise<BookingModel[]> => {
const upToDateBookingModels: BookingModel[] = [];

orderModel.bookingModels.forEach(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;
};
Expand Down
2 changes: 1 addition & 1 deletion src/router/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { CapabilityId } from '@octocloud/types';

export async function parseCapabilities(ctx: Koa.Context, next: Koa.Next): Promise<void> {
const capabilities: string = ctx.get('Octo-Capabilities') ?? '';
ctx.capabilities = capabilities.split(',') as CapabilityId[];
ctx.capabilities = capabilities.replaceAll(' ', '').split(',') as CapabilityId[];
await next();
}
Loading