"use strict";async function getTicket(e,t,s){var i,r,a,o,c,n;try{null===(i=null==s?void 0:s.step)||void 0===i||i.call(s,"Querying database for support ticket");const c=await db_1.models.supportTicket.findOne({where:{id:t,userId:e},include:[{model:db_1.models.user,as:"agent",attributes:["avatar","firstName","lastName","lastLogin"]}]});if(!c){null===(r=null==s?void 0:s.fail)||void 0===r||r.call(s,"Ticket not found");throw(0,error_1.createError)({message:"Ticket not found",statusCode:404})}null===(a=null==s?void 0:s.step)||void 0===a||a.call(s,"Processing ticket data");const n=c.get({plain:!0});if("string"==typeof n.messages)try{n.messages=JSON.parse(n.messages)}catch(e){console_1.logger.warn("SUPPORT","Failed to parse messages JSON");n.messages=[]}if("string"==typeof n.tags)try{n.tags=JSON.parse(n.tags)}catch(e){console_1.logger.warn("SUPPORT","Failed to parse tags JSON");n.tags=[]}n.messages=n.messages||[];n.tags=n.tags||[];null===(o=null==s?void 0:s.success)||void 0===o||o.call(s,"Ticket data processed successfully");return n}catch(e){if(e.statusCode){null===(c=null==s?void 0:s.fail)||void 0===c||c.call(s,e.message);throw e}console_1.logger.error("SUPPORT","Error fetching ticket",e);null===(n=null==s?void 0:s.fail)||void 0===n||n.call(s,"Failed to fetch ticket");throw(0,error_1.createError)({statusCode:500,message:"Failed to fetch ticket"})}}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;exports.getTicket=getTicket;const error_1=require("@b/utils/error"),db_1=require("@b/db"),console_1=require("@b/utils/console"),query_1=require("@b/utils/query");exports.metadata={summary:"Retrieves a single ticket details for the logged-in user",description:"Fetches detailed information about a specific support ticket identified by its ID, including associated chat details.",operationId:"getTicket",tags:["Support"],requiresAuth:!0,logModule:"USER",logTitle:"Get support ticket",parameters:[{index:0,name:"id",in:"path",required:!0,description:"The ID of the ticket to retrieve",schema:{type:"string"}}],responses:{200:{description:"Ticket details retrieved successfully",content:{"application/json":{schema:{type:"object",properties:{status:{type:"boolean",description:"Indicates if the request was successful"},statusCode:{type:"number",description:"HTTP status code",example:200},data:{type:"object",properties:{id:{type:"string",description:"ID of the ticket"},userId:{type:"string",description:"ID of the user who created the ticket"},agentId:{type:"string",description:"ID of the agent assigned to the ticket"},subject:{type:"string",description:"Subject of the ticket"},importance:{type:"string",description:"Importance level of the ticket"},status:{type:"string",description:"Status of the ticket"},messages:{type:"array",description:"Messages associated with the ticket"},createdAt:{type:"string",format:"date-time",description:"Date and time the ticket was created"},updatedAt:{type:"string",format:"date-time",description:"Date and time the ticket was updated"}}}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Support Ticket"),500:query_1.serverErrorResponse}};exports.default=async e=>{var t,s,i;const{user:r,params:a,ctx:o}=e;if(!(null==r?void 0:r.id)){null===(t=null==o?void 0:o.fail)||void 0===t||t.call(o,"User not authenticated");throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"})}null===(s=null==o?void 0:o.step)||void 0===s||s.call(o,"Retrieving support ticket");const c=await getTicket(r.id,a.id,o);null===(i=null==o?void 0:o.success)||void 0===i||i.call(o,"Support ticket retrieved successfully");return c};