from slugify import slugify from atlas_um.audit import admin_audit from atlas_um.helpers.either import Right from atlas_um.helpers.services import BaseLogicService from atlas_um.pgdb import ( BusinessUnit, JobCategory, PersonnelType, ResourceGroup, InternalUserDomain, ClaimName, ClaimValue, Tag, ) from atlas_um.caching import strategies as caching_strategies from atlas_um.consts import SystemEvents from atlas_um.extensions import cache class UpdateBusinessUnitService(BaseLogicService): @admin_audit.track(SystemEvents.updating_business_unit) @cache.invalidated_with(caching_strategies.DNAAccountsDetailsCache) def process(self, obj=None, **kwargs): if obj is None: obj = BusinessUnit() self.session.add(obj) obj.name = kwargs.get("name") if not obj.slug: obj.slug = slugify(obj.name) obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdateJobCategoryService(BaseLogicService): @admin_audit.track(SystemEvents.updating_job_category) @cache.invalidated_with(caching_strategies.DNAAccountsDetailsCache) def process(self, obj=None, **kwargs): if obj is None: obj = JobCategory() self.session.add(obj) obj.name = kwargs.get("name") if not obj.slug: obj.slug = slugify(obj.name) obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdatePersonnelTypeService(BaseLogicService): @admin_audit.track(SystemEvents.updating_personnel_type) @cache.invalidated_with(caching_strategies.DNAAccountsDetailsCache) def process(self, obj=None, **kwargs): if obj is None: obj = PersonnelType() self.session.add(obj) obj.name = kwargs.get("name") if not obj.slug: obj.slug = slugify(obj.name) obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdateTagService(BaseLogicService): @admin_audit.track(SystemEvents.updating_tag) @cache.invalidated_with(caching_strategies.DNAAccountsDetailsCache) def process(self, obj=None, **kwargs): if obj is None: obj = Tag() self.session.add(obj) obj.name = kwargs.get("name") if not obj.slug: obj.slug = slugify(obj.name) obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdateResourceGroupService(BaseLogicService): @admin_audit.track(SystemEvents.updating_resource_group) @cache.invalidated_with(caching_strategies.ResourceGroupsCache) @cache.invalidated_with(caching_strategies.DNAAccountsCache) def process(self, obj=None, **kwargs): if obj is None: obj = ResourceGroup() self.session.add(obj) obj.name = kwargs.get("name") obj.external_id = kwargs.get("external_id") if not obj.namespace_url: obj.namespace_url = slugify(obj.name) if not obj.external_id: obj.external_id = obj.namespace_url obj.service_url = kwargs.get("service_url", "") obj.session_ttl = kwargs.get("session_ttl") obj.active_session_ttl = kwargs.get("active_session_ttl") obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdateInternalUserDomainService(BaseLogicService): @admin_audit.track(SystemEvents.updating_internal_user_domain) def process(self, obj=None, **kwargs): if obj is None: obj = InternalUserDomain() self.session.add(obj) obj.domain = kwargs.get("domain") return Right(obj) class UpdateClaimNameService(BaseLogicService): @admin_audit.track(SystemEvents.updating_claim_name) @cache.invalidated_with(caching_strategies.ClaimNamesCache) def process(self, obj=None, **kwargs): if obj is None: obj = ClaimName() obj.resource_group_id = kwargs.get("resource_group_id") self.session.add(obj) obj.friendly = kwargs.get("friendly") obj.external_id = kwargs.get("external_id") if not obj.path: obj.path = slugify(obj.friendly) if not obj.external_id: obj.external_id = obj.path obj.claim_values_serializer = kwargs.get("claim_values_serializer") obj.claim_values_source = kwargs.get("claim_values_source") obj.parent = kwargs.get("parent") obj.optional = kwargs.get("optional", False) obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdateGlobalClaimNameService(BaseLogicService): @admin_audit.track(SystemEvents.updating_global_claim_name) @cache.invalidated_with(caching_strategies.ClaimNamesCache) def process(self, obj=None, **kwargs): if obj is None: obj = ClaimName() self.session.add(obj) obj.friendly = kwargs.get("friendly") obj.external_id = kwargs.get("external_id") if not obj.path: obj.path = slugify(obj.friendly) if not obj.external_id: obj.external_id = obj.path obj.claim_values_serializer = kwargs.get("claim_values_serializer") obj.parent = kwargs.get("parent") obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj) class UpdateClaimValueService(BaseLogicService): @admin_audit.track(SystemEvents.updating_claim_value) @cache.invalidated_with(caching_strategies.ClaimValuesCache) def process(self, obj=None, **kwargs): if obj is None: obj = ClaimValue() obj.claim_name_id = kwargs.get("claim_name_id") self.session.add(obj) obj.friendly = kwargs.get("friendly") obj.external_id = kwargs.get("external_id") if not obj.external_id: obj.external_id = slugify(obj.friendly) obj.components = kwargs.get("components") or {} obj.parent = kwargs.get("parent") obj.is_deleted = kwargs.get("is_deleted", False) or not kwargs.get( "is_active", True ) return Right(obj)