85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import { companyFromIccid } from "#domain/companies.js";
|
|
import { BodyValidator, Validator } from "sim-shared/aplication/BodyValidator.js";
|
|
|
|
const offers = new Map([
|
|
["mensual", "SAVEFAMILY1"],
|
|
["anual", "SAVEFAMILY2"],
|
|
["SAVEFAMILY1", "SAVEFAMILY1"],
|
|
["SAVEFAMILY2", "SAVEFAMILY2"],
|
|
])
|
|
|
|
const iccidLongitudValidator = <Validator<{ iccid: string }>>{
|
|
field: "iccid",
|
|
errorMsg: "La longitud del iccid es incorrecta debera ser de 19 caracteres",
|
|
validationFunc: (a: { iccid: string }) => a.iccid.length == 19,
|
|
}
|
|
|
|
const iccidRequired = <Validator<{ iccid: string }>>{
|
|
field: "iccid",
|
|
errorMsg: "El iccid debe estara definido",
|
|
validationFunc: (a: { iccid: string }) => a.iccid != undefined,
|
|
}
|
|
|
|
const iccidWithValidCompany = <Validator<{ iccid: string }>>{
|
|
field: "iccid",
|
|
errorMsg: "El iccid no corresponde a una compañia registrada",
|
|
validationFunc: (a: { iccid: string }) => companyFromIccid(a.iccid) != undefined,
|
|
}
|
|
|
|
const offerExists = <Validator<{ offer: string }>>{
|
|
field: "offer",
|
|
errorMsg: "La oferta introducida no es valida",
|
|
validationFunc: (a: { offer: string }) => offers.has(a.offer),
|
|
}
|
|
|
|
const isUuidv7 = <Validator<{ uuid?: string }>>{
|
|
field: "uuid",
|
|
errorMsg: "El uuid no es un uuidv7 valido",
|
|
validationFunc: (a) => a.uuid != undefined && a.uuid.length < 36
|
|
}
|
|
|
|
const definedId = <Validator<{ id?: number }>>{
|
|
field: "id",
|
|
errorMsg: "El id no se ha definido",
|
|
validationFunc: (e) => e.id != undefined
|
|
}
|
|
|
|
const isIntegerId = <Validator<{ id?: number }>>{
|
|
field: "id",
|
|
errorMsg: "El id no se ha definido",
|
|
validationFunc: (e) => Number.isInteger(e.id)
|
|
}
|
|
|
|
const validNumericId = <Validator<{ id?: number }>>{
|
|
field: "id",
|
|
errorMsg: "El id introducido no es un numero >= 0",
|
|
validationFunc: (e) => e.id! >= 0
|
|
}
|
|
|
|
export const activationValidator = new BodyValidator<{ iccid: string, offer: string }>(
|
|
[
|
|
iccidRequired,
|
|
iccidLongitudValidator,
|
|
iccidWithValidCompany,
|
|
offerExists,
|
|
]
|
|
)
|
|
|
|
export const iccidValidator = new BodyValidator<{ iccid: string }>(
|
|
[
|
|
iccidRequired,
|
|
iccidLongitudValidator,
|
|
iccidWithValidCompany,
|
|
]
|
|
)
|
|
|
|
export const uuidValidator = new BodyValidator<{ uuid?: string }>([
|
|
isUuidv7
|
|
])
|
|
|
|
export const idValidator = new BodyValidator<{ id?: number }>([
|
|
definedId,
|
|
isIntegerId,
|
|
validNumericId
|
|
])
|