Ver Fonte

add the handle error on finished item

gdias há 4 meses atrás
pai
commit
7f0a362556
1 ficheiros alterados com 41 adições e 18 exclusões
  1. 41 18
      src/lib/component/EndCommand.svelte

+ 41 - 18
src/lib/component/EndCommand.svelte

@@ -24,6 +24,7 @@
 
 	let selectedCategory = null;
 	let isPaymentModalOpen = false;
+	let itemsOnKitchen = false;
 
 	let orderItems = [];
 	let products = [];
@@ -144,21 +145,36 @@
 			console.warn('Selecione uma forma de pagamento');
 			return;
 		}
-		const myHeaders = new Headers();
-		myHeaders.append('Authorization', `Bearer ${token}`);
-		myHeaders.append('Content-Type', 'application/json');
-		await fetch('https://dev2.mixtech.dev.br/order/update', {
-			method: 'POST',
-			headers: myHeaders,
-			body: JSON.stringify({
-				company_id: company,
-				order_id: orderId,
-				order_flag: selectedPaymentMethod,
-				status_status: 'Finalizada'
-			})
-		});
-		isPaymentModalOpen = false;
-		goto('/dashboard/tables');
+		try {
+			const myHeaders = new Headers();
+			myHeaders.append('Authorization', `Bearer ${token}`);
+			myHeaders.append('Content-Type', 'application/json');
+			const response = await fetch('https://dev2.mixtech.dev.br/order/update', {
+				method: 'POST',
+				headers: myHeaders,
+				body: JSON.stringify({
+					company_id: company,
+					order_id: orderId,
+					order_flag: selectedPaymentMethod,
+					status_status: 'Finalizada'
+				})
+			});
+
+			if (!response.ok) {
+				const errorData = await response.json().catch(() => null);
+				if (errorData.msg == 'Comanda possui itens de cozinha não finalizados.') {
+					itemsOnKitchen = true;
+					setInterval(() => {
+						itemsOnKitchen = false;
+					}, 7000);
+				}
+				return;
+			}
+			isPaymentModalOpen = false;
+			goto('/dashboard/tables');
+		} catch {
+			console.log('deu erro');
+		}
 	}
 </script>
 
@@ -273,6 +289,11 @@
 				>
 					Fechar Atendimento
 				</button>
+				{#if itemsOnKitchen}
+					<div class="mt-2 flex justify-center rounded-md bg-yellow-600 text-center">
+						<h1>Existe items em andamento na cozinha</h1>
+					</div>
+				{/if}
 			</div>
 		</div>
 	</div>
@@ -323,10 +344,12 @@
 							<span>{method}</span>
 						</label>
 					{/each}
-
-					<p>Método selecionado: {selectedPaymentMethod}</p>
 				</div>
-
+				{#if itemsOnKitchen}
+					<div class="mb-4 mt-2 w-full rounded-md bg-yellow-600 text-center">
+						<h1>Existe items em andamento na cozinha</h1>
+					</div>
+				{/if}
 				<div class="flex space-x-3">
 					<button
 						on:click={() => (isPaymentModalOpen = false)}