|
| 1 | +<script> |
| 2 | + import { createTooltip } from '@melt-ui/svelte'; |
| 3 | + import { Truck } from 'lucide-svelte'; |
| 4 | +
|
| 5 | + const tbAppendToken = import.meta.env.VITE_TB_APPEND_TOKEN; |
| 6 | + const host = import.meta.env.VITE_TB_HOST; |
| 7 | +
|
| 8 | + const { |
| 9 | + elements: { trigger, content }, |
| 10 | + states: { open } |
| 11 | + } = createTooltip({ |
| 12 | + positioning: { |
| 13 | + placement: 'bottom' |
| 14 | + }, |
| 15 | + openDelay: 0, |
| 16 | + closeDelay: 0, |
| 17 | + closeOnPointerDown: false, |
| 18 | + forceVisible: true |
| 19 | + }); |
| 20 | +
|
| 21 | + const resetStock = async () => { |
| 22 | + try { |
| 23 | + console.log('Starting truncation...'); |
| 24 | + await truncateLatestAvailability(); // Wait for truncation to complete |
| 25 | + console.log('Truncation complete, now appending stock...'); |
| 26 | + appendStock(); // Call appendStock only if truncation is successful |
| 27 | + } catch (error) { |
| 28 | + console.error('Failed to reset stock:', error); // Log any errors |
| 29 | + } |
| 30 | + }; |
| 31 | +
|
| 32 | + const truncateLatestAvailability = async () => { |
| 33 | + try { |
| 34 | + const response = await fetch(`https://${host}/v0/datasources/mat_stock_latest/truncate`, { |
| 35 | + method: 'POST', |
| 36 | + headers: { Authorization: `Bearer ${tbAppendToken}` } |
| 37 | + }); |
| 38 | +
|
| 39 | + // Check if the response is okay |
| 40 | + if (!response.ok) { |
| 41 | + const errorText = await response.text(); // Get the error text if available |
| 42 | + throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`); |
| 43 | + } |
| 44 | +
|
| 45 | + // Handle 205 Reset Content response |
| 46 | + if (response.status === 205) { |
| 47 | + console.log('Truncation successful, no content to return.'); |
| 48 | + return; // No content to return, just exit the function |
| 49 | + } |
| 50 | +
|
| 51 | + // Attempt to parse the response as JSON for other status codes |
| 52 | + const res = await response.json(); |
| 53 | + console.log(res); |
| 54 | + return res; // Ensure this returns a value |
| 55 | + } catch (error) { |
| 56 | + console.error('Error truncating availability:', error); |
| 57 | + throw error; // Rethrow the error to be caught in resetStock |
| 58 | + } |
| 59 | + }; |
| 60 | +
|
| 61 | + const appendStock = () => { |
| 62 | + const data = [ |
| 63 | + { |
| 64 | + timestamp: new Date().toISOString(), |
| 65 | + product: '5d0cgAl5BTk', |
| 66 | + store: 'warehouse_1', |
| 67 | + amount: 10000 |
| 68 | + }, |
| 69 | + { |
| 70 | + timestamp: new Date().toISOString(), |
| 71 | + product: 'YY4YaHKh2jQ', |
| 72 | + store: 'warehouse_1', |
| 73 | + amount: 10000 |
| 74 | + }, |
| 75 | + { |
| 76 | + timestamp: new Date().toISOString(), |
| 77 | + product: 'p8Drpg_duLw', |
| 78 | + store: 'warehouse_1', |
| 79 | + amount: 10000 |
| 80 | + }, |
| 81 | + { |
| 82 | + timestamp: new Date().toISOString(), |
| 83 | + product: 'sZzx0cUDX98', |
| 84 | + store: 'warehouse_1', |
| 85 | + amount: 50000 |
| 86 | + }, |
| 87 | + { |
| 88 | + timestamp: new Date().toISOString(), |
| 89 | + product: 'xFmXLq_KJxg', |
| 90 | + store: 'warehouse_1', |
| 91 | + amount: 1000 |
| 92 | + }, |
| 93 | + { |
| 94 | + timestamp: new Date().toISOString(), |
| 95 | + product: '6cHumpSxTvs', |
| 96 | + store: 'warehouse_1', |
| 97 | + amount: 100 |
| 98 | + }, |
| 99 | + { |
| 100 | + timestamp: new Date().toISOString(), |
| 101 | + product: 'Fg15LdqpWrs', |
| 102 | + store: 'warehouse_1', |
| 103 | + amount: 85 |
| 104 | + }, |
| 105 | + { |
| 106 | + timestamp: new Date().toISOString(), |
| 107 | + product: 'Zu7A1GCSjZE', |
| 108 | + store: 'warehouse_1', |
| 109 | + amount: 0 |
| 110 | + }, |
| 111 | + { |
| 112 | + timestamp: new Date().toISOString(), |
| 113 | + product: 'fSdBxY0NxVI', |
| 114 | + store: 'warehouse_1', |
| 115 | + amount: 0 |
| 116 | + } |
| 117 | + ]; |
| 118 | + const ndjson = data.map((row) => JSON.stringify(row)).join('\n'); |
| 119 | + fetch(`https://${host}/v0/events?name=warehouse_stock`, { |
| 120 | + method: 'POST', |
| 121 | + body: ndjson, |
| 122 | + headers: { Authorization: `Bearer ${tbAppendToken}` } |
| 123 | + }) |
| 124 | + .then((res) => res.json()) |
| 125 | + .then((data) => console.log(data)) |
| 126 | + .catch((error) => console.error('Error appending stock:', error)); // Handle errors in appending |
| 127 | + }; |
| 128 | +
|
| 129 | +</script> |
| 130 | + |
| 131 | +<button |
| 132 | + class="relative flex h-8 w-8 items-center justify-center rounded-md border border-neutral-200 text-black transition-colors dark:border-neutral-700 dark:text-white" |
| 133 | + on:click={resetStock} |
| 134 | + use:melt={$trigger} |
| 135 | +> |
| 136 | + <Truck class="h-4 w-4" color="#fff" /> |
| 137 | +</button> |
| 138 | + |
| 139 | +{#if $open} |
| 140 | + <div use:melt={$content} class="z-50 rounded-lg bg-secondary shadow"> |
| 141 | + <p class="px-4 py-1 text-white text-sm">Reset stock</p> |
| 142 | + </div> |
| 143 | +{/if} |
0 commit comments