Skip to content

Commit

Permalink
chore: update formatRawProjectTypes to reference schema instead of …
Browse files Browse the repository at this point in the history
…static database table (#445)
  • Loading branch information
jessicamcinchak authored Jul 12, 2024
1 parent 7666a78 commit eb3b635
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 85 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@formatjs/intl-listformat": "^7.5.7",
"@mui/material": "^5.15.2",
"@types/geojson": "^7946.0.14",
"ajv": "^8.16.0",
"ajv-formats": "^2.1.1",
"cheerio": "1.0.0-rc.12",
Expand All @@ -69,6 +69,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@types/geojson": "^7946.0.14",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.6",
"@types/node": "^18.16.19",
Expand Down
36 changes: 32 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions src/requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { FlowClient } from "./flow";
import { Auth, getGraphQLClient } from "./graphql";
import { PaymentRequestClient } from "./payment-request";
import { formatRawProjectTypes } from "./project-types";
import { SessionClient } from "./session";
import { TeamClient } from "./team";
import { UserClient } from "./user";
Expand Down Expand Up @@ -68,15 +67,4 @@ export class CoreDomainClient {
): Promise<string[]> {
return getDocumentTemplateNamesForSession(this.client, sessionId);
}

/**
* Convert and formats raw project types as a formatted list
* @example
* const result = formatRawProjectTypes(["swimmingPool.addition", "extension.rear", "window.new"])
* console.log(result)
* // Logs "Addition of a swimming pool, rear extension, and new window installation"
*/
async formatRawProjectTypes(rawProjectTypes: string[] = []): Promise<string> {
return formatRawProjectTypes(this.client, rawProjectTypes);
}
}
17 changes: 0 additions & 17 deletions src/requests/project-types.test.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/requests/project-types.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./digitalPlanningSchema";
export * from "./encryption";
export * from "./govPayMetadata";
export * from "./projectTypes";
47 changes: 47 additions & 0 deletions src/utils/projectTypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
formatHumanReadableProjectTypes,
formatRawProjectTypes,
} from "./projectTypes";

describe("formatRawProjectTypes()", () => {
it("correctly displays the schema descriptions for a list of valid raw project types", () => {
const result = formatRawProjectTypes([
"alter.boundary.add.gate",
"alter.equipment.charging",
"extend.outbuilding.swimmingPool",
]);
expect(result).toEqual(
"Add a new gate, install a car charging point, and add an outbuilding - swimming pool",
);
});

it("returns 'Unknown' if none of the raw project types are supported by the schema", () => {
const result = formatRawProjectTypes(["alter.typo", "new.typo"]);
expect(result).toEqual("Unknown");
});

it("skips invalid raw project types and correctly shows descriptions of valid ones", () => {
const result = formatRawProjectTypes([
"alter.typo",
"alter.boundary.add.gate",
"alter.equipment.charging",
]);
expect(result).toEqual("Add a new gate and install a car charging point");
});
});

describe("formatHumanReadableProjectTypes()", () => {
it("returns a correctly formatted string", () => {
const result = formatHumanReadableProjectTypes([
"first",
"second",
"third",
]);
expect(result).toEqual("First, second, and third");
});

it("returns an empty string if no raw project types are provided", () => {
const result = formatHumanReadableProjectTypes([]);
expect(result).toEqual("");
});
});
31 changes: 31 additions & 0 deletions src/utils/projectTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "@formatjs/intl-listformat/locale-data/en";
import "@formatjs/intl-listformat/polyfill";

import { getValidSchemaDictionary } from "./digitalPlanningSchema";

export function formatRawProjectTypes(rawProjectTypes: string[]): string {
const schemaProjectTypes = getValidSchemaDictionary("ProjectType");
if (schemaProjectTypes) {
const matchingDescriptions = Object.entries(schemaProjectTypes)
.filter(([key, _description]) => rawProjectTypes.includes(key))
.map(([_key, description]) => description.toLocaleLowerCase());
if (matchingDescriptions.length === 0) {
return "Unknown";
}
return formatHumanReadableProjectTypes(matchingDescriptions);
} else {
return "Unknown";
}
}

export function formatHumanReadableProjectTypes(
projectTypes: string[],
): string {
// Join in readable format - en-US ensures we use Oxford commas
const formatter = new Intl.ListFormat("en-US", { type: "conjunction" });
const joinedList = formatter.format(projectTypes);
// Convert first character to uppercase
const formattedProjectTypes =
joinedList.charAt(0).toUpperCase() + joinedList.slice(1);
return formattedProjectTypes;
}

0 comments on commit eb3b635

Please sign in to comment.