|
| 1 | +import { api as originalApi } from "../../../../digital-form-builder/designer/server/plugins/routes"; |
| 2 | +import { preAwardApiClient } from "../../lib/preAwardApiClient"; |
| 3 | +import config from "../../config"; |
| 4 | +import { ServerRoute, ResponseObject } from "@hapi/hapi"; |
| 5 | + |
| 6 | +// Extend the original getFormWithId with Pre-Award API support |
| 7 | +export const getFormWithId: ServerRoute = { |
| 8 | + ...originalApi.getFormWithId, |
| 9 | + options: { |
| 10 | + ...originalApi.getFormWithId.options || {}, |
| 11 | + handler: async (request, h) => { |
| 12 | + const { id } = request.params; |
| 13 | + const formJson = await preAwardApiClient.getFormDraft(id); |
| 14 | + return h.response(formJson).type("application/json"); |
| 15 | + }, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +// Extend the original putFormWithId with Pre-Award API support |
| 20 | +export const putFormWithId: ServerRoute = { |
| 21 | + ...originalApi.putFormWithId, |
| 22 | + options: { |
| 23 | + ...originalApi.putFormWithId.options || {}, |
| 24 | + handler: async (request, h) => { |
| 25 | + const { id } = request.params; |
| 26 | + const { Schema } = await import("../../../../digital-form-builder/model/src"); |
| 27 | + const { value, error } = Schema.validate(request.payload, { |
| 28 | + abortEarly: false, |
| 29 | + }); |
| 30 | + |
| 31 | + if (error) { |
| 32 | + throw new Error("Schema validation failed, reason: " + error.message); |
| 33 | + } |
| 34 | + const formWithName = { ...value, name: id}; |
| 35 | + await preAwardApiClient.createOrUpdateForm(id, formWithName); |
| 36 | + |
| 37 | + return h.response({ ok: true }).code(204); |
| 38 | + }, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +// Extend the original getAllPersistedConfigurations with Pre-Award API support |
| 43 | +export const getAllPersistedConfigurations: ServerRoute = { |
| 44 | + ...originalApi.getAllPersistedConfigurations, |
| 45 | + options: { |
| 46 | + ...originalApi.getAllPersistedConfigurations.options || {}, |
| 47 | + handler: async (request, h): Promise<ResponseObject | undefined> => { |
| 48 | + const forms = await preAwardApiClient.getAllForms(); |
| 49 | + const response = forms.map(form => ({ |
| 50 | + Key: form.name, |
| 51 | + DisplayName: form.name, |
| 52 | + LastModified: form.updated_at |
| 53 | + })); |
| 54 | + return h.response(response).type("application/json"); |
| 55 | + }, |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +// Use original log route as-is |
| 60 | +export const log = originalApi.log; |
0 commit comments