Coverage for src/kwai/api/v1/auth/schemas/user_invitation.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Schemas for a user invitation resource.""" 

2 

3from typing import Self 

4 

5from pydantic import BaseModel 

6 

7from kwai.api.schemas.resources import UserInvitationResourceIdentifier 

8from kwai.core.json_api import Document, ResourceData 

9from kwai.modules.identity.user_invitations.user_invitation import UserInvitationEntity 

10 

11 

12class UserInvitationAttributes(BaseModel): 

13 """Attributes of a user invitation JSON:API resource.""" 

14 

15 email: str 

16 first_name: str 

17 last_name: str 

18 remark: str = "" 

19 expired_at: str | None = None 

20 confirmed_at: str | None = None 

21 

22 

23class UserInvitationResource( 

24 UserInvitationResourceIdentifier, 

25 ResourceData[UserInvitationAttributes, None], 

26): 

27 """A JSON:API resource of a user invitation.""" 

28 

29 

30class UserInvitationDocument(Document[UserInvitationResource, None]): 

31 """A JSON:API document for one or more user invitations.""" 

32 

33 @classmethod 

34 def create(cls, user_invitation: UserInvitationEntity) -> Self: 

35 """Create a document for a user invitation.""" 

36 return cls( 

37 data=UserInvitationResource( 

38 id=str(user_invitation.uuid), 

39 attributes=UserInvitationAttributes( 

40 email=str(user_invitation.email), 

41 first_name=user_invitation.name.first_name or "", 

42 last_name=user_invitation.name.last_name or "", 

43 remark=user_invitation.remark, 

44 expired_at=str(user_invitation.expired_at), 

45 confirmed_at=( 

46 str(user_invitation.confirmed_at) 

47 if user_invitation.confirmed 

48 else None 

49 ), 

50 ), 

51 ) 

52 )