diff --git a/alembic/versions/20250415_182746_3f75cae3_update_contact.py b/alembic/versions/20250415_182746_3f75cae3_update_contact.py new file mode 100644 index 0000000..4b2c769 --- /dev/null +++ b/alembic/versions/20250415_182746_3f75cae3_update_contact.py @@ -0,0 +1,19 @@ +"""add phone_number field to contacts table +Revision ID: 8f3a91d2e4c5 +Revises: 0002 +Create Date: 2024-01-30 10:00:00.000000 +""" +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = '8f3a91d2e4c5' +down_revision = '0002' +branch_labels = None +depends_on = None + +def upgrade(): + op.add_column('contacts', sa.Column('phone_number', sa.String(), nullable=True)) + +def downgrade(): + op.drop_column('contacts', 'phone_number') \ No newline at end of file diff --git a/endpoints/contact.post.py b/endpoints/contact.post.py index 7831b01..88260b6 100644 --- a/endpoints/contact.post.py +++ b/endpoints/contact.post.py @@ -11,12 +11,6 @@ async def create_contact_submission( contact_data: ContactCreate, db: Session = Depends(get_db) ): - """Create a new contact submission with email validation""" - # Sanitize input data sanitized_data = sanitize_contact_data(contact_data) - - # Create contact using helper function - contact = create_contact(db=db, contact_data=sanitized_data) - - # Format response - return format_contact_response(contact) \ No newline at end of file + new_contact = create_contact(db=db, contact_data=sanitized_data) + return format_contact_response(new_contact) \ No newline at end of file diff --git a/helpers/contact_helpers.py b/helpers/contact_helpers.py index b8d4bb2..4ea9a03 100644 --- a/helpers/contact_helpers.py +++ b/helpers/contact_helpers.py @@ -35,6 +35,14 @@ def validate_contact_data(contact_data: ContactCreate) -> Dict[str, str]: if not contact_data.message or not contact_data.message.strip(): errors["message"] = "Message is required" + # Validate phone number format if provided + if contact_data.phone_number: + # Basic phone number format validation + # Remove any spaces, dashes, or parentheses + cleaned_phone = ''.join(filter(str.isdigit, contact_data.phone_number)) + if not (10 <= len(cleaned_phone) <= 15): + errors["phone_number"] = "Phone number must be between 10 and 15 digits" + return errors def create_contact(db: Session, contact_data: ContactCreate) -> Contact: @@ -91,10 +99,21 @@ def sanitize_contact_data(contact_data: ContactCreate) -> ContactCreate: email = contact_data.email.strip().lower() validate_email(email) + # Sanitize phone number if provided + phone_number = None + if contact_data.phone_number: + # Remove any non-digit characters except '+' at the start + phone_number = contact_data.phone_number.strip() + if phone_number.startswith('+'): + phone_number = '+' + ''.join(filter(str.isdigit, phone_number[1:])) + else: + phone_number = ''.join(filter(str.isdigit, phone_number)) + # Create a new dict with sanitized values sanitized_data = ContactCreate( name=contact_data.name.strip(), email=email, + phone_number=phone_number, message=contact_data.message.strip() ) return sanitized_data diff --git a/models/contact.py b/models/contact.py index 914a82d..1d8b0c2 100644 --- a/models/contact.py +++ b/models/contact.py @@ -10,6 +10,7 @@ class Contact(Base): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = Column(String, nullable=False) email = Column(String, nullable=False, index=True) + phone_number = Column(String, nullable=True) message = Column(Text, nullable=False) created_at = Column(DateTime, default=func.now()) diff --git a/schemas/contact.py b/schemas/contact.py index 9d8a1d1..64b4fe3 100644 --- a/schemas/contact.py +++ b/schemas/contact.py @@ -6,6 +6,7 @@ from uuid import UUID class ContactBase(BaseModel): name: str = Field(..., min_length=1, max_length=255, description="Contact name") email: EmailStr = Field(..., description="Contact email address", regex=r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$") + phone_number: Optional[str] = Field(None, description="Contact phone number") message: str = Field(..., min_length=1, description="Contact message") class ContactCreate(ContactBase): @@ -14,6 +15,7 @@ class ContactCreate(ContactBase): class ContactUpdate(BaseModel): name: Optional[str] = Field(None, min_length=1, max_length=255, description="Contact name") email: Optional[EmailStr] = Field(None, description="Contact email address", regex=r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$") + phone_number: Optional[str] = Field(None, description="Contact phone number") message: Optional[str] = Field(None, min_length=1, description="Contact message") class ContactSchema(ContactBase): @@ -27,7 +29,8 @@ class ContactSchema(ContactBase): "example": { "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "name": "John Doe", - "email": "john.doe@example.com", + "email": "john.doe@example.com", + "phone_number": "+1234567890", "message": "Hello, I would like to get in touch.", "created_at": "2023-01-01T12:00:00", "updated_at": "2023-01-01T12:00:00"