import{GenericError as e}from"../utils/errors/ErrorsClass.mjs";import{validateOrganization as t}from"../utils/validation/validateOrganization.mjs";import{OrganizationModel as n}from"../models/organization.model.mjs";const r=async(e,t,r,i)=>{let a=n.find(e).skip(t).limit(r);return i&&Object.keys(i).length>0&&(a=a.sort(i)),await a},i=async t=>{let r=await n.findById(t);if(!r)throw new e(`ORGANIZATION_NOT_FOUND`,{organizationId:t});return r},a=async t=>{let r=await n.countDocuments(t);if(r===void 0)throw new e(`ORGANIZATION_COUNT_FAILED`,{filters:t});return r},o=async(r,i)=>{let a=t(r,[`name`]);if(Object.keys(a).length>0)throw new e(`ORGANIZATION_INVALID_FIELDS`,{errors:a});try{return await n.create({creatorId:i,membersIds:[i],adminsIds:[i],...r})}catch(t){throw new e(`ORGANIZATION_CREATION_FAILED`,{error:t.message})}},s=async(r,a)=>{let o=t(a,Object.keys(a));if(Object.keys(o).length>0)throw new e(`ORGANIZATION_INVALID_FIELDS`,{organizationId:r,errors:o});if((await n.updateOne({_id:r},a)).matchedCount===0)throw new e(`ORGANIZATION_UPDATE_FAILED`,{organizationId:r});return await i(r)},c=async t=>{let r=await n.findByIdAndDelete(t);if(!r)throw new e(`ORGANIZATION_NOT_FOUND`,{organizationId:t});return r},l=async(t,r)=>{let a=t.plan??{};if(typeof a?.toObject==`function`&&(a=a.toObject()),(await n.updateOne({_id:t.id},{$set:{plan:{...a,...r}}})).matchedCount===0)throw new e(`ORGANIZATION_UPDATE_FAILED`,{organizationId:t.id});return await i(t.id)};export{a as countOrganizations,o as createOrganization,c as deleteOrganizationById,r as findOrganizations,i as getOrganizationById,s as updateOrganizationById,l as updatePlan};
//# sourceMappingURL=organization.service.mjs.map