"use strict";async function updateTags(t,e,s){await db_1.models.postTag.destroy({where:{postId:t.id},transaction:s});const o=[];for(const t of e){const e=(0,utils_1.slugify)(t.toLowerCase());let i=await db_1.models.tag.findOne({where:{slug:e},transaction:s});i||(i=await db_1.models.tag.create({name:t,slug:e},{transaction:s}));o.push(i)}await db_1.models.postTag.bulkCreate(o.map(e=>({postId:t.id,tagId:e.id})),{transaction:s})}Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const utils_1=require("@b/utils"),db_1=require("@b/db"),error_1=require("@b/utils/error");exports.metadata={summary:"Updates a blog post identified by id",description:"This endpoint updates an existing blog post.",operationId:"updatePost",tags:["Admin","Content"],requiresAuth:!0,parameters:[{index:0,name:"id",in:"path",description:"The id of the blog post to update",required:!0,schema:{type:"string",description:"Post Slug"}}],requestBody:{required:!0,description:"Updated blog post data",content:{"application/json":{schema:{type:"object",properties:{title:{type:"string",description:"Title of the post"},content:{type:"string",description:"Content of the post"},description:{type:"string",description:"Description of the post"},categoryId:{type:"string",description:"Category ID for the post"},status:{type:"string",description:"New status of the blog post",enum:["PUBLISHED","DRAFT"]},tags:{type:"array",description:"Array of tag names associated with the post",items:{type:"string"}}},required:["title","content","categoryId","status"]}}}},responses:{200:{description:"Blog post updated successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string",description:"Confirmation message of successful author creation"}}}}}},401:{description:"Unauthorized, user must be authenticated"},404:{description:"Blog post not found"},500:{description:"Internal server error"}},permission:"edit.blog.post",logModule:"ADMIN_BLOG",logTitle:"Update blog post with tags"};exports.default=async t=>{const{params:e,body:s,ctx:o}=t,{id:i}=e,{content:a,tags:n,category:r,description:d,title:p,status:c}=s;null==o||o.step("Validating blog post ID and data");return await db_1.sequelize.transaction(async t=>{null==o||o.step("Checking if post exists");const e=await db_1.models.post.findOne({where:{id:i},include:[{model:db_1.models.postTag,as:"postTags"}],transaction:t});if(!e)throw(0,error_1.createError)({statusCode:404,message:"Post not found or you don't have permission to edit it."});null==o||o.step("Updating post fields");e.title=p;e.content=a;e.description=d;e.status=c;await e.save();if(r){null==o||o.step("Updating post category");await e.setCategory(r,{transaction:t})}if(n){null==o||o.step(`Updating ${n.length} tags`);await updateTags(e,n,t)}null==o||o.success("Post updated successfully with tags");return{message:"Post updated successfully"}}).catch(t=>{null==o||o.fail("Failed to update post");throw t})};