felicity-lims/backend/felicity_lims/felicity/gql/client/mutations.py

133 lines
5.5 KiB
Python
Raw Normal View History

2021-01-06 19:52:14 +08:00
import logging
2021-09-22 15:34:32 +08:00
import inspect
from typing import Optional, List, Dict
import strawberry
from strawberry.types import Info
2021-01-06 19:52:14 +08:00
from felicity.apps.client import schemas, models
2021-03-26 04:22:59 +08:00
from felicity.gql.client.types import ClientType, ClientContactType
2021-09-22 15:34:32 +08:00
from felicity.utils import get_passed_args
2021-01-06 19:52:14 +08:00
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
2021-09-22 15:34:32 +08:00
@strawberry.type
class ClientMutations:
@strawberry.mutation
2021-09-27 23:45:22 +08:00
async def create_client(self, info: Info, name: str, code: str, district_uid: Optional[int] = None,
email: Optional[str] = None, email_cc: Optional[str] = None,
consent_email: Optional[bool] = False, phone_mobile: Optional[str] = None,
phone_business: Optional[str] = None, consent_sms: Optional[bool] = False,
2021-09-22 15:34:32 +08:00
internal_use: Optional[bool] = False, active: Optional[bool] = True) -> ClientType:
inspector = inspect.getargvalues(inspect.currentframe())
passed_args = get_passed_args(inspector)
2021-01-06 19:52:14 +08:00
if not code or not name:
2021-09-22 15:34:32 +08:00
raise Exception("Please Provide a name and a unique client code")
exists = await models.Client.get(code=code)
2021-01-06 19:52:14 +08:00
if exists:
2021-09-22 15:34:32 +08:00
raise Exception(f"Client code {code} already belong to client {exists.name}")
# incoming = {
# "name": name,
# "code": code,
# "active": active,
# }
incoming: Dict = dict()
for k, v in passed_args.items():
2021-01-06 19:52:14 +08:00
incoming[k] = v
2021-09-22 15:34:32 +08:00
obj_in = schemas.ClientCreate(**incoming)
client = await models.Client.create(obj_in)
return client
@strawberry.mutation
async def update_client(self, info, uid: int, name: Optional[str], code: Optional[str], district_uid: Optional[int],
email: Optional[str], email_cc: Optional[str], consent_email: Optional[bool],
mobile_phone: Optional[str], business_phone: Optional[str], consent_sms: Optional[bool],
internal_use: Optional[bool] = False, active: Optional[bool] = True) -> ClientType:
inspector = inspect.getargvalues(inspect.currentframe())
passed_args = get_passed_args(inspector)
2021-01-06 19:52:14 +08:00
if not uid:
2021-09-22 15:34:32 +08:00
raise Exception("No uid provided to identify update obj")
client = await models.Client.get(uid=uid)
2021-01-06 19:52:14 +08:00
if not client:
2021-09-22 15:34:32 +08:00
raise Exception(f"Client with uid {uid} not found. Cannot update obj ...")
2021-01-06 19:52:14 +08:00
2021-09-22 15:34:32 +08:00
obj_data = client.to_dict()
2021-01-06 19:52:14 +08:00
for field in obj_data:
2021-09-22 15:34:32 +08:00
if field in passed_args:
2021-01-06 19:52:14 +08:00
try:
2021-09-22 15:34:32 +08:00
setattr(client, field, passed_args[field])
2021-01-06 19:52:14 +08:00
except Exception as e:
logger.warning(f"failed to set attribute {field}: {e}")
2021-09-22 15:34:32 +08:00
obj_in = schemas.ClientUpdate(**client.to_dict())
client = await client.update(obj_in)
return client
@strawberry.mutation
async def create_client_contact(self, info, first_name: str, client_uid: str, last_name: Optional[str],
email: Optional[str], email_cc: Optional[str], mobile_phone: Optional[str],
consent_sms: Optional[bool] = False, is_active: bool = True) -> ClientContactType:
inspector = inspect.getargvalues(inspect.currentframe())
passed_args = get_passed_args(inspector)
2021-03-26 04:22:59 +08:00
if not client_uid or not first_name:
2021-09-22 15:34:32 +08:00
raise Exception("Please Provide a first_name and a client uid")
2021-03-26 04:22:59 +08:00
client_exists = models.Client.get(uid=client_uid)
if not client_exists:
2021-09-22 15:34:32 +08:00
raise Exception(f"Client with uid {client_uid} does not exist")
2021-03-26 04:22:59 +08:00
contact_exists = models.ClientContact.where(client_uid=client_uid, first_name=first_name).all()
if contact_exists:
2021-09-22 15:34:32 +08:00
raise Exception(f"Client Contact with name {first_name} already exists")
# incoming = {
# "first_name": first_name,
# "client_uid": client_uid,
# "is_active": True,
# "is_superuser": False,
# }
incoming: Dict = dict()
for k, v in passed_args.items():
2021-03-26 04:22:59 +08:00
incoming[k] = v
obj_in = schemas.ClientContactCreate(**incoming)
2021-09-22 15:34:32 +08:00
client_contact = await models.ClientContact.create(obj_in)
return client_contact
@strawberry.mutation
2021-09-27 23:45:22 +08:00
async def update_client_contact(self, info, uid: int, first_name: str, last_name: Optional[str],
2021-09-22 15:34:32 +08:00
email: Optional[str], email_cc: Optional[str], mobile_phone: Optional[str],
consent_sms: Optional[bool] = False, is_active: bool = True) -> ClientContactType:
inspector = inspect.getargvalues(inspect.currentframe())
passed_args = get_passed_args(inspector)
2021-03-26 04:22:59 +08:00
if not uid:
2021-09-22 15:34:32 +08:00
raise Exception("No uid provided to identify update obj")
2021-03-26 04:22:59 +08:00
2021-09-22 15:34:32 +08:00
client_contact = await models.ClientContact.get(uid=uid)
2021-03-26 04:22:59 +08:00
if not client_contact:
2021-09-22 15:34:32 +08:00
raise Exception(f"Client Contact with uid {uid} not found. Cannot update obj ...")
2021-03-26 04:22:59 +08:00
2021-09-22 15:34:32 +08:00
obj_data = client_contact.to_dict()
2021-03-26 04:22:59 +08:00
for field in obj_data:
2021-09-22 15:34:32 +08:00
if field in passed_args:
2021-03-26 04:22:59 +08:00
try:
2021-09-22 15:34:32 +08:00
setattr(client_contact, field, passed_args[field])
2021-03-26 04:22:59 +08:00
except Exception as e:
logger.warning(f"failed to set attribute {field}: {e}")
obj_in = schemas.ClientContactUpdate(**client_contact.to_dict())
2021-09-22 15:34:32 +08:00
client_contact = await client_contact.update(obj_in)
return client_contact