diff --git a/src/api/order/controllers/order.ts b/src/api/order/controllers/order.ts index 52be99d..d0d2830 100644 --- a/src/api/order/controllers/order.ts +++ b/src/api/order/controllers/order.ts @@ -68,6 +68,7 @@ export default factories.createCoreController("api::order.order", ({ strapi }) = where: { id }, ...orderDefaultParams }); + if (!order) return ctx.notFound("Order not found"); order.cart = order?.cart.map((item) => ({ ...item, product: { @@ -75,7 +76,6 @@ export default factories.createCoreController("api::order.order", ({ strapi }) = totalProductPrice: calculateTotalProductPrice(item.product) } })); - if (!order) return ctx.notFound("Order not found"); return await sanitize.contentAPI.output(order, strapi.getModel("api::order.order")); }, @@ -85,6 +85,7 @@ export default factories.createCoreController("api::order.order", ({ strapi }) = where: { uuid }, ...orderDefaultParams }); + if (!order) return ctx.notFound("Order not found"); order.cart = order?.cart.map((item) => ({ ...item, product: { @@ -92,7 +93,6 @@ export default factories.createCoreController("api::order.order", ({ strapi }) = totalProductPrice: calculateTotalProductPrice(item.product) } })); - if (!order) return ctx.notFound("Order not found"); return await sanitize.contentAPI.output(order, strapi.getModel("api::order.order")); }, @@ -245,6 +245,7 @@ export default factories.createCoreController("api::order.order", ({ strapi }) = const { uuid } = await sanitize.contentAPI.query(ctx.params, strapi.getModel("api::order.order")); strapi.log.info(`app:i:order-controller: Started invoice generation`); const orderUnsafe = (await strapi.service("api::order.order").findOneByUuid(uuid)) as Order; + if (!orderUnsafe) return ctx.notFound("Order not found"); if (!orderUnsafe.paymentAuthorised) { return ctx.locked("Payment not authorised, cannot generate invoice"); @@ -263,6 +264,7 @@ export default factories.createCoreController("api::order.order", ({ strapi }) = const { uuid } = await sanitize.contentAPI.query(ctx.params, strapi.getModel("api::order.order")); strapi.log.info(`app:i:order-controller: Started delivery note generation`); const orderUnsafe = (await strapi.service("api::order.order").findOneByUuid(uuid)) as Order; + if (!orderUnsafe) return ctx.notFound("Order not found"); if (!orderUnsafe.paymentAuthorised) { return ctx.locked("Payment not authorised, cannot generate delivery note"); diff --git a/src/api/order/services/order.ts b/src/api/order/services/order.ts index dd846be..a73f856 100644 --- a/src/api/order/services/order.ts +++ b/src/api/order/services/order.ts @@ -1,7 +1,7 @@ import path from "path"; import fs from "fs"; import { factories } from "@strapi/strapi"; -import { sanitize } from "@strapi/utils"; +import { errors, sanitize } from "@strapi/utils"; import { ID } from "@strapi/database/dist/types"; import { vatDecimal, vatIncludedDecimal, baseUrl, adminEmail, shopName, shopContactName, shopAddress, shopPhone, shopEmail, shopLogoUrl, shopSecondaryLogoUrl } from "../../../../config/constants"; import { calculateTotalProductPrice, productDefaultParams } from "../../product/services/product"; @@ -32,6 +32,7 @@ export default factories.createCoreService("api::order.order", ({ strapi }) => ( where: { uuid }, ...orderDefaultParams }); + if (!orderUnsafe) return null; orderUnsafe.cart = orderUnsafe?.cart.map((item: CartProduct) => ({ ...item, product: { @@ -80,6 +81,7 @@ export default factories.createCoreService("api::order.order", ({ strapi }) => ( sendInvoice: async (uuid: string, blob?: any) => { strapi.log.info(`app:i:order-service: - Sending invoice for order ${uuid}`); const oderUnsafe = (await strapi.service("api::order.order").findOneByUuid(uuid)) as Partial; + if (!oderUnsafe) throw new errors.NotFoundError("Order not found"); const shopContext = { shopName, shopContactName, shopAddress, shopPhone, shopEmail, shopLogoUrl, shopSecondaryLogoUrl }; try { @@ -119,6 +121,7 @@ export default factories.createCoreService("api::order.order", ({ strapi }) => ( sendDeliveryNote: async (uuid: string, blob?: ArrayBuffer) => { strapi.log.info(`app:i:order-service: - Sending delivery note for order ${uuid}`); const oderUnsafe = (await strapi.service("api::order.order").findOneByUuid(uuid)) as Partial; + if (!oderUnsafe) throw new errors.NotFoundError("Order not found"); const shopContext = { shopName, shopContactName, shopAddress, shopPhone, shopEmail, shopLogoUrl, shopSecondaryLogoUrl }; try { const emailConfig: MessageBody = {