1import{y as S,r as N,u as C,j as e,B as y,D as f,O as _,A as P,b6 as k,q as A,s as D,v as L,w as I,Y as R,x as E,Z as T,W as q,l as w,t as v,p as $,aa as O}from"./index-C_LAm9Ed.js";import{C as o,c as u}from"./card-BK1KwCMy.js";import{C as F}from"./chevron-up-CweXBgB3.js";import{f as Q}from"./formatDistanceToNow-1LYRh9ff.js";import"./constructNow-Cf82Ez9F.js";import"./en-US-C5P1YE9G.js";import"./constants-BmbwCt7Y.js";import"./getRoundingMethod-CmAN89uG.js";import"./endOfMonth-JeVkgMzO.js";const B="6a0144758e591d8944e1526b";function H({lastActivity:i,isLoading:s}){var m,d,a;const[r,x]=N.useState(!1);if(s)return e.jsx(o,{children:e.jsx(u,{className:"p-5",children:e.jsx("div",{className:"h-4 w-40 bg-gray-100 rounded animate-pulse"})})});if(!i)return e.jsx(o,{children:e.jsx(u,{className:"p-5 text-sm text-muted-foreground",children:"Never run yet."})});const t=i.metadata||{},j=i.created_date?Q($(i.created_date),{addSuffix:!0}):"â";return e.jsx(o,{children:e.jsxs(u,{className:"p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"font-semibold text-sm",children:"Last run"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:j})]}),e.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-4 gap-3 text-center",children:[e.jsx(g,{label:"Checked",value:t.total_checked??"â"}),e.jsx(g,{label:"Changes",value:((m=t.status_changes)==null?void 0:m.length)??"â"}),e.jsx(g,{label:"Cascaded",value:t.subs_cascaded??"â"}),e.jsx(g,{label:"Errors",value:((d=t.errors)==null?void 0:d.length)??"â",color:((a=t.errors)==null?void 0:a.length)>0?"text-red-600":void 0})]}),Array.isArray(t.status_changes)&&t.status_changes.length>0&&e.jsxs("div",{children:[e.jsxs("button",{className:"flex items-center gap-1 text-xs text-primary hover:underline",onClick:()=>x(c=>!c),children:[r?e.jsx(F,{className:"w-3 h-3"}):e.jsx(O,{className:"w-3 h-3"}),r?"Hide":"Show"," ",t.status_changes.length," change",t.status_changes.length!==1?"s":""]}),r&&e.jsx("div",{className:"mt-2 rounded border border-gray-200 divide-y text-xs max-h-60 overflow-y-auto",children:t.status_changes.map((c,h)=>e.jsxs("div",{className:"flex items-center gap-2 px-3 py-1.5 font-mono",children:[e.jsx("span",{className:"text-muted-foreground truncate flex-1",children:c.paypal_subscription_id}),e.jsx("span",{className:"text-gray-500",children:c.old}),e.jsx("span",{children:"â"}),e.jsx("span",{className:"font-semibold",children:c.new})]},h))})]})]})})}function g({label:i,value:s,color:r}){return e.jsxs("div",{className:"rounded-lg bg-gray-50 py-2 px-3",children:[e.jsx("div",{className:`text-lg font-bold tabular-nums ${r||"text-foreground"}`,children:s}),e.jsx("div",{className:"text-xs text-muted-foreground",children:i})]})}function V(){const i=S(),[s,r]=N.useState(null),[x,t]=N.useState(""),{data:j,isLoading:m}=C({queryKey:["paypalSyncLastRun"],queryFn:async()=>{const a=await w.entities.ProjectActivity.filter({project_id:B,action:"paypal_status_sync"},"-created_date",1);return(a==null?void 0:a[0])||null},refetchOnWindowFocus:!1});async function d(a,c){var h,b,X;r(a==="all_active"?"active":a==="all"?"all":"single");try{const n={mode:a};a==="single"&&c&&(n.paypal_subscription_id=c);const p=await w.functions.invoke("syncPayPalSubs",n),l=(p==null?void 0:p.data)||p||{};if((l==null?void 0:l.ok)===!1)throw new Error((l==null?void 0:l.error)||"Sync failed");v.success(`Synced ${l.total_checked} subs â ${(l.status_changes||[]).length} changes, ${l.subs_cascaded} Subscriptions cascaded`),l.errors_count>0&&v.warning(`${l.errors_count} error${l.errors_count!==1?"s":""} â check ProjectActivity for detail`),i.invalidateQueries({queryKey:["paypalSyncLastRun"]})}catch(n){v.error(((b=(h=n==null?void 0:n.response)==null?void 0:h.data)==null?void 0:b.error)||((X=n==null?void 0:n.data)==null?void 0:X.error)||(n==null?void 0:n.message)||"Sync failed")}finally{r(null)}}return e.jsxs("div",{className:"max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-6 space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold text-foreground tracking-tight",children:"PayPal Status Sync"}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:["Queries the PayPal API for each subscription and syncs the live status into Plaza. Suspended or cancelled PayPal subs cascade to ",e.jsx("code",{children:"Subscription.status='past_due'"}),", unblocking the PastDueBanner on the customer portal."]})]}),e.jsx(H,{lastActivity:j,isLoading:m}),e.jsx(o,{children:e.jsxs(u,{className:"p-5 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("div",{className:"font-medium text-sm",children:"Sync active subscriptions"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Checks all PayPalSubscriptions with status=active (recommended daily action)."})]}),e.jsxs(y,{onClick:()=>d("all_active"),disabled:!!s,className:"shrink-0",children:[s==="active"?e.jsx(f,{className:"w-4 h-4 animate-spin"}):e.jsx(_,{className:"w-4 h-4"}),s==="active"?"Syncingâ¦":"Sync active subs now"]})]})}),e.jsx(o,{children:e.jsxs(u,{className:"p-5 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("div",{className:"font-medium text-sm",children:"Full sweep"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Checks ALL PayPal subscriptions. Can take several minutes."})]}),e.jsxs(P,{children:[e.jsx(k,{asChild:!0,children:e.jsxs(y,{variant:"outline",disabled:!!s,className:"shrink-0",children:[s==="all"?e.jsx(f,{className:"w-4 h-4 animate-spin"}):null,s==="all"?"Syncingâ¦":"Sync ALL subs (full sweep)"]})}),e.jsxs(A,{children:[e.jsxs(D,{children:[e.jsx(L,{children:"Run full sweep?"}),e.jsx(I,{children:"This will check ALL PayPal subscriptions (not just active ones) and can take several minutes. Each record is queried sequentially with a 200ms delay.
1Continue?"})]}),e.jsxs(R,{children:[e.jsx(E,{children:"Cancel"}),e.jsx(T,{onClick:()=>d("all"),children:"Run full sweep"})]})]})]})]})}),e.jsx(o,{children:e.jsxs(u,{className:"p-5 space-y-3",children:[e.jsxs("div",{children:[e.jsx("div",{className:"font-medium text-sm",children:"Sync single subscription by ID"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Enter a PayPal subscription ID (e.g. I-XXXXXXXXXXXXXXXXXX)."})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(q,{placeholder:"I-XXXXXXXXXXXXXXXXXX",value:x,onChange:a=>t(a.target.value.trim()),className:"font-mono text-sm"}),e.jsxs(y,{onClick:()=>d("single",x),disabled:!!s||!x,className:"shrink-0",children:[s==="single"?e.jsx(f,{className:"w-4 h-4 animate-spin"}):null,s==="single"?"Syncingâ¦":"Sync"]})]})]})})]})}export{V as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.