2022-09-29 16:29:36 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2021-01-05 14:28:49 +00:00
|
|
|
from apiserver.apierrors import errors
|
|
|
|
from apiserver.apimodels.users import CreateRequest
|
2024-12-05 20:32:28 +00:00
|
|
|
from apiserver.config.info import get_version
|
2021-01-05 14:28:49 +00:00
|
|
|
from apiserver.database.errors import translate_errors_context
|
|
|
|
from apiserver.database.model.user import User
|
2019-06-10 21:24:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserBLL:
|
|
|
|
@staticmethod
|
|
|
|
def create(request: CreateRequest):
|
|
|
|
user_id = request.id
|
|
|
|
with translate_errors_context("creating user"):
|
|
|
|
if user_id and User.objects(id=user_id).only("id"):
|
|
|
|
raise errors.bad_request.UserIdExists(id=user_id)
|
|
|
|
|
2024-12-05 20:32:28 +00:00
|
|
|
user = User(
|
|
|
|
**request.to_struct(),
|
|
|
|
created=datetime.utcnow(),
|
|
|
|
created_in_version=get_version(),
|
|
|
|
)
|
2019-06-10 21:24:35 +00:00
|
|
|
user.save(force_insert=True)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete(user_id: str):
|
|
|
|
with translate_errors_context("deleting user"):
|
|
|
|
res = User.objects(id=user_id).delete()
|
|
|
|
if not res:
|
|
|
|
raise errors.bad_request.InvalidUserId(id=user_id)
|