"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),error_1=require("@b/utils/error"),query_1=require("@b/utils/query"),utils_1=require("../utils");exports.metadata={summary:"Update a KYC Application",description:"Updates an existing KYC application for the authenticated user. Expects a JSON payload with a 'fields' object containing key/value pairs for each field as defined in the KYC level configuration. The application to update is identified by the 'id' parameter in the path. The level is derived from the existing application record.",operationId:"updateKycApplication",tags:["KYC","Application"],requiresAuth:!0,logModule:"KYC",logTitle:"Update KYC application",parameters:[{index:0,name:"id",in:"path",description:"The ID of the KYC application to update",required:!0,schema:{type:"string"}}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{fields:{type:"object",description:"An object where keys are field IDs and values are the submitted data"}},required:["fields"]}}}},responses:{200:{description:"KYC application updated successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"},application:{type:"object"}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("KYC Application"),500:query_1.serverErrorResponse}};exports.default=async e=>{const{user:t,body:i,params:a,ctx:r}=e;if(!t)throw(0,error_1.createError)({statusCode:401,message:"Authentication required"});null==r||r.step("Validating request parameters");const{id:o}=a;if(!o){null==r||r.fail("Missing application ID in path");throw(0,error_1.createError)({statusCode:400,message:"Missing application id in path"})}const{fields:s}=i;if(!s||"object"!=typeof s){null==r||r.fail("Missing or invalid fields in request body");throw(0,error_1.createError)({statusCode:400,message:"Missing or invalid required field: fields"})}null==r||r.step("Retrieving existing KYC application");const n=await db_1.models.kycApplication.findOne({where:{id:o,userId:t.id}});if(!n){null==r||r.fail("KYC application not found");throw(0,error_1.createError)({statusCode:404,message:"KYC application not found"})}null==r||r.step("Validating application update eligibility");if(!["ADDITIONAL_INFO_REQUIRED","REJECTED"].includes(n.status)){const e={PENDING:"Your application is currently under review and cannot be modified.",APPROVED:"Your application has already been approved and cannot be modified."};null==r||r.fail(`Application status ${n.status} cannot be updated`);throw(0,error_1.createError)({statusCode:400,message:e[n.status]||`Applications with status "${n.status}" cannot be updated.`})}null==r||r.step("Retrieving KYC level configuration");const l=n.levelId,d=await db_1.models.kycLevel.findByPk(l);if(!d){null==r||r.fail("KYC level not found");throw(0,error_1.createError)({statusCode:404,message:"KYC level not found"})}null==r||r.step("Parsing KYC level field configuration");let p=d.fields;if("string"==typeof p)try{p=JSON.parse(p)}catch(e){null==r||r.fail("Failed to parse KYC level configuration");throw(0,error_1.createError)({statusCode:500,message:"Invalid KYC level configuration: unable to parse fields"})}if(!Array.isArray(p)){null==r||r.fail("Invalid KYC level configuration format");throw(0,error_1.createError)({statusCode:500,message:"Invalid KYC level configuration"})}null==r||r.step(`Validating ${p.length} updated fields`);for(const e of p){const t=s[e.id],i=(0,utils_1.validateKycField)(e,t);if(i){null==r||r.fail(`Field validation failed for: ${e.id}`);throw(0,error_1.createError)({statusCode:400,message:`Validation error for field "${e.id}": ${i}`})}}try{null==r||r.step("Updating KYC application record");await n.update({data:s,updatedAt:new Date,status:"PENDING"});null==r||r.success("KYC application updated and resubmitted for review");return{message:"KYC application updated successfully.",application:n}}catch(e){null==r||r.fail(`Failed to update application: ${e.message}`);throw(0,error_1.createError)({statusCode:500,message:e.message||"Internal Server Error."})}};