"use strict";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"),sequelize_1=require("sequelize"),error_1=require("@b/utils/error");exports.metadata={summary:"Updates an existing page",operationId:"updatePage",tags:["Admin","Content","Page"],parameters:[{index:0,name:"id",in:"path",description:"ID of the page to update",required:!0,schema:{type:"string"}}],requestBody:{required:!0,description:"Updated data for the page",content:{"application/json":{schema:utils_1.pageUpdateSchema}}},responses:(0,query_1.updateRecordResponses)("Page"),requiresAuth:!0,permission:"edit.page",logModule:"ADMIN_CMS",logTitle:"Update page"};exports.default=async e=>{const{body:t,params:s,user:a,ctx:r}=e,{id:i}=s;null==r||r.step("Validating page data");if(t.settings)try{JSON.parse(t.settings)}catch(e){null==r||r.fail("Invalid settings JSON");throw(0,error_1.createError)({statusCode:400,message:"settings: Must be valid JSON"})}null==r||r.step("Preparing update data");const o={lastModifiedBy:(null==a?void 0:a.id)||null};["title","content","description","image","slug","status","order","isHome","isBuilderPage","template","category","seoTitle","seoDescription","seoKeywords","ogImage","ogTitle","ogDescription","settings","customCss","customJs"].forEach(e=>{if("slug"===e&&(void 0===t[e]||null===t[e]||""===t[e])){null==r||r.fail("Slug is required");throw(0,error_1.createError)({statusCode:400,message:"slug: Slug is required."})}void 0!==t[e]&&(o[e]=t[e])});if(!0===o.isHome){null==r||r.step("Validating home page constraints");if(await db_1.models.page.findOne({where:{isHome:!0,id:{[sequelize_1.Op.ne]:i}}})){null==r||r.fail("Another page is already set as home page");throw(0,error_1.createError)({statusCode:409,message:"isHome: Only one page can be marked as home page. Please unset home on the other page first."})}}null==r||r.step("Updating page in database");const u=await(0,query_1.updateRecord)("page",i,o);null==r||r.success("Page updated successfully");return u};