Skip to content

Commit

Permalink
Bedre støtte for userKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesperpaulsen committed Feb 7, 2024
1 parent d043466 commit 67f8f77
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
31 changes: 31 additions & 0 deletions v3/packages/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { SuccessResponse } from './responses/SuccessResponse';
import { mockMe } from '../internarbeidsflate-decorator-v3/src/__mocks__/mock-handlers'
import { NotFoundResponse } from './responses/NotFoundResponse';
import { BunServerWebsocket } from './types';

type Metadata = { ident: string };

const serve = () => {
type Context = { aktivEnhet: string | undefined; aktivBruker: string | undefined };
const context: Context = { aktivEnhet: '0118', aktivBruker: '10108000398' };
const clients: Record<string, BunServerWebsocket> = {};
const codeToFnr: Record<string, string> = {}

const app = new CustomServer();

Expand Down Expand Up @@ -79,6 +81,35 @@ const serve = () => {
return new SuccessResponse({...context});
});

app.post('/modiacontextholder/api/fnr-code/retrieve', async (request) => {
if (!request.body) {
return new BadRequestResponse('No body provided');
}

const { code }: { code: string } = await Bun.readableStreamToJSON(request.body);

const fnr = codeToFnr[code]

if (!fnr) {
return new NotFoundResponse()
}

return new SuccessResponse({ fnr, code });
});

app.post('/modiacontextholder/api/fnr-code/generate', async (request) => {
if (!request.body) {
return new BadRequestResponse('No body provided');
}

const { fnr }: { fnr: string } = await Bun.readableStreamToJSON(request.body);

const code = crypto.randomUUID()

codeToFnr[code] = fnr

return new SuccessResponse({ fnr, code });
});
app.addWebSocketHandler<Metadata>('/ws/:ident', {
open: (ws, params) => {
const ident = params.ident;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ interface AktivEnhetResponse {
aktivEnhet: string | undefined;
}

interface FnrCodeResponse {
fnr: string;
code: string;
}

export class ContextHolderAPI extends ApiClient {
constructor(url: string, token?: string) {
super(url, token);
}

readonly exhangeUserKeyForFnr = (
userKey: string,
): Promise<FetchResponse<string>> => {
return this.post<string>({
path: `/bytt-bruker-nokkel`,
body: { userKey },
code: string,
): Promise<FetchResponse<FnrCodeResponse>> => {
return this.post<FnrCodeResponse>({
path: `/fnr-code/retrieve`,
body: { code },
});
};

Expand All @@ -40,22 +44,22 @@ export class ContextHolderAPI extends ApiClient {
};

readonly getEnhet = (enhetId: string): Promise<FetchResponse<Enhet>> => {
return this.get<Enhet>({ path: `/enhet/${enhetId}` })
return this.get<Enhet>({ path: `/enhet/${enhetId}` });
};

readonly getVeilederDetails = (): Promise<FetchResponse<Veileder>> => {
return this.get<Veileder>({ path: `/decorator` })
return this.get<Veileder>({ path: `/decorator` });
};

readonly getVeiledersActiveFnr = (): Promise<
FetchResponse<AktivBrukerResponse>
> => {
return this.get<AktivBrukerResponse>({ path: `/v2/aktivbruker` })
return this.get<AktivBrukerResponse>({ path: `/v2/aktivbruker` });
};

readonly getVeiledersActiveEnhet = (): Promise<
FetchResponse<AktivEnhetResponse>
> => {
return this.get<AktivEnhetResponse>({ path: `/v2/aktivenhet` })
return this.get<AktivEnhetResponse>({ path: `/v2/aktivenhet` });
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export class FnrValueManager extends ContextValueManager {
if (fnrKey) {
await this.changeFnrLocallyAndExternally(fnrKey);
}

};

#registerPropsHandler = () => {
Expand All @@ -58,10 +57,8 @@ export class FnrValueManager extends ContextValueManager {
);
};

readonly updateFnrLocallyToMatchContextHolder = async (
) => {
const activeUser =
await this.contextHolderApi.getVeiledersActiveFnr();
readonly updateFnrLocallyToMatchContextHolder = async () => {
const activeUser = await this.contextHolderApi.getVeiledersActiveFnr();
if (activeUser.error) {
this.#errorMessageManager.addErrorMessage(
PredefiniertFeilmeldinger.HENT_BRUKER_CONTEXT_FEILET,
Expand Down Expand Up @@ -132,16 +129,18 @@ export class FnrValueManager extends ContextValueManager {
): Promise<string | undefined> => {
let fnr = initialFnr;

if (userKey?.length) {
if (userKey?.length && !erGyldigFodselsnummer(userKey)) {
const res = await this.contextHolderApi.exhangeUserKeyForFnr(userKey);
if (res.error) {
if (res.error || !res.data) {
this.#errorMessageManager.addErrorMessage({
code: ErrorMessageCode.BYTT_BRUKERNØKKEL_FEILET,
message: 'Klarte ikke å bytte inn brukernøkkel til fnr.',
});
} else {
fnr = res.data;
fnr = res.data.fnr;
}
} else if (userKey?.length && erGyldigFodselsnummer(userKey)) {
fnr = userKey;
}
return fnr;
};
Expand Down

0 comments on commit 67f8f77

Please sign in to comment.