Skip to content

Commit

Permalink
Parse varsel API response (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
andnorda authored May 10, 2024
1 parent 30c16c2 commit f91bbf8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 31 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"js-cookie": "^3.0.5",
"postcss-modules": "latest",
"prettier": "^3.2.5",
"ts-pattern": "^5.0.5",
"zod": "^3.22.4"
"ts-pattern": "^5.0.5"
}
}
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"decorator-shared": "workspace:*",
"unleash-client": "^4.1.1",
"csp-header": "^5.2.1",
"@builder.io/partytown": "^0.8.1"
"@builder.io/partytown": "^0.8.1",
"zod": "^3.22.4"
},
"devDependencies": {
"msw": "^2.2.14",
Expand Down
20 changes: 19 additions & 1 deletion packages/server/src/notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import { SetupServerApi, setupServer } from "msw/node";
import { env } from "./env/server";
import { Varsler, getNotifications } from "./notifications";

type AnyOkUnion = {
ok: boolean;
[key: string]: unknown;
};

export function expectOK<T extends AnyOkUnion>(
result: T,
): asserts result is Extract<T, { ok: true }> {
expect(result.ok).toBe(true);
}

export function expectNotOK<T extends AnyOkUnion>(
result: T,
): asserts result is Extract<T, { ok: false }> {
expect(result.ok).toBe(false);
}

describe("notifications", () => {
let server: SetupServerApi;

Expand Down Expand Up @@ -64,7 +81,8 @@ describe("notifications", () => {
}),
});

expect(result).toEqual([
expectOK(result);
expect(result.data).toEqual([
{
id: "a",
type: "task",
Expand Down
85 changes: 65 additions & 20 deletions packages/server/src/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { env } from "./env/server";
import { z } from "zod";

type Varsel = {
eventId: string;
type: "oppgave" | "beskjed";
tidspunkt: string;
isMasked: boolean;
tekst: string | null;
link: string | null;
eksternVarslingKanaler: string[];
};
const varselSchema = z.object({
eventId: z.string(),
type: z.enum(["oppgave", "beskjed"]),
tidspunkt: z.string(),
isMasked: z.boolean(),
tekst: z.string().nullable(),
link: z.string().nullable(),
eksternVarslingKanaler: z.array(z.string()),
});

export type Varsler = {
oppgaver: Varsel[];
beskjeder: Varsel[];
};
const varslerSchema = z.object({
oppgaver: z.array(varselSchema),
beskjeder: z.array(varselSchema),
});

type Varsel = z.infer<typeof varselSchema>;

export type Varsler = z.infer<typeof varslerSchema>;

export type MaskedNotification = {
id: string;
Expand Down Expand Up @@ -54,11 +59,51 @@ const varslerToNotifications = (varsler: Varsler): Notification[] =>
),
);

export const getNotifications = async ({ request }: { request: Request }) =>
fetch(`${env.VARSEL_API_URL}/varselbjelle/varsler`, {
headers: {
cookie: request.headers.get("cookie") || "",
type Result<Data> = { ok: true; data: Data } | { ok: false; error: Error };

export const getNotifications = async ({
request,
}: {
request: Request;
}): Promise<Result<Notification[]>> => {
const fetchResult = await fetch(
`${env.VARSEL_API_URL}/varselbjelle/varsler`,
{
headers: {
cookie: request.headers.get("cookie") || "",
},
},
})
.then((res) => res.json() as Promise<Varsler>)
.then(varslerToNotifications);
);

if (!fetchResult.ok) {
return {
ok: false,
error: Error(await fetchResult.text()),
};
}

try {
const json = await fetchResult.json();

const validationResult = varslerSchema.safeParse(json);
if (!validationResult.success) {
return {
ok: false,
error: validationResult.error,
};
}

return {
ok: true,
data: varslerToNotifications(validationResult.data),
};
} catch (error) {
if (error instanceof Error) {
return {
ok: false,
error,
};
}
throw error;
}
};
19 changes: 12 additions & 7 deletions packages/server/src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { makeFrontpageUrl } from "decorator-shared/urls";
import { csrHandler } from "./csr";
import { search } from "./search";
import { getNotifications } from "./notifications";
import html from "decorator-shared/html";

type FileSystemService = {
getFile: (path: string) => Blob;
Expand Down Expand Up @@ -165,20 +166,24 @@ const requestHandler = async (

// @TODO: Tests for important urls, like logout
return match(data.context)
.with("privatperson", async () =>
UserMenuDropdown({
.with("privatperson", async () => {
const result = await getNotifications({
request,
});
if (!result.ok) {
return html`<div>Error</div>`;
}
return UserMenuDropdown({
texts: localTexts,
name: data.name,
notifications: await getNotifications({
request,
}),
notifications: result.data,
level: data.level,
logoutUrl: logoutUrl as string,
minsideUrl: clientEnv.MIN_SIDE_URL,
personopplysningerUrl:
clientEnv.PERSONOPPLYSNINGER_URL,
}),
)
});
})
.with("arbeidsgiver", () =>
ArbeidsgiverUserMenu({
texts: localTexts,
Expand Down

0 comments on commit f91bbf8

Please sign in to comment.