"use strict";(()=>{var e={};e.id=1061,e.ids=[1061],e.modules={10846:e=>{e.exports=require("next/dist/compiled/next-server/app-page.runtime.prod.js")},44870:e=>{e.exports=require("next/dist/compiled/next-server/app-route.runtime.prod.js")},29294:e=>{e.exports=require("next/dist/server/app-render/work-async-storage.external.js")},63033:e=>{e.exports=require("next/dist/server/app-render/work-unit-async-storage.external.js")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},14985:e=>{e.exports=require("dns")},94735:e=>{e.exports=require("events")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},55591:e=>{e.exports=require("https")},91645:e=>{e.exports=require("net")},21820:e=>{e.exports=require("os")},33873:e=>{e.exports=require("path")},19771:e=>{e.exports=require("process")},27910:e=>{e.exports=require("stream")},41204:e=>{e.exports=require("string_decoder")},66136:e=>{e.exports=require("timers")},34631:e=>{e.exports=require("tls")},79551:e=>{e.exports=require("url")},28354:e=>{e.exports=require("util")},74075:e=>{e.exports=require("zlib")},4573:e=>{e.exports=require("node:buffer")},77598:e=>{e.exports=require("node:crypto")},53053:e=>{e.exports=require("node:diagnostics_channel")},50229:(e,r,t)=>{t.r(r),t.d(r,{patchFetch:()=>w,routeModule:()=>R,serverHooks:()=>j,workAsyncStorage:()=>E,workUnitAsyncStorage:()=>N});var n={};t.r(n),t.d(n,{POST:()=>v});var i=t(42706),s=t(28203),o=t(45994),a=t(77598),u=t.n(a),l=t(34926),p=t(65658),c=t(39187),d=t(98095),m=t(70357),f=t(42770),g=t(50675),x=t(23951),h=t(53721),S=t(71747);let q=p.Ik({firstName:p.Yj().min(1),lastName:p.Yj().min(1),email:p.Yj().email(),phone:p.Yj().optional(),role:p.Yj().min(1),institutionIds:p.YO(p.Yj().min(1)).min(1),continueUrl:p.Yj().url().optional()});async function v(e){try{var r;if("mysql"!==(0,d.f)())return c.NextResponse.json({error:"Auth local deshabilitada."},{status:404});let t=(0,h.Z)(e);if(!t)return c.NextResponse.json({error:"Unauthorized"},{status:401});if(!(0,S.s)(t,"users:create"))return c.NextResponse.json({error:"Permission denied."},{status:403});let n=String(t.orgId||"").trim();if(!n)return c.NextResponse.json({error:"Caller has no orgId."},{status:403});let i=await e.json().catch(()=>null),s=q.safeParse(i);if(!s.success)return c.NextResponse.json({error:"Payload invalido."},{status:400});let o=s.data.firstName.trim(),a=s.data.lastName.trim(),p=(r=s.data.email,String(r||"").trim().toLowerCase()),v=String(s.data.phone||"").trim(),R=s.data.role.trim(),E=s.data.institutionIds.map(e=>e.trim()),N=function(e,r){let t=new URL(String(r||"").trim()||String(e.headers.get("origin")||"").trim()||e.nextUrl.origin);if(!("localhost"===t.hostname||"127.0.0.1"===t.hostname||"0.0.0.0"===t.hostname)&&"https:"!==t.protocol)throw Error("continueUrl/origin invalido: fuera de localhost debe ser https.");return t.origin}(e,s.data.continueUrl),j=`${o} ${a}`.trim(),w=(0,x.T)(),[y]=await w.query(`
        SELECT id
        FROM roles
        WHERE id = ?
        LIMIT 1
      `,[R]);if(!y.length)return c.NextResponse.json({error:"Rol no existe."},{status:400});let O=Array.from(new Set(E)),A=O.map(()=>"?").join(", "),[T]=await w.query(`
        SELECT id
        FROM institutions
        WHERE org_id = ?
          AND id IN (${A})
      `,[n,...O]);if(T.length!==O.length)return c.NextResponse.json({error:"Institution no existe o esta fuera de tu org."},{status:403});if(await (0,g.t7)(p))return c.NextResponse.json({error:"Ya existe un usuario con ese email."},{status:409});let _=u().randomBytes(32).toString("hex"),I=await l.Ay.hash(_,12);await (0,g.Z2)({email:p,passwordHash:I,status:"pending",firstName:o,lastName:a,displayName:j,phone:v,role:R,orgId:n,institutionIds:O});let{token:$,expiresAt:b}=await (0,f.Vo)(p),C=`${N}/set-password?mode=reset&email=${encodeURIComponent(p)}&token=${encodeURIComponent($)}`,U=[`Hola ${j},`,"","Tu cuenta en OMR SEG fue creada.","Para activar acceso y crear tu contrasena, abre este enlace:",C,"",`Este enlace expira el ${b.toISOString()}.`].join("\n"),H=`
      <p>Hola <strong>${j}</strong>,</p>
      <p>Tu cuenta en <strong>OMR SEG</strong> fue creada.</p>
      <p><a href="${C}">Activar cuenta y crear contrasena</a></p>
      <p>Si el boton no funciona, usa este enlace:</p>
      <p>${C}</p>
      <p>Este enlace expira el <strong>${b.toISOString()}</strong>.</p>
    `;await (0,m.x)({to:p,subject:"OMR SEG - Activacion de cuenta",text:U,html:H});let P=await (0,g.t7)(p);return c.NextResponse.json({success:!0,uid:String(P?.id||"").trim()||p})}catch(e){return console.error("[POST /api/auth/admin/create-user] error:",e),c.NextResponse.json({error:"Error interno."},{status:500})}}let R=new i.AppRouteRouteModule({definition:{kind:s.RouteKind.APP_ROUTE,page:"/api/auth/admin/create-user/route",pathname:"/api/auth/admin/create-user",filename:"route",bundlePath:"app/api/auth/admin/create-user/route"},resolvedPagePath:"C:\\omrwebapp-git-roregar.cl\\src\\app\\api\\auth\\admin\\create-user\\route.ts",nextConfigOutput:"standalone",userland:n}),{workAsyncStorage:E,workUnitAsyncStorage:N,serverHooks:j}=R;function w(){return(0,o.patchFetch)({workAsyncStorage:E,workUnitAsyncStorage:N})}},71747:(e,r,t)=>{function n(e){return String(e||"").trim().toLowerCase()}function i(e,r){if(!e)return!1;if(function(e){let r=n(e);return"admin"===r||r.startsWith("admin_")||r.includes(":admin")}(e.roleId))return!0;let t=n(r);return(e.permissions||[]).some(e=>n(e)===t)}t.d(r,{s:()=>i})},53721:(e,r,t)=>{t.d(r,{Z:()=>i});var n=t(89249);function i(e){let r=String(e.cookies.get(n.CA())?.value||"").trim();if(!r)return null;let t=(0,n.zP)(r);return t?.user||null}},89249:(e,r,t)=>{t.d(r,{$G:()=>f,C0:()=>g,CA:()=>l,Q7:()=>c,zP:()=>m});var n=t(77598),i=t.n(n);let s="omr_local_session";function o(e){return(Buffer.isBuffer(e)?e:Buffer.from(e)).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}function a(e){let r=String(e||"").replace(/-/g,"+").replace(/_/g,"/"),t=r.length%4==0?"":"=".repeat(4-r.length%4);return Buffer.from(r+t,"base64")}function u(){return String(process.env.AUTH_SESSION_SECRET||process.env.NEXTAUTH_SECRET||process.env.AUTH_DB_PASSWORD||"").trim()||"omr-local-session-dev-secret-change-me"}function l(){return String(process.env.AUTH_SESSION_COOKIE_NAME||s).trim()||s}function p(){let e=Number(process.env.AUTH_SESSION_TTL_HOURS||168);return Math.round(3600*(Number.isFinite(e)&&e>0?e:168))}function c(e){let r=Math.floor(Date.now()/1e3),t={v:1,iat:r,exp:r+p(),user:e},n=o(JSON.stringify({alg:"HS256",typ:"JWT"})),s=o(JSON.stringify(t)),a=`${n}.${s}`,l=o(i().createHmac("sha256",u()).update(a).digest());return`${a}.${l}`}function d(e){try{return JSON.parse(e)}catch{return null}}function m(e){let r=String(e||"").trim().split(".");if(3!==r.length)return null;let[t,n,s]=r;if(!t||!n||!s)return null;let l=`${t}.${n}`,p=o(i().createHmac("sha256",u()).update(l).digest()),c=Buffer.from(p),m=Buffer.from(s);if(c.length!==m.length||!i().timingSafeEqual(c,m))return null;let f=d(a(t).toString("utf8"));if(!f||"HS256"!==f.alg||"JWT"!==f.typ)return null;let g=d(a(n).toString("utf8"));if(!g||1!==g.v)return null;let x=Math.floor(Date.now()/1e3);if(!Number.isFinite(g.exp)||g.exp<=x)return null;let h=g.user;return h&&String(h.id||"").trim()&&String(h.email||"").trim()?g:null}function f(e,r){e.cookies.set(l(),r,{httpOnly:!0,sameSite:"lax",secure:!0,path:"/",maxAge:p()})}function g(e){e.cookies.set(l(),"",{httpOnly:!0,sameSite:"lax",secure:!0,path:"/",maxAge:0})}}};var r=require("../../../../../webpack-runtime.js");r.C(e);var t=e=>r(r.s=e),n=r.X(0,[1989,5452,820,5658,4926,8721,4032],()=>t(50229));module.exports=n})();