Skip to content

Commit

Permalink
[14.0][FIX] shopinvader_membership subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
acsonefho committed Mar 21, 2022
1 parent 05bf9c9 commit f29efdc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
29 changes: 24 additions & 5 deletions shopinvader_membership/services/membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from odoo.osv import expression
from odoo.tools.translate import _

from odoo.addons.base_rest import restapi
from odoo.addons.base_rest.components.service import to_int
from odoo.addons.component.core import Component

Expand Down Expand Up @@ -41,33 +42,51 @@ def search(self, **params):
"""
return self._paginate_search(**params)

def subscribe(self, _id):
@restapi.method(
[(["/<int:_id>/subscribe"], "GET")],
input_param=restapi.CerberusValidator({}),
output_param=restapi.CerberusValidator("_validator_return_subscribe"),
)
def get_subscribe(self, _id):
"""
Subscribe to a membership product with logged user
:param _id: id of product.product
:return: dict with invoice_id
"""
product_id = _id
if not self._is_logged_in():
raise UserError(_("A user should be logged"))
membership_product = self.env["product.product"].search(
[("id", "=", _id), ("membership", "=", True)]
[("id", "=", product_id), ("membership", "=", True)], limit=1
)
if not membership_product:
raise UserError(_("No membership product found with id %s") % _id)
raise UserError(_("No membership product found with id %s") % product_id)
wizard = self.env["membership.invoice"].create(
{"product_id": _id, "member_price": membership_product.list_price}
{"product_id": product_id, "member_price": membership_product.list_price}
)
invoices_views_dict = wizard.with_context(
active_ids=self.partner.ids
).membership_invoice()
return {"invoice_id": invoices_views_dict.get("domain")[0][2][0]}

@restapi.method(
[(["/subscribe"], "POST")],
input_param=restapi.CerberusValidator("_validator_subscribe"),
output_param=restapi.CerberusValidator("_validator_return_subscribe"),
)
def post_subscribe(self, **params):
"""
Subscribe to a membership product with logged user
:return: dict with invoice_id
"""
return self.get_subscribe(_id=params.get("membership_product_id"))

def _validator_subscribe(self):
"""
Validator for the subscribe
:return: dict
"""
return {"membership_product_id": {"type": "integer"}}
return {"membership_product_id": {"type": "integer", "required": False}}

def _validator_return_subscribe(self):
"""
Expand Down
6 changes: 3 additions & 3 deletions shopinvader_membership/tests/test_membership_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ def test_get_multi_membership_lines(self):
def test_subscription(self):
# Check first not logged
with self.assertRaises(UserError) as e:
self.service_guest.dispatch("subscribe", self.product.id)
self.service_guest.dispatch("get_subscribe", self.product.id)
self.assertEqual(e.exception.args[0], "A user should be logged")
# Then with a logged user but with a non membership product
self.product.write({"membership": False})
with self.assertRaises(UserError) as e:
self.service.dispatch("subscribe", self.product.id)
self.service.dispatch("get_subscribe", self.product.id)
self.assertIn("No membership product found with", e.exception.args[0])
# Then user logged and real membership product
self.product.write({"membership": True})
result = self.service.dispatch("subscribe", self.product.id)
result = self.service.dispatch("get_subscribe", self.product.id)
invoice_id = result.get("invoice_id")
invoice = self.env["account.move"].browse(invoice_id)
self.assertEqual(self.partner, invoice.partner_id)
Expand Down

0 comments on commit f29efdc

Please sign in to comment.