add strategy that each job title will send message with the details

pull/231/head
Yariv Menachem 2024-12-31 17:08:42 +02:00
parent 3c2fed876b
commit 6ff635890a
5 changed files with 31 additions and 10 deletions

View File

@ -37,7 +37,7 @@ class JobRepository:
The job document if found, otherwise None.
"""
result = self.collection.find_one({"id": job_id})
return result
return JobPost(**result)
def update(self, job_data: dict) -> bool:
"""

View File

@ -268,7 +268,7 @@ class JobPost(BaseModel):
company_name: str | None
job_url: str
job_url_direct: str | None = None
location: Optional[Location]
location: Optional[Location] = None
description: str | None = None
company_url: str | None = None
@ -276,7 +276,7 @@ class JobPost(BaseModel):
job_type: list[JobType] | None = None
compensation: Compensation | None = None
date_posted: date | None
date_posted: Optional[date] = None
datetime_posted: datetime | None = None
emails: list[str] | None = None
is_remote: bool | None = None
@ -304,8 +304,6 @@ class JobPost(BaseModel):
def model_dump(self, exclude: set = None):
data = super().model_dump(exclude=exclude)
# Use `Location`'s custom serialization logic
if self.location:
data['location'] = self.location.display_location()
# Serialize `job_type` as a list of strings
if self.job_type:

View File

@ -1,4 +1,5 @@
import os
from dotenv import load_dotenv
from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ReactionEmoji

View File

@ -3,9 +3,10 @@ from __future__ import annotations
from telegram import MaybeInaccessibleMessage
from telegram.constants import ReactionEmoji
from db.job_repository import JobRepository
from jobspy import create_logger
from telegram_bot import TelegramBot
from telegram_handler.button_callback.button_fire_strategy import FireStrategy
from telegram_handler.button_callback.button_job_title_strategy import JobTitleStrategy
from telegram_handler.button_callback.button_poo_strategy import PooStrategy
from telegram_handler.button_callback.button_strategy import ButtonStrategy
@ -15,12 +16,13 @@ class ButtonCallBackContext:
The Context defines the interface
"""
def __init__(self, data: str, message: MaybeInaccessibleMessage,job_id:str) -> None:
def __init__(self, data: str, message: MaybeInaccessibleMessage, job_id: str) -> None:
self._logger = create_logger("Button CallBack Context")
self._message = message
self._data = data
self._telegram_bot = TelegramBot()
self._job_id = job_id
self._strategy = None
self._job_repository = JobRepository()
@property
def strategy(self) -> ButtonStrategy:
@ -43,9 +45,13 @@ class ButtonCallBackContext:
async def run(self) -> None:
self._logger.info("Starting")
if ReactionEmoji.FIRE.name == self._data:
self.strategy = FireStrategy(self._message,self._job_id)
self._strategy = FireStrategy(self._message, self._job_id)
elif ReactionEmoji.PILE_OF_POO.name == self._data:
self.strategy = PooStrategy(self._message)
self._strategy = PooStrategy(self._message)
elif self._data:
job = self._job_repository.find_by_id(self._data)
if job:
self._strategy = JobTitleStrategy(job)
else:
self._logger.error("Invalid enum value")
return

View File

@ -0,0 +1,16 @@
from jobspy import JobPost
from telegram_bot import TelegramBot
from telegram_handler.button_callback.button_strategy import ButtonStrategy
class JobTitleStrategy(ButtonStrategy):
def __init__(self, job: JobPost) -> None:
"""
Usually, the Context accepts a strategy through the constructor, but
also provides a setter to change it at runtime.
"""
self._job = job
self.telegram_bot = TelegramBot()
async def execute(self):
await self.telegram_bot.send_job(self._job)