felicity-lims/felicity/api/gql/client/mutations.py

224 lines
7.6 KiB
Python
Raw Normal View History

2021-01-06 19:52:14 +08:00
import logging
from typing import Dict, Optional
2021-01-06 19:52:14 +08:00
import strawberry # noqa
2023-04-16 19:32:26 +08:00
from api.gql import OperationError, DeletedItem, auth_from_info, verify_user_auth
2023-04-10 20:23:31 +08:00
from api.gql.client.types import ClientContactType, ClientType
from apps.client import models, schemas
from core.uid_gen import FelicityID
2023-03-19 23:21:32 +08:00
from strawberry.types import Info # noqa
2021-01-06 19:52:14 +08:00
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
2022-11-06 20:09:44 +08:00
ClientResponse = strawberry.union(
"ClientResponse", (ClientType, OperationError), description="" # noqa
)
2022-11-06 20:09:44 +08:00
ClientContactResponse = strawberry.union(
"ClientContactResponse", (ClientContactType, OperationError), description="" # noqa
)
2023-04-16 19:32:26 +08:00
DeleteContactResponse = strawberry.union(
"DeleteContactResponse", (DeletedItem, OperationError), description="" # noqa
)
@strawberry.input
class ClientInputType:
name: str
code: str
2023-04-25 22:42:39 +08:00
district_uid: FelicityID | None = None
email: str | None = None
email_cc: str | None = None
consent_email: bool| None = False
phone_mobile: str | None = None
phone_business: str | None = None
consent_sms: bool| None = False
internal_use: bool| None = False
active: bool| None = True
@strawberry.input
class ClientContactInputType:
first_name: str
2023-03-19 23:21:32 +08:00
client_uid: FelicityID
2023-04-25 22:42:39 +08:00
last_name: str | None = None
email: str | None = None
email_cc: str | None = None
mobile_phone: str | None = None
consent_sms: bool| None = False
is_active: bool = True
2021-01-06 19:52:14 +08:00
2021-09-22 15:34:32 +08:00
@strawberry.type
class ClientMutations:
@strawberry.mutation
2022-11-06 20:09:44 +08:00
async def create_client(
self, info: Info, payload: ClientInputType
) -> ClientResponse:
2021-09-22 15:34:32 +08:00
is_authenticated, felicity_user = await auth_from_info(info)
2022-11-20 21:20:41 +08:00
success, auth_err = verify_user_auth(
2022-11-06 20:09:44 +08:00
is_authenticated,
felicity_user,
"Only Authenticated user can create clients",
)
2022-11-20 21:20:41 +08:00
if not success:
return auth_err
2021-09-22 15:34:32 +08:00
if not payload.code or not payload.name:
return OperationError(
error="Please Provide a name and a unique client code"
)
2021-09-22 15:34:32 +08:00
exists = await models.Client.get(code=payload.code)
2021-01-06 19:52:14 +08:00
if exists:
return OperationError(
error=f"Client code {payload.code} already belong to client {exists.name}"
)
2023-04-25 22:42:39 +08:00
incoming: dict = {
"created_by_uid": felicity_user.uid,
"updated_by_uid": felicity_user.uid,
}
for k, v in payload.__dict__.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)
2022-11-20 21:20:41 +08:00
client: models.Client = await models.Client.create(obj_in)
return ClientType(**client.marshal_simple())
2021-09-22 15:34:32 +08:00
@strawberry.mutation
2022-11-06 20:09:44 +08:00
async def update_client(
2023-03-19 23:21:32 +08:00
self, info, uid: FelicityID, payload: ClientInputType
2022-11-06 20:09:44 +08:00
) -> ClientResponse:
2021-09-22 15:34:32 +08:00
is_authenticated, felicity_user = await auth_from_info(info)
2022-11-06 20:09:44 +08:00
verify_user_auth(
is_authenticated,
felicity_user,
"Only Authenticated user can update clients",
)
2021-09-22 15:34:32 +08:00
2021-01-06 19:52:14 +08:00
if not uid:
2022-11-06 20:09:44 +08:00
return OperationError(error="No uid provided to identify update obj")
2021-09-22 15:34:32 +08:00
client = await models.Client.get(uid=uid)
2021-01-06 19:52:14 +08:00
if not client:
return OperationError(
error=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:
if field in payload.__dict__:
2021-01-06 19:52:14 +08:00
try:
setattr(client, field, payload.__dict__[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)
2022-11-20 21:20:41 +08:00
return ClientType(**client.marshal_simple())
2021-09-22 15:34:32 +08:00
@strawberry.mutation
2022-11-06 20:09:44 +08:00
async def create_client_contact(
self, info, payload: ClientContactInputType
) -> ClientContactResponse:
2021-09-22 15:34:32 +08:00
is_authenticated, felicity_user = await auth_from_info(info)
2022-11-06 20:09:44 +08:00
verify_user_auth(
is_authenticated,
felicity_user,
"Only Authenticated user can create client contacts",
)
2021-09-22 15:34:32 +08:00
if not payload.client_uid or not payload.first_name:
2022-11-06 20:09:44 +08:00
return OperationError(error="Please Provide a first_name and a client uid")
2021-09-22 15:34:32 +08:00
client_exists = await models.Client.get(uid=payload.client_uid)
2021-03-26 04:22:59 +08:00
if not client_exists:
return OperationError(
error=f"Client with uid {payload.client_uid} does not exist"
)
2021-09-22 15:34:32 +08:00
2022-11-06 20:09:44 +08:00
contact_exists = await models.ClientContact.get_all(
client_uid=payload.client_uid, first_name=payload.first_name
) # noqa
2021-10-01 15:37:34 +08:00
logger.warning(contact_exists)
2021-03-26 04:22:59 +08:00
if contact_exists:
return OperationError(
error=f"Client Contact with name {payload.first_name} already exists"
)
2023-04-25 22:42:39 +08:00
incoming: dict = {
"created_by_uid": felicity_user.uid,
"updated_by_uid": felicity_user.uid,
}
for k, v in payload.__dict__.items():
2021-03-26 04:22:59 +08:00
incoming[k] = v
obj_in = schemas.ClientContactCreate(**incoming)
2022-11-20 21:20:41 +08:00
client_contact: models.ClientContact = await models.ClientContact.create(obj_in)
2023-04-16 19:32:26 +08:00
return ClientContactType(**client_contact.marshal_simple(exclude=["is_superuser", "auth_uid"]))
2021-09-22 15:34:32 +08:00
@strawberry.mutation
2022-11-06 20:09:44 +08:00
async def update_client_contact(
2023-03-19 23:21:32 +08:00
self, info, uid: FelicityID, payload: ClientContactInputType
2022-11-06 20:09:44 +08:00
) -> ClientContactResponse:
2021-09-22 15:34:32 +08:00
is_authenticated, felicity_user = await auth_from_info(info)
2022-11-06 20:09:44 +08:00
verify_user_auth(
is_authenticated,
felicity_user,
"Only Authenticated user can update client contacts",
)
2021-09-22 15:34:32 +08:00
2021-03-26 04:22:59 +08:00
if not uid:
2022-11-06 20:09:44 +08:00
return OperationError(error="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:
return OperationError(
error=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:
if field in payload.__dict__:
2021-03-26 04:22:59 +08:00
try:
setattr(client_contact, field, payload.__dict__[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)
2023-04-16 19:32:26 +08:00
return ClientContactType(**client_contact.marshal_simple(exclude=["is_superuser", "auth_uid"]))
@strawberry.mutation
async def delete_client_contact(self, info, uid: FelicityID) -> DeleteContactResponse:
is_authenticated, felicity_user = await auth_from_info(info)
verify_user_auth(
is_authenticated,
felicity_user,
"Only Authenticated user can delete/deactivate client contacts",
)
if not uid:
return OperationError(error="No uid provided to identify deletion obj")
client_contact = await models.ClientContact.get(uid=uid)
if not client_contact:
return OperationError(
error=f"Client Contact with uid {uid} not found. Cannot delete obj ..."
)
try:
setattr(client_contact, "is_active", False)
except Exception as e:
logger.warning(f"failed to set attribute {field}: {e}")
obj_in = schemas.ClientContactUpdate(**client_contact.to_dict())
client_contact = await client_contact.update(obj_in)
return DeletedItem(uid=client_contact.uid)