Docs y mejora de deteccion de id de entrada
This commit is contained in:
@@ -20,14 +20,25 @@ export class BodyValidator<T extends Object> {
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
@@ -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 })
|
||||
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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])
|
||||
|
||||
Reference in New Issue
Block a user