Skip to content

API Reference parrot_formdesigner.services.project_service

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.services.project_service

View source

ProjectService — CRUD sobre fieldsync.projects + mapping Workday.

Implementa el system of record de proyectos internos (FEAT-302 Module 2):

  • create_project / get_project / list_projects
  • map_to_workday (upsert en fieldsync.workday_cost_center_mappings)

Reglas de negocio confirmadas (§8 spec):

  • accounting_code es el cost center interno; NUNCA se importa desde Workday (Workday es destino de salida, no fuente).
  • UNIQUE (client_id, accounting_code) — duplicate → DuplicateAccountingCodeError.
  • map_to_workday hace UPSERT por project_id (un proyecto tiene como máximo un Workday mapping).
  • NUNCA se escribe en networkninja.projects.

Diseño:

  • Pool inyectado en constructor → testable sin DB real.
  • SQL 100% parametrizado ($1, $2…); nombres de tabla fijados en constantes.
  • Pydantic v2 para modelos de datos.

Uso::

svc = ProjectService(pool)
proj = await svc.create_project(
    accounting_code="ACC-001",
    name="Q1 Campaign",
    client_id=42,
    org_id=7,
    tenant="acme",
)
mapping = await svc.map_to_workday(proj.project_id, "WD-99001", tenant="acme")

Class DuplicateAccountingCodeError

Raised when (client_id, accounting_code) already exists.

Parameter Type Default Description
client_id int
accounting_code str

Attributes:

Attribute Type Description
client_id Client the conflict belongs to.
accounting_code The duplicate code.

Class Project

A project stored in fieldsync.projects.

Fields:

Field Type Default Description
project_id int Auto-incremented serial PK.
name str | None Human-readable project name.
accounting_code str Internal cost center code (source of truth).
client_id int Client this project belongs to.
org_id int | None Organization identifier.
start_timestamp datetime | None None Optional project start date.
end_timestamp datetime | None None Optional project end date.
is_active bool True Whether the project is currently active.
tenant str | None None Tenant slug (metadata, not stored in the table directly).

Class ProjectNotFoundError

Raised when a project lookup returns no row.

Parameter Type Default Description
project_id int

Attributes:

Attribute Type Description
project_id The missing project identifier.

Class ProjectService

CRUD service for fieldsync.projects and Workday mappings.

Example::

svc = ProjectService(pool)
proj = await svc.create_project(
    accounting_code="ACC-001",
    name="My Project",
    client_id=42,
    org_id=7,
    tenant="acme",
)
await svc.map_to_workday(proj.project_id, "WD-12345", tenant="acme")
Parameter Type Default Description
pool Any asyncpg pool (or fake pool for tests).

Method ProjectService.create_project

Create a new project in fieldsync.projects.

create_project(*, accounting_code: str, name: str | None = None, client_id: int, org_id: int | None = None, tenant: str | None = None) -> Project

Parameter Type Default Description
accounting_code str Internal cost center code (UNIQUE per client).
name str | None None Human-readable project name.
client_id int Client this project belongs to.
org_id int | None None Organization identifier.
tenant str | None None Tenant slug (stored in the returned model metadata only).

Returns: Project — The newly created Project with its DB-assigned project_id.

Raises:

  • DuplicateAccountingCodeError — If (client_id, accounting_code) already exists.

Method ProjectService.get_project

Retrieve a project by its primary key, scoped to org_id.

get_project(project_id: int, *, org_id: int, tenant: str | None = None) -> Project

Parameter Type Default Description
project_id int Primary key of the project.
org_id int Organization the caller is scoped to (hard isolation).
tenant str | None None Optional tenant slug (stored in returned model).

Returns: Project — Project model populated from the DB row.

Raises:

  • ProjectNotFoundError — If no project with project_id exists within org_id.

Method ProjectService.list_projects

List projects within org_id (hard isolation), optionally by client.

list_projects(*, org_id: int, client_id: int | None = None, tenant: str | None = None) -> list[Project]

Parameter Type Default Description
org_id int Organization the caller is scoped to (REQUIRED — every query is filtered by it; there is no cross-tenant list path).
client_id int | None None Optionally narrow to a single client within the org.
tenant str | None None Tenant slug stored in returned models.

Returns: list[Project] — List of Project instances (empty list if none match).

Method ProjectService.map_to_workday

Create or update a Workday cost center mapping for a project.

Uses UPSERT — if a mapping already exists for project_id, its workday_code is updated in place.

map_to_workday(project_id: int, workday_code: str, *, tenant: str | None = None, direction: str = 'internal_to_workday') -> WorkdayCostCenterMapping

Parameter Type Default Description
project_id int FK to fieldsync.projects.
workday_code str Workday cost center identifier.
tenant str | None None Tenant slug (not stored in the mapping table; used for logs).
direction str 'internal_to_workday' Flow direction (default "internal_to_workday").

Returns: WorkdayCostCenterMapping — WorkdayCostCenterMapping reflecting the persisted row.

Raises:

  • Exception — Any DB error (e.g. FK violation if project_id unknown).

Class WorkdayCostCenterMapping

Mapping from an internal project to a Workday cost center code.

Fields:

Field Type Default Description
project_id int FK to fieldsync.projects.
workday_code str Workday cost center identifier (output side only).
direction str 'internal_to_workday' Flow direction; default is "internal_to_workday".

Alias is_unique_violation

Re-exported from parrot_formdesigner.services._db_utils.is_unique_violation.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally