Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/conode/application/register_company/register_company.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass
from uuid import uuid4

from conode.application.errors import CompanyAlreadyExistsError
from conode.application.interfaces.repositories import (
CompanyRepository,
RolePermissionsRepository,
Expand All @@ -10,7 +9,7 @@
)
from conode.application.interfaces.transaction_manager import TransactionManager
from conode.application.services import AccessControlService, RoleManagmentService
from conode.domain.company import Company, CompanyId, CompanyName
from conode.domain.company import Company, CompanyId
from conode.domain.grant import UserGrant, UserGrantId
from conode.domain.role import (
EntityType,
Expand Down Expand Up @@ -38,15 +37,6 @@ async def execute(self, request: RegisterCompanyRequestDTO) -> Company:
async with self.transaction_manager:
user = await self.access_control_service.get_authorized_user()

company = await self.company_repository.get_by_name(
CompanyName(request.name),
)
if company is not None:
raise CompanyAlreadyExistsError(
"Company with this name already exists",
[{"key": "name", "value": request.name}],
)

company = Company.new(
company_id=CompanyId(uuid4()),
name=request.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""unique-company-name

Revision ID: a3029168e306
Revises: 74a91a825965
Create Date: 2026-08-09 12:07:58.394174

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'a3029168e306'
down_revision: Union[str, Sequence[str], None] = '74a91a825965'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint(None, 'company_record', ['name'])
# ### end Alembic commands ###


def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'company_record', type_='unique')
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion src/conode/infrastructure/persistence/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
ForeignKey("user_record.id", ondelete="CASCADE"),
nullable=False,
),
Column("name", CompanyNameType, nullable=False),
Column("name", CompanyNameType, nullable=False, unique=True),
Column("description", CompanyDescriptionType, nullable=False),
Column("verified", Boolean, nullable=False),
Column("created_at", DateTime(timezone=True), nullable=False),
Expand Down
31 changes: 19 additions & 12 deletions src/conode/infrastructure/repositories/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import structlog
from sqlalchemy import insert, select, update
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession

from conode.application.errors import CompanyNotFoundError
from conode.application.errors import CompanyAlreadyExistsError, CompanyNotFoundError
from conode.application.interfaces.repositories import CompanyRepository
from conode.domain.company.model import Company, CompanyId, CompanyName
from conode.domain.user import UserId
Expand All @@ -18,17 +19,23 @@ class CompanyRepositoryImpl(CompanyRepository):

async def create(self, company: Company) -> None:
logger.debug("Repository create company", company_id=company.id)
await self.session.execute(
insert(Company).values(
id=company.id,
name=company.name,
description=company.description,
verified=company.verified,
owner_id=company.owner_id,
created_at=company.created_at,
updated_at=company.updated_at,
),
)
try:
await self.session.execute(
insert(Company).values(
id=company.id,
name=company.name,
description=company.description,
verified=company.verified,
owner_id=company.owner_id,
created_at=company.created_at,
updated_at=company.updated_at,
),
)
except IntegrityError as e:
raise CompanyAlreadyExistsError(
"Company with this name already exists",
[{"key": "name", "value": company.name.value}],
) from e

async def update(self, company: Company) -> None:
logger.debug("Repository update company", company_id=company.id)
Expand Down
Loading