files support

This commit is contained in:
Robert 2024-03-03 18:34:20 +07:00
parent ad8c293af1
commit 9be715e16a
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22
4 changed files with 55 additions and 5 deletions

View File

@ -2,7 +2,11 @@ from loguru import logger
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from chain_service.database.models.chain import Chain from chain_service.database.models.chain import Chain
from chain_service.dependencies.chain_repository import ChainRepositoryDependency from chain_service.dependencies.chain_repository import ChainRepositoryDependency
from chain_service.dependencies.file_uploader_service import (
FileUploaderServiceDependency,
)
router = APIRouter(prefix="/chain") router = APIRouter(prefix="/chain")
@ -11,9 +15,11 @@ router = APIRouter(prefix="/chain")
async def chain_upsert_controller( async def chain_upsert_controller(
chain: Chain, chain: Chain,
chain_repository: ChainRepositoryDependency, chain_repository: ChainRepositoryDependency,
file_uploader_service: FileUploaderServiceDependency,
): ):
try: try:
upserted_chain = await chain_repository.upsert(chain) upserted_chain = await chain_repository.upsert(chain)
await file_uploader_service.upload_from_chain(upserted_chain)
return upserted_chain return upserted_chain
except Exception: except Exception:

View File

@ -1,5 +1,9 @@
from .planfix_client import PlanfixClientDependency from .planfix_client import PlanfixClientDependency
from chain_service.services.progress_action.factory import ProgressActionServiceFactory from chain_service.services.progress_action.factory import ProgressActionServiceFactory
from chain_service.dependencies.uploaded_file_repository import (
UploadedFileRepositoryDependency,
)
from fastapi import Depends from fastapi import Depends
from typing import Annotated from typing import Annotated
@ -7,8 +11,11 @@ from typing import Annotated
def get_progress_action_service_factory( def get_progress_action_service_factory(
planfix_client: PlanfixClientDependency, planfix_client: PlanfixClientDependency,
uploaded_file_repository: UploadedFileRepositoryDependency,
) -> ProgressActionServiceFactory: ) -> ProgressActionServiceFactory:
return ProgressActionServiceFactory(planfix_client=planfix_client) return ProgressActionServiceFactory(
planfix_client=planfix_client, uploaded_file_repository=uploaded_file_repository
)
ProgressActionServiceFactoryDependency = Annotated[ ProgressActionServiceFactoryDependency = Annotated[

View File

@ -1,10 +1,13 @@
from .base import BaseProgressActionService from .base import BaseProgressActionService
from chain_service.repositories.uploaded_file import UploadedFileRepository
from chain_service.database.models.progress_chain import ( from chain_service.database.models.progress_chain import (
CommentProgressAction, CommentProgressAction,
ProgressChain, ProgressChain,
) )
from typing import List
from loguru import logger
from planfix_client import PlanfixClient from planfix_client import PlanfixClient
@ -13,14 +16,41 @@ class CommentProgressActionService(BaseProgressActionService):
def __init__( def __init__(
self, self,
planfix_client: PlanfixClient, planfix_client: PlanfixClient,
uploaded_file_repository: UploadedFileRepository,
progress_chain: ProgressChain, progress_chain: ProgressChain,
progress_action: CommentProgressAction, progress_action: CommentProgressAction,
): ):
self.planfix_client = planfix_client self.planfix_client = planfix_client
self.uploaded_file_repository = uploaded_file_repository
self.progress_chain = progress_chain self.progress_chain = progress_chain
self.progress_action = progress_action self.progress_action = progress_action
async def process(self): async def process(self):
await self.planfix_client.create_comment( payload = {"task_id": self.progress_chain.task_id}
task_id=self.progress_chain.task_id, description=self.progress_action.text
) if self.progress_action.text:
payload["description"] = self.progress_action.text
if self.progress_action.file_urls:
payload["file_ids"] = await self.get_file_ids()
await self.planfix_client.create_comment(**payload)
async def get_file_ids(self) -> List[int]:
file_ids = list()
for file_url in self.progress_action.file_urls:
uploaded_file = await self.uploaded_file_repository.get_by_file_url(
file_url
)
if not uploaded_file:
logger.error(
f"Uploaded file id not found while running progress_action={self.progress_chain}"
)
continue
file_ids.append(uploaded_file.file_id)
return file_ids

View File

@ -1,3 +1,4 @@
from chain_service.repositories.uploaded_file import UploadedFileRepository
from chain_service.database.models.progress_chain import ( from chain_service.database.models.progress_chain import (
ProgressChain, ProgressChain,
BaseProgressAction, BaseProgressAction,
@ -12,8 +13,13 @@ from planfix_client import PlanfixClient
class ProgressActionServiceFactory: class ProgressActionServiceFactory:
def __init__(self, planfix_client: PlanfixClient): def __init__(
self,
planfix_client: PlanfixClient,
uploaded_file_repository: UploadedFileRepository,
):
self.planfix_client = planfix_client self.planfix_client = planfix_client
self.uploaded_file_repository = uploaded_file_repository
def __call__( def __call__(
self, progress_chain: ProgressChain, progress_action: BaseProgressAction self, progress_chain: ProgressChain, progress_action: BaseProgressAction
@ -29,6 +35,7 @@ class ProgressActionServiceFactory:
case "comment": case "comment":
return CommentProgressActionService( return CommentProgressActionService(
planfix_client=self.planfix_client, planfix_client=self.planfix_client,
uploaded_file_repository=self.uploaded_file_repository,
progress_chain=progress_chain, progress_chain=progress_chain,
progress_action=progress_action, progress_action=progress_action,
) )