Docs y mejora de deteccion de id de entrada
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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