feat: work shifts history
This commit is contained in:
		@@ -1,15 +1,17 @@
 | 
			
		||||
from datetime import datetime, date, timedelta
 | 
			
		||||
from datetime import date, timedelta
 | 
			
		||||
 | 
			
		||||
from sqlalchemy import select, delete
 | 
			
		||||
import math
 | 
			
		||||
from fastapi import HTTPException, status
 | 
			
		||||
from sqlalchemy import select, delete, func
 | 
			
		||||
from sqlalchemy.orm import joinedload
 | 
			
		||||
 | 
			
		||||
from models import WorkShift, User
 | 
			
		||||
from schemas.base import PaginationSchema
 | 
			
		||||
from schemas.time_tracking import UpdateTimeTrackingRecordRequest
 | 
			
		||||
from schemas.user import *
 | 
			
		||||
from schemas.work_shifts import StartShiftResponse, FinishShiftResponse, ActiveWorkShiftsResponse, DeleteShiftResponse, \
 | 
			
		||||
    FinishShiftByIdResponse
 | 
			
		||||
from schemas.work_shifts import *
 | 
			
		||||
from services.base import BaseService
 | 
			
		||||
from services.time_tracking import TimeTrackingService
 | 
			
		||||
from utils.dependecies import is_valid_pagination
 | 
			
		||||
from utils.work_time import hours_to_hours_and_minutes
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -100,18 +102,44 @@ class WorkShiftsService(BaseService):
 | 
			
		||||
        hours, minutes = hours_to_hours_and_minutes(hours)
 | 
			
		||||
        return FinishShiftByIdResponse(ok=True, message=f"Смена закончена. Отработано {hours} ч. {minutes} мин.")
 | 
			
		||||
 | 
			
		||||
    async def get_active_shifts(self) -> ActiveWorkShiftsResponse:
 | 
			
		||||
    async def get_shifts(self, is_active: bool, pagination: PaginationSchema) -> GetWorkShiftsResponse:
 | 
			
		||||
        if not is_valid_pagination(pagination):
 | 
			
		||||
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Invalid pagination')
 | 
			
		||||
        page = max(0, pagination.page - 1)
 | 
			
		||||
 | 
			
		||||
        total_shifts = await self.session.scalar(
 | 
			
		||||
            select(func.count())
 | 
			
		||||
            .select_from(WorkShift)
 | 
			
		||||
            .where(WorkShift.finished_at.is_(None) if is_active else WorkShift.finished_at.is_not(None))
 | 
			
		||||
        )
 | 
			
		||||
        if not total_shifts:
 | 
			
		||||
            return GetWorkShiftsResponse(
 | 
			
		||||
                shifts=[],
 | 
			
		||||
                pagination_info=PaginationInfoSchema(
 | 
			
		||||
                    total_pages=0,
 | 
			
		||||
                    total_items=0
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        total_pages = math.ceil(total_shifts / pagination.items_per_page)
 | 
			
		||||
 | 
			
		||||
        stmt = (
 | 
			
		||||
            select(WorkShift)
 | 
			
		||||
            .options(joinedload(WorkShift.user))
 | 
			
		||||
            .where(WorkShift.finished_at == None)
 | 
			
		||||
            .where(WorkShift.finished_at.is_(None) if is_active else WorkShift.finished_at.is_not(None))
 | 
			
		||||
            .order_by(WorkShift.started_at.desc())
 | 
			
		||||
            .offset(page * pagination.items_per_page)
 | 
			
		||||
            .limit(pagination.items_per_page)
 | 
			
		||||
        )
 | 
			
		||||
        shifts = (await self.session.execute(stmt)).scalars().all()
 | 
			
		||||
 | 
			
		||||
        shifts = await self.session.execute(stmt)
 | 
			
		||||
        shifts = shifts.scalars().all()
 | 
			
		||||
        response = ActiveWorkShiftsResponse(shifts=shifts)
 | 
			
		||||
        return response
 | 
			
		||||
        return GetWorkShiftsResponse(
 | 
			
		||||
            shifts=shifts,
 | 
			
		||||
            pagination_info=PaginationInfoSchema(
 | 
			
		||||
                total_pages=total_pages,
 | 
			
		||||
                total_items=total_shifts,
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    async def delete_shift(self, shift_id: int) -> DeleteShiftResponse:
 | 
			
		||||
        stmt = (
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user