move CRUD operations to helpers

This commit is contained in:
Thomas Ruoff
2021-06-07 23:32:54 +02:00
parent 865bbb20fa
commit 92477e5325
9 changed files with 102 additions and 93 deletions

View File

@@ -0,0 +1,39 @@
import { VALIDATION_ERRORS } from '../db/enums'
interface ValidationErrors {
[key: string]: { properties: { message: string }; kind: string }
}
function getDefinedValidationMessage(kind: string) {
switch (kind) {
case VALIDATION_ERRORS.AT_LEAST_ONE_DAY_BOOKED:
return 'Mindestens ein angefragter Tag ist nicht mehr verfügbar'
default:
return null
}
}
export class ValidationError extends Error {
private errors: ValidationErrors
constructor(errors: ValidationErrors) {
super()
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ValidationError)
}
this.name = this.constructor.name
this.errors = errors
}
get message() {
return Object.entries<{ properties: { message: string }; kind: string }>(
this.errors
)
.map(([_key, value]) => {
const validationMessage = getDefinedValidationMessage(value.kind)
return validationMessage || `${value?.properties?.message}`
})
.join(',')
}
}