Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
intrn(validator): find ongoing consultation
Browse files Browse the repository at this point in the history
Co-authored-by: Kenneth Trecy Tobias <19201.tobias.kennethtrecy.c@gmail.com>
  • Loading branch information
lemredd and KennethTrecy committed Nov 27, 2022
1 parent 7df87eb commit 986a3e1
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions server/validators/manager/is_consultation_ongoing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Request } from "!/types/dependent"
import type { ValidationState, ValidationConstraints } from "!/types/validation"
import type { DeserializedConsultationDocument } from "$/types/documents/consultation"

import deserialize from "$/object/deserialize"

import Manager from "%/managers/consultation"

Expand All @@ -19,9 +22,32 @@ export default async function(
const manager = new Manager(constraints.request)

const modelID = state.value
const canStart = await manager.canStart(Number(modelID))
const foundModel = await manager.findWithID(Number(modelID))

if (foundModel.data === null) {
const error = {
"field": constraints.field,
"friendlyName": constraints.friendlyName,
"messageMaker": (
field: string,
value: string
) => {
const subject = `The consultation with an ID of "${value}"`
const predicate = "cannot be started because there are other started ones."

return `${subject} ${predicate}`
}
}

throw error
}

const deserializedFoundModel = deserialize(foundModel) as DeserializedConsultationDocument
const hasNotFinished = deserializedFoundModel.data.finishedAt === null
const isNotCanceled = deserializedFoundModel.data.deletedAt === null
const hasStarted = deserializedFoundModel.data.startedAt !== null

if (canStart) return state
if (hasStarted && hasNotFinished && isNotCanceled) return state

const error = {
"field": constraints.field,
Expand All @@ -31,7 +57,7 @@ export default async function(
value: string
) => {
const subject = `The consultation with an ID of "${value}"`
const predicate = "cannot be started because there are other started ones."
const predicate = "should be ongoing."

return `${subject} ${predicate}`
}
Expand Down

0 comments on commit 986a3e1

Please sign in to comment.