async def get_all_jobs(offset: Optional[int] = Query(None, gte=0), limit: Optional[int] = Query(None, gte=0), job: JobFilter = Depends(job_filter)): with session_scope() as db: crud = CRUDJob(db) jobs, total = crud.get_many(offset=offset, limit=limit, job_filter=job) return {"jobs": jobs, "total": total}
async def update_applied_job(employer_id: Optional[int], payload: PayloadUpdateApply, identities: Identities = Depends( auth.check_token)): if identities.user_type != UserType.employer.value: raise AuthenticationError if payload.status not in [ ApplyStatus.pending.value, ApplyStatus.approved.value, ApplyStatus.rejected.value ]: raise BadRequestsError("'status' is not legal") with session_scope() as db: crud = CRUDemployer(db) user = db.query( Employer.user_id).filter(Employer.id == employer_id).first() if not user: raise BadRequestsError("employer_id is not legal") if user.user_id != identities.id: raise AuthenticationError crud.update_apply_status(payload.apply_id, payload.status) return { "status": "updated", "apply_id": payload.apply_id, "employer_id": employer_id, "updated": f"apply status to {payload.status}" }
async def login(user: LoginRequestBody): with session_scope() as db: db_user = db.query(User).filter( User.email == user.email, User.password == user.password).first() if not db_user: raise HTTPException(status_code=401, detail="Email or Password is incorrect") payload = { "id": db_user.id, "email": db_user.email, "name": db_user.name, "phone_number": db_user.phone_number, "user_type": db_user.user_type } UserTypeModel = user_type_models_mapping.get(db_user.user_type) if UserTypeModel: user_type = db.query(UserTypeModel.id).filter( UserTypeModel.user_id == db_user.id).first() if user_type: field_name = user_type_fields_mapping[db_user.user_type] payload.update({field_name: user_type.id}) token = auth.create_token(payload) response = LoginResponse(**token.dict(), **payload) return response
async def apply_job(payload: CreateApplyPayload, identities: Identities = Depends(auth.check_token)): if identities.user_type != UserType.employee.value: raise AuthenticationError with session_scope() as db: job = db.query(Job.is_open).filter(Job.id == payload.job_id).first() if not job: raise JobNotFoundError({'job_id': payload.job_id}) if not job.is_open: raise BadRequestsError("Job is closed") employee = db.query(Employee.id).filter(Employee.user_id == identities.id).first() if not employee: raise BadRequestsError("User is not employee!!") applied = db.query(Apply).filter(Apply.employee_id == employee.id, Apply.job_id == payload.job_id).first() if applied: raise BadRequestsError('You are applied this job!') crud = CRUDApply(db) apply_info = { 'employee_id': employee.id, 'job_id': payload.job_id, 'description': payload.description, 'cv': payload.cv } crud.create(ApplyInDB(**apply_info)) return payload
async def get_job(job_id: str): with session_scope() as db: crud = CRUDJob(db) job = crud.get(job_id) if not job: return {} return job
async def upgrade(identities: Identities = Depends(auth.check_token)): check_requests_body(identities) with session_scope() as db: crud = CRUDemployee(db) crud.create(user_id=identities.id) user = db.query(User).get(identities.id) user.user_type = UserType.employee.value return {"status": "upgraded", "user_type": "employee"}
async def get_apply(apply_id: str, identities: Identities = Depends(auth.check_token)): if identities.user_type not in {UserType.employee.value, UserType.employer.value}: raise AuthorizationError() apply = None with session_scope() as db: apply = db.query(Apply).filter(Apply.id == apply_id).first() if not apply: raise ApplyNotFoundError(apply_id) return ApplyBase(id=apply.id, job_id=apply.job_id, description=apply.description, cv=apply.cv, status=apply.status.value)
async def register(user: RegisterRequestBody): with session_scope() as db: db_user = db.query(User).filter(User.email == user.email).first() if db_user: raise HTTPException(status_code=400, detail="Email already registered") new_user = User(email=user.email, password=user.password, name=user.name, phone_number=user.phone_number) db.add(new_user) return user
async def delete(job_id: str, identities=Depends(auth.check_token)): if identities.user_type != UserType.employer.value: raise AuthorizationError() with session_scope() as db: job = db.query(Job.employer_id, Employer.user_id).join( Employer.jobs).filter(Job.id == job_id).first() if not job: raise JobNotFoundError(job_id) if job.user_id != identities.id: raise AuthorizationError() crud = CRUDJob(db) crud.delete(job_id) return {"status": "deleted", "job_id": job_id}
async def upgrade(payload: EmployerCreate, identities: Identities = Depends(auth.check_token)): check_requests_body(payload, identities) with session_scope() as db: crud = CRUDemployer(db) crud.create(EmployerInDB(user_id=identities.id, **payload.dict())) user = db.query(User).get(identities.id) user.user_type = UserType.employer.value return { "status": "upgraded", "user_type": "employer", "info": payload.dict() }
async def create_tags(job_id: int, payload: PayloadTag): with session_scope() as db: crud = CRUDTag(db) not_exist_tags = crud.get_not_exist_tags(payload.tags) if not_exist_tags: crud.create_many(not_exist_tags) tags = crud.get_many(payload.tags) if tags: job = db.query(Job).get(job_id) if not job: raise JobNotFoundError(info={'job_id': job_id}) job.tags.extend(tags) return payload
async def create_job(job_info: JobBase = Body(...), identities=Depends(auth.check_token)): if identities.user_type != UserType.employer.value: raise AuthorizationError() with session_scope() as db: employer = db.query( Employer.id).filter(Employer.user_id == identities.id).first() if not employer: raise BadRequestsError("User is not employer") crud = CRUDJob(db) job = JobModel(**job_info.dict(), employer_id=employer.id) crud.create(job) return job
async def delete_apply(apply_id: str, identities: Identities = Depends(auth.check_token)): if identities.user_type != UserType.employee.value: raise AuthorizationError() with session_scope() as db: apply = db.query(Employee.user_id).join(Employee.applies).filter(Apply.id == apply_id).first() if not apply: raise ApplyNotFoundError(apply_id) if apply.user_id != identities.id: raise AuthorizationError() crud = CRUDApply(db) crud.delete(apply_id) return { "status": "deleted", "apply_id": apply_id }
async def update_apply(apply_id: str, update_info: EmployeeUpdateApplyModel = Body(...), identities: Identities = Depends(auth.check_token)): if identities.user_type != UserType.employee.value: raise AuthorizationError() with session_scope() as db: apply = db.query(Employee.user_id).join(Employee.applies).filter(Apply.id == apply_id).first() if not apply: raise ApplyNotFoundError(apply_id) if apply.user_id != identities.id: raise AuthorizationError() crud = CRUDApply(db) crud.update(apply_id, **update_info.dict(exclude_unset=True)) return { "status": "updated", "apply_id": apply_id, "updated": update_info.dict(exclude_unset=True) }
def check_token(bearer_token: str = Header(..., alias="Authorization")): if not bearer_token.startswith("Bearer "): raise AuthenError("Header token missing Bearer") _, _, token = bearer_token.partition("Bearer ") if not token: raise AuthenError("Header token missing access_token") try: decoded = jwt.decode(token, settings.TOKEN_SECRET_KEY, algorithms=["HS256"]) except InvalidTokenError as ex: print(ex) raise AuthenError("Token invalid") else: identities = Identities(**decoded) with session_scope() as db: if is_out_of_date(db, identities): raise AuthenError("Token out of date") return identities
async def update(job_id: str, job_info: UpdateJobModel = Body(...), identities=Depends(auth.check_token)): if identities.user_type != UserType.employer.value: raise AuthorizationError() with session_scope() as db: job = db.query(Job.employer_id, Employer.user_id).join( Employer.jobs).filter(Job.id == job_id).first() if not job: raise JobNotFoundError(job_id) if job.user_id != identities.id: raise AuthorizationError() crud = CRUDJob(db) crud.update(job_id, **job_info.dict(exclude_unset=True)) return { "status": "updated", "job_id": job_id, "updated": job_info.dict(exclude_unset=True) }
async def update_employer(employer_id: Optional[int], employer_info: EmployerUpdate = Body(...), identities: Identities = Depends(auth.check_token)): if identities.user_type != UserType.employer.value: raise AuthenticationError with session_scope() as db: user = db.query( Employer.user_id).filter(Employer.id == employer_id).first() if not user: raise BadRequestsError("employer_id is not legal") if user.user_id != identities.id: raise AuthenticationError crud = CRUDemployer(db) crud.update(employer_id, **employer_info.dict()) return { "status": "updated", "employer_id": employer_id, "updated": employer_info.dict() }
async def applied_jobs(employer_id: Optional[int], offset: Optional[int] = Query(None, gte=0), limit: Optional[int] = Query(None, gte=0), applied: ApplyFilter = Depends(apply_filter), employee: EmployeeFilter = Depends(employee_filter), job: JobFilter = Depends(job_filter), identities: Identities = Depends(auth.check_token)): if identities.user_type != UserType.employer.value: raise AuthenticationError with session_scope() as db: crud = CRUDemployer(db) user = db.query( Employer.user_id).filter(Employer.id == employer_id).first() if not user: raise BadRequestsError("employer_id is not legal") if user.user_id != identities.id: raise AuthenticationError applied_jobs_info, total = crud.get_applied_job( employer_id, offset, limit, applied, employee, job) return {"applied": applied_jobs_info, "total": total}
def get_user_updated_time(user_id): with session_scope() as db: user = db.query(User.updated_at).filter(User.id == user_id).first() updated = user.updated_at.isoformat() return updated