Skip to content

Commit

Permalink
[IMP] api: Unify scheduling and address endpoints
Browse files Browse the repository at this point in the history
- Merge the endpoints for scheduling activities for both sale orders and invoices into a single endpoint and merge the endpoints delivery and invoicing address. This refactoring simplifies the API by reducing the number of endpoints and centralizing similar functionalities, enhancing the efficiency and maintainability of our code.

- Remove import unnecessary

Closes #25
  • Loading branch information
JAntonioSalas committed May 22, 2024
1 parent dfbaf01 commit 41c1a4e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 136 deletions.
179 changes: 44 additions & 135 deletions hantec_api_ecommerce/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,94 +113,57 @@ def update_contact(self, partner=False):

return {"message": f"Contact with ID: {partner.id} successfully updated."}

@route("/address_invoice", methods=["POST"], type="json", auth="user")
def create_address_invoice(self):
"""Creates or updates the billing address for a given partner.
This function creates or updates the billing address of a specified partner
using the details provided in the JSON request body.
JSON request body:
- partner_id (int): The ID of the partner.
- invoice_data (dict): A dictionary containing the billing address details.
JSON response:
- message (str): A message indicating that the billing address has been successfully created or updated.
- invoice_id (int): The ID of the created or updated billing address.
Returns:
dict: A dictionary with a success message and the billing address ID.
"""
env = request.env
data = request.jsonrequest
partner_id = data.get("partner_id")
invoice_data = data.get("invoice_data")

# Search if a billing address already exists
invoice_address = env["res.partner"].search(
[("parent_id", "=", partner_id), ("type", "=", "invoice")], limit=1
)

if invoice_address:
# Update existing billing address
invoice_address.write(invoice_data)
else:
# Add a new billing address
invoice_data.update({"type": "invoice", "parent_id": partner_id})
invoice_address = env["res.partner"].create(invoice_data)

return {
"message": "Billing address successfully updated/created.",
"invoice_id": invoice_address.id,
}

@route("/delivery_address", methods=["POST"], type="json", auth="user")
def delivery_address(self):
"""Creates or updates the delivery address for a given partner.
@route(
["/create_delivery_address", "/create_invoice_address"],
methods=["POST"],
type="json",
auth="user",
)
def create_address(self):
"""Creates or updates the billing or delivery address for a given partner.
This function creates or updates the delivery address of a specified partner
This function creates or updates the billing or delivery address of a specified partner
using the details provided in the JSON request body.
JSON request body:
- partner_id (int): The ID of the partner.
- address_data (dict): A dictionary containing the address details.
- address_type (str): The type of address to update or create ("invoice" or "delivery").
- only_create (bool, optional): A boolean indicating if only creation should be done,
omitting the update of an existing address (default is False).
JSON response:
- message (str): A message indicating that the delivery address has been successfully created or updated.
- delivery_address_id (int): The ID of the created or updated delivery address.
- message (str): A message indicating that the address has been successfully created or updated.
- address_id (int): The ID of the created or updated address.
Returns:
dict: A dictionary with a success message and the delivery address ID.
dict: A dictionary with a success message and the address ID.
"""
env = request.env
data = request.jsonrequest
partner_id = data.get("partner_id")
delivery_data = data.get("address_data")
address_data = data.get("address_data")
address_type = data.get("address_type") # "invoice" or "delivery"
only_create = data.get("only_create", False) # Boolean to omit address update

# Create or update the delivery address
delivery_address = env["res.partner"].search(
[("parent_id", "=", partner_id), ("type", "=", "delivery")], limit=1
# Search for existing address of the given type
address = env["res.partner"].search(
[("parent_id", "=", partner_id), ("type", "=", address_type)], limit=1
)

if only_create:
delivery_address = False

if delivery_address:
# Update existing delivery address
delivery_address.write(delivery_data)
address = False
if address:
# Update existing address
address.write(address_data)
else:
# Add a new delivery address
delivery_data.update({"type": "delivery", "parent_id": partner_id})
delivery_address = env["res.partner"].create(delivery_data)
# Add a new address
address_data.update({"type": address_type, "parent_id": partner_id})
address = env["res.partner"].create(address_data)

return {
"message": "Delivery address successfully updated/created.",
"delivery_address_id": delivery_address.id,
"message": f"{address_type} address successfully updated/created.",
"address_id": address.id,
}

@route("/create_sale_order", methods=["POST"], type="json", auth="user")
Expand Down Expand Up @@ -602,66 +565,20 @@ def confirm_sale_order(self, order=False):

return {"message": f"Sale order with ID: {order.id} successfully confirmed."}

@route("/create_schedule_activity", methods=["POST"], type="json", auth="user")
def create_schedule_activity(self):
"""Creates a scheduled activity associated with an existing sale order.
This function creates a scheduled activity in Odoo related to a specific sale order,
using the details provided in the JSON request body.
JSON request body:
- sale_order_id (int): ID of the existing sale order.
- activity_type_id (int): ID of the activity type.
- summary (str, optional): Summary text of the activity.
- date_deadline (str): Deadline date for the activity in 'YYYY-MM-DD' format.
- note (str, optional): Note for the activity.
- user_id (int, optional): ID of the user assigned to the activity. If not provided, the current user's ID is used.
JSON response:
- message (str): A confirmation message with the ID of the scheduled activity and the sale order ID.
Returns:
dict: A dictionary with a confirmation message.
"""
data = request.jsonrequest
sale_order_id = data.get("sale_order_id") # ID of the existing sale order
activity_type_id = data.get("activity_type_id") # ID of the activity type
summary = data.get("summary", "") # Summary text of the activity (optional)
date_deadline = data.get("date_deadline") # Deadline date for the activity
note = data.get("note", "") # Note for the activity (optional)
user_id = data.get("user_id", request.env.uid) # User assigned to the activity

# Create the scheduled activity
activity = request.env["mail.activity"].create(
{
"activity_type_id": activity_type_id,
"note": note,
"date_deadline": date_deadline,
"res_model_id": request.env["ir.model"]._get("sale.order").id,
"res_id": sale_order_id,
"user_id": user_id,
"summary": summary,
}
)

return {
"message": f"Scheduled activity created with ID: {activity.id} for sale order {sale_order_id}."
}

@route(
'/create_schedule_activity_invoice/<model("account.move"):invoice>',
[
'/create_schedule_activity/<model("sale.order"):order>',
'/create_schedule_activity_invoice/<model("account.move"):order>',
],
methods=["POST"],
type="json",
auth="user",
)
def create_schedule_activity_invoice(self, invoice=False):
"""Creates a scheduled activity associated with an existing invoice.
def create_schedule_activity(self, order=False):
"""Creates a scheduled activity associated with an existing sale order or invoice.
This function creates a scheduled activity in Odoo related to a specific invoice,
using the details provided in the JSON request body.
URL parameter:
- invoice (account.move): The invoice model instance.
This function creates a scheduled activity in Odoo related to a specific sale order
or invoice, using the details provided in the JSON request body.
JSON request body:
- activity_type_id (int): ID of the activity type.
Expand All @@ -671,29 +588,21 @@ def create_schedule_activity_invoice(self, invoice=False):
- user_id (int, optional): ID of the user assigned to the activity. If not provided, the current user's ID is used.
JSON response:
- message (str): Confirmation message with the ID of the scheduled activity and the invoice ID.
- message (str): A confirmation message with the ID of the scheduled activity and the sale order or invoice ID.
Returns:
dict: Dictionary with a confirmation message.
dict: A dictionary with a confirmation message.
"""
data = request.jsonrequest
activity_type_id = data.get("activity_type_id") # ID of the activity type
summary = data.get("summary", "") # Summary text of the activity (optional)
date_deadline = data.get("date_deadline") # Deadline date for the activity
note = data.get("note", "") # Note for the activity (optional)
user_id = data.get("user_id", request.env.uid) # User assigned to the activity

# Schedule the activity
activity = invoice.activity_schedule(
activity_type_id=activity_type_id,
summary=summary,
note=note,
user_id=user_id,
date_deadline=date_deadline,
activity = order.activity_schedule(
activity_type_id=request.jsonrequest.get("activity_type_id"),
summary=request.jsonrequest.get("summary", ""),
note=request.jsonrequest.get("note", ""),
date_deadline=request.jsonrequest.get("date_deadline"),
user_id=request.jsonrequest.get("user_id", request.env.uid),
)

return {
"message": f"Scheduled activity created with ID: {activity.id} for invoice {invoice.id}."
"message": f"Scheduled activity created with ID: {activity.id} for sale order {order.id}."
}

@route(
Expand Down
2 changes: 1 addition & 1 deletion hantec_api_ecommerce/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import models, fields
from odoo import models


class SaleOrder(models.Model):
Expand Down

0 comments on commit 41c1a4e

Please sign in to comment.