From cc9b3c04c9f327c1751ac82cd0f4b0e0cb564b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20H=C3=B6ld?= Date: Thu, 11 Apr 2019 16:46:35 +0200 Subject: [PATCH] api: Fix schema for workflowitem_update Set billingData, amount, currency and exchange rate as forbidden when the amountType of an update is 'N/A'. This fixes the error during provisioning. --- .../service/domain/workflow/workflowitem.ts | 44 +++++++++---------- .../domain/workflow/workflowitem_updated.ts | 22 +++++++--- api/src/workflowitem_update.ts | 17 +++---- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/api/src/service/domain/workflow/workflowitem.ts b/api/src/service/domain/workflow/workflowitem.ts index dc852de64..47dfa4504 100644 --- a/api/src/service/domain/workflow/workflowitem.ts +++ b/api/src/service/domain/workflow/workflowitem.ts @@ -72,47 +72,45 @@ const schema = Joi.object().keys({ displayName: Joi.string().required(), exchangeRate: Joi.string() .when("amountType", { - is: Joi.valid("disbursed", "allocated"), - then: Joi.required(), - otherwise: Joi.optional(), + is: Joi.valid("N/A"), + then: Joi.forbidden(), }) .when("status", { is: Joi.valid("closed"), then: Joi.required(), otherwise: Joi.optional(), - }) - .when("amountType", { is: Joi.valid("N/A"), then: Joi.forbidden() }), + }), billingDate: Joi.date() .iso() .when("amountType", { is: Joi.valid("N/A"), then: Joi.forbidden(), }) - .concat( - Joi.date() - .iso() - .when("status", { - is: Joi.valid("closed"), - then: Joi.required(), - otherwise: Joi.optional(), - }), - ), + .when("status", { + is: Joi.valid("closed"), + then: Joi.required(), + otherwise: Joi.optional(), + }), amount: Joi.string() .when("amountType", { - is: Joi.valid("disbursed", "allocated"), + is: Joi.valid("N/A"), + then: Joi.forbidden(), + }) + .when("status", { + is: Joi.valid("closed"), then: Joi.required(), otherwise: Joi.optional(), - }) - .when("status", { is: Joi.valid("closed"), then: Joi.required(), otherwise: Joi.optional() }) - .when("amountType", { is: Joi.valid("N/A"), then: Joi.forbidden() }), + }), currency: Joi.string() .when("amountType", { - is: Joi.valid("disbursed", "allocated"), - then: Joi.required(), - otherwise: Joi.forbidden(), + is: Joi.valid("N/A"), + then: Joi.forbidden(), }) - .when("status", { is: Joi.valid("closed"), then: Joi.required(), otherwise: Joi.optional() }) - .when("amountType", { is: Joi.valid("N/A"), then: Joi.forbidden() }), + .when("status", { + is: Joi.valid("closed"), + then: Joi.required(), + otherwise: Joi.optional(), + }), amountType: Joi.string() .valid("N/A", "disbursed", "allocated") .required(), diff --git a/api/src/service/domain/workflow/workflowitem_updated.ts b/api/src/service/domain/workflow/workflowitem_updated.ts index 47fd3e613..0c7bccc60 100644 --- a/api/src/service/domain/workflow/workflowitem_updated.ts +++ b/api/src/service/domain/workflow/workflowitem_updated.ts @@ -28,11 +28,19 @@ export interface Modification { export const modificationSchema = Joi.object({ displayName: Joi.string(), description: Joi.string().allow(""), - amount: Joi.string(), - currency: Joi.string(), + exchangeRate: Joi.string().when("amountType", { is: Joi.valid("N/A"), then: Joi.forbidden() }), + billingDate: Joi.date() + .iso() + .when("amountType", { is: Joi.valid("N/A"), then: Joi.forbidden() }), + amount: Joi.string().when("amountType", { + is: Joi.valid("N/A"), + then: Joi.forbidden(), + }), + currency: Joi.string().when("amountType", { + is: Joi.valid("N/A"), + then: Joi.forbidden(), + }), amountType: Joi.valid("N/A", "disbursed", "allocated"), - exchangeRate: Joi.string(), - billingDate: Joi.date().iso(), dueDate: Joi.date().iso(), documents: Joi.array().items(storedDocumentSchema), additionalData: AdditionalData.schema, @@ -172,8 +180,10 @@ function updateDocuments(workflowitem: Workflowitem.Workflowitem, documents?: St } else { // We already know a document with the same ID. if (existingDocument.hash !== document.hash) { - throw new VError(`cannot update document ${document.id}, ` + - `as changing existing documents is not allowed`); + throw new VError( + `cannot update document ${document.id}, ` + + `as changing existing documents is not allowed`, + ); } } }); diff --git a/api/src/workflowitem_update.ts b/api/src/workflowitem_update.ts index 9f02d7cd4..a57e477c4 100644 --- a/api/src/workflowitem_update.ts +++ b/api/src/workflowitem_update.ts @@ -7,12 +7,12 @@ import * as NotAuthenticated from "./http_errors/not_authenticated"; import { AuthenticatedRequest } from "./httpd/lib"; import { Ctx } from "./lib/ctx"; import * as Result from "./result"; -import * as AdditionalData from "./service/domain/additional_data"; import { ServiceUser } from "./service/domain/organization/service_user"; import { UploadedDocument, uploadedDocumentSchema } from "./service/domain/workflow/document"; import * as Project from "./service/domain/workflow/project"; import * as Subproject from "./service/domain/workflow/subproject"; import * as Workflowitem from "./service/domain/workflow/workflowitem"; +import * as WorkflowitemUpdated from "./service/domain/workflow/workflowitem_updated"; import * as WorkflowitemUpdate from "./service/workflowitem_update"; interface RequestBodyV1 { @@ -40,17 +40,10 @@ const requestBodyV1Schema = Joi.object({ projectId: Project.idSchema.required(), subprojectId: Subproject.idSchema.required(), workflowitemId: Workflowitem.idSchema.required(), - displayName: Joi.string(), - description: Joi.string().allow(""), - amountType: Joi.string().valid("N/A", "disbursed", "allocated"), - amount: Joi.string(), - currency: Joi.string(), - exchangeRate: Joi.string(), - billingDate: Joi.date().iso(), - dueDate: Joi.date().iso(), - documents: Joi.array().items(uploadedDocumentSchema), - additionalData: AdditionalData.schema, - }).required(), + }) + .concat(WorkflowitemUpdated.modificationSchema) + .keys({ documents: Joi.array().items(uploadedDocumentSchema) }) + .required(), }); type RequestBody = RequestBodyV1;