Skip to content

Commit

Permalink
småendringer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesperpaulsen committed Jan 24, 2024
1 parent 83f6f8e commit 6d3b54f
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 409 deletions.
Binary file modified v3/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions v3/packages/dev-server/CustomServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class CustomServer {
constructor() {
this.#serverOptions = {
development: true,
port: 4000,
fetch: this.#fetch,
websocket: {
open: this.#openWS,
Expand Down
70 changes: 23 additions & 47 deletions v3/packages/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@ import { CustomServer } from './CustomServer';
import { BadRequestResponse } from './responses/BadRequestResponse';
import { InternalServerErrorResponse } from './responses/InternalServerErrorResponse';
import { SuccessResponse } from './responses/SuccessResponse';
import { Enhet, Veileder } from '../internarbeidsflate-decorator-v3/src/index';
import { mockMe } from '../internarbeidsflate-decorator-v3/src/__mocks__/mock-handlers'
import { NotFoundResponse } from './responses/NotFoundResponse';
import { BunServerWebsocket } from './types';
type Metadata = { ident: string };

const enheter: Enhet[] = [
{
enhetId: 'IT29000',
navn: 'IT',
},
{
enhetId: 'FAKE',
navn: 'FAKE AVDELING',
},
];

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

const app = new CustomServer();
Expand All @@ -39,44 +30,24 @@ const serve = () => {
return new SuccessResponse();
});

app.get('/modiacontextholder/:ident/details', (request) => {
if (!request.haveParam('ident')) {
return new BadRequestResponse('Missing ident');
}

const response: Veileder = {
navn: 'Test Behandler',
ident: request.params['ident'],
enheter,
etternavn: 'Behandler',
fornavn: 'Test',
};

return new SuccessResponse(response);
app.get('/modiacontextholder/api/context/decorator', () => {
return new SuccessResponse(mockMe);
});

app.get('/modiacontextholder/:ident/aktiv-bruker', (request) => {
if (!request.haveParam('ident')) {
return new BadRequestResponse('Missing ident');
}

return new SuccessResponse('10108000398');
app.get('/modiacontextholder/api/context/v2/aktivbruker', () => {
return new SuccessResponse({aktivBruker: context.aktivBruker });
});

app.get('/modiacontextholder/:ident/aktiv-enhet', (request) => {
if (!request.haveParam('ident')) {
return new BadRequestResponse('Missing ident');
}

return new SuccessResponse(enheter[0].enhetId);
app.get('/modiacontextholder/api/context/v2/aktivenhet', () => {
return new SuccessResponse({aktivEnhet: context.aktivEnhet });
});

app.get('/modiacontextholder/enhet/:enhetId', (request) => {
app.get('/modiacontextholder/api/context/enhet/:enhetId', (request) => {
if (!request.haveParam('enhetId')) {
return new BadRequestResponse('Missing enhetId');
}

const response = enheter.find(
const response = mockMe.enheter.find(
(enhet) => enhet.enhetId === request.params.enhetId,
);

Expand All @@ -87,21 +58,26 @@ const serve = () => {
return new SuccessResponse(response);
});

app.post('/modiacontextholder', async (request) => {
app.post('/modiacontextholder/api/context', async (request) => {
console.log('HALLO')
if (!request.body) {
return new BadRequestResponse('No body provided');
}

const {
fnr,
enhet,
}: { fnr?: string | undefined; enhet?: string | undefined } =
eventType,
verdi,
}: { eventType: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER'; verdi: string } =
await Bun.readableStreamToJSON(request.body);

if (enhet) broadCastToClients({ type: 'NY_AKTIV_ENHET', payload: enhet });
if (fnr) broadCastToClients({ type: 'NY_AKTIV_BRUKER', payload: fnr });
if (eventType === 'NY_AKTIV_BRUKER') {
context.aktivBruker = verdi
} else if (eventType === 'NY_AKTIV_ENHET') {
context.aktivEnhet = verdi
}
broadCastToClients({ eventType, verdi });

return new SuccessResponse({ fnr, enhet });
return new SuccessResponse({...context});
});

app.addWebSocketHandler<Metadata>('/ws/:ident', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,134 +1,156 @@
import { http, HttpHandler, HttpResponse } from 'msw'
import { http, HttpHandler, HttpResponse } from 'msw';
import WS from 'vitest-websocket-mock';
import { FailureConfig } from './mock-error-config';
import { Veileder } from '../types/Veileder';
export const urlPrefix = 'http://localhost:8080';
export const urlPrefix = 'http://localhost:4000';

export const mockMe: Veileder = {
ident: 'Z999999',
navn: 'Fornavn Ettersen',
fornavn: 'Fornavn',
etternavn: 'Ettersen',
enheter: [
{ enhetId: '0219', navn: 'NAV Bærum' },
{ enhetId: '0118', navn: 'NAV Aremark' },
{ enhetId: '0604', navn: 'NAV Kongsberg' },
{ enhetId: '0602', navn: 'NAV Drammer' }
]
ident: 'Z999999',
navn: 'Fornavn Ettersen',
fornavn: 'Fornavn',
etternavn: 'Ettersen',
enheter: [
{ enhetId: '0219', navn: 'NAV Bærum' },
{ enhetId: '0118', navn: 'NAV Aremark' },
{ enhetId: '0604', navn: 'NAV Kongsberg' },
{ enhetId: '0602', navn: 'NAV Drammer' },
],
};


const getUrl = (path: string) => `${urlPrefix}/modiacontextholder/api/context${path}`
const getUrl = (path: string) =>
`${urlPrefix}/modiacontextholder/api/context${path}`;

const getErrorResponse = (status = 500) => {
return new HttpResponse(null, { status })
}
return new HttpResponse(null, { status });
};

const getSuccessResponse = ({ status, body }: { status?: number | null, body?: unknown } = { status: 200, body: null }) => {
return new HttpResponse(JSON.stringify(body ?? {}), { status: status ?? 200 })
}
const getSuccessResponse = (
{ status, body }: { status?: number | null; body?: unknown } = {
status: 200,
body: null,
},
) => {
return new HttpResponse(JSON.stringify(body ?? {}), {
status: status ?? 200,
});
};

type Context = { aktivEnhet: string | null; aktivBruker: string | null };
const context: Context = { aktivEnhet: '0118', aktivBruker: '10108000398' };
let context: Context = { aktivEnhet: '0118', aktivBruker: '10108000398' };

export const updateMockContext = (key: keyof Context, value: string | null) => context[key] = value
export const updateMockContext = (newContext: Partial<Context>) => {
console.log('updating mock', newContext)
context = {
...context,
...newContext,
}
};

const controlSignal = (data: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER') => {
return JSON.stringify({ type: 'control', data });
}
return JSON.stringify({ type: 'control', data });
};

export const getHandlers = (ws: WS, errorConfig: FailureConfig): HttpHandler[] => {
ws.on('message', console.log)
return [
http.post(getUrl(''), async ({ request }) => {
const { eventType, verdi } = await request.json() as { eventType: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER', verdi: string }
if (eventType === 'NY_AKTIV_ENHET') {
if (errorConfig.contextholder.updateEnhet) {
return getErrorResponse()
}
context.aktivEnhet = verdi
ws.send(controlSignal('NY_AKTIV_ENHET'))
return getSuccessResponse()
}
else if (eventType === 'NY_AKTIV_BRUKER') {
if (errorConfig.contextholder.updateBruker) {
return getErrorResponse()
}
context.aktivBruker = verdi
ws.send(controlSignal('NY_AKTIV_BRUKER'))
return getSuccessResponse()
}
else {
return getErrorResponse()
}
}),
http.delete(getUrl('/aktivenhet'), () => {
if (errorConfig.contextholder.deleteEnhet) {
return getErrorResponse()
}
context.aktivEnhet = null
ws.send(controlSignal('NY_AKTIV_ENHET'))
return getSuccessResponse()
}),
http.delete(getUrl('/aktivbruker'), () => {
if (errorConfig.contextholder.deleteBruker) {
return getErrorResponse()
}
context.aktivBruker = null
ws.send(controlSignal('NY_AKTIV_BRUKER'))
return getSuccessResponse()
}),
http.get(getUrl('/aktivenhet'), () => {
if (errorConfig.contextholder.getEnhet) {
return getErrorResponse()
}
return getSuccessResponse({ body: { aktivEnhet: context.aktivEnhet } })
}),
http.get(getUrl('/aktivbruker'), () => {
if (errorConfig.contextholder.getBruker) {
return getErrorResponse()
}
return getSuccessResponse({ body: { aktivBruker: context.aktivBruker } })
}),
http.get(getUrl(''), () => {
if (errorConfig.contextholder.get) {
return getErrorResponse()
}
return getSuccessResponse({ body: { aktivEnhet: context.aktivEnhet, aktivBruker: context.aktivBruker } })
}),
http.get(getUrl('/aktor/v2'), async ({ request }) => {
const { fnr } = await request.json() as { fnr: string }
const data = {
fnr,
aktorId: `000${fnr}000`
};
if (errorConfig.aktorIdEndpoint) {
return getErrorResponse()
}
return getSuccessResponse({ body: data })
}),
http.get(getUrl('/decorator'), () => {
if (errorConfig.meEndpoint) {
return getErrorResponse()
}
return getSuccessResponse({ body: mockMe })
}),
http.get(getUrl('/enhet/:enhetId'), ({ params }) => {
const { enhetId } = params
if (errorConfig.enhetEndpoint) {
return getErrorResponse()
}
const enhet = mockMe.enheter.find((enhet) => enhet.enhetId === enhetId)
if (!enhet) {
return getErrorResponse(404)
}
return getSuccessResponse({ body: enhet })
}),
http.post(getUrl('/bytt-bruker-nokkel'), async ({ request }) => {
const { userKey } = await request.json() as { userKey: string }
console.log(userKey)
return getSuccessResponse({ body: `10108000398` })
})

]
}
export const getHandlers = (
ws: WS,
errorConfig: FailureConfig,
): HttpHandler[] => {
ws.on('message', console.log);
return [
http.post(getUrl(''), async ({ request }) => {
const { eventType, verdi } = (await request.json()) as {
eventType: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER';
verdi: string;
};
if (eventType === 'NY_AKTIV_ENHET') {
if (errorConfig.contextholder.updateEnhet) {
return getErrorResponse();
}
context.aktivEnhet = verdi;
ws.send(controlSignal('NY_AKTIV_ENHET'));
return getSuccessResponse();
} else if (eventType === 'NY_AKTIV_BRUKER') {
if (errorConfig.contextholder.updateBruker) {
return getErrorResponse();
}
context.aktivBruker = verdi;
ws.send(controlSignal('NY_AKTIV_BRUKER'));
return getSuccessResponse();
} else {
return getErrorResponse();
}
}),
http.delete(getUrl('/aktivenhet'), () => {
if (errorConfig.contextholder.deleteEnhet) {
return getErrorResponse();
}
context.aktivEnhet = null;
ws.send(controlSignal('NY_AKTIV_ENHET'));
return getSuccessResponse();
}),
http.delete(getUrl('/aktivbruker'), () => {
if (errorConfig.contextholder.deleteBruker) {
return getErrorResponse();
}
context.aktivBruker = null;
ws.send(controlSignal('NY_AKTIV_BRUKER'));
return getSuccessResponse();
}),
http.get(getUrl('/v2/aktivenhet'), () => {
if (errorConfig.contextholder.getEnhet) {
return getErrorResponse();
}
return getSuccessResponse({ body: { aktivEnhet: context.aktivEnhet } });
}),
http.get(getUrl('/v2/aktivbruker'), () => {
if (errorConfig.contextholder.getBruker) {
return getErrorResponse();
}
return getSuccessResponse({ body: { aktivBruker: context.aktivBruker } });
}),
http.get(getUrl(''), () => {
if (errorConfig.contextholder.get) {
return getErrorResponse();
}
return getSuccessResponse({
body: {
aktivEnhet: context.aktivEnhet,
aktivBruker: context.aktivBruker,
},
});
}),
http.get(getUrl('/aktor/v2'), async ({ request }) => {
const { fnr } = (await request.json()) as { fnr: string };
const data = {
fnr,
aktorId: `000${fnr}000`,
};
if (errorConfig.aktorIdEndpoint) {
return getErrorResponse();
}
return getSuccessResponse({ body: data });
}),
http.get(getUrl('/decorator'), () => {
if (errorConfig.meEndpoint) {
return getErrorResponse();
}
return getSuccessResponse({ body: mockMe });
}),
http.get(getUrl('/enhet/:enhetId'), ({ params }) => {
const { enhetId } = params;
if (errorConfig.enhetEndpoint) {
return getErrorResponse();
}
const enhet = mockMe.enheter.find((enhet) => enhet.enhetId === enhetId);
if (!enhet) {
return getErrorResponse(404);
}
return getSuccessResponse({ body: enhet });
}),
http.post(getUrl('/bytt-bruker-nokkel'), async ({ request }) => {
const { userKey } = (await request.json()) as { userKey: string };
console.log(userKey);
return getSuccessResponse({ body: `10108000398` });
}),
];
};
Loading

0 comments on commit 6d3b54f

Please sign in to comment.