Skip to content

Commit 611a345

Browse files
committed
FLS-1450: Integrate Pre-Award API with form routes
- Extend existing API routes with Pre-Award API support - Update form save operations to use Pre-Award API - Add new form creation with Pre-Award API integration - Maintain backward compatibility with original routes
1 parent 100bed3 commit 611a345

File tree

4 files changed

+120
-9
lines changed

4 files changed

+120
-9
lines changed

designer/server/plugins/DesignerRouteRegister.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import {newConfig, api, app} from "../../../digital-form-builder/designer/server/plugins/routes";
1+
import {app} from "../../../digital-form-builder/designer/server/plugins/routes";
22
import {envStore, flagg} from "flagg";
33
import {putFormWithIdRouteRegister} from "./routes/PutFormWithIdRouteRegister";
4+
import {registerNewFormWithRunner} from "./routes/newConfig";
5+
import {getFormWithId, getAllPersistedConfigurations, log} from "./routes/api";
46
import config from "../config";
57
import {jwtAuthStrategyName} from "./AuthPlugin";
68

@@ -83,15 +85,15 @@ export const designerPlugin = {
8385
// @ts-ignore
8486
app.redirectOldUrlToDesigner.options.auth = jwtAuthStrategyName
8587
// @ts-ignore
86-
newConfig.registerNewFormWithRunner.options.auth = jwtAuthStrategyName
88+
registerNewFormWithRunner.options.auth = jwtAuthStrategyName
8789
// @ts-ignore
88-
api.getFormWithId.options.auth = jwtAuthStrategyName
90+
getFormWithId.options.auth = jwtAuthStrategyName
8991
// @ts-ignore
9092
putFormWithIdRouteRegister.options.auth = jwtAuthStrategyName
9193
// @ts-ignore
92-
api.getAllPersistedConfigurations.options.auth = jwtAuthStrategyName
94+
getAllPersistedConfigurations.options.auth = jwtAuthStrategyName
9395
// @ts-ignore
94-
api.log.options.auth = jwtAuthStrategyName
96+
log.options.auth = jwtAuthStrategyName
9597
}
9698

9799
server.route(startRoute);
@@ -118,6 +120,7 @@ export const designerPlugin = {
118120
store: envStore(process.env),
119121
definitions: {
120122
featureEditPageDuplicateButton: {default: false},
123+
121124
},
122125
});
123126

@@ -128,11 +131,11 @@ export const designerPlugin = {
128131
},
129132
});
130133

131-
server.route(newConfig.registerNewFormWithRunner);
132-
server.route(api.getFormWithId);
134+
server.route(registerNewFormWithRunner);
135+
server.route(getFormWithId);
133136
server.route(putFormWithIdRouteRegister);
134-
server.route(api.getAllPersistedConfigurations);
135-
server.route(api.log);
137+
server.route(getAllPersistedConfigurations);
138+
server.route(log);
136139
},
137140
},
138141
};

designer/server/plugins/routes/PutFormWithIdRouteRegister.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {ServerRoute} from "@hapi/hapi";
22
import {AdapterSchema} from "@communitiesuk/model";
33
import {publish} from "../../../../digital-form-builder/designer/server/lib/publish";
4+
import {preAwardApiClient} from "../../lib/preAwardApiClient";
5+
import config from "../../config";
46

57

68
export const putFormWithIdRouteRegister: ServerRoute = {
@@ -31,6 +33,10 @@ export const putFormWithIdRouteRegister: ServerRoute = {
3133
`${id}`,
3234
JSON.stringify(value)
3335
);
36+
// Save to Pre-Award API
37+
const formData = { name: id, form_json: value };
38+
await preAwardApiClient.createOrUpdateForm(formData);
39+
// Publish to runner for preview
3440
await publish(id, value);
3541
return h.response({ok: true}).code(204);
3642
} catch (err) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 formData = { name: id, form_json: value };
35+
await preAwardApiClient.createOrUpdateForm(formData);
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;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { newConfig as originalNewConfig } from "../../../../digital-form-builder/designer/server/plugins/routes";
2+
import { preAwardApiClient } from "../../lib/preAwardApiClient";
3+
import config from "../../config";
4+
import { ServerRoute } from "@hapi/hapi";
5+
import { HapiRequest } from "../../../../digital-form-builder/designer/server/types";
6+
import { nanoid } from "nanoid";
7+
import newFormJson from "../../../../digital-form-builder/designer/new-form.json";
8+
9+
// Extend the original registerNewFormWithRunner with Pre-Award API support
10+
export const registerNewFormWithRunner: ServerRoute = {
11+
...originalNewConfig.registerNewFormWithRunner,
12+
options: {
13+
...originalNewConfig.registerNewFormWithRunner.options,
14+
handler: async (request: HapiRequest, h) => {
15+
const { selected, name } = request.payload as any;
16+
17+
if (name && name !== "" && !name.match(/^[a-zA-Z0-9 _-]+$/)) {
18+
return h
19+
.response("Form name should not contain special characters")
20+
.type("application/json")
21+
.code(400);
22+
}
23+
24+
const newName = name === "" ? nanoid(10) : name;
25+
26+
if (selected.Key === "New") {
27+
const formData = { name: newName, form_json: newFormJson };
28+
await preAwardApiClient.createOrUpdateForm(formData);
29+
} else {
30+
const existingForm = await preAwardApiClient.getFormDraft(selected.Key);
31+
const formData = { name: newName, form_json: existingForm };
32+
await preAwardApiClient.createOrUpdateForm(formData);
33+
}
34+
35+
const response = JSON.stringify({
36+
id: `${newName}`,
37+
previewUrl: config.previewUrl,
38+
});
39+
return h.response(response).type("application/json").code(200);
40+
},
41+
},
42+
};

0 commit comments

Comments
 (0)