59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# ITerativ GmbH
|
|
# http://www.iterativ.ch/
|
|
#
|
|
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
|
|
#
|
|
# Created on 03.02.20
|
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
|
import graphene
|
|
from graphene import relay
|
|
|
|
from core.hep_client import HepClient, HepClientException
|
|
from users.user_signup_login_handler import check_and_create_licenses, create_role_for_user
|
|
|
|
|
|
class CouponError(graphene.ObjectType):
|
|
field = graphene.String()
|
|
|
|
|
|
class Coupon(relay.ClientIDMutation):
|
|
class Input:
|
|
coupon_code_input = graphene.String()
|
|
|
|
success = graphene.Boolean()
|
|
errors = graphene.List(CouponError) # todo: change for consistency
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
coupon_code = kwargs.get('coupon_code_input')
|
|
hep_client = HepClient()
|
|
|
|
try:
|
|
hep_id = info.context.user.hep_id
|
|
except AttributeError:
|
|
return cls(success=False, errors=[{'field': 'not_authenticated'}])
|
|
|
|
try:
|
|
response = hep_client.coupon_redeem(coupon_code, hep_id)
|
|
except HepClientException:
|
|
return cls(success=False, errors=[{'field': 'unknown_error'}])
|
|
|
|
if not response:
|
|
return cls(success=False, errors=[{'field': 'invalid_coupon'}])
|
|
|
|
license, error_msg = check_and_create_licenses(hep_client, info.context.user)
|
|
|
|
# todo fail if no license
|
|
if error_msg:
|
|
return info.context.user, error_msg
|
|
|
|
create_role_for_user(info.context.user, license.for_role.key)
|
|
|
|
return cls(success=True, errors=[])
|
|
|
|
|
|
class CouponMutations:
|
|
redeem_coupon = Coupon.Field()
|