WINTER4000 commited on
Commit
5ddc4d7
·
verified ·
1 Parent(s): 40dc8d7

Strip Twist Gateway: redirect like IDT/GenScript

Browse files
Files changed (1) hide show
  1. dee/static/app.js +15 -389
dee/static/app.js CHANGED
@@ -1789,20 +1789,19 @@ async function synthesizeDna(dnaSeq, variantId, vendorKey) {
1789
  `);
1790
  if (!verifyOk) return;
1791
 
1792
- // Twist gets the in-app gateway flow: live quote + order placement
1793
- // against http://127.0.0.1:8765. GenScript + IDT remain external
1794
- // homepage redirects (no API integration exists for them here) — so
1795
- // for those vendors the entire user flow depends on the DNA actually
1796
- // landing on the clipboard before the new tab opens. If the clipboard
1797
- // write fails we MUST NOT open the vendor tab, otherwise the user
1798
- // pastes whatever they had on the clipboard before (likely a
1799
- // different variant) into the vendor's order form. Wrong DNA shipped,
1800
- // hard to detect, exactly the failure mode this audit is closing.
1801
- if (vendorKey === 'twist') {
1802
- await openTwistGateway(dnaSeq, variantId);
1803
- return;
1804
- }
1805
-
1806
  const vendor = VENDORS[vendorKey] || VENDORS.genscript;
1807
  const ok = await copyToClipboardOrToast(dnaSeq, `${variantId} DNA for ${vendor.name}`);
1808
  if (!ok) {
@@ -1820,378 +1819,6 @@ async function synthesizeDna(dnaSeq, variantId, vendorKey) {
1820
  }, 1100);
1821
  }
1822
 
1823
- // ============================================================= TWIST GATEWAY
1824
- // In-app quote + order flow against the local Twist Gateway FastAPI service
1825
- // (http://127.0.0.1:8765). The gateway is a faithful mock of Twist
1826
- // Bioscience's quoting + ordering API surface, so swapping the real Twist
1827
- // SDK in later is a base-URL change.
1828
-
1829
- const TWIST_BASE = 'http://127.0.0.1:8765';
1830
-
1831
- // ---- Error extraction ------------------------------------------------------
1832
- // FastAPI returns several response shapes depending on the failure mode:
1833
- // * 422 validation error: {"detail": [{"loc": [...], "msg": "...", ...}]}
1834
- // * 404/410 from our route: {"detail": "<string>"}
1835
- // * Other 5xx: free-form JSON
1836
- // And `fetch()` itself throws a TypeError for network/CORS failures with a
1837
- // plain English message. The naive `data?.detail || JSON.stringify(data)`
1838
- // coerces the array case into the literal string "[object Object]". This
1839
- // helper normalizes every shape to a useful human-readable string.
1840
- function extractErrorMessage(data) {
1841
- if (data == null) return 'Empty response body';
1842
- if (typeof data === 'string') return data;
1843
- if (Array.isArray(data)) {
1844
- return data.map(extractErrorMessage).join('; ');
1845
- }
1846
- if (typeof data === 'object') {
1847
- if ('detail' in data) {
1848
- const d = data.detail;
1849
- if (typeof d === 'string') return d;
1850
- if (Array.isArray(d)) {
1851
- return d.map((entry) => {
1852
- if (!entry || typeof entry !== 'object') return String(entry);
1853
- if (typeof entry.msg !== 'string') return JSON.stringify(entry);
1854
- const loc = Array.isArray(entry.loc) ? entry.loc.join('.') : '';
1855
- return loc ? `${loc}: ${entry.msg}` : entry.msg;
1856
- }).join('; ');
1857
- }
1858
- return JSON.stringify(d);
1859
- }
1860
- if (typeof data.message === 'string') return data.message;
1861
- return JSON.stringify(data);
1862
- }
1863
- return String(data);
1864
- }
1865
-
1866
- // Tiny fetch wrapper that ALWAYS resolves with a string `.message` on error
1867
- // (so the UI never has to defend against `[object Object]`).
1868
- async function gatewayFetch(path, options = {}) {
1869
- const url = `${TWIST_BASE}${path}`;
1870
- let response;
1871
- try {
1872
- response = await fetch(url, {
1873
- ...options,
1874
- headers: { 'Content-Type': 'application/json', ...(options.headers || {}) },
1875
- });
1876
- } catch (netErr) {
1877
- // TypeError from fetch — network failure, CORS preflight rejection,
1878
- // or DNS error. Build a clear "is the gateway running?" message.
1879
- const err = new Error(
1880
- `Network request to ${url} failed: ${netErr?.message || 'fetch threw without a message'}`,
1881
- );
1882
- err.kind = 'network';
1883
- err.cause = netErr;
1884
- throw err;
1885
- }
1886
-
1887
- let body = null;
1888
- const contentType = response.headers.get('content-type') || '';
1889
- if (contentType.includes('application/json')) {
1890
- try {
1891
- body = await response.json();
1892
- } catch (_) {
1893
- body = null;
1894
- }
1895
- } else {
1896
- body = await response.text();
1897
- }
1898
-
1899
- if (!response.ok) {
1900
- const message = extractErrorMessage(body) || `HTTP ${response.status} ${response.statusText}`;
1901
- const err = new Error(message);
1902
- err.kind = 'http';
1903
- err.status = response.status;
1904
- err.body = body;
1905
- throw err;
1906
- }
1907
- return body;
1908
- }
1909
-
1910
- // Per-modal-open state so the order form has the quote payload to send.
1911
- const twistState = { quote: null, variantId: null };
1912
-
1913
- async function openTwistGateway(dnaSeq, variantId) {
1914
- const modal = document.getElementById('twistModal');
1915
- const body = document.getElementById('twistModalBody');
1916
- const subtitle = document.getElementById('twistModalSub');
1917
- if (!modal || !body) return;
1918
-
1919
- twistState.quote = null;
1920
- twistState.variantId = variantId;
1921
- subtitle.textContent = `Live quote from the local Twist Gateway · ${variantId}`;
1922
- body.innerHTML = `
1923
- <div class="modal-loading">
1924
- <span class="identify-spinner"></span>
1925
- <span>Requesting quote from Twist Gateway…</span>
1926
- </div>
1927
- `;
1928
- modal.hidden = false;
1929
- bindModalDismiss();
1930
-
1931
- try {
1932
- const data = await gatewayFetch('/api/v1/twist/quote', {
1933
- method: 'POST',
1934
- body: JSON.stringify({
1935
- variants: [{ id: variantId, nucleotide_seq: dnaSeq }],
1936
- shipping_priority: 'standard',
1937
- customer_reference: `DEE-${variantId}`,
1938
- }),
1939
- });
1940
- twistState.quote = data;
1941
- renderQuotePanel(data);
1942
- } catch (err) {
1943
- renderGatewayError(body, err);
1944
- }
1945
- }
1946
-
1947
- function renderGatewayError(host, err) {
1948
- const isNetwork = err && err.kind === 'network';
1949
- const headline = isNetwork ? 'Twist Gateway unreachable' : 'Twist Gateway returned an error';
1950
- const detailLine = escapeHtml(err?.message || String(err));
1951
- const hint = isNetwork
1952
- ? `Is the gateway running at <code>${escapeHtml(TWIST_BASE)}</code>? ` +
1953
- `Start it with <code>uvicorn twist_gateway.main:app --port 8765</code> ` +
1954
- `from the <code>twist_gateway/</code> project (run <code>source .venv/bin/activate</code> first).`
1955
- : err?.status === 410
1956
- ? `The quote expired before checkout. Close this modal and click Twist again to fetch a fresh one.`
1957
- : err?.status === 422
1958
- ? `The request shape didn't match the gateway's schema. See the message above for the offending field.`
1959
- : `HTTP ${escapeHtml(String(err?.status || 'unknown'))} from the gateway. See message above for details.`;
1960
-
1961
- host.innerHTML = `
1962
- <div class="error-banner">
1963
- <strong>${escapeHtml(headline)}</strong>
1964
- <div>${detailLine}</div>
1965
- <div class="error-hint">${hint}</div>
1966
- </div>
1967
- <div class="quote-actions" style="margin-top:14px">
1968
- <button class="ghost" data-close-modal>Close</button>
1969
- <button class="ghost" id="twistRetry">Retry</button>
1970
- </div>
1971
- `;
1972
- bindModalDismiss();
1973
- const retry = document.getElementById('twistRetry');
1974
- if (retry) {
1975
- retry.addEventListener('click', () => {
1976
- // Re-fire whatever step we were on. If we already had a quote
1977
- // (so this was an order-submit error) restore the quote view;
1978
- // otherwise re-request from the original DNA payload.
1979
- if (twistState.quote) {
1980
- renderQuotePanel(twistState.quote);
1981
- } else if (twistState.variantId) {
1982
- openTwistGateway('', twistState.variantId);
1983
- }
1984
- });
1985
- }
1986
- }
1987
-
1988
- function bindModalDismiss() {
1989
- document.querySelectorAll('#twistModal [data-close-modal]').forEach((el) => {
1990
- if (el.dataset.wired === '1') return;
1991
- el.dataset.wired = '1';
1992
- el.addEventListener('click', closeTwistModal);
1993
- });
1994
- if (!bindModalDismiss._esc) {
1995
- bindModalDismiss._esc = true;
1996
- document.addEventListener('keydown', (e) => {
1997
- const modal = document.getElementById('twistModal');
1998
- if (e.key === 'Escape' && modal && !modal.hidden) closeTwistModal();
1999
- });
2000
- }
2001
- }
2002
-
2003
- function closeTwistModal() {
2004
- const modal = document.getElementById('twistModal');
2005
- if (modal) modal.hidden = true;
2006
- twistState.quote = null;
2007
- }
2008
-
2009
- function renderQuotePanel(quote) {
2010
- const body = document.getElementById('twistModalBody');
2011
- if (!body) return;
2012
- const fmtUsd = (n) => `$${Number(n).toFixed(2)}`;
2013
-
2014
- const statusClass = quote.biosecurity_passed ? '' : 'review';
2015
- const statusLabel = quote.biosecurity_passed
2016
- ? 'Biosecurity passed'
2017
- : 'Manual biosecurity review required';
2018
-
2019
- const lineItemsHtml = quote.line_items.map((li) => {
2020
- const flagsHtml = (li.complexity_flags || []).map((f) => {
2021
- const isBiosec = f === 'biosecurity_review';
2022
- const label = String(f).replace(/_/g, ' ');
2023
- return `<span class="complexity-flag ${isBiosec ? 'biosec' : ''}">${escapeHtml(label)}</span>`;
2024
- }).join('');
2025
- return `
2026
- <div class="qli">
2027
- <div>
2028
- <div class="qli-name">${escapeHtml(li.variant_id)}</div>
2029
- <div class="qli-meta">${li.length_bp} bp · ${li.gc_percent}% GC · $${li.price_per_bp_usd.toFixed(4)}/bp · ${li.estimated_turnaround_days}d</div>
2030
- ${flagsHtml ? `<div class="complexity-row">${flagsHtml}</div>` : ''}
2031
- </div>
2032
- <div class="qli-price">${fmtUsd(li.price_usd)}</div>
2033
- </div>
2034
- `;
2035
- }).join('');
2036
-
2037
- body.innerHTML = `
2038
- <div class="quote-hero">
2039
- <div class="quote-hero-row">
2040
- <div class="quote-id">${escapeHtml(quote.quote_id)} · expires ${escapeHtml(new Date(quote.expires_at).toLocaleString())}</div>
2041
- <span class="quote-status-pill ${statusClass}">${escapeHtml(statusLabel)}</span>
2042
- </div>
2043
- <div class="quote-hero-numbers">
2044
- <div class="quote-hero-num">
2045
- <span class="quote-hero-label">Total</span>
2046
- <span class="quote-hero-val">${fmtUsd(quote.total_usd)}</span>
2047
- </div>
2048
- <div class="quote-hero-divider"></div>
2049
- <div class="quote-hero-num">
2050
- <span class="quote-hero-label">Estimated turnaround</span>
2051
- <span class="quote-hero-val">${quote.max_turnaround_days} <span class="quote-hero-unit">days</span></span>
2052
- </div>
2053
- </div>
2054
- </div>
2055
-
2056
- <div class="quote-grid">
2057
- <div class="quote-tile"><span class="label">Subtotal</span><span class="val">${fmtUsd(quote.subtotal_usd)}</span></div>
2058
- <div class="quote-tile"><span class="label">Shipping</span><span class="val">${fmtUsd(quote.shipping_usd)}</span></div>
2059
- <div class="quote-tile"><span class="label">Variants</span><span class="val">${quote.line_items.length}</span></div>
2060
- </div>
2061
-
2062
- <div class="quote-lineitems">${lineItemsHtml}</div>
2063
-
2064
- <div class="quote-actions">
2065
- <button class="ghost" data-close-modal>Cancel</button>
2066
- <button class="primary" id="twistProceed">Confirm Order →</button>
2067
- </div>
2068
- `;
2069
- bindModalDismiss();
2070
- document.getElementById('twistProceed').addEventListener('click', renderOrderForm);
2071
- }
2072
-
2073
- function renderOrderForm() {
2074
- const body = document.getElementById('twistModalBody');
2075
- if (!body || !twistState.quote) return;
2076
-
2077
- body.innerHTML = `
2078
- <form class="order-form" id="twistOrderForm" autocomplete="off">
2079
- <div class="quote-headline">
2080
- <div>
2081
- <div class="quote-id">${escapeHtml(twistState.quote.quote_id)}</div>
2082
- <div class="quote-total-label">Charging</div>
2083
- <div class="quote-total">$${Number(twistState.quote.total_usd).toFixed(2)}</div>
2084
- </div>
2085
- </div>
2086
-
2087
- <div class="order-form-grid">
2088
- <div class="order-field field-full">
2089
- <label for="orderEmail">Billing email</label>
2090
- <input id="orderEmail" type="email" required placeholder="researcher@yourlab.org" />
2091
- </div>
2092
- <div class="order-field field-full">
2093
- <label for="orderName">Recipient name</label>
2094
- <input id="orderName" type="text" required placeholder="Lab name or person" />
2095
- </div>
2096
- <div class="order-field field-full">
2097
- <label for="orderLine1">Address line 1</label>
2098
- <input id="orderLine1" type="text" required placeholder="Street + building" />
2099
- </div>
2100
- <div class="order-field">
2101
- <label for="orderCity">City</label>
2102
- <input id="orderCity" type="text" required />
2103
- </div>
2104
- <div class="order-field">
2105
- <label for="orderRegion">State / region</label>
2106
- <input id="orderRegion" type="text" required />
2107
- </div>
2108
- <div class="order-field">
2109
- <label for="orderPostal">Postal code</label>
2110
- <input id="orderPostal" type="text" required />
2111
- </div>
2112
- <div class="order-field">
2113
- <label for="orderCountry">Country (ISO-2)</label>
2114
- <input id="orderCountry" type="text" required maxlength="2" placeholder="US" />
2115
- </div>
2116
- <div class="order-field field-full">
2117
- <label for="orderPO">PO number (optional)</label>
2118
- <input id="orderPO" type="text" placeholder="PO-2026-001" />
2119
- </div>
2120
- </div>
2121
-
2122
- <label class="order-terms">
2123
- <input id="orderTerms" type="checkbox" required />
2124
- <span>I confirm this is a mock order against the local Twist Gateway. No real DNA will be synthesized and no payment will be charged.</span>
2125
- </label>
2126
-
2127
- <div class="quote-actions">
2128
- <button type="button" class="ghost" id="twistBack">Back to quote</button>
2129
- <button type="submit" class="primary">Confirm Order →</button>
2130
- </div>
2131
- </form>
2132
- `;
2133
- document.getElementById('twistBack').addEventListener('click', () => renderQuotePanel(twistState.quote));
2134
- document.getElementById('twistOrderForm').addEventListener('submit', submitTwistOrder);
2135
- }
2136
-
2137
- async function submitTwistOrder(e) {
2138
- e.preventDefault();
2139
- if (!twistState.quote) return;
2140
- const body = document.getElementById('twistModalBody');
2141
- const form = e.target;
2142
-
2143
- const payload = {
2144
- quote_id: twistState.quote.quote_id,
2145
- billing_email: form.querySelector('#orderEmail').value.trim(),
2146
- shipping_address: {
2147
- recipient_name: form.querySelector('#orderName').value.trim(),
2148
- line1: form.querySelector('#orderLine1').value.trim(),
2149
- city: form.querySelector('#orderCity').value.trim(),
2150
- state_or_region: form.querySelector('#orderRegion').value.trim(),
2151
- postal_code: form.querySelector('#orderPostal').value.trim(),
2152
- country: form.querySelector('#orderCountry').value.trim().toUpperCase(),
2153
- },
2154
- accept_terms: form.querySelector('#orderTerms').checked,
2155
- po_number: form.querySelector('#orderPO').value.trim() || null,
2156
- };
2157
-
2158
- body.innerHTML = `<div class="modal-loading"><span class="identify-spinner"></span><span>Submitting order to Twist Gateway…</span></div>`;
2159
- try {
2160
- const data = await gatewayFetch('/api/v1/twist/order', {
2161
- method: 'POST',
2162
- body: JSON.stringify(payload),
2163
- });
2164
- renderOrderConfirmation(data);
2165
- } catch (err) {
2166
- renderGatewayError(body, err);
2167
- }
2168
- }
2169
-
2170
- function renderOrderConfirmation(confirm) {
2171
- const body = document.getElementById('twistModalBody');
2172
- if (!body) return;
2173
- body.innerHTML = `
2174
- <div class="order-confirm">
2175
- <div class="checkmark">
2176
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round">
2177
- <path d="M20 6 9 17l-5-5"/>
2178
- </svg>
2179
- </div>
2180
- <h3>${escapeHtml(confirm.status)}</h3>
2181
- <div class="order-id">${escapeHtml(confirm.order_id)}</div>
2182
- <div class="order-detail">
2183
- ${confirm.variants_ordered} variant${confirm.variants_ordered === 1 ? '' : 's'} · charged $${Number(confirm.total_paid_usd).toFixed(2)} · estimated ship ${escapeHtml(confirm.estimated_ship_date)}
2184
- </div>
2185
- <div class="order-detail">${escapeHtml(confirm.message)}</div>
2186
- <div class="quote-actions" style="justify-content:center; margin-top:6px">
2187
- <button class="ghost" data-close-modal>Done</button>
2188
- </div>
2189
- </div>
2190
- `;
2191
- bindModalDismiss();
2192
- showToast(`Mock order ${confirm.order_id} placed via Twist Gateway.`, 'info');
2193
- }
2194
-
2195
  // Download dropdown. Trigger toggles a menu of formats; clicking an item
2196
  // kicks off a real browser download via a hidden <a download> link.
2197
  function wireDownloadMenu() {
@@ -3050,9 +2677,8 @@ async function mountESMFoldViewer(host, pdbText) {
3050
  }
3051
  }
3052
 
3053
- // Mirrors bindModalDismiss() for the #twistModal. Wires the backdrop, the
3054
- // × button, and the Esc key to closeFoldModal. Idempotent — safe to call on
3055
- // every open.
3056
  function bindFoldModalDismiss() {
3057
  document.querySelectorAll('#foldModal [data-close-modal]').forEach((el) => {
3058
  if (el.dataset.wired === '1') return;
 
1789
  `);
1790
  if (!verifyOk) return;
1791
 
1792
+ // All three vendors (Twist, IDT, GenScript) are external homepage
1793
+ // redirects: we copy the DNA to clipboard, then open the vendor's
1794
+ // site in a new tab so the user pastes into their order form.
1795
+ //
1796
+ // The local Twist Gateway in-app flow was removed when we shipped
1797
+ // the public site — it required a localhost FastAPI service that
1798
+ // doesn't exist in production. The full gateway code is preserved
1799
+ // in git history if we ever wire up the real Twist API.
1800
+ //
1801
+ // SAFETY: if the clipboard write fails we MUST NOT open the vendor
1802
+ // tab — the user would paste whatever was on the clipboard before
1803
+ // (likely a different variant) into the vendor's order form. Wrong
1804
+ // DNA shipped, hard to detect.
 
1805
  const vendor = VENDORS[vendorKey] || VENDORS.genscript;
1806
  const ok = await copyToClipboardOrToast(dnaSeq, `${variantId} DNA for ${vendor.name}`);
1807
  if (!ok) {
 
1819
  }, 1100);
1820
  }
1821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1822
  // Download dropdown. Trigger toggles a menu of formats; clicking an item
1823
  // kicks off a real browser download via a hidden <a download> link.
1824
  function wireDownloadMenu() {
 
2677
  }
2678
  }
2679
 
2680
+ // Wires the #foldModal backdrop, × button, and Esc key to closeFoldModal.
2681
+ // Idempotent — safe to call on every open.
 
2682
  function bindFoldModalDismiss() {
2683
  document.querySelectorAll('#foldModal [data-close-modal]').forEach((el) => {
2684
  if (el.dataset.wired === '1') return;