Files
sakhteman/app/repositories/design_progress.py
T

105 lines
4.4 KiB
Python
Raw Normal View History

2024-02-28 06:12:45 +03:30
import copy
import uuid
from typing import List
from uuid import UUID
2024-03-09 23:01:16 +03:30
from fastapi import HTTPException
2024-03-11 00:32:57 +03:30
from app.models.design_progress import DesignProgressModel, CreateDesignProgress, RecordModel, GetRecord, \
UpdateDesignProgress
2024-02-28 06:12:45 +03:30
from core.repositories.base import BaseRepository
from core.service.mongo_db import use_collection
from core.utils.crudify import mongo_soft_deleter
COLLECTION = "design_progress"
2024-03-23 15:42:14 +03:30
2024-03-25 20:44:47 +03:30
class DesignProgressRepository(BaseRepository):
def __init__(self):
2024-02-28 06:12:45 +03:30
super().__init__(collection=COLLECTION, model=CreateDesignProgress)
2024-03-11 00:32:57 +03:30
2024-03-23 15:42:14 +03:30
async def create(self, _inp: CreateDesignProgress):
2024-03-11 00:32:57 +03:30
exist = await use_collection(COLLECTION).find_one({
2024-03-23 15:42:14 +03:30
"project_id": _inp.project_id,
"type": _inp.type,
"sub_type": _inp.sub_type
2024-03-11 00:32:57 +03:30
})
if not exist:
progress = DesignProgressModel(
project_id=_inp.project_id,
type=_inp.type,
parent_id=_inp.parent_id,
sub_type=_inp.sub_type,
)
progress.init_id().cu_at()
await use_collection(COLLECTION).insert_one(progress.mongo_dict())
get_record = GetRecord(
2024-02-28 06:12:45 +03:30
project_id=_inp.project_id,
type=_inp.type,
2024-03-11 00:32:57 +03:30
sub_type=_inp.sub_type
2024-03-07 16:13:09 +03:30
)
return await self.get(get_record)
2024-02-28 06:12:45 +03:30
2024-03-23 15:42:14 +03:30
async def get(self, get_record: GetRecord):
progress = await use_collection(COLLECTION).find_one(
{"project_id": get_record.project_id, "type": get_record.type, "sub_type": get_record.sub_type})
2024-03-09 23:01:16 +03:30
if progress:
2024-03-23 15:42:14 +03:30
progress['records'] = await use_collection('design_progress_record').find(
{"design_progress_id": progress["_id"]}).to_list(None)
2024-02-28 06:12:45 +03:30
return progress
2024-03-11 00:32:57 +03:30
async def update(self, _id: UUID, _inp: UpdateDesignProgress):
2024-03-24 21:37:56 +03:30
start_at = None
2024-03-23 15:42:14 +03:30
design_progress = await use_collection(COLLECTION).find_one({"_id": _id})
2024-03-11 00:32:57 +03:30
design_progress = DesignProgressModel(**design_progress)
2024-03-24 22:07:08 +03:30
if design_progress.type == 1:
2024-03-24 21:37:56 +03:30
records = await use_collection('design_progress_record').find_one(
{"design_progress_id": _id})
start_at = records['send']['date_at']
else:
design_progress_before = await use_collection(COLLECTION).find_one({
"type":design_progress.type-1,"project_id":design_progress.project_id
})
if design_progress_before and "complete_at" in design_progress_before:
start_at = design_progress_before["complete_at"]
2024-03-23 15:42:14 +03:30
await use_collection(COLLECTION).update_one({"_id": _id}, {"$set": {
"start_at": start_at, "complete_at": _inp.complete_at
2024-03-11 00:32:57 +03:30
}})
2024-03-14 14:52:02 +03:30
if design_progress.type == 1:
2024-03-23 15:42:14 +03:30
await use_collection('project').update_one({"_id": design_progress.project_id}, {"$set": {"sts": 2}})
2024-03-14 14:52:02 +03:30
elif design_progress.type == 7:
await use_collection('project').update_one({"_id": design_progress.project_id}, {"$set": {"sts": 4}})
2024-03-11 00:32:57 +03:30
get_record = GetRecord(
project_id=design_progress.project_id,
type=design_progress.type,
sub_type=design_progress.sub_type
)
return await self.get(get_record)
2024-03-23 15:42:14 +03:30
async def check_complete(self, get_record: GetRecord):
result = {
"exist": False
2024-03-23 15:40:15 +03:30
}
2024-03-23 15:42:14 +03:30
if get_record.type:
2024-03-23 14:28:05 +03:30
if get_record.type > 1:
progress = await use_collection(COLLECTION).find_one(
{"project_id": get_record.project_id, "type": int(get_record.type) - 1,
2024-03-23 16:47:10 +03:30
"complete_at": {"$exists": 1},"sub_type":{"$exists":0}})
2024-03-23 14:28:05 +03:30
if not progress:
2024-03-23 15:42:14 +03:30
return {"exist": True, "detail": 'before type not complete'}
2024-03-23 14:26:21 +03:30
if get_record.type < 7:
progress = await use_collection(COLLECTION).find(
2024-03-23 15:42:14 +03:30
{"project_id": get_record.project_id, "type": int(get_record.type) + 1}).to_list(None)
2024-03-23 14:26:21 +03:30
if progress:
ids = [i["_id"] for i in progress]
records = await use_collection('design_progress_record').find_one(
2024-03-23 15:42:14 +03:30
{"design_progress_id": {"$in": ids}})
2024-03-23 14:26:21 +03:30
if records:
2024-03-23 15:42:14 +03:30
return {"exist": True, "detail": "next record is exist"}
2024-03-23 14:28:05 +03:30
2024-03-23 14:26:21 +03:30
return result
2024-03-25 20:44:47 +03:30
class DesignProgressRecordRepository(BaseRepository):
def __init__(self):
2024-03-11 00:32:57 +03:30
super().__init__(collection='design_progress_record', model=RecordModel)
2024-03-25 20:44:47 +03:30