From cbff3b4a01b98319fca8899ff809ed2d896212d1 Mon Sep 17 00:00:00 2001 From: Gustavo Valverde Date: Fri, 28 Jul 2023 14:44:43 +0100 Subject: [PATCH] ref(ory): use schema `Citizens Schema v0.3.4` (#63) * ref(ory): use schema `Citizens Schema v0.2.0` * fix(api): add firstname * fix(ory): correct schema adaptations * fix(schema): `emails` is an array * fix(schema): firstName is name * fix(schema): revert email array * ref(ory): adapting props to `Schema v0.3.4` --------- Co-authored-by: Marluan Espiritusanto --- .../citizen-complete-data.interface.ts | 4 ++- src/constants/messages.ts | 10 +++--- src/pages/api/citizens/[cedula].ts | 35 +++++++++++++++---- src/pages/api/types/citizens.type.ts | 11 ++++++ src/pages/api/types/iam.type.ts | 18 ++++++++-- src/pages/api/types/index.ts | 2 ++ src/pages/register/stepper/step3.tsx | 10 ++++-- 7 files changed, 72 insertions(+), 18 deletions(-) diff --git a/src/common/interfaces/citizen-complete-data.interface.ts b/src/common/interfaces/citizen-complete-data.interface.ts index 52d322ce..58e9619c 100644 --- a/src/common/interfaces/citizen-complete-data.interface.ts +++ b/src/common/interfaces/citizen-complete-data.interface.ts @@ -1,6 +1,8 @@ export interface CitizenCompleteData { id: string; - name: string; + names: string; firstSurname: string; secondSurname: string; + gender: string; + birthDate: string; } diff --git a/src/constants/messages.ts b/src/constants/messages.ts index dbd8c1b1..34e3c4d7 100644 --- a/src/constants/messages.ts +++ b/src/constants/messages.ts @@ -1,5 +1,5 @@ export const CREATE_BROWSER_REGISTRATION_FLOW_ERROR = - '¡Ups! Ha ocurrido un error, favor intentarlo más tarde.'; + '¡Ups! Ha ocurrido un error, por favor inténtelo más tarde.'; export const INVALID_CEDULA_ERROR = 'Al parecer ha introducido una cédula inválida'; export const IDENTITY_ALREADY_EXISTS_ERROR = @@ -7,12 +7,12 @@ export const IDENTITY_ALREADY_EXISTS_ERROR = export const INVALID_CEDULA_NUMBER_ERROR = 'Al parecer ha introducido una numeración de cédula inválida'; export const RECAPTCHA_ISSUES_ERROR = - 'Al parecer el reCaptcha no ha validado correctamente su solicitud, intente nuevamente más tarde'; + 'Al parecer el reCaptcha no ha validado correctamente su solicitud, refresque e intente nuevamente'; export const RECAPTCHA_VALIDATION_ERROR = - 'No podemos validar si eres un humano, intenta desde otro navegador o dispositivo.'; + 'No podemos validar la seguridad de su navegador, verifique si tiene actualizaciones pendientes o intente con otro dispositivo.'; export const VALIDATE_PASSWORD_ERROR = 'Al parecer ha ocurrido un error al intentar validar su contraseña, inténtelo de nuevo más tarde.'; export const CREATE_IDENTITY_ERROR = - 'Ha ocurrido un error intentando crear la cuenta del ciudadano. Por favor, inténtelo más tarde.'; + 'Ha ocurrido un error intentando crear la cuenta ciudadana. Por favor, inténtelo más tarde.'; export const NON_ACCEPTED_TERMS_AND_CONDS_ERROR = - 'Para continuar, se deben leer y aceptar los términos y políticas de privacidad.'; + 'Para continuar, debe leer y aceptar los términos y políticas de privacidad.'; diff --git a/src/pages/api/citizens/[cedula].ts b/src/pages/api/citizens/[cedula].ts index 237c8110..f34ae834 100644 --- a/src/pages/api/citizens/[cedula].ts +++ b/src/pages/api/citizens/[cedula].ts @@ -3,16 +3,20 @@ import axios from 'axios'; import { CitizensBasicInformationResponse, + CitizensBirthInformationResponse, CitizensTokenResponse, } from '../types'; export default async function handler( req: NextApiRequest, res: NextApiResponse<{ - name: string; id: string; + name?: string; + names?: string; firstSurname?: string; secondSurname?: string; + gender?: string; + birthDate?: string; } | void> ): Promise { const { token } = req.cookies; @@ -49,15 +53,32 @@ export default async function handler( } ); - const { names, id, firstSurname, secondSurname } = citizen.payload; + const { names, id, firstSurname, secondSurname, gender } = citizen.payload; + const name = names.split(' ')[0]; if (validated) { - return res - .status(200) - .json({ name: names, id, firstSurname, secondSurname }); - } + const { data: citizensBirthData } = + await http.get( + `/${cedula}/info/birth?api-key=${process.env.CEDULA_API_KEY}`, + { + headers: { + Authorization: `Bearer ${citizensToken.access_token}`, + }, + } + ); - const name = names.split(' ')[0]; + let { birthDate } = citizensBirthData.payload; + birthDate = birthDate.split('T')[0]; + + return res.status(200).json({ + names, + id, + firstSurname, + secondSurname, + gender, + birthDate, + }); + } return res.status(200).json({ name, id }); } diff --git a/src/pages/api/types/citizens.type.ts b/src/pages/api/types/citizens.type.ts index 5c5de946..26aa3127 100644 --- a/src/pages/api/types/citizens.type.ts +++ b/src/pages/api/types/citizens.type.ts @@ -5,12 +5,23 @@ export type CitizenBasicInformation = { secondSurname: string; gender: 'M' | 'F'; }; +export type CitizenBirthInformation = { + id: string; + birthPlace: string; + birthDate: string; + nationality: string; +}; export type CitizensBasicInformationResponse = { valid: boolean; payload: CitizenBasicInformation; }; +export type CitizensBirthInformationResponse = { + valid: boolean; + payload: CitizenBirthInformation; +}; + export type CitizensTokenResponse = { access_token: string; token_type: string; diff --git a/src/pages/api/types/iam.type.ts b/src/pages/api/types/iam.type.ts index 98bfdfb0..2052bb18 100644 --- a/src/pages/api/types/iam.type.ts +++ b/src/pages/api/types/iam.type.ts @@ -8,7 +8,7 @@ export interface Identity { traits: Traits; verifiable_addresses: VerifiableAddress[]; recovery_addresses: RecoveryAddress[]; - metadata_public: any; + metadata_public: MetadataPublic; metadata_admin: any; created_at: string; updated_at: string; @@ -37,9 +37,23 @@ export interface Webauthn { export interface Traits { cedula: string; + shortName: shortName; email: string; + phone: string; +} + +export interface shortName { firstName: string; - lastName: string; + firstSurname: string; +} + +export interface MetadataPublic { + fullName: fullName; +} + +export interface fullName { + givenName: string; + surname: string; } export interface VerifiableAddress { diff --git a/src/pages/api/types/index.ts b/src/pages/api/types/index.ts index c2f1b3f0..2d42d0d2 100644 --- a/src/pages/api/types/index.ts +++ b/src/pages/api/types/index.ts @@ -2,6 +2,8 @@ export type { CitizenBasicInformation, CitizensBasicInformationResponse, CitizensTokenResponse, + CitizenBirthInformation, + CitizensBirthInformationResponse, } from './citizens.type'; export type { Identity } from './iam.type'; export type { ReCaptchaResponse } from './recaptcha.type'; diff --git a/src/pages/register/stepper/step3.tsx b/src/pages/register/stepper/step3.tsx index 0c9e3138..5e4cb84f 100644 --- a/src/pages/register/stepper/step3.tsx +++ b/src/pages/register/stepper/step3.tsx @@ -126,7 +126,7 @@ export default function Step3({ handleNext, infoCedula }: any) { (n: any) => n.attributes['name'] === 'csrf_token' ); const csrf_token = node?.attributes.value as string; - const lastName = `${citizen.firstSurname} ${citizen.secondSurname}`; + const surname = `${citizen.firstSurname} ${citizen.secondSurname}`; const method = 'password'; const updateRegistrationFlowBody: UpdateRegistrationFlowBody = { @@ -136,8 +136,12 @@ export default function Step3({ handleNext, infoCedula }: any) { traits: { email: form.email, cedula: citizen.id, - firstName: citizen.name, - lastName, + fullName: { + givenName: citizen.names, + surname, + }, + birthDate: citizen.birthDate, + gender: citizen.gender, }, };