"use strict";function sanitizeAdminNotes(e){if(!e)return"";let t=e.replace(/<[^>]*>/g,"");t=t.replace(/javascript:/gi,"");t=t.replace(/on\w+\s*=/gi,"");t=t.replace(/<script[^>]*>.*?<\/script>/gis,"");t=t.replace(/(\b(SELECT|INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|EXEC|UNION)\b)/gi,"");t=t.replace(/\s+/g," ").trim();t=t.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g,"");return t}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const query_1=require("@b/utils/query"),utils_1=require("../utils"),db_1=require("@b/db"),error_1=require("@b/utils/error"),emails_1=require("@b/utils/emails"),redis_1=require("@b/utils/redis");exports.metadata={summary:"Updates an existing KYC application",operationId:"updateKycApplication",tags:["Admin","CRM","KYC"],parameters:[{index:0,name:"id",in:"path",description:"ID of the KYC application to update",required:!0,schema:{type:"string"}}],requestBody:{required:!0,description:"Updated data for the KYC application",content:{"application/json":{schema:utils_1.kycUpdateSchema}}},responses:(0,query_1.updateRecordResponses)("KYC Application"),requiresAuth:!0,permission:"edit.kyc.application",logModule:"ADMIN_CRM",logTitle:"Update KYC application"};exports.default=async e=>{const{body:t,params:i,ctx:a}=e,{id:r}=i,{status:s,adminNotes:n}=t;null==a||a.step(`Updating KYC application ${r}`);if(void 0!==n){if("string"!=typeof n)throw(0,error_1.createError)({statusCode:400,message:"Admin notes must be a string"});if(n.length>5e3)throw(0,error_1.createError)({statusCode:400,message:"Admin notes cannot exceed 5000 characters"});const e=sanitizeAdminNotes(n);e!==n&&console.warn("Admin notes contained potentially dangerous content and were sanitized");t.adminNotes=e}if(void 0!==s){const e=["PENDING","APPROVED","REJECTED","ADDITIONAL_INFO_REQUIRED"];if(!e.includes(s))throw(0,error_1.createError)({statusCode:400,message:`Invalid status. Must be one of: ${e.join(", ")}`})}null==a||a.step("Fetching KYC application");const o=await db_1.models.kycApplication.findByPk(r,{include:[{model:db_1.models.user,as:"user"}]});if(!o)throw(0,error_1.createError)(404,"KYC application not found");const d=o.status;null==a||a.step("Updating application fields");s&&(o.status=s);void 0!==t.adminNotes&&(o.adminNotes=t.adminNotes);o.reviewedAt=new Date;await o.save();if(s&&s!==d)try{const e=redis_1.RedisSingleton.getInstance();await e.del(`user:${o.userId}:profile`)}catch(e){console.error("Error clearing user cache:",e)}const l=await db_1.models.kycLevel.findByPk(o.levelId);o.level=l?l.level:"N/A";let c=null;"APPROVED"===s?c="KycApproved":"REJECTED"===s?c="KycRejected":"ADDITIONAL_INFO_REQUIRED"===s&&(c="KycUpdate");if(c){null==a||a.step(`Sending ${c} email notification`);try{await(0,emails_1.sendKycEmail)(o.user,o,c,a)}catch(e){null==a||a.warn("Failed to send KYC email notification");console.error("Error sending KYC email:",e)}}null==a||a.success(`KYC application updated to ${s||d}`);return{message:"KYC application updated successfully"}};