id_document.ts (2971B)
1 import { Admin } from "#core/domain/admin.ts"; 2 import { IDDocument, IDDocumentState } from "#core/domain/id_document.ts"; 3 import { IDInfo } from "#core/domain/id_info.ts"; 4 import { Picture } from "#core/domain/picture.ts"; 5 import { UUID } from "#core/domain/uuid.ts"; 6 7 export type IDInfoDto = { 8 firstName: string; 9 lastName: string; 10 birthDate: Date; 11 sex: string; 12 nationality: string; 13 country: string; 14 }; 15 16 export type IDDocumentDto = { [K in keyof IDInfoDto]: IDInfoDto[K] | null } & { 17 uuid: string; 18 state: string; 19 back: string | null; 20 front: string | null; 21 faceLeft: string | null; 22 faceFront: string | null; 23 faceRight: string | null; 24 admin: string | null; 25 version: number; 26 }; 27 28 const ToIDDocumentState = { 29 capturing: IDDocumentState.CAPTURING, 30 registered: IDDocumentState.REGISTERED, 31 approved: IDDocumentState.APPROVED, 32 declined: IDDocumentState.DECLINED, 33 }; 34 35 const FromIDDocumentState = { 36 [IDDocumentState.CAPTURING]: "capturing", 37 [IDDocumentState.REGISTERED]: "registered", 38 [IDDocumentState.APPROVED]: "approved", 39 [IDDocumentState.DECLINED]: "declined", 40 }; 41 42 export function mapToIDInfo(dto: IDInfoDto) { 43 return new IDInfo( 44 dto.firstName, 45 dto.lastName, 46 dto.birthDate, 47 dto.sex, 48 dto.nationality, 49 dto.country, 50 ); 51 } 52 53 export function mapFromIDInfo(info: IDInfo): IDInfoDto { 54 return { 55 firstName: info.firstName, 56 lastName: info.lastName, 57 birthDate: info.birthDate, 58 sex: info.sex, 59 nationality: info.nationality, 60 country: info.country, 61 }; 62 } 63 64 export function mapToIDDocument(dto: IDDocumentDto) { 65 return new IDDocument( 66 new UUID(dto.uuid), 67 ToIDDocumentState[dto.state as keyof typeof ToIDDocumentState], 68 dto.firstName ? mapToIDInfo(dto as never) : null, 69 dto.front ? new Picture(dto.front) : null, 70 dto.back ? new Picture(dto.back) : null, 71 dto.faceLeft ? new Picture(dto.faceLeft) : null, 72 dto.faceFront ? new Picture(dto.faceFront) : null, 73 dto.faceRight ? new Picture(dto.faceRight) : null, 74 dto.admin ? new Admin(new UUID(dto.admin)) : null, 75 dto.version, 76 ); 77 } 78 79 export function mapFromIDDocument(idDocument: IDDocument) { 80 return { 81 uuid: idDocument.uuid.toString(), 82 state: FromIDDocumentState[idDocument.state], 83 info: idDocument.info ? mapFromIDInfo(idDocument.info) : null, 84 front: idDocument.front?.toString() ?? null, 85 back: idDocument.back?.toString() ?? null, 86 faceLeft: idDocument.faceLeft?.toString() ?? null, 87 faceFront: idDocument.faceFront?.toString() ?? null, 88 faceRight: idDocument.faceRight?.toString() ?? null, 89 admin: idDocument.admin?.uuid.toString() ?? null, 90 firstName: idDocument.info?.firstName ?? null, 91 lastName: idDocument.info?.lastName ?? null, 92 birthDate: idDocument.info?.birthDate ?? null, 93 sex: idDocument.info?.sex ?? null, 94 nationality: idDocument.info?.nationality ?? null, 95 country: idDocument.info?.country ?? null, 96 version: idDocument.version, 97 }; 98 }