From 4d7e4965749c85a6fed74cbdf7b6b422e397e385 Mon Sep 17 00:00:00 2001 From: Alvar San Martin Date: Tue, 10 Mar 2026 17:02:41 +0100 Subject: [PATCH] Docs y mejora de deteccion de id de entrada --- docs/nfc-server/Generate activation label.yml | 21 +++++++++++++------ src/aplication/BodyValidator.ts | 13 +++++++++++- src/aplication/Nfc.controller.ts | 3 ++- src/aplication/Nfc.usecases.ts | 15 ++++++++++++- src/aplication/validators.ts | 9 +++++++- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/docs/nfc-server/Generate activation label.yml b/docs/nfc-server/Generate activation label.yml index f187082..b942913 100644 --- a/docs/nfc-server/Generate activation label.yml +++ b/docs/nfc-server/Generate activation label.yml @@ -26,12 +26,12 @@ runtime: test("Body must include a data field", () => { const body = res.getBody() - expect(body.data).toBeDefined() + expect(body.data).to.not.be.undefined }) test("Body should include the label", () => { const body = res.getBody() - expect(body.data.label).toBeDefined() + expect(body.data.label).to.not.be.undefined }) settings: @@ -50,9 +50,18 @@ examples: type: json data: |- { - "card_id": "", + "card_id": "019cd877-ab5f-718c-9d77-3cbf6972e535", "override": false } + + { + "card_id": "019cd877-ab5f-718c-9d77-3cbf6972e535", + "override": true + } + + { + "card_id": "019cd877-ab5f-718c-9d77-3cbf6972e535", + } response: status: 200 statusText: OK @@ -77,7 +86,7 @@ examples: type: json data: |- { - "card_id": "", + "card_id": "1234", // no es un uuid valido "override": false } response: @@ -91,8 +100,8 @@ examples: data: |- { "error": { - "msg": "", - "field": "" + "msg": "El campo card_id no es un uuidv7", + "field": "card_id" } } - name: 500 Response diff --git a/src/aplication/BodyValidator.ts b/src/aplication/BodyValidator.ts index 32efce0..74bf500 100644 --- a/src/aplication/BodyValidator.ts +++ b/src/aplication/BodyValidator.ts @@ -20,14 +20,25 @@ export class BodyValidator { public validate(obj: T): Result<{ msg: string, field: string }, boolean> { for (const validator of this.validatorList) { - if (validator.validationFunc(obj) == false) + // Como no manejamos las funciones de validacion pueden lanzar excepciones + try { + if (validator.validationFunc(obj) == false) + return { + error: { + msg: validator.errorMsg, + field: String(validator.field) + } + } + } catch (e) { return { error: { msg: validator.errorMsg, field: String(validator.field) } } + } } + return { data: true }; diff --git a/src/aplication/Nfc.controller.ts b/src/aplication/Nfc.controller.ts index 6eba4c5..c99ec45 100644 --- a/src/aplication/Nfc.controller.ts +++ b/src/aplication/Nfc.controller.ts @@ -27,7 +27,8 @@ export class NfcController { return 1; } - const { card_id, override } = body.card_id + const { card_id, override } = body + // Para en el futuro merter los args const pormUsecase = this.nfcUsecases.generateActivationLabel() const useCaseRes = await pormUsecase({ card_id, override }) diff --git a/src/aplication/Nfc.usecases.ts b/src/aplication/Nfc.usecases.ts index 75b4003..0200030 100644 --- a/src/aplication/Nfc.usecases.ts +++ b/src/aplication/Nfc.usecases.ts @@ -83,6 +83,7 @@ export class NfcUsecases { * Se puede forzar generar uno nuevo. */ public generateActivationLabel() { + return async (args: { card_id: string, override?: boolean, @@ -101,10 +102,14 @@ export class NfcUsecases { assert(code != undefined) const label = labelTemplate(code) - this.nfcRepository.createActivationCode({ + + const guardadoResult = await this.nfcRepository.createActivationCode({ cardId: args.card_id, code: code }) + if (guardadoResult.error != undefined) { + return guardadoResult + } return { data: { @@ -121,6 +126,14 @@ export class NfcUsecases { // Introducir en la bdd + const guardadoResult = await this.nfcRepository.createActivationCode({ + cardId: args.card_id, + code: codigo + }) + if (guardadoResult.error != undefined) { + return guardadoResult + } + // Caso base: codigo limpio generado return { data: { diff --git a/src/aplication/validators.ts b/src/aplication/validators.ts index 548cb6e..508825d 100644 --- a/src/aplication/validators.ts +++ b/src/aplication/validators.ts @@ -1,3 +1,4 @@ +import { UUID, uuidv7 } from "uuidv7"; import { BodyValidator, type Validator } from "./BodyValidator.js"; const cardIdExists: Validator<{ card_id?: string }> = { @@ -6,4 +7,10 @@ const cardIdExists: Validator<{ card_id?: string }> = { errorMsg: "El campo card_id esta undefined" } -export const baseValidator = new BodyValidator([cardIdExists]) +const cardIdIsUUIDv7: Validator<{ card_id: string }> = { + field: "card_id", + validationFunc: (body) => UUID.parse(body.card_id) != undefined, + errorMsg: "El campo card_id no es un uuidv7" +} + +export const baseValidator = new BodyValidator([cardIdExists, cardIdIsUUIDv7])