import{entitiesRelationsDtoSchema as d,entityRelationDtoSchema as c}from"../../../plugins/catalog-entities/schemas/dto-schemas.js";import{CatalogEntitiesService as g}from"../../../plugins/catalog-entities/database/catalog-entities-service.js";import{createPaginationParamsValidator as m}from"../../../providers/database/pagination/schemas.js";import{createValidator as l}from"../../../plugins/catalog-entities/utils/ajv-validator.js";import{ENTITY_RELATION_TO_DATABASE as y}from"../../../plugins/catalog-entities/database/mappers/field-transformations.js";import{CacheService as u}from"../../../persistence/cache/services/cache-service.js";import{CATALOG_FILTERS_CACHE_NAMESPACE as E}from"../../../constants/plugins/catalog-entities.js";const R=l(c,{errorPrefix:"Entity relation validation failed:",dataVar:"relation"}),f=l(d,{errorPrefix:"Entities relations validation failed:",dataVar:"relation"}),p=["id","organizationId","projectId","sourceKey","targetKey","sourceId","targetId","type","createdAt","updatedAt"],s=async a=>{await(await u.getInstance({baseDbDir:a})).deleteByNamespace(E)},j=async(a,e)=>{const n=e.req.param("entityRelationId");if(!n)return e.json({message:"Entity relation id is required"},400);const r=await a.getEntityRelationById(n);return r?e.json(r):e.json({message:"Entity relation not found"},404)},I=async(a,e)=>{try{const n=e.req.query(),r=m(p,y).parse(n),t=await a.getEntitiesRelations(r);return e.json(t)}catch(n){return console.error(n),e.json({message:"Failed to get entities relations"},500)}},w=async(a,e,n)=>{const r=e.get("logger");try{const t=await e.req.json(),i=R(t),o=await a.createEntityRelation(i);return o?(s(n),e.json(o)):e.json({message:"Failed to create entity relation"},500)}catch(t){return r.error(t),t instanceof Error&&t.message.includes("validation failed")?e.json({message:t.message},400):e.json({message:"Failed to create entity relation"},500)}},h=async(a,e,n)=>{const r=e.get("logger");try{const t=await e.req.json(),i=f(t),o=await a.createEntitiesRelations(i);return o.length?(s(n),e.json(o,207)):e.json({message:"Failed to create entity relations"},500)}catch(t){return r.error(t),t instanceof Error&&t.message.includes("validation failed")?e.json({message:t.message},400):e.json({message:"Failed to create entity relations"},500)}},q=async(a,e,n)=>{const r=e.get("logger"),t=e.req.param("entityRelationId");if(!t)return e.json({message:"Entity relation id is required"},400);try{const i=await e.req.json(),o=await a.updateEntityRelation(t,i);return o?(s(n),e.json(o)):e.json({message:"Failed to update entity"},500)}catch(i){return r.error(i),i instanceof Error&&i.message.includes("validation failed")?e.json({message:i.message},400):e.json({message:i.message},500)}},v=async(a,e,n)=>{const r=e.req.param("entityRelationId");return r?(await a.deleteEntityRelation(r),s(n),new Response(null,{status:204})):e.json({message:"Entity relation id is required"},400)},T={GET:I,POST:w,PUT:h},A={GET:j,DELETE:v,PATCH:q};function _(a){return async e=>{const n=await g.getInstance({baseDbDir:a.serverOutDir}),r=e.req.method,t=e.req.param("entityRelationId")?A:T;return t[r]?await t[r](n,e,a.serverOutDir):e.json({message:"Method not allowed"},405)}}export{_ as catalogRelationsHandler};
