feat: Implemented get associate party to contact_mechanism

This commit is contained in:
Rodia 2025-03-15 15:00:58 -05:00
parent becf5f929a
commit 09cabd6798
3 changed files with 200 additions and 87 deletions

View File

@ -10,6 +10,37 @@ import json
sale_order_application = user_application('sale_order')
@app.route(
'/<database_name>/sale_order/associate_party/<contact_method>', methods=[
"GET"])
@allow_null_origin
@with_pool
@with_transaction()
@sale_order_application
def get_associate_party(request, pool, contact_method: str):
ContactMechanism = pool.get('party.contact_mechanism')
associate_contact_method = ContactMechanism.search(
[('value', 'ilike', contact_method)])
if associate_contact_method:
response_data = {
'id': associate_contact_method[0].id,
'associate_party': associate_contact_method[0].party.id,
'status': 'success',
'type': associate_contact_method[0].type,
'message': 'Associate Party found',
}
return json.dumps(response_data), 200
response_data = {
'id': None,
'associate_party': None,
'status': 'success',
'type': None,
'message': 'Associate Party not found',
}
return json.dumps(response_data), 404
@app.route(
'/<database_name>/sale_order/order/<order>', methods=['GET'])
@allow_null_origin

View File

@ -2,7 +2,8 @@ import pudb
import requests
import json
url = 'http://localhost:18030'
url = 'http://localhost:8000'
key = 'f46f14d77db646b0ac0802e7bdab9cbb' + (
'1d53ad96387242e1918c45854dce5238707fed31daa64cab88569d119512153') + (
'64db6ced393b44f198ab9a3967b6f4ddf')
@ -10,6 +11,13 @@ db = 'tryton'
application_name = 'sale_order'
base_url = '{}/{}/{}'.format(url, db, application_name)
get_associate_party = requests.get(
base_url + '/associate_party/alejandro.ayala@gmail.com',
headers={
'Authorization': f'bearer {key}',
})
post_sale_order = requests.post(
base_url + '/order',
headers={

View File

@ -12,63 +12,79 @@ from trytond.tests.tools import activate_modules
class SaleOrderApiRouteTestCase(RouteTestCase):
"""Sale Order API Routes"""
module = 'sale_order'
module = "sale_order"
key = uuid.uuid4().hex
@classmethod
def setUpClass(cls):
activate_modules('sale_order', create_company)
activate_modules("sale_order", create_company)
def setUp(self):
pool = Pool(DB_NAME)
transaction = Transaction().start(DB_NAME, 0, _lock_tables=[
'product_product'])
transaction = Transaction().start(
DB_NAME, 0, _lock_tables=["product_product"])
with transaction:
Party = pool.get('party.party')
ProductTemplate = pool.get('product.template')
Product = pool.get('product.product')
Uom = pool.get('product.uom')
Application = pool.get('res.user.application')
Party = pool.get("party.party")
ProductTemplate = pool.get("product.template")
Product = pool.get("product.product")
Uom = pool.get("product.uom")
Application = pool.get("res.user.application")
Application(
key=self.key,
user=1,
application='sale_order',
state='validated'
application="sale_order",
state="validated"
).save()
self.unit = Uom.search([('name', '=', 'Unit')])[0].id
self.productTemplate, = ProductTemplate.create([{
'name': 'Product',
'default_uom': self.unit,
}])
self.unit = Uom.search([("name", "=", "Unit")])[0].id
(self.productTemplate,) = ProductTemplate.create(
[
{
"name": "Product",
"default_uom": self.unit,
}
]
)
self.product, = Product.create([
{'template': self.productTemplate.id}
])
{"template": self.productTemplate.id}])
self.party, = Party.create([{
'name': "Dunkan"
"name": "Dunkan",
"contact_mechanisms": [['create', [{
'type': 'mobile',
'value': '3102223334'
}]]]
}])
def test_post_sale_order(self):
client = self.client()
response = client.post(
f'/{self.db_name}/sale_order/order',
f"/{self.db_name}/sale_order/order",
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": 'on_site',
"lines": [[
"create", [{
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10"
}]
]]
}))
"unitprice": "10",
}
],
]
],
}
),
)
self.assertEqual(response.status_code, HTTPStatus.OK)
@ -77,92 +93,150 @@ class SaleOrderApiRouteTestCase(RouteTestCase):
order = json.loads(
client.post(
f'/{self.db_name}/sale_order/order',
f"/{self.db_name}/sale_order/order",
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": 'on_site',
"lines": [[
"create", [{
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10"
}]
]]
})).get_data().decode())
"unitprice": "10",
}
],
]
],
}
),
)
.get_data()
.decode()
)
response = client.get(
f"/{self.db_name}/sale_order/order/{json.loads(order[0])['id']}",
headers={
'Authorization': f'bearer {self.key}',
})
"Authorization": f"bearer {self.key}",
},
)
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(len(
json.loads(response.text)
), 1)
self.assertEqual(len(json.loads(response.text)), 1)
def test_confirm_sale_order(self):
client = self.client()
order = json.loads(
client.post(
f'/{self.db_name}/sale_order/order',
f"/{self.db_name}/sale_order/order",
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": 'on_site',
"lines": [[
"create", [{
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10"
}]
]]
})).get_data().decode())
"unitprice": "10",
}
],
]
],
}
),
)
.get_data()
.decode()
)
confirm_url = f"/{self.db_name}/sale_order/confirm_order/"
order_id = json.loads(order[0])['id']
order_id = json.loads(order[0])["id"]
response = client.post(
f"{confirm_url}{order_id}",
headers={
'Authorization': f'bearer {self.key}',
})
"Authorization": f"bearer {self.key}",
},
)
response_data = json.loads(
json.loads(response.text)[0])
response_data = json.loads(json.loads(response.text)[0])
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(response_data['state'], 'confirmed')
self.assertEqual(response_data["state"], "confirmed")
def test_post_line_order(self):
client = self.client()
response = client.post(
f'/{self.db_name}/sale_order/order',
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
f"/{self.db_name}/sale_order/order",
headers={"Authorization": f"bearer {self.key}"},
data=json.dumps({
"party": self.party.id,
"pickup_location": 'on_site'})
"pickup_location": "on_site"}),
)
order = json.loads(json.loads(response.text)[0]).get("id")
response = client.post(
f'/{self.db_name}/sale_order/{order}/order_line',
f"/{self.db_name}/sale_order/{order}/order_line",
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"order": order,
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10"
}))
"unitprice": "10",
}
),
)
self.assertEqual(response.status_code, HTTPStatus.OK)
def test_get_party_asociate_contact_method(self):
"""
Recibe un numbero de telofono y retorna el tercero asociado
a este numero de telofono.
Args:
phone: string
Return:
party: int
"""
client = self.client()
response = client.get(
f"/{self.db_name}/sale_order/associate_party/3102223334",
headers={
"Authorization": f"bearer {self.key}",
},
)
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(response.text)[1], 200)
self.assertEqual(json.loads(json.loads(response.text)[0])[
'associate_party'], 2)
def test_create_contact_method(self):
"""
Crea un tercero asociandolo a un metodo de contacto
Args:
email, phone : string
Return:
party: int
"""