def _create_from_draft( self, draft: types.PaymentDraft, id: typing.Optional[str] = None ) -> types.Payment: object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4()) payment = types.Payment( id=str(object_id), key=draft.key, version=1, created_at=datetime.datetime.now(datetime.timezone.utc), last_modified_at=datetime.datetime.now(datetime.timezone.utc), customer=draft.customer, amount_authorized=draft.amount_authorized, amount_paid=utils._money_to_typed(draft.amount_paid), amount_planned=utils._money_to_typed(draft.amount_planned), amount_refunded=utils._money_to_typed(draft.amount_refunded), anonymous_id=draft.anonymous_id, payment_method_info=draft.payment_method_info, payment_status=draft.payment_status, transactions=[ self._create_transaction_draft(transaction) for transaction in draft.transactions or [] ], custom=utils.create_from_draft(draft.custom), ) return payment
def _create_from_draft( self, draft: models.ShippingMethodDraft, id: typing.Optional[str] = None) -> models.ShippingMethod: object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4()) zone_rates: typing.List[models.ZoneRate] = [] for rate_draft in draft.zone_rates or []: shipping_rates: typing.List[models.ShippingRate] = [] for shipping_rate_draft in rate_draft.shipping_rates or []: shipping_rate = models.ShippingRate( price=utils._money_to_typed(shipping_rate_draft.price), free_above=utils._money_to_typed( shipping_rate_draft.free_above), tiers=copy.deepcopy(shipping_rate_draft.tiers), ) shipping_rates.append(shipping_rate) zone_rate = models.ZoneRate(zone=rate_draft.zone, shipping_rates=shipping_rates) zone_rates.append(zone_rate) return models.ShippingMethod( id=str(object_id), key=draft.key, version=1, created_at=datetime.datetime.now(datetime.timezone.utc), last_modified_at=datetime.datetime.now(datetime.timezone.utc), name=draft.name, description=draft.description, tax_category=draft.tax_category, zone_rates=zone_rates, is_default=draft.is_default, predicate=draft.predicate, )
def _create_transaction_draft( self, draft: types.TransactionDraft) -> types.Transaction: return types.Transaction( id=str(uuid.uuid4()), timestamp=draft.timestamp, type=draft.type, amount=utils._money_to_typed(draft.amount), interaction_id=draft.interaction_id, state=draft.state, )
def updater(self, obj, action): draft: types.TransactionDraft = getattr(action, "transaction") transaction = types.Transaction( id=str(uuid.uuid4()), timestamp=draft.timestamp, type=draft.type, amount=utils._money_to_typed(draft.amount), interaction_id=draft.interaction_id, state=draft.state, ) transaction = schemas.TransactionSchema().dump(transaction) if not obj["transactions"]: obj["transactions"] = [] new = copy.deepcopy(obj) new["transactions"].append(transaction) return new
def convert_draft_price(price_draft: models.PriceDraft, price_id: str = None) -> models.Price: tiers: Optional[List[models.PriceTier]] = None if price_draft.tiers: tiers = [utils.create_from_draft(tier) for tier in price_draft.tiers] return models.Price( id=price_id or str(uuid.uuid4()), country=price_draft.country, channel=price_draft.channel, value=utils._money_to_typed(price_draft.value), valid_from=price_draft.valid_from, valid_until=price_draft.valid_until, discounted=price_draft.discounted, custom=utils.create_from_draft(price_draft.custom), tiers=tiers, )