From be6815faa8164ac7059cb9bc4b43ddb71e90b034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 23 Jul 2024 17:16:51 +0100 Subject: [PATCH] feat(wip): Initial setup of a generic application type --- schemas/discriminated.json | 159 ++++++++++++++++++ scripts/build-schema.sh | 4 +- .../enums/ApplicationTypes.ts | 5 + types/schemas/discriminated/User.ts | 30 ++++ types/schemas/discriminated/index.ts | 33 ++++ 5 files changed, 229 insertions(+), 2 deletions(-) create mode 100644 schemas/discriminated.json create mode 100644 types/schemas/discriminated/User.ts create mode 100644 types/schemas/discriminated/index.ts diff --git a/schemas/discriminated.json b/schemas/discriminated.json new file mode 100644 index 00000000..29649659 --- /dev/null +++ b/schemas/discriminated.json @@ -0,0 +1,159 @@ +{ + "$id": "@next", + "$schema": "http://json-schema.org/draft-07/schema#", + "anyOf": [ + { + "$ref": "#/definitions/PlanningPermissionApplication" + }, + { + "$ref": "#/definitions/PriorApprovalApplication" + }, + { + "$ref": "#/definitions/WorksToTreesApplication" + } + ], + "definitions": { + "PPUser": { + "additionalProperties": false, + "properties": { + "ppSpecificProperty": { + "type": "boolean" + }, + "role": { + "enum": [ + "applicant", + "agent", + "proxy" + ], + "type": "string" + } + }, + "required": [ + "ppSpecificProperty", + "role" + ], + "type": "object" + }, + "PlanningPermissionApplication": { + "additionalProperties": false, + "description": "Application for a \"Planning Permission\" project", + "properties": { + "data": { + "additionalProperties": false, + "properties": { + "user": { + "$ref": "#/definitions/PPUser" + } + }, + "required": [ + "user" + ], + "type": "object" + }, + "primaryApplicationType": { + "const": "pp", + "type": "string" + } + }, + "required": [ + "primaryApplicationType", + "data" + ], + "type": "object" + }, + "PriorApprovalApplication": { + "additionalProperties": false, + "description": "Application for a \"Prior Approval\" project", + "properties": { + "data": { + "additionalProperties": false, + "properties": { + "user": { + "$ref": "#/definitions/UserBase" + } + }, + "required": [ + "user" + ], + "type": "object" + }, + "primaryApplicationType": { + "const": "pa", + "type": "string" + } + }, + "required": [ + "primaryApplicationType", + "data" + ], + "type": "object" + }, + "UserBase": { + "additionalProperties": false, + "properties": { + "role": { + "enum": [ + "applicant", + "agent", + "proxy" + ], + "type": "string" + } + }, + "required": [ + "role" + ], + "type": "object" + }, + "WTTUser": { + "additionalProperties": false, + "properties": { + "role": { + "enum": [ + "applicant", + "agent", + "proxy" + ], + "type": "string" + }, + "wttSpecificProperty": { + "type": "boolean" + } + }, + "required": [ + "role", + "wttSpecificProperty" + ], + "type": "object" + }, + "WorksToTreesApplication": { + "additionalProperties": false, + "description": "Application for a \"Works to trees\" project", + "properties": { + "data": { + "additionalProperties": false, + "properties": { + "user": { + "$ref": "#/definitions/WTTUser" + } + }, + "required": [ + "user" + ], + "type": "object" + }, + "primaryApplicationType": { + "const": "wtt", + "type": "string" + } + }, + "required": [ + "primaryApplicationType", + "data" + ], + "type": "object" + } + }, + "description": "The root specification for a planning application in England generated by a digital planning service", + "title": "App" +} \ No newline at end of file diff --git a/scripts/build-schema.sh b/scripts/build-schema.sh index 7eee9132..0b7dd37a 100755 --- a/scripts/build-schema.sh +++ b/scripts/build-schema.sh @@ -7,8 +7,8 @@ version="${VERSION:-@next}" echo "Version set to $version" -dirs=("digitalPlanningApplication") -types=("DigitalPlanningApplication") +dirs=("digitalPlanningApplication" "discriminated") +types=("DigitalPlanningApplication" "App") for i in "${!dirs[@]}"; do dir=${dirs[$i]} diff --git a/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts b/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts index e57fcd6d..1e177531 100644 --- a/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts +++ b/types/schemas/digitalPlanningApplication/enums/ApplicationTypes.ts @@ -13,6 +13,7 @@ export const ApplicationTypes = { 'approval.reservedMatters': 'Approval of reserved matters', complianceConfirmation: 'Written confirmation of compliance with a planning condition', + // TODO: Sort out typo here! environmnentalImpact: 'Environmental Impact Decision', 'environmentalImpact.scoping': 'Environmental Impact Decision - Scoping', 'environmentalImpact.screening': 'Environmental Impact Decision - Screening', @@ -168,6 +169,10 @@ type GenericApplicationType = { description: (typeof ApplicationTypes)[TKey]; }; +type ExtractPrimaryKeys = T extends `${infer K}.${string}` ? K : T; + +export type PrimaryApplicationTypes = ExtractPrimaryKeys; + type ApplicationTypeMap = { [K in ApplicationTypeKeys]: GenericApplicationType; }; diff --git a/types/schemas/discriminated/User.ts b/types/schemas/discriminated/User.ts new file mode 100644 index 00000000..09afce99 --- /dev/null +++ b/types/schemas/discriminated/User.ts @@ -0,0 +1,30 @@ +import {PrimaryApplicationTypes} from '../digitalPlanningApplication/enums/ApplicationTypes'; + +export interface UserBase { + role: 'applicant' | 'agent' | 'proxy'; +} + +export interface WTTUser extends UserBase { + wttSpecificProperty: boolean; +} + +export interface PPUser extends UserBase { + ppSpecificProperty: boolean; +} + +/** + * @internal + */ +interface UserVariants { + wtt: WTTUser; + pp: PPUser; +} + +/** + * @internal + */ +export type User = + T extends keyof UserVariants + ? UserVariants[T] + : // Default value for non-specific application types + UserBase; diff --git a/types/schemas/discriminated/index.ts b/types/schemas/discriminated/index.ts new file mode 100644 index 00000000..580f77b9 --- /dev/null +++ b/types/schemas/discriminated/index.ts @@ -0,0 +1,33 @@ +import {PrimaryApplicationTypes} from '../digitalPlanningApplication/enums/ApplicationTypes'; +import {User} from './User'; + +interface GenericApplication { + primaryApplicationType: T; + data: { + user: User; + }; +} + +/** + * @description Application for a "Planning Permission" project + */ +export type PlanningPermissionApplication = GenericApplication<'pp'>; + +/** + * @description Application for a "Prior Approval" project + */ +export type PriorApprovalApplication = GenericApplication<'pa'>; + +/** + * @description Application for a "Works to trees" project + */ +export type WorksToTreesApplication = GenericApplication<'wtt'>; + +/** + * @title App + * @description The root specification for a planning application in England generated by a digital planning service + */ +export type App = + | PlanningPermissionApplication + | PriorApprovalApplication + | WorksToTreesApplication;