1import{j as e}from"./vendor-radix-Cshc0K_j.js";import{a as se,u as Z,b as M}from"./vendor-query-CjzA0R3G.js";import{r as N}from"./vendor-react-CfDhItdx.js";import{A as ne,a as re,b as te,c as oe,d as le,e as me,f as de,g as ce}from"./alert-dialog-pVEpA8iY.js";import{z as pe,w as xe,s as d,l as U,j as f,aH as ue,C as k,c as E,L as R,d as V,f as X,bt as he,g as v,aE as J,aw as A,i as ge,a6 as w,aQ as fe,Z as _e,Q as je,T as ye,U as ve,V as we,W as be,$ as B,E as Ne}from"./index-y9XPgVzP.js";import{C as Y}from"./confirm-dialog-BZ_uRgQ6.js";import{I as Ce}from"./info-DZ3Hl51h.js";import{a as O}from"./vendor-date-DbbbO-EH.js";import"./vendor-supabase-cPZYlnRj.js";import"./vendor-charts-DiRvbK1V.js";import"./vendor-forms-Cpwam5X-.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const ee=pe("Key",[["path",{d:"m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4",key:"g0fldk"}],["path",{d:"m21 2-9.6 9.6",key:"1j0ho8"}],["circle",{cx:"7.5",cy:"15.5",r:"5.5",key:"yqb3hr"}]]);function Ae(){const C=se(),{toast:_}=xe(),[l,$]=N.useState(null),[c,z]=N.useState(null),[H,P]=N.useState(""),[p,D]=N.useState(null),[S,G]=N.useState(null);function q(a,s){navigator.clipboard.writeText(a).then(()=>{G(s),setTimeout(()=>G(null),2e3)})}function ae(a){const s=a.mailboxes.map(r=>`Email: ${r.email} 7Password: ${r.password}`).join(` 8 9`),n=[`Email credentials for ${a.domain}:`,"",s,"","Webmail: https://webmail.migadu.com","IMAP: imap.migadu.com port 993 (SSL)","SMTP: smtp.migadu.com port 465 (SSL)","Username: your full email address"].join(` 10`);q(n,"all")}const{data:F,isLoading:K,refetch:ie}=Z({queryKey:["admin-pending-emails"],queryFn:async()=>{const{data:a,error:s}=await d.from("company_subscriptions").select("company_id, email_addon_purchased, email_addon_purchased_at, email_addon_admin_granted, email_addon_admin_granted_at, pending_mailbox_config").or("email_addon_purchased.eq.true,email_addon_admin_granted.eq.true").eq("email_addon_provisioned",!1);if(s)throw s;if(!a||a.length===0)return[];const n=a.map(i=>i.company_id).filter(i=>i!==null);if(n.length===0)return[];const{data:r,error:t}=await d.from("companies").select("id, company_name, email").in("id",n);if(t)throw t;const{data:o,error:j}=await d.from("company_domains").select("id, company_id, domain_name, domain_status").in("company_id",n).eq("domain_status","active");if(j)throw j;const g=new Map((r==null?void 0:r.map(i=>[i.id,i]))||[]),y=new Map((o==null?void 0:o.map(i=>[i.company_id,i]))||[]);return a.filter(i=>{var W;if(!i.company_id)return!1;const u=y.get(i.company_id),m=i.pending_mailbox_config;return!!u&&(((W=m==null?void 0:m.mailboxes)==null?void 0:W.length)??0)>0}).map(i=>{const u=g.get(i.company_id),m=y.get(i.company_id);return{company_id:i.company_id,company_name:(u==null?void 0:u.company_name)||"Unknown",primary_email:(u==null?void 0:u.email)||"",domain_name:(m==null?void 0:m.domain_name)||"",domain_id:(m==null?void 0:m.id)||"",mailbox_config:i.pending_mailbox_config,enabled_at:i.email_addon_purchased_at||i.email_addon_admin_granted_at||"",entitlement_source:i.email_addon_purchased?"purchase":"admin-grant"}})},refetchInterval:3e4,refetchIntervalInBackground:!1}),L=M({mutationFn:async({companyId:a,domainId:s,mailboxes:n})=>{const{data:r,error:t}=await d.functions.invoke("provision-email",{body:{company_id:a,domain_id:s,mailboxes:n}});if(t)throw t;if(r!=null&&r.error)throw new Error(r.error);return r},onSuccess:()=>{C.invalidateQueries({queryKey:["admin-domains"]}),C.invalidateQueries({queryKey:["admin-pending-emails"]}),_({title:"â Email Provisioned",description:"Mailboxes created successfully. Customer has been notified."})},onError:a=>{_({variant:"destructive",title:"Provisioning Failed",description:a.message})}}),{data:I}=Z({queryKey:["admin-provisioned-emails"],queryFn:async()=>{var y;const{data:a,error:s}=await d.from("company_domains").select("id, company_id, domain_name, email_provider, email_enabled_at, cloudflare_zone_id").eq("email_provider","migadu").order("email_enabled_at",{ascending:!1});if(s)throw s;if(!a||a.length===0)return[];const n=a.map(i=>
10i.company_id).filter(Boolean),r=a.map(i=>i.id),[t,o]=await Promise.all([d.from("companies").select("id, company_name, email").in("id",n),d.from("company_mailboxes").select("domain_id, email, local_part, is_primary").in("domain_id",r).order("is_primary",{ascending:!1}).order("email",{ascending:!0})]),j=new Map(((y=t.data)==null?void 0:y.map(i=>[i.id,i]))||[]),g=new Map;for(const i of o.data||[])i.domain_id&&(g.has(i.domain_id)||g.set(i.domain_id,[]),g.get(i.domain_id).push({email:i.email,local_part:i.local_part,is_primary:i.is_primary??!1}));return a.map(i=>{var u,m;return{domain_id:i.id,company_id:i.company_id,company_name:((u=j.get(i.company_id))==null?void 0:u.company_name)||"Unknown",primary_email:((m=j.get(i.company_id))==null?void 0:m.email)||"",domain_name:i.domain_name,cloudflare_zone_id:i.cloudflare_zone_id||"",provisioned_at:i.email_enabled_at,mailboxes:g.get(i.id)||[]}})},refetchInterval:3e4,refetchIntervalInBackground:!1}),[x,Q]=N.useState(null),T=M({mutationFn:async({domainId:a,companyId:s,domainName:n,cloudflareZoneId:r})=>{const{data:t,error:o}=await d.functions.invoke("cleanup-email",{body:{domain_name:n,cloudflare_zone_id:r}});o?U.error("Cleanup function error:",o):t!=null&&t.error&&U.error("Cleanup returned error:",t.error);const{error:j}=await d.from("company_mailboxes").delete().eq("domain_id",a);j&&U.error("Failed to delete mailboxes:",j);const{error:g}=await d.from("company_domains").update({email_provider:null,email_enabled_at:null,email_configured:!1}).eq("id",a);if(g)throw g;const{error:y}=await d.from("company_subscriptions").update({email_addon_provisioned:!1,email_addon_provisioned_at:null}).eq("company_id",s);if(y)throw y},onSuccess:()=>{C.invalidateQueries({queryKey:["admin-provisioned-emails"]}),C.invalidateQueries({queryKey:["admin-pending-emails"]}),C.invalidateQueries({queryKey:["admin-pending-emails-count"]}),_({title:"Email provisioning reset",description:"Domain removed from Migadu, DNS cleaned up, and moved back to pending queue."})},onError:a=>{_({variant:"destructive",title:"Reset Failed",description:a.message})}}),b=M({mutationFn:async a=>{var r;const{data:s,error:n}=await d.functions.invoke("admin-resend-email-credentials",{body:{domain_id:a,send_email:!1}});if(n){const t=await((r=n.context)==null?void 0:r.json().catch(()=>null));throw new Error((t==null?void 0:t.error)||(t==null?void 0:t.message)||n.message||"Unknown error")}if(s!=null&&s.error)throw new Error(s.error);return s},onSuccess:a=>{D({domain:a.domain,mailboxes:a.mailboxes})},onError:a=>{_({variant:"destructive",title:"Failed to get credentials",description:a.message})}}),h=M({mutationFn:async({domainId:a,recipientEmail:s})=>{var t;const{data:n,error:r}=await d.functions.invoke("admin-resend-email-credentials",{body:{domain_id:a,recipient_email:s,send_email:!0}});if(r){const o=await((t=r.context)==null?void 0:t.json().catch(()=>null));throw new Error((o==null?void 0:o.error)||(o==null?void 0:o.message)||r.message||"Unknown error")}if(n!=null&&n.error)throw new Error(n.error);return n},onSuccess:a=>{if(a.email_sent){_({title:"Credentials resent",description:`Fresh mailbox passwords were emailed to ${a.sent_to||"the customer"}.`});return}D({domain:a.domain,mailboxes:a.mailboxes,note:`Email delivery failed${a.email_error?": "+a.email_error:""}. Copy credentials below.`})},onError:a=>{_({variant:"destructive",title:"Resend Failed",description:a.message})}});return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Email Provisioning"}),e.jsx("p",{className:"text-muted-foreground",children:"Set up professional email for customers with purchased or admin-granted access"})]}),e.jsxs(f,{variant:"outline",onClick:()=>ie(),disabled:K,children:[e.jsx(ue,{className:`h-4 w-4 mr-2 ${K?"animate-spin":""}`}),"Refresh"]})]}),e.jsx(k,{children:e.jsx(E,{className:"py-4",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Ce,{className:"h-5 w-5 text-blue-500 mt-0.5 shrink-0"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:'Click "Provision Email" to set everything up automatically. The system will add the domain to Migadu, configure DNS, create mailboxes, and email the customer their login details.'})]})})}),K?e.jsx(k,{children:e.jsxs(E,{className:"py-12 text-center",children:[e.jsx(R,{className:"h-8 w-8 animate-spin mx-auto mb-4 text-muted-foreground"}),e.jsx("p",{className:"text-muted-foreground",children:"Loading pending email requests..."})]})}):F&&F.length>0?e.jsxs(k,{children:[e.jsxs(V,{children:[e.jsxs(X,{className:"flex items-center gap-2",children:[e.jsx(he,{className:"h-5 w-5"}),"Pending Setup",e.jsx(v,{variant:"destructive",className:"ml-2",children:F.length})]}),e.jsx(J,{children:"These customers have Professional Email enabled and have chosen their mailbox names"})]}),e.jsx(E,{children:e.jsx("div",{className:"space-y-4",children:F.map(a=>e.jsxs("div",{className:"flex items-start justify-between p-4 bg-muted/50 rounded-lg border",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"font-semibold",children:a.company_name}),e.jsx(v,{variant:"outline",children:a.domain_name}),e.jsx(v,{variant:"secondary",children:a.entitlement_source==="purchase"?"Purchased":"Admin grant"})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:a.primary_email}),e.jsx("div",{className:"flex flex-wrap gap-2",children:a.mailbox_config.mailboxes.map((s,n)=>e.jsxs(v,{variant:s.isPrimary?"default":"secondary",children:[e.jsx(A,{className:"h-3 w-3 mr-1"}),s.localPart,"@",a.domain_name]},n))}),e.jsxs("div",{className:"flex items-center gap-4 text-xs text-muted-foreground",children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(ge,{className:"h-3 w-3"}),"Enabled ",O(new Date(a.enabled_at),{addSuffix:!0})]}),a.mailbox_config.submitted_at&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(w,{className:"h-3 w-3"}),"Names chosen ",O(new Date(a.mailbox_config.submitted_at),{addSuffix:!0})]})]})]}),e.jsx(f,{onClick:()=>$(a),disabled:L.isPending,size:"sm",children:L.isPending?e.jsxs(e.Fragment,{children:[e.jsx(R,{className:"h-4 w-4 mr-2 animate-spin"}),"Provisioning..."]}):e.jsxs(e.Fragment,{children:[e.jsx(A,{className:"h-4 w-4 mr-2"}),"Provision Email"]})})]},a.company_id))})})]}):e.jsx(k,{children:e.jsxs(E,{className:"py-12 text-center",children:[e.jsx(w,{className:"h-12 w-12 text-green-500 mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-semibold",children:"All Caught Up!"}),e.jsx("p",{className:"text-muted-foreground mt-1",children:"No pending email provisioning requests"})]})}),I&&I.length>0&&e.jsxs(k,{children:[e.jsxs(V,{children:[e.jsxs(X,{className:"flex items-center gap-2",children:[e.jsx(w,{className:"h-5 w-5 text-green-500"}),"Provisioned",e.jsx(v,{variant:"secondary",className:"ml-2",children:I.length})]}),e.jsx(J,{children:'Active email domains. "Get Credentials" resets passwords and shows them here only â no email sent.'})]}),e.jsx(E,{children:e.jsx("div",{className:"space-y-3",children:I.map(a=>e.jsxs("div",{className:"p-3 bg-muted/30 rounded-lg border space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("p",{className:"font-medium text-sm",children:a.company_name}),e.jsx(v,{variant:"outline",className:"text-xs",children:a.domain_name}),e.jsxs(v,{variant:"secondary",className:"text-xs bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400",children:[e.jsx(w,{className:"h-3 w-3 mr-1"}),"Active"]}),a.provisioned_at&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["provisioned ",O(new Date(a.provisioned_at),{addSuffix:!0})]})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsx(f,{variant:"outline",size:"sm",onClick:()=>b.mutate(a.domain_id),disabled:b.isPending||h.isPending||T.isPending,children:b.isPending&&b.variables===a.domain_id?e.jsxs(e.Fragment,{children:[e.jsx(R,{className:"h-3.5 w-3.5 mr-1.5 animate-spin"}),"Getting..."]}):e.jsxs(e.Fragment,{children:[e.jsx(ee,{className:"h-3.5 w-3.5 mr-1.5"}),"Get Credentials"]})}),e.jsx(f,{variant:"outline",size:"sm",onClick:()=>{z(a),P(a.primary_email)},disabled:b.isPending||h.isPending||T.isPending,children:h.isPending&&(c==null?void 0:c.domain_id)===a.domain_id?e.jsxs(e.Fragment,{children:[e.jsx(R,{className:"h-3.5 w-3.5 mr-1.5 animate-spin"}),"Sending..."]}):e.jsxs(e.Fragment,{children:[e.jsx(A,{className:"h-3.5 w-3.5 mr-1.5"}),"Resend Email"]})}),e.jsxs(f,{variant:"outline",size:"sm",onClick:()=>Q(a),disabled:b.isPending||h.isPending||T.isPending,className:"text-destructive hover:text-destructive hover:bg-destructive/10",children:[e.jsx(fe,{className:"h-3.5 w-3.5 mr-1.5"}),"Reset"]})]})]}),a.mailboxes.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1.5 pt-1",children:a.mailboxes.map(s=>e.jsxs("button",{type:"button",onClick:()=>q(s.email,s.email),className:"flex items-center gap-1 rounded-md border bg-background px-2 py-0.5 text-xs font-mono text-muted-foreground hover:text-foreground hover:border-foreground
10/30 transition-colors",title:"Click to copy",children:[S===s.email?e.jsx(w,{className:"h-3 w-3 text-green-500"}):e.jsx(A,{className:"h-3 w-3"}),s.email,s.is_primary&&e.jsx(v,{variant:"secondary",className:"ml-1 h-3.5 text-[10px] px-1",children:"primary"})]},s.email))})]},a.domain_id))})})]}),e.jsx(Y,{open:!!l,onOpenChange:a=>!a&&$(null),title:`Provision email for ${l==null?void 0:l.company_name}?`,description:l?`Domain: ${l.domain_name} 11Mailboxes: ${l.mailbox_config.mailboxes.map(a=>a.localPart+"@"+l.domain_name).join(", ")} 12Customer email: ${l.primary_email} 13 14This will set up email automatically. The customer will receive their login details by email.`:"",confirmText:"Provision Email",onConfirm:()=>{l&&L.mutate({companyId:l.company_id,domainId:l.domain_id,mailboxes:l.mailbox_config.mailboxes}),$(null)}}),e.jsx(Y,{open:!!x,onOpenChange:a=>!a&&Q(null),title:`Reset email for ${x==null?void 0:x.company_name}?`,description:x?`This will fully reset email provisioning for ${x.domain_name}. 15 16⢠Domain and mailboxes will be deleted from Migadu 17⢠MX, SPF, DKIM, DMARC, and verification DNS records will be removed 18⢠Database records will be cleaned up 19⢠The domain will move back to the pending queue 20 21The customer will NOT be notified.`:"",confirmText:"Reset Provisioning",variant:"destructive",onConfirm:()=>{x&&T.mutate({domainId:x.domain_id,companyId:x.company_id,domainName:x.domain_name,cloudflareZoneId:x.cloudflare_zone_id}),Q(null)}}),e.jsx(ne,{open:!!c,onOpenChange:a=>{a||(z(null),P(""))},children:e.jsxs(re,{children:[e.jsxs(te,{children:[e.jsxs(oe,{children:["Resend credentials for ",c==null?void 0:c.company_name,"?"]}),e.jsxs(le,{className:"space-y-3",children:[e.jsxs("span",{className:"block",children:["This will generate brand new passwords for every mailbox on ",c==null?void 0:c.domain_name," and email them to the recipient below."]}),e.jsx("span",{className:"block",children:"Existing mailbox passwords will stop working immediately."})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{htmlFor:"resend-recipient-email",className:"text-sm font-medium",children:"Send credentials to"}),e.jsx(_e,{id:"resend-recipient-email",type:"email",value:H,onChange:a=>P(a.target.value),placeholder:"[email protected]",disabled:h.isPending}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Defaults to the company contact email. Change if needed."})]}),e.jsxs(me,{children:[e.jsx(de,{disabled:h.isPending,children:"Cancel"}),e.jsx(ce,{disabled:h.isPending,onClick:a=>{a.preventDefault();const s=H.trim();if(!s){_({variant:"destructive",title:"Recipient required",description:"Enter the email address to receive the credentials."});return}c&&h.mutate({domainId:c.domain_id,recipientEmail:s}),z(null),P("")},children:h.isPending?"Sending...":"Reset & Send Email"})]})]})}),e.jsx(je,{open:!!p,onOpenChange:a=>!a&&D(null),children:e.jsxs(ye,{className:"max-w-md",children:[e.jsxs(ve,{children:[e.jsxs(we,{className:"flex items-center gap-2",children:[e.jsx(ee,{className:"h-4 w-4"}),"Credentials â ",p==null?void 0:p.domain]}),e.jsx(be,{children:(p==null?void 0:p.note)??"New passwords generated. Copy and share with the customer."})]}),e.jsx("div",{className:"space-y-3",children:p==null?void 0:p.mailboxes.map(a=>e.jsxs("div",{className:"rounded-md border bg-muted/30 p-3 space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-sm font-medium",children:a.email}),e.jsx(f,{variant:"ghost",size:"sm",className:"h-7 px-2 text-xs",onClick:()=>q(`${a.email} 22${a.password}`,a.email),children:S===a.email?e.jsx(w,{className:"h-3.5 w-3.5 text-green-500"}):e.jsx(B,{className:"h-3.5 w-3.5"})})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("p",{className:"font-mono text-sm tracking-wide",children:a.password}),e.jsx(f,{variant:"ghost",size:"sm",className:"h-7 px-2 text-xs shrink-0",onClick:()=>q(a.password,`pw-${a.email}`),children:S===`pw-${a.email}`?e.jsx(w,{className:"h-3.5 w-3.5 text-green-500"}):e.jsx(B,{className:"h-3.5 w-3.5"})})]})]},a.email))}),e.jsxs("div",{className:"flex items-center justify-between pt-2 border-t",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Webmail: webmail.migadu.com"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(f,{variant:"outline",size:"sm",onClick:()=>p&&ae(p),children:[S==="all"?e.jsx(w,{className:"h-3.5 w-3.5 mr-1.5 text-green-500"}):e.jsx(B,{className:"h-3.5 w-3.5 mr-1.5"}),"Copy All"]}),e.jsx(f,{size:"sm",onClick:()=>D(null),children:"Done"})]})]})]})}),e.jsx("div",{className:"flex justify-end",children:e.jsxs("a",{href:"https://admin.migadu.com/domains",target:"_blank",rel:"noopener noreferrer",className:"text-xs text-muted-foreground hover:underline flex items-center gap-1",children:[e.jsx(Ne,{className:"h-3 w-3"}),"Open Migadu Admin"]})})]})}export{Ae as default}; 23//# sourceMappingURL=EmailProvisioning-CkCwUeeJ.js.map
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.