Skip to content

Commit

Permalink
Merge pull request #212 from openkfw/fix-workflowitem-update
Browse files Browse the repository at this point in the history
api: Fix schema for workflowitem_update
  • Loading branch information
kevinbader authored Apr 11, 2019
2 parents 24076ad + cc9b3c0 commit ae192ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
44 changes: 21 additions & 23 deletions api/src/service/domain/workflow/workflowitem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
22 changes: 16 additions & 6 deletions api/src/service/domain/workflow/workflowitem_updated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`,
);
}
}
});
Expand Down
17 changes: 5 additions & 12 deletions api/src/workflowitem_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ae192ea

Please sign in to comment.