Skip to content

Commit

Permalink
OM-87 WorkerVoucher mutations added (#3)
Browse files Browse the repository at this point in the history
* OM-81 Added voucher enquire query

* OM-71 Splitted rights in migrations

* OM-87 WorkerVoucher mutations added

---------

Co-authored-by: Damian Borowiecki <dborowiecki7@gmail.com>
  • Loading branch information
malinowskikam and dborowiecki authored Dec 4, 2023
1 parent 0331798 commit 9da887d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
107 changes: 107 additions & 0 deletions worker_voucher/gql_mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import graphene as graphene
from django.db import transaction
from pydantic.error_wrappers import ValidationError
from django.contrib.auth.models import AnonymousUser

from core.gql.gql_mutations.base_mutation import BaseMutation
from core.schema import OpenIMISMutation
from worker_voucher.apps import WorkerVoucherConfig
from worker_voucher.models import WorkerVoucher
from worker_voucher.services import WorkerVoucherService


class CreateWorkerVoucherInput(OpenIMISMutation.Input):
code = graphene.String(max_length=255, required=True)
status = graphene.String(max_length=255, required=False)
assigned_date = graphene.Date(required=True)
expiry_date = graphene.Date(required=True)
insuree_id = graphene.Int(required=True)
policyholder_id = graphene.ID(required=True)
json_ext = graphene.types.json.JSONString(required=False)


class UpdateWorkerVoucherInput(CreateWorkerVoucherInput):
id = graphene.ID(required=True)


class CreateWorkerVoucherMutation(BaseMutation):
_mutation_class = "CreateWorkerVoucherMutation"
_mutation_module = "worker_voucher"
_model = WorkerVoucher

@classmethod
def _validate_mutation(cls, user, **data):
if type(user) is AnonymousUser or not user.id or not user.has_perms(
WorkerVoucherConfig.gql_worker_voucher_create_perms):
raise ValidationError("mutation.authentication_required")

@classmethod
def _mutate(cls, user, **data):
if "client_mutation_id" in data:
data.pop('client_mutation_id')
if "client_mutation_label" in data:
data.pop('client_mutation_label')

service = WorkerVoucherService(user)
response = service.create(data)
if not response['success']:
return response
return None

class Input(CreateWorkerVoucherInput):
pass


class UpdateWorkerVoucherMutation(BaseMutation):
_mutation_class = "UpdateWorkerVoucherMutation"
_mutation_module = "worker_voucher"
_model = WorkerVoucher

@classmethod
def _validate_mutation(cls, user, **data):
if type(user) is AnonymousUser or not user.id or not user.has_perms(
WorkerVoucherConfig.gql_worker_voucher_update_perms):
raise ValidationError("mutation.authentication_required")

@classmethod
def _mutate(cls, user, **data):
if "client_mutation_label" in data:
data.pop('client_mutation_label')

service = WorkerVoucherService(user)
response = service.update(data)
if not response['success']:
return response
return None

class Input(UpdateWorkerVoucherInput):
pass


class DeleteWorkerVoucherMutation(BaseMutation):
_mutation_class = "DeleteWorkerVoucherMutation"
_mutation_module = "worker_voucher"
_model = WorkerVoucher

@classmethod
def _validate_mutation(cls, user, **data):
if type(user) is AnonymousUser or not user.id or not user.has_perms(
WorkerVoucherConfig.gql_worker_voucher_delete_perms):
raise ValidationError("mutation.authentication_required")

@classmethod
def _mutate(cls, user, **data):
if "client_mutation_id" in data:
data.pop('client_mutation_id')
if "client_mutation_label" in data:
data.pop('client_mutation_label')

service = WorkerVoucherService(user)
ids = data.get('ids')
if ids:
with transaction.atomic():
for id in ids:
service.delete({'id': id})

class Input(OpenIMISMutation.Input):
ids = graphene.List(graphene.UUID)
9 changes: 9 additions & 0 deletions worker_voucher/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from core.utils import append_validity_filter
from worker_voucher.apps import WorkerVoucherConfig
from worker_voucher.gql_queries import WorkerVoucherGQLType
from worker_voucher.gql_mutations import CreateWorkerVoucherMutation, UpdateWorkerVoucherMutation, \
DeleteWorkerVoucherMutation
from worker_voucher.models import WorkerVoucher
from worker_voucher.services import get_voucher_worker_enquire_filters

Expand Down Expand Up @@ -52,3 +54,10 @@ def resolve_enquire_worker(self, info, national_id=None, **kwargs):
def _check_permissions(user, perms):
if type(user) is AnonymousUser or not user.id or not user.has_perms(perms):
raise PermissionError(_("Unauthorized"))


class Mutation(graphene.ObjectType):
createWorkerVoucher = CreateWorkerVoucherMutation.Field()
updateWorkerVoucher = UpdateWorkerVoucherMutation.Field()
deleteWorkerVoucher = DeleteWorkerVoucherMutation.Field()

0 comments on commit 9da887d

Please sign in to comment.