"use strict";async function getOrCreateLiveChat(e,t){var i,r,a,s,l;try{null===(i=null==t?void 0:t.step)||void 0===i||i.call(t,"Checking for existing live chat ticket");let l=await db_1.models.supportTicket.findOne({where:{userId:e,type:"LIVE",status:{[sequelize_1.Op.ne]:"CLOSED"}},include:[{model:db_1.models.user,as:"agent",attributes:["avatar","firstName","lastName","lastLogin"]}]});if(l)null===(s=null==t?void 0:t.success)||void 0===s||s.call(t,"Existing live chat ticket found");else{null===(r=null==t?void 0:t.step)||void 0===r||r.call(t,"Creating new live chat ticket");l=await db_1.models.supportTicket.create({userId:e,type:"LIVE",subject:"Live Chat",messages:[],importance:"LOW",status:"PENDING"});null===(a=null==t?void 0:t.success)||void 0===a||a.call(t,"New live chat ticket created")}return l.get({plain:!0})}catch(e){null===(l=null==t?void 0:t.fail)||void 0===l||l.call(t,e.message);throw e}}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;exports.getOrCreateLiveChat=getOrCreateLiveChat;const db_1=require("@b/db"),error_1=require("@b/utils/error"),query_1=require("@b/utils/query"),sequelize_1=require("sequelize");exports.metadata={summary:"Retrieves or creates a live chat ticket",description:"Fetches the existing live chat ticket for the authenticated user, or creates a new one if none exists.",operationId:"getOrCreateLiveChat",tags:["Support"],requiresAuth:!0,logModule:"USER",logTitle:"Get or create live chat",responses:(0,query_1.createRecordResponses)("Support Ticket")};exports.default=async e=>{var t,i,r;const{user:a,ctx:s}=e;if(!(null==a?void 0:a.id)){null===(t=null==s?void 0:s.fail)||void 0===t||t.call(s,"User not authenticated");throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"})}null===(i=null==s?void 0:s.step)||void 0===i||i.call(s,"Getting or creating live chat session");const l=await getOrCreateLiveChat(a.id,s);null===(r=null==s?void 0:s.success)||void 0===r||r.call(s,"Live chat session retrieved");return l};