Skip to content

Commit

Permalink
feat: add update attendance record api
Browse files Browse the repository at this point in the history
  • Loading branch information
Will413028 committed Jan 31, 2024
1 parent f34f767 commit 17ac134
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
14 changes: 10 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ def get_all_attendance_records(db: Session = Depends(get_db), attendance_type: O

@app.post("/attendances", status_code=status.HTTP_201_CREATED)
def create_attendance(settings: Annotated[Settings, Depends(get_settings)], db: Session = Depends(get_db), current_user: dict = Depends(jwt.get_current_user)):
try:
service.create_attendance(db, current_user.id, settings)
except Exception:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Create Attendance Record failed')
return service.create_attendance(db, current_user.id, settings)


@app.put("/attendances/{id}", response_model=schemas.AttendanceRecord)
def update_attendance_record(id: int, update_data: schemas.AttendanceRecordUpdateInput, db: Session = Depends(get_db)):
attendance_record = service.get_attendance_record_by_id(db, id)
if not attendance_record:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Attendance Record not found')

return service.update_attendance_record(db, update_data, attendance_record)
7 changes: 5 additions & 2 deletions schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ class Config:
from_attributes = True


class AttendanceCreateInput(BaseModel):
user_id: int
class AttendanceRecordUpdateInput(BaseModel):
attendance_date: Optional[datetime] = None
time_in: Optional[datetime] = None
time_out: Optional[datetime] = None
attendance_type: Optional[str] = None
61 changes: 44 additions & 17 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,40 @@ def create_user(db: Session, user: schemas.UserCreateInput):
return db_user


def create_attendance(db: Session, user_id: schemas.AttendanceCreateInput, config: Settings):
def update_user(db: Session, update_data: schemas.UserUpdateInput, user: models.User) -> models.User:
update_data: dict = update_data.model_dump(exclude_unset=True, exclude_none=True)
for key, value in update_data.items():
setattr(user, key, value)

try:
db.commit()
db.refresh(user)
except Exception:
db.rollback()
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='User update failed')

return user


def delete_user(db: Session, user: models.User):
try:
db.delete(user)
db.commit()
except Exception:
db.rollback()
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='User delete failed')


def get_attendance_record_by_id(db: Session, id: int) -> models.AttendanceRecord:
query = select(models.AttendanceRecord).where(models.AttendanceRecord.id == id)
attendance_record = db.execute(query).scalar()

return attendance_record


def create_attendance(db: Session, user_id: schemas.AttendanceRecordUpdateInput, config: Settings):
message = None

current_time = datetime.now()

workday = get_workday(current_time, config.workday_cut_off_time)
Expand All @@ -77,8 +110,10 @@ def create_attendance(db: Session, user_id: schemas.AttendanceCreateInput, confi

if user_is_leave_early:
today_attendance_record.attendance_type = 'Early Leave'
message = 'User early leave'
else:
today_attendance_record.attendance_type = 'Present'
message = 'User Present'

try:
db.commit()
Expand All @@ -98,33 +133,25 @@ def create_attendance(db: Session, user_id: schemas.AttendanceCreateInput, confi
db.rollback()
print(e)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Attendance create failed')
return message


def update_user(db: Session, update_data: schemas.UserUpdateInput, user: models.User) -> models.User:
def update_attendance_record(db: Session, update_data: schemas.AttendanceRecordUpdateInput, attendance_record: models.AttendanceRecord) -> models.AttendanceRecord:
update_data: dict = update_data.model_dump(exclude_unset=True, exclude_none=True)
for key, value in update_data.items():
setattr(user, key, value)
setattr(attendance_record, key, value)

try:
db.commit()
db.refresh(user)
db.refresh(attendance_record)
except Exception:
db.rollback()
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='User update failed')
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Attendance Record update failed')

return user


def delete_user(db: Session, user: models.User):
try:
db.delete(user)
db.commit()
except Exception:
db.rollback()
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='User delete failed')
return attendance_record


def get_workday(date_time, workday_cutoff_str):
def get_workday(date_time, workday_cutoff_str) -> date:

workday_cutoff = datetime.strptime(workday_cutoff_str, '%H:%M').time()

Expand All @@ -144,7 +171,7 @@ def get_user_attendance_by_workday(db: Session, user_id: int, workday: date) ->
return attendance_record


def is_leave_early(time_in: datetime, minimum_working_hours: int, current_time: datetime):
def is_leave_early(time_in: datetime, minimum_working_hours: int, current_time: datetime) -> bool:

working_hours = (current_time - time_in).total_seconds() / 3600

Expand Down

0 comments on commit 17ac134

Please sign in to comment.