Skip to content

Commit

Permalink
feat(postal-code): add postal code endpoint (#530)
Browse files Browse the repository at this point in the history
* feat(postal-code): add postal code endpoint

* refactor(postal-code-lookup): use http handler and rename stuff

* refactor(postal-code-lookup): adjust endpoint name
  • Loading branch information
AdrianAndersen authored Jul 5, 2024
1 parent 0808666 commit 67115a5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/collections/delivery/delivery.collection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deliverySchema } from "./delivery.schema";
import { DeliveryPatchHook } from "./hooks/delivery.patch.hook";
import { DeliveryPostHook } from "./hooks/delivery.post.hook";
import { PostalCodeLookupOperation } from "./operations/postal-code-lookup.operation";
import { BlCollection, BlCollectionName, BlEndpoint } from "../bl-collection";

export class DeliveryCollection implements BlCollection {
Expand All @@ -13,6 +14,12 @@ export class DeliveryCollection implements BlCollection {
restriction: {
permissions: ["customer", "employee", "manager", "admin", "super"],
},
operations: [
{
name: "postal-code-lookup",
operation: new PostalCodeLookupOperation(),
},
],
},
{
method: "getAll",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { BlapiResponse, BlError } from "@boklisten/bl-model";

import { isNullish } from "../../../helper/typescript-helpers";
import { HttpHandler } from "../../../http/http.handler";
import { Operation } from "../../../operation/operation";
import { BlApiRequest } from "../../../request/bl-api-request";

type SimplifiedBringPostalCodeResponse = {
postal_codes?:
| [
{
city: string;
},
]
| [];
validation_errors?: [
{
code: string;
description: string;
},
];
};

export interface PostalCodeLookupSpec {
postalCode: string;
}

export function verifyPostalCodeLookupSpec(
postalCodeSpec: unknown,
): postalCodeSpec is PostalCodeLookupSpec {
const m = postalCodeSpec as Record<string, unknown> | null | undefined;
return !!m && typeof m["postalCode"] === "string";
}

export class PostalCodeLookupOperation implements Operation {
private httpHandler: HttpHandler;

constructor(httpHandler?: HttpHandler) {
this.httpHandler = httpHandler ? httpHandler : new HttpHandler();
}

async run(blApiRequest: BlApiRequest): Promise<BlapiResponse> {
const postalCodeLookupSpec = blApiRequest.data;
if (!verifyPostalCodeLookupSpec(postalCodeLookupSpec)) {
throw new BlError(`Malformed PostalCodeSpec`).code(701);
}
const bringAuthHeaders = {
"X-MyBring-API-Key": process.env["BRING_API_KEY"],
"X-MyBring-API-Uid": process.env["BRING_API_ID"],
};
try {
const response = (await this.httpHandler.getWithQuery(
`https://api.bring.com/address/api/NO/postal-codes/${postalCodeLookupSpec.postalCode}`,
"",
bringAuthHeaders,
)) as SimplifiedBringPostalCodeResponse;

if (
response.validation_errors ||
isNullish(response.postal_codes) ||
response.postal_codes.length === 0
) {
return new BlapiResponse([{ postalCity: null }]);
}

return new BlapiResponse([{ postalCity: response.postal_codes[0].city }]);
} catch (error) {
throw new BlError("failed to lookup postal code").data(error).code(200);
}
}
}

0 comments on commit 67115a5

Please sign in to comment.