"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const error_1=require("@b/utils/error"),db_1=require("@b/db");exports.metadata={summary:"Updates an API key",description:"Updates an API key's details such as permissions, IP whitelist, or IP restriction.",operationId:"updateApiKey",tags:["API Key Management"],logModule:"USER",logTitle:"Update API key",parameters:[{index:0,name:"id",in:"path",required:!0,description:"The ID of the API key to update",schema:{type:"string"}}],requestBody:{description:"Data for updating the API key",content:{"application/json":{schema:{type:"object",properties:{permissions:{type:"array",items:{type:"string"},description:"Updated permissions associated with the API key"},ipWhitelist:{type:"array",items:{type:"string"},description:"Updated IP whitelist for the API key"},ipRestriction:{type:"boolean",description:"Updated IP restriction setting (true for restricted, false for unrestricted)"}}}}}},responses:{200:{description:"API key updated successfully",content:{"application/json":{schema:{type:"object",properties:{id:{type:"string"},permissions:{type:"array",items:{type:"string"}},ipWhitelist:{type:"array",items:{type:"string"}},ipRestriction:{type:"boolean"}}}}}},401:{description:"Unauthorized"},404:{description:"API key not found"},500:{description:"Server error"}},requiresAuth:!0};exports.default=async e=>{const{user:t,params:i,body:s,ctx:r}=e;if(!t){null==r||r.fail("User not authenticated");throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"})}const{id:o}=i,{permissions:n,ipWhitelist:a,ipRestriction:p}=s;null==r||r.step("Finding API key");const d=await db_1.models.apiKey.findOne({where:{id:o,userId:t.id}});if(!d){null==r||r.fail("API Key not found");throw(0,error_1.createError)({statusCode:404,message:"API Key not found"})}null==r||r.step("Validating and preparing update fields");const c={};void 0!==n&&(c.permissions=n);void 0!==a&&(c.ipWhitelist=a);void 0!==p&&(c.ipRestriction=Boolean(p));null==r||r.step("Updating API key");const u=await d.update(c);null==r||r.success("API Key updated successfully");return u};