From 2ec936225ccb10a90d11f9cd22287ed20eb5d3cc Mon Sep 17 00:00:00 2001 From: Jeppe Bundgaard Date: Thu, 27 Nov 2025 10:25:53 +0100 Subject: [PATCH] Refactor booking notifications and product restriction logic - Enable department notification for new bookings. - Enhance Slack notification details for bookings, including customer, vehicle, and items. - Replace `isGuest` with `isProductDetailsRestricted` for better clarity in `productsRoute`. - Adjust product parsing to handle restricted product details consistently. --- .../nginx/app/objects/order_bookings_o.php | 29 ++++++++++-- services/nginx/app/routes/productsRoute.php | 44 ++++++++++++------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/services/nginx/app/objects/order_bookings_o.php b/services/nginx/app/objects/order_bookings_o.php index ca42d969..4606bb8b 100644 --- a/services/nginx/app/objects/order_bookings_o.php +++ b/services/nginx/app/objects/order_bookings_o.php @@ -100,7 +100,7 @@ class order_bookings_o extends db public function notifyNewBooking(): void { self::requireSelected(); - $department_notification = false; + $department_notification = true; $customer_notification = true; /** * Get the department, branding and customer @@ -130,8 +130,31 @@ class order_bookings_o extends db if ($deliverSlack) { // Construct email $message = "*" . $branding->name->value() . "*\n"; - $message .= "_New Booking Created (TEST)_\n\n"; - $message .= "*Customer:* " . $customer->getCustomerName($customer_array['customer_number']) . " (`" . $customer_array['customer_number'] . "`)\n"; + $message .= "Ny booking fra *" . $customer->getCustomerName($customer_array['customer_number']) . "* (Kundenr: " . $customer_array['customer_number'] . ", Bookingnr: " . $this->id . ")\n"; + $message .= "Dato: " . date('d-m-Y H:i', strtotime($this->datetime->value())) . "\n"; + $message .= "Køretøj: " . $this->reg_1->value() . (!empty($this->reg_2->value()) ? ", " . $this->reg_2->value() : "") . ( + !empty($this->reg_3->value()) ? ", " . $this->reg_3->value() : "" + ) ."\n"; + // If the booking has a note, add it to the message + if (!empty($this->note->value())) { + $message .= "Note: " . $this->note->value() . "\n"; + } + // If the booking has items, add them to the message + $items = array_map(function ($item) { + // Get the product name + $product = (new products_o())->select((int)$item['id']); + if (!$product->exists()) { + return null; + } + $item['name'] = $product->name->value(); + if (!isset($item['quantity'])) { + $item['quantity'] = 1; + } + return "- " . $item['quantity'] . " x " . $item['name']; + }, $this->items->value()); + if (!empty($items)) { + $message .= "Ydelser:\n" . implode("\n", array_filter($items)) . "\n"; + } // Send a notification to the department $slack = new slack(); $slack->send_department_booking_notification($department->id, $message); diff --git a/services/nginx/app/routes/productsRoute.php b/services/nginx/app/routes/productsRoute.php index 674c80f3..65d2cc48 100644 --- a/services/nginx/app/routes/productsRoute.php +++ b/services/nginx/app/routes/productsRoute.php @@ -131,15 +131,15 @@ class productsRoute // Check if the user is logged in global $response; $permission_node = 'list_products'; - $isGuest = true; + $isProductDetailsRestricted = true; if ($this->isAuthenticated()) { - $isGuest = false; + $isProductDetailsRestricted = false; $this->requirePermission($permission_node); } // Get the user object $user = (new authentication())->get_user(); // Set the user id to 0 if guest - $responsibleUserId = $isGuest ? 0 : $user->id; + $responsibleUserId = $isProductDetailsRestricted ? 0 : $user->id; function parseProduct($product, $isGuest): array { $tmpProduct = [ @@ -173,17 +173,27 @@ class productsRoute 'requires_note' => false, 'addons' => $tmpProduct['display_in_booking_form'] ? array_map(function ($option) { + if ($option['product']['display_in_booking_form'] === false) { + $option['product']['name'] = 'Login to view'; + $option['product']['description'] = ''; + $option['product']['economic_product_id'] = 0; + $option['product']['apply_category_discount'] = false; + $option['product']['requires_note'] = false; + $option['product']['restricted'] = true; + } $option['price'] = 0; $option['product']['price'] = 0; + $option['restricted'] = $option['product']['restricted'] ?? false; return $option; - }, $tmpProduct['addons']) : [] + }, $tmpProduct['addons']) : [], + 'restricted' => !$tmpProduct['display_in_booking_form'] ]; } return $isGuest ? $tmpProductGuest : $tmpProduct; } // Check if the request was successful - if ($user || $isGuest) { + if ($user || $isProductDetailsRestricted) { // Define the variables $customer = self::getCustomerIfProvided(); // This is only used if the customer_id parameter is provided $departmentId = self::getDepartmentIdIfProvided(); // This is only used if the department_id parameter is provided @@ -213,8 +223,8 @@ class productsRoute } else { // Get all products $products = (array)(new products_o())->listObjectsWithPaginationIfSet( - function ($product) use ($isGuest) { - return parseProduct($product, $isGuest); + function ($product) use ($isProductDetailsRestricted) { + return parseProduct($product, $isProductDetailsRestricted); } ); } @@ -224,10 +234,10 @@ class productsRoute // return parseProduct($product); // }, self::parseProductsPrice($products, $customer, $departmentId)) //); - $result = array_map(function ($product) use ($customer, $departmentId, $isGuest) { - $productArray = parseProduct($product, $isGuest); + $result = array_map(function ($product) use ($customer, $departmentId, $isProductDetailsRestricted) { + $productArray = parseProduct($product, $isProductDetailsRestricted); // Get the price of the product with the department pricing and customer discounts applied - $productArray = parseProduct(self::parseProductsPrice([$productArray], $customer, $departmentId)[0], $isGuest); + $productArray = parseProduct(self::parseProductsPrice([$productArray], $customer, $departmentId)[0], $isProductDetailsRestricted); // Get the options for the product $productArray['addons'] = self::parseOptionsPrice($productArray['addons'], $customer, $departmentId); // Return the product with the updated price @@ -243,7 +253,7 @@ class productsRoute // Return the product $response->success( parseProduct( - (new products_o())->select((int)self::getParameter('id'))->asArray(), $isGuest + (new products_o())->select((int)self::getParameter('id'))->asArray(), $isProductDetailsRestricted ) ); } @@ -261,8 +271,8 @@ class productsRoute $products = (new products_o())->applyDepartmentPricing((array)$products, (int)$data['department_id']); } $response->success( - array_map(function ($product) use ($isGuest) { - return parseProduct($product, $isGuest); + array_map(function ($product) use ($isProductDetailsRestricted) { + return parseProduct($product, $isProductDetailsRestricted); }, $products) ); } @@ -279,9 +289,9 @@ class productsRoute // Return the list of products $response->success( (new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet( - function ($product) use ($isGuest, $departmentSpecificProductIds) { + function ($product) use ($isProductDetailsRestricted, $departmentSpecificProductIds) { // Only include products that are in the department specific product ids - return parseProduct($product, $isGuest); + return parseProduct($product, $isProductDetailsRestricted); }, (new products_o())->forceRestrictFilters([ 'id' => $departmentSpecificProductIds, @@ -291,8 +301,8 @@ class productsRoute } // Return the list of products $response->success( - (new products_o())->listObjectsWithPaginationIfSet(function ($product) use ($isGuest) { - return parseProduct($product, $isGuest); + (new products_o())->listObjectsWithPaginationIfSet(function ($product) use ($isProductDetailsRestricted) { + return parseProduct($product, $isProductDetailsRestricted); }) ); } else {