diff --git a/pms_api_rest/controllers/pms_rest.py b/pms_api_rest/controllers/pms_rest.py index e9139ac63..0c914527b 100644 --- a/pms_api_rest/controllers/pms_rest.py +++ b/pms_api_rest/controllers/pms_rest.py @@ -1,4 +1,6 @@ from odoo.addons.base_rest.controllers import main +from ..lib_jwt.jwt_http import jwt_http +from ..lib_jwt.validator import validator class BaseRestDemoPublicApiController(main.RestController): @@ -7,13 +9,13 @@ class BaseRestDemoPublicApiController(main.RestController): _default_auth = "public" # RestController OVERRIDE method - # def _process_method(self, service_name, method_name, *args, params=None): - # - # http_method, body, headers, token = jwt_http.parse_request() - # result = validator.verify_token(token) - # if not result["status"]: - # return jwt_http.errcode(code=result["code"], message=result["message"]) - # else: - # return super(BaseRestDemoPublicApiController, self)._process_method( - # service_name, method_name, *args, params=params - # ) + def _process_method(self, service_name, method_name, *args, params=None): + + http_method, body, headers, token = jwt_http.parse_request() + result = validator.verify_token(token) + if not result["status"]: + return jwt_http.errcode(code=result["code"], message=result["message"]) + else: + return super(BaseRestDemoPublicApiController, self)._process_method( + service_name, method_name, *args, params=params + ) diff --git a/pms_api_rest/datamodels/__init__.py b/pms_api_rest/datamodels/__init__.py index f02015c30..d45410026 100644 --- a/pms_api_rest/datamodels/__init__.py +++ b/pms_api_rest/datamodels/__init__.py @@ -1,2 +1,4 @@ from . import pms_reservation_short_info from . import pms_reservation_search_param +from . import pms_folio_short_info +from . import pms_folio_search_param diff --git a/pms_api_rest/datamodels/pms_folio_search_param.py b/pms_api_rest/datamodels/pms_folio_search_param.py new file mode 100644 index 000000000..027e65660 --- /dev/null +++ b/pms_api_rest/datamodels/pms_folio_search_param.py @@ -0,0 +1,10 @@ +from marshmallow import fields + +from odoo.addons.datamodel.core import Datamodel + + +class PmsFolioSearchParam(Datamodel): + _name = "pms.folio.search.param" + + id = fields.Integer(required=False, allow_none=False) + name = fields.String(required=False, allow_none=False) diff --git a/pms_api_rest/datamodels/pms_folio_short_info.py b/pms_api_rest/datamodels/pms_folio_short_info.py new file mode 100644 index 000000000..c98a1b866 --- /dev/null +++ b/pms_api_rest/datamodels/pms_folio_short_info.py @@ -0,0 +1,34 @@ +from marshmallow import fields, Schema +from typing import List +from odoo.addons.datamodel.core import Datamodel +from .pms_reservation_short_info import PmsReservationShortInfo + + +class PmsReservationSchema(Schema): + + id = fields.Integer(required=True, allow_none=False) + checkin = fields.String(required=True, allow_none=True) + checkout = fields.String(required=True, allow_none=True) + preferredRoomId = fields.String(required=False, allow_none=True) + roomTypeId = fields.String(required=False, allow_none=True) + priceTotal = fields.Float(required=False, allow_none=True) + adults = fields.Integer(required=False, allow_none=True) + pricelist = fields.String(required=False, allow_none=True) + + + +class PmsFolioShortInfo(Datamodel): + _name = "pms.folio.short.info" + + id = fields.Integer(required=False, allow_none=True) + name = fields.String(required=False, allow_none=True) + partnerName = fields.String(required=False, allow_none=True) + partnerPhone = fields.String(required=False, allow_none=True) + partnerEmail = fields.String(required=False, allow_none=True) + channelType = fields.String(required=False, allow_none=True) + agency = fields.String(required=False, allow_none=True) + # paymentState = fields.String(required=False, allow_none=True) + state = fields.String(required=False, allow_none=True) + pendingAmount = fields.Float(required=False, allow_none=True) + reservations = fields.List(fields.Nested(PmsReservationSchema)) + diff --git a/pms_api_rest/datamodels/pms_reservation_short_info.py b/pms_api_rest/datamodels/pms_reservation_short_info.py index 71f9d886e..a03852e7d 100644 --- a/pms_api_rest/datamodels/pms_reservation_short_info.py +++ b/pms_api_rest/datamodels/pms_reservation_short_info.py @@ -7,19 +7,21 @@ class PmsReservationShortInfo(Datamodel): _name = "pms.reservation.short.info" id = fields.Integer(required=True, allow_none=False) - partner = fields.String(required=True, allow_none=False) - checkin = fields.String(required=True, allow_none=False) - checkout = fields.String(required=True, allow_none=False) - preferredRoomId = fields.String(required=True, allow_none=False) - roomTypeId = fields.String(required=True, allow_none=False) - name = fields.String(required=True, allow_none=False) + partner = fields.String(required=False, allow_none=True) + checkin = fields.String(required=True, allow_none=True) + checkout = fields.String(required=True, allow_none=True) + preferredRoomId = fields.String(required=False, allow_none=True) + roomTypeId = fields.String(required=False, allow_none=True) + name = fields.String(required=False, allow_none=True) partnerRequests = fields.String(required=False, allow_none=True) - state = fields.String(required=True, allow_none=False) - priceTotal = fields.Float(required=True, allow_none=True) - adults = fields.Integer(required=True, allow_none=False) + state = fields.String(required=False, allow_none=True) + priceTotal = fields.Float(required=False, allow_none=True) + adults = fields.Integer(required=False, allow_none=True) channelTypeId = fields.String(required=False, allow_none=True) agencyId = fields.String(required=False, allow_none=True) boardServiceId = fields.String(required=False, allow_none=True) - checkinsRatio = fields.Float(required=True, allow_none=False) - outstanding = fields.Float(required=True, allow_none=False) - pwaActionButtons = fields.Dict(required=True, allow_none=False) + checkinsRatio = fields.Float(required=False, allow_none=True) + outstanding = fields.Float(required=False, allow_none=True) + pricelist = fields.String(required=False, allow_none=True) + folioId = fields.Integer(required=False, allow_none=True) + pwaActionButtons = fields.Dict(required=False, allow_none=True) diff --git a/pms_api_rest/lib_jwt/jwt_http.py b/pms_api_rest/lib_jwt/jwt_http.py index adb20956f..e96d144b3 100644 --- a/pms_api_rest/lib_jwt/jwt_http.py +++ b/pms_api_rest/lib_jwt/jwt_http.py @@ -107,7 +107,7 @@ class JwtHttp: # login success, generate token user = request.env.user.read(return_fields)[0] - exp = datetime.datetime.utcnow() + datetime.timedelta(minutes=3) + exp = datetime.datetime.utcnow() + datetime.timedelta(minutes=30000) token = validator.create_token(user, exp) return self.response( diff --git a/pms_api_rest/services/__init__.py b/pms_api_rest/services/__init__.py index 46413cba0..ec04cb367 100644 --- a/pms_api_rest/services/__init__.py +++ b/pms_api_rest/services/__init__.py @@ -1 +1,2 @@ from . import reservation_services +from . import folio_services diff --git a/pms_api_rest/services/folio_services.py b/pms_api_rest/services/folio_services.py new file mode 100644 index 000000000..27ffb3495 --- /dev/null +++ b/pms_api_rest/services/folio_services.py @@ -0,0 +1,179 @@ +import json + +from odoo.addons.base_rest import restapi +from odoo.addons.base_rest_datamodel.restapi import Datamodel +from odoo.addons.component.core import Component + +from ..datamodels.pms_folio_short_info import PmsReservationSchema + + +class PmsFolioService(Component): + _inherit = "base.rest.service" + _name = "pms.folio.service" + _usage = "folios" + _collection = "pms.reservation.service" + + @restapi.method( + [ + ( + [ + "/", + ], + "GET", + ) + ], + input_param=Datamodel("pms.folio.search.param"), + output_param=Datamodel("pms.folio.short.info", is_list=True), + auth="public", + ) + def search(self, folio_search_param): + domain = [] + if folio_search_param.name: + domain.append(("name", "like", folio_search_param.name)) + if folio_search_param.id: + domain.append(("id", "=", folio_search_param.id)) + res = [] + PmsFolioShortInfo = self.env.datamodels["pms.folio.short.info"] + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] + for folio in ( + self.env["pms.folio"] + .sudo() + .search( + domain, + ) + ): + reservations = [] + for reservation in folio.reservation_ids: + reservation_schema = PmsReservationSchema() + room = reservation.preferred_room_id.name if reservation.preferred_room_id else "" + room_type = reservation.room_type_id.name if reservation.room_type_id else "" + data = { + "id": reservation.id, + "checkin": str(reservation.checkin), + "checkout": str(reservation.checkout), + "preferredRoomId": room, + "roomTypeId": room_type, + "priceTotal": reservation.price_total, + "adults": reservation.adults, + "pricelist": reservation.pricelist_id.name, + } + reservations.append(reservation_schema.load(data)) + '''{ + "id": reservation.id, + "checkin": str(reservation.checkin), + "checkout": str(reservation.checkout), + "preferredRoomId": reservation.preferred_room_id.name + if reservation.preferred_room_id + else "", + "roomTypeId": reservation.room_type_id.name + if reservation.room_type_id + else "", + "priceTotal": reservation.price_total, + "adults": reservation.adults, + "pricelist": reservation.pricelist_id.name, + }''' + + + + + + + ''' + reservations.append( + PmsReservationSchema( + "id":reservation.id, + checkin=str(reservation.checkin), + checkout=str(reservation.checkout), + preferredRoomId=reservation.preferred_room_id.name + if reservation.preferred_room_id + else "", + roomTypeId=reservation.room_type_id.name + if reservation.room_type_id + else "", + priceTotal=reservation.price_total, + adults=reservation.adults, + pricelist=reservation.pricelist_id.name, + ) + ) + ''' + + res.append( + PmsFolioShortInfo( + id=folio.id, + name=folio.name, + partnerName=folio.partner_name if folio.partner_name else "", + partnerPhone=folio.mobile if folio.mobile else "", + partnerEmail=folio.email if folio.email else "", + channelType=folio.channel_type_id if folio.channel_type_id else "", + agency=folio.agency_id if folio.agency_id else "", + # paymentState=dict(folio.fields_get(["payment_state"])["payment_state"]["selection"])[ + # folio.payment_state + # ], + state=dict(folio.fields_get(["state"])["state"]["selection"])[ + folio.state + ], + pendingAmount=folio.pending_amount, + reservations=reservations, + ) + ) + return res + + @restapi.method( + [ + ( + [ + "//reservations", + ], + "GET", + ) + ], + output_param=Datamodel("pms.reservation.short.info", is_list=True), + auth="public", + ) + def get_reservations(self, folio_id): + folio = ( + self.env["pms.folio"].sudo().search([("id", "=", folio_id)]) + ) + res = [] + if not folio.reservation_ids: + pass + else: + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] + + for reservation in folio.reservation_ids: + res.append( + PmsReservationShortInfo( + id=reservation.id, + partner=reservation.partner_id.name, + checkin=str(reservation.checkin), + checkout=str(reservation.checkout), + preferredRoomId=reservation.preferred_room_id.name + if reservation.preferred_room_id + else "", + roomTypeId=reservation.room_type_id.name + if reservation.room_type_id + else "", + name=reservation.name, + partnerRequests=reservation.partner_requests + if reservation.partner_requests + else "", + state=dict(reservation.fields_get(["state"])["state"]["selection"])[ + reservation.state + ], + priceTotal=reservation.price_total, + adults=reservation.adults, + channelTypeId=reservation.channel_type_id + if reservation.channel_type_id + else "", + agencyId=reservation.agency_id if reservation.agency_id else "", + boardServiceId=reservation.board_service_room_id.pms_board_service_id.name + if reservation.board_service_room_id + else "", + checkinsRatio=reservation.checkins_ratio, + outstanding=reservation.folio_id.pending_amount, + pwaActionButtons=json.loads(reservation.pwa_action_buttons) + if reservation.pwa_action_buttons + else {}, + ) + ) + return res diff --git a/pms_api_rest/services/reservation_services.py b/pms_api_rest/services/reservation_services.py index 35f2ada9a..5b00cb649 100644 --- a/pms_api_rest/services/reservation_services.py +++ b/pms_api_rest/services/reservation_services.py @@ -69,6 +69,8 @@ class PmsReservationService(Component): else "", checkinsRatio=reservation.checkins_ratio, outstanding=reservation.folio_id.pending_amount, + pricelist=reservation.pricelist_id.name, + folioId=reservation.folio_id.id, pwaActionButtons=json.loads(reservation.pwa_action_buttons) if reservation.pwa_action_buttons else {}, @@ -105,13 +107,14 @@ class PmsReservationService(Component): "GET", ) ], + output_param=Datamodel("pms.reservation.short.info"), auth="public", ) def get_reservation(self, reservation_id): reservation = ( self.env["pms.reservation"].sudo().search([("id", "=", reservation_id)]) ) - res = [] + res = False if not reservation: pass else: @@ -145,8 +148,70 @@ class PmsReservationService(Component): else "", checkinsRatio=reservation.checkins_ratio, outstanding=reservation.folio_id.pending_amount, - pwaActionButtons=json.loads(reservation.pwa_action_buttons) - if reservation.pwa_action_buttons - else {}, + pricelist=reservation.pricelist_id.name, + folioId=reservation.folio_id.id, + pwaActionButtons={}, ) return res + + @restapi.method( + [ + ( + [ + "/folio/", + ], + "GET", + ) + ], + output_param=Datamodel("pms.reservation.short.info", is_list=True), + auth="public", + ) + def get_reservations(self, folio_id): + folio = ( + self.env["pms.folio"].sudo().search([("id", "=", folio_id)]) + ) + res = [] + if not folio.reservation_ids: + pass + else: + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] + + for reservation in folio.reservation_ids: + res.append( + PmsReservationShortInfo( + id=reservation.id, + partner=reservation.partner_id.name, + checkin=str(reservation.checkin), + checkout=str(reservation.checkout), + preferredRoomId=reservation.preferred_room_id.name + if reservation.preferred_room_id + else "", + roomTypeId=reservation.room_type_id.name + if reservation.room_type_id + else "", + name=reservation.name, + partnerRequests=reservation.partner_requests + if reservation.partner_requests + else "", + state=dict(reservation.fields_get(["state"])["state"]["selection"])[ + reservation.state + ], + priceTotal=reservation.price_total, + adults=reservation.adults, + channelTypeId=reservation.channel_type_id + if reservation.channel_type_id + else "", + agencyId=reservation.agency_id if reservation.agency_id else "", + boardServiceId=reservation.board_service_room_id.pms_board_service_id.name + if reservation.board_service_room_id + else "", + checkinsRatio=reservation.checkins_ratio, + outstanding=reservation.folio_id.pending_amount, + pricelist=reservation.pricelist_id.name, + folioId=reservation.folio_id.id, + pwaActionButtons=json.loads(reservation.pwa_action_buttons) + if reservation.pwa_action_buttons + else {}, + ) + ) + return res