Files
eamco_office_api/app/search/views.py
2025-09-01 16:42:59 -04:00

68 lines
2.8 KiB
Python
Executable File

from flask import request, jsonify
from app.search import search
from app import db
from app.classes.customer import Customer_Customer, Customer_Customer_schema
from app.classes.delivery import Delivery_Delivery, Delivery_Delivery_schema
# We now need `and_` to combine our search conditions
from sqlalchemy import or_, and_
@search.route("/customer", methods=["GET"])
def search_customers():
"""
Searches for customers based on a keyword.
The new logic searches across multiple fields (name, address, town, phone, etc.)
for each word provided in the search query.
"""
keyword = request.args.get('q')
if not keyword or len(keyword) < 2:
return jsonify([])
# --- NEW SEARCH LOGIC ---
# 1. Split the incoming search query into individual words.
# e.g., "john main st" -> ["john", "main", "st"]
search_words = keyword.split()
# 2. We'll build a list of search conditions. Each condition will check
# if a word exists in ANY of the relevant customer fields.
conditions = []
for word in search_words:
search_pattern = f"%{word}%"
# For each word, create an OR clause to check against all fields
conditions.append(
or_(
Customer_Customer.customer_first_name.ilike(search_pattern),
Customer_Customer.customer_last_name.ilike(search_pattern),
Customer_Customer.customer_address.ilike(search_pattern),
Customer_Customer.customer_town.ilike(search_pattern),
Customer_Customer.customer_phone_number.ilike(search_pattern),
Customer_Customer.account_number.ilike(search_pattern)
# Add any other fields you want to be searchable here
)
)
# 3. Combine all our word conditions with AND. This means the customer
# record must match ALL the words from the search query.
# e.g., for "john main", it must contain "john" AND "main" somewhere.
query = db.session.query(Customer_Customer).filter(and_(*conditions))
# Limiting results is still good practice
customer_list = query.order_by(Customer_Customer.customer_last_name.asc()).limit(20).all()
customer_schema = Customer_Customer_schema(many=True)
return jsonify(customer_schema.dump(customer_list))
# The /delivery route is fine as is, so no changes needed there.
@search.route("/delivery", methods=["GET"])
def search_delivery():
# ... (no changes to this function)
keyword = request.args.get('q')
if not keyword or not keyword.isdigit():
return jsonify([])
delivery_ticket = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.id == keyword).first()
if delivery_ticket:
delivery_schema = Delivery_Delivery_schema(many=False)
return jsonify(delivery_schema.dump(delivery_ticket))
else:
return jsonify({})