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
24 changes: 24 additions & 0 deletions modal_backend/routes/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
NoteInfoPost,
NoteRatingGet,
NoteRatingPost,
NoteStatus,
NoteTextGet,
NoteTextPost,
NotificationGet,
Expand Down Expand Up @@ -185,3 +186,26 @@ async def create_note_images(
session=db.session, type_id=5, **note.model_dump(), admin_id=user.get("id"), status=ModalStatus.ACTIVE
)
return NoteImageGet.model_validate(new_note)


@note.patch("/{id}/status", response_model=NoteStatus)
async def update_note_status(id: int, user=Depends(UnionAuth(scopes=["modal.note.patch"]))) -> NoteStatus:
"""
Архивирует модалку, если она активна

Если модалка в архиве, возвращает её в активное состояние, убирая из `group_ids` и `service_ids` ненайденные группы и сервисы

Права: `["modal.note.patch"]`

Исключение **ObjectNotFound**, если `id` не найден
Comment thread
petrCher marked this conversation as resolved.

Исключения, которые могут возникнуть при возврате в активное состояние:

Исключение **ForbiddenAction**, если ни одна группа из списка `group_ids` не найдена

Исключение **ForbiddenAction**, если ни один сервис из списка `service_ids` не найден

Исключение **ForbiddenAction**, если время модалки истекло
"""
updated_note = await NoteService.update_status(db, id=id)
return NoteStatus.model_validate(updated_note)
5 changes: 5 additions & 0 deletions modal_backend/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ class GroupGet(Base):
class GroupPost(Base):
group_id: int
name: str


class NoteStatus(Base):
id: int
status: ModalStatus
48 changes: 46 additions & 2 deletions modal_backend/utils/services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime, timezone

from requests import Session

from modal_backend.exceptions import AlreadyExists, ObjectNotFound
from modal_backend.models.db import Group, Note, NoteType, Service
from modal_backend.exceptions import AlreadyExists, ForbiddenAction, ObjectNotFound
from modal_backend.models.db import Group, ModalStatus, Note, NoteType, Service
from modal_backend.schemas.base import StatusResponseModel
from modal_backend.schemas.models import GroupPost, NoteTypePost, ServicePost

Expand Down Expand Up @@ -41,6 +43,48 @@ async def get_notes_by_filters(

return notes

@classmethod
async def update_status(cls, db: Session, id: int) -> Note:
note = Note.get(session=db.session, id=id)
if note.status == ModalStatus.ACTIVE:
updated_note = Note.update(id=id, session=db.session, status=ModalStatus.ARCHIVED)
return updated_note
else:
group_ids = note.group_ids
new_group_ids = []
for group_id in group_ids:
group = db.session.query(Group).filter(Group.id == group_id, Group.is_deleted == False).one_or_none()
if group is not None:
new_group_ids.append(group.id)
if new_group_ids == []:
raise ForbiddenAction(Note)

service_ids = note.service_ids
new_service_ids = []
for service_id in service_ids:
service = (
db.session.query(Service)
.filter(Service.id == service_id, Service.is_deleted == False)
.one_or_none()
)
if service is not None:
new_service_ids.append(service.id)
if new_service_ids == []:
raise ForbiddenAction(Note)

end_ts = note.end_ts
time = datetime.now(timezone.utc).replace(tzinfo=None)
if note.is_always == False and time >= end_ts:
raise ForbiddenAction(Note)
updated_note = Note.update(
id=id,
session=db.session,
group_ids=new_group_ids,
service_ids=new_service_ids,
status=ModalStatus.ACTIVE,
)
return updated_note


class NoteTypeService:
"""
Expand Down
Loading