File size: 1,455 Bytes
b4f2238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Business logic services."""

from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import ClassVar

from apps.core.filesystem.folder_node_path import normalize_folder_node_path
from apps.core.protocols import IDocumentTemplateBindingService

logger = logging.getLogger("apps.contracts")


@dataclass(frozen=True)
class ContractSubdirPathResolver:
    template_binding_service: IDocumentTemplateBindingService | None

    SUBDIR_KEY_TO_CONTRACT_SUB_TYPE: ClassVar = {
        "contract_documents": "contract",
        "supplementary_agreements": "supplementary_agreement",
    }

    def resolve(self, *, case_type: str, subdir_key: str) -> str | None:
        contract_sub_type = self.SUBDIR_KEY_TO_CONTRACT_SUB_TYPE.get(subdir_key)
        if not contract_sub_type:
            return None

        if self.template_binding_service is None:
            return None

        try:
            folder_node_path = self.template_binding_service.get_contract_subdir_path_internal(
                case_type=case_type,
                contract_sub_type=contract_sub_type,
            )
            if not folder_node_path:
                return None
            return normalize_folder_node_path(folder_node_path)
        except Exception:
            logger.exception(
                "resolve_contract_subdir_path_failed", extra={"case_type": case_type, "subdir_key": subdir_key}
            )
            raise