Skip to content

Commit

Permalink
feat(wip): Initial setup of a generic application type
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Jul 23, 2024
1 parent ef0937d commit be6815f
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 2 deletions.
159 changes: 159 additions & 0 deletions schemas/discriminated.json
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 2 additions & 2 deletions scripts/build-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -168,6 +169,10 @@ type GenericApplicationType<TKey extends ApplicationTypeKeys> = {
description: (typeof ApplicationTypes)[TKey];
};

type ExtractPrimaryKeys<T> = T extends `${infer K}.${string}` ? K : T;

export type PrimaryApplicationTypes = ExtractPrimaryKeys<ApplicationTypeKeys>;

type ApplicationTypeMap = {
[K in ApplicationTypeKeys]: GenericApplicationType<K>;
};
Expand Down
30 changes: 30 additions & 0 deletions types/schemas/discriminated/User.ts
Original file line number Diff line number Diff line change
@@ -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 PrimaryApplicationTypes> =
T extends keyof UserVariants
? UserVariants[T]
: // Default value for non-specific application types
UserBase;
33 changes: 33 additions & 0 deletions types/schemas/discriminated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {PrimaryApplicationTypes} from '../digitalPlanningApplication/enums/ApplicationTypes';
import {User} from './User';

interface GenericApplication<T extends PrimaryApplicationTypes> {
primaryApplicationType: T;
data: {
user: User<T>;
};
}

/**
* @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;

0 comments on commit be6815f

Please sign in to comment.