feat(api): Reflected api-changes to different api-requests and fixed mock-api. (TV-371)

Squashed commit of the following:

commit 01ef668c8ddd8e0d8ff459d60f45ca0c7780f184
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 17 11:53:36 2021 +0200

    Fixed issue with authentication

commit e40f4728203388175f405184693b3604001afc5e
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 17 11:09:46 2021 +0200

    Added some more fixes to API requests based on latest changes

commit 920eb03302ee65852141f7bcce72d832507d2c07
Merge: c2d5d7c bfa8ed5
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 17 09:34:48 2021 +0200

    Merge branch 'develop' into feature/TV-371-api-fix

commit c2d5d7c9a7b812a57234b0038da79bc1d0c07881
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 17 07:50:11 2021 +0200

    Updated mock-api

commit 5578b0ffdba27e0e80dabe05e920b96e46331fb0
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Mon Aug 16 15:17:46 2021 +0200

    Fixed fetching of userinfo and organizations

commit 116b3092bf9a685354738146ef7cd0eb619fa009
Merge: 6a32f19 01dc4b3
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Mon Aug 16 13:09:53 2021 +0200

    Merge branch 'develop' into feature/TV-371-api-fix

commit 6a32f1997e9ddb912cf2995a333faa9aa0205852
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Mon Aug 16 13:07:38 2021 +0200

    Updated API-endpoints
This commit is contained in:
Erik Tiekstra
2021-08-17 11:55:22 +02:00
parent 39cdcff53d
commit d003842e4e
21 changed files with 247 additions and 200 deletions

View File

@@ -28,13 +28,13 @@
<div class="avrop-table__cell">
<digi-typography>
<strong class="avrop-table__label">Startdatum:</strong>
<span>{{deltagare?.startDate | date: 'yyyy-MM-dd'}}</span>
<digi-typography-time *ngIf="deltagare?.startDate" [afDateTime]="deltagare?.startDate"></digi-typography-time>
</digi-typography>
</div>
<div class="avrop-table__cell">
<digi-typography>
<strong class="avrop-table__label">Slutdatum:</strong>
<span>{{deltagare?.endDate | date: 'yyyy-MM-dd'}}</span>
<digi-typography-time *ngIf="deltagare?.endDate" [afDateTime]="deltagare?.endDate"></digi-typography-time>
</digi-typography>
</div>
</div>
@@ -42,7 +42,7 @@
<div class="avrop-table__cell">
<digi-typography>
<strong class="avrop-table__label">Språkstöd/Tolk:</strong>
<span>{{deltagare?.sprakstod.beskrivning + '/' + deltagare?.tolkbehov.beskrivning}}</span>
<span>{{deltagare?.sprakstod + '/' + deltagare?.tolkbehov}}</span>
</digi-typography>
</div>
<div class="avrop-table__cell">

View File

@@ -0,0 +1,8 @@
export interface AuthenticationResponse {
id: string;
access_token: string;
scope: string;
id_token: string;
token_type: string;
expires_in: number;
}

View File

@@ -0,0 +1,4 @@
export interface OrganizationResponse {
name: string;
organizationnumber: string;
}

View File

@@ -0,0 +1,6 @@
export interface UserInfoResponse {
id: string;
firstName: string;
lastName: string;
roles: string[];
}

View File

@@ -1,18 +1,11 @@
export interface AuthenticationResult {
import { AuthenticationResponse } from './api/authentication.response.model';
export interface Authentication {
idToken: string;
expiresIn: number;
}
export interface AuthenticationApiResponse {
id: string;
access_token: string;
scope: string;
id_token: string;
token_type: string;
expires_in: number;
}
export function mapAuthApiResponseToAuthenticationResult(data: AuthenticationApiResponse): AuthenticationResult {
export function mapAuthApiResponseToAuthenticationResult(data: AuthenticationResponse): Authentication {
const { id_token, expires_in } = data;
return {
idToken: id_token,

View File

@@ -0,0 +1,10 @@
export interface Environment {
environment: 'api' | 'local' | 'prod';
clientId: string;
loginUrl: string;
production: boolean;
api: {
url: string;
headers: { [key: string]: string };
};
}

View File

@@ -1,12 +1,15 @@
import { OrganizationResponse } from './api/organization.response.model';
export interface Organization {
id: string;
name: string;
kaNumber: number;
address: {
street: string;
houseNumber: number;
postalCode: string;
city: string;
kommun: string;
organizationNumber: string;
}
export function mapResponseToOrganization(data: OrganizationResponse): Organization {
const { name, organizationnumber } = data;
return {
name,
organizationNumber: organizationnumber,
};
}

View File

@@ -0,0 +1,21 @@
import { UserInfoResponse } from './api/user-info.response.model';
export interface UserInfo {
id: string;
firstName: string;
lastName: string;
fullName: string;
roles: string[];
}
export function mapResponseToUserInfo(data: UserInfoResponse): UserInfo {
const { id, firstName, lastName, roles } = data;
return {
id,
firstName,
lastName,
fullName: `${firstName} ${lastName}`,
roles,
};
}

View File

@@ -1,39 +1,21 @@
import { Authorization } from './authorization.model';
import { Organization } from './organization.model';
import { OrganizationResponse } from './api/organization.response.model';
import { UserInfoResponse } from './api/user-info.response.model';
import { mapResponseToOrganization, Organization } from './organization.model';
import { UserInfo } from './user-info.model';
export interface User {
id: string;
firstName: string;
lastName: string;
fullName: string;
ssn: string;
export interface User extends UserInfo {
organizations: Organization[];
authorizations: Authorization[];
}
export interface UserApiResponse {
data: UserApiResponseData;
}
export interface UserApiResponseData {
id: string;
firstName: string;
lastName: string;
ssn: string;
organizations: Organization[];
authorizations: Authorization[];
}
export function mapUserApiResponseToUser(data: UserApiResponseData): User {
const { id, firstName, lastName, ssn, organizations, authorizations } = data;
export function mapUserApiResponseToUser(userInfo: UserInfoResponse, organizations: OrganizationResponse[]): User {
const { id, firstName, lastName, roles } = userInfo;
return {
id,
firstName,
lastName,
fullName: `${firstName} ${lastName}`,
ssn,
organizations,
authorizations,
roles,
organizations: organizations ? organizations.map(organization => mapResponseToOrganization(organization)) : [],
};
}

View File

@@ -1,11 +1,8 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@dafa-environment';
import {
AuthenticationApiResponse,
AuthenticationResult,
mapAuthApiResponseToAuthenticationResult,
} from '@dafa-models/authentication.model';
import { AuthenticationResponse } from '@dafa-models/api/authentication.response.model';
import { Authentication, mapAuthApiResponseToAuthenticationResult } from '@dafa-models/authentication.model';
import { add, isBefore } from 'date-fns';
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
@@ -33,7 +30,7 @@ export class AuthenticationService {
return `${environment.api.url}/auth/token?accessCode=${code}`;
}
private _setSession(authenticationResult: AuthenticationResult): void {
private _setSession(authenticationResult: Authentication): void {
const expiresAt = add(new Date(), { seconds: authenticationResult.expiresIn });
this._token$.next(authenticationResult.idToken);
@@ -42,11 +39,14 @@ export class AuthenticationService {
localStorage.setItem('expires_at', JSON.stringify(expiresAt.valueOf()));
}
login$(authorizationCodeFromCiam: string): Observable<AuthenticationResult> {
login$(authorizationCodeFromCiam: string): Observable<Authentication> {
return this.httpClient
.get<AuthenticationApiResponse>(AuthenticationService._authTokenApiUrl(authorizationCodeFromCiam), API_HEADERS)
.get<{ data: AuthenticationResponse }>(
AuthenticationService._authTokenApiUrl(authorizationCodeFromCiam),
API_HEADERS
)
.pipe(
map(response => mapAuthApiResponseToAuthenticationResult(response)),
map(({ data }) => mapAuthApiResponseToAuthenticationResult(data)),
tap(authenticationResult => {
this._setSession(authenticationResult);
})

View File

@@ -18,9 +18,7 @@ export class AuthorizationService {
private _authorizationsApiUrl = `${environment.api.url}/authorizations`;
public authorizations$: Observable<Authorization[]> = this.httpClient
.get<AuthorizationApiResponse>(this._authorizationsApiUrl, API_HEADERS)
.pipe(
map(response => response.data.map(authorization => mapAuthorizationApiResponseToAuthorization(authorization)))
);
.pipe(map(({ data }) => data.map(authorization => mapAuthorizationApiResponseToAuthorization(authorization))));
constructor(private httpClient: HttpClient) {}
}

View File

@@ -29,7 +29,7 @@ const tempMockDelay = 300;
export class AvropApiService {
private _apiBaseUrl = `${environment.api.url}/avrop`;
constructor(private httpClient: HttpClient) { }
constructor(private httpClient: HttpClient) {}
getNyaAvrop$(
tjanstIds: MultiselectFilterOption[],
@@ -41,10 +41,8 @@ export class AvropApiService {
return this.httpClient
.get<{ data: AvropResponse[] }>(`${this._apiBaseUrl}`, { ...API_HEADERS })
.pipe(
filter(response => !!response),
map(response => {
return response.data.map(avrop => mapAvropResponseToAvrop(avrop))
})
filter(response => !!response?.data),
map(({ data }) => data.map(avrop => mapAvropResponseToAvrop(avrop)))
);
}
@@ -58,29 +56,24 @@ export class AvropApiService {
selectedKommuner: MultiselectFilterOption[],
selectedUtforandeVerksamheter: MultiselectFilterOption[]
): Observable<MultiselectFilterOption[]> {
return this.httpClient.get<{ data: TjanstResponse[] }>(`${this._apiBaseUrl}/tjanster`)
.pipe(
filter(response => !!response),
map(response => {
return response.data.map(tjanster => {
return { label: mapTjanstResponseToTjanst(tjanster).name }
})
})
);
return this.httpClient.get<{ data: TjanstResponse[] }>(`${this._apiBaseUrl}/tjanster`).pipe(
filter(response => !!response?.data),
map(({ data }) => data.map(tjanster => ({ label: mapTjanstResponseToTjanst(tjanster).name })))
);
}
getSelectableUtforandeVerksamheter$(
selectedTjanster: MultiselectFilterOption[],
selectedKommuner: MultiselectFilterOption[]
): Observable<MultiselectFilterOption[]> {
return this.httpClient.get<{ data: UtforandeVerksamhetResponse[] }>(`${this._apiBaseUrl}/utforandeverksamheter`, { ...API_HEADERS })
return this.httpClient
.get<{ data: UtforandeVerksamhetResponse[] }>(`${this._apiBaseUrl}/utforandeverksamheter`, { ...API_HEADERS })
.pipe(
filter(response => !!response),
map(response => {
return response.data.map(utforandeverksamheter => {
return { label: mapUtforandeVerksamhetResponseToUtforandeVerksamhet(utforandeverksamheter).name }
})
}
filter(response => !!response?.data),
map(({ data }) =>
data.map(utforandeverksamheter => ({
label: mapUtforandeVerksamhetResponseToUtforandeVerksamhet(utforandeverksamheter).name,
}))
)
);
}
@@ -89,14 +82,11 @@ export class AvropApiService {
selectedTjanster: MultiselectFilterOption[],
selectedUtforandeVerksamheter: MultiselectFilterOption[]
): Observable<MultiselectFilterOption[]> {
return this.httpClient.get<{ data: KommunResponse[] }>(`${this._apiBaseUrl}/kommuner`, { ...API_HEADERS })
return this.httpClient
.get<{ data: KommunResponse[] }>(`${this._apiBaseUrl}/kommuner`, { ...API_HEADERS })
.pipe(
filter(response => !!response),
map(response => {
return response.data.map(kommun => {
return { label: mapKommunResponseToKommun(kommun).name }
})
})
filter(response => !!response?.data),
map(({ data }) => data.map(kommun => ({ label: mapKommunResponseToKommun(kommun).name })))
);
}

View File

@@ -31,8 +31,7 @@ const API_HEADERS = { headers: environment.api.headers };
providedIn: 'root',
})
export class DeltagareService extends UnsubscribeDirective {
private _apiBaseUrl = `${environment.api.url}/customerinfo`;
private _apiAvropUrl = `${environment.api.url}/avrop`;
private _apiBaseUrl = `${environment.api.url}/deltagare`;
private _currentDeltagareId$ = new BehaviorSubject<string>(null);
constructor(private httpClient: HttpClient, private errorService: ErrorService) {
@@ -53,8 +52,8 @@ export class DeltagareService extends UnsubscribeDirective {
public deltagare$: Observable<Deltagare> = this._deltagare$.asObservable();
public allDeltagare$: Observable<DeltagareCompact[]> = this.httpClient
.get<{ data: AvropResponse[] }>(`${this._apiAvropUrl}`, { ...API_HEADERS })
.pipe(map(response => response.data.map(deltagare => mapResponseToDeltagareCompact(deltagare))));
.get<{ data: AvropResponse[] }>(`${this._apiBaseUrl}`, { ...API_HEADERS })
.pipe(map(({ data }) => data.map(deltagare => mapResponseToDeltagareCompact(deltagare))));
public setCurrentDeltagareId(currentDeltagareId: string): void {
this._deltagare$.next(null);
@@ -63,9 +62,9 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchContactInformation$(id: string): Observable<ContactInformation | Partial<ContactInformation>> {
return this.httpClient
.get<{ data: ContactInformationResponse }>(`${this._apiBaseUrl}/contact/${id}`, { ...API_HEADERS })
.get<{ data: ContactInformationResponse }>(`${this._apiBaseUrl}/${id}/contact`, { ...API_HEADERS })
.pipe(
map(response => mapResponseToContactInformation(response.data)),
map(({ data }) => mapResponseToContactInformation(data)),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of({});
@@ -75,9 +74,9 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchDriversLicense$(id: string): Observable<DriversLicense | Partial<DriversLicense>> {
return this.httpClient
.get<{ data: DriversLicenseResponse }>(`${this._apiBaseUrl}/driverlicense/${id}`, { ...API_HEADERS })
.get<{ data: DriversLicenseResponse }>(`${this._apiBaseUrl}/${id}/driverlicense`, { ...API_HEADERS })
.pipe(
map(response => mapResponseToDriversLicense(response.data)),
map(({ data }) => mapResponseToDriversLicense(data)),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of({});
@@ -87,9 +86,9 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchHighestEducation$(id: string): Observable<HighestEducation | Partial<HighestEducation>> {
return this.httpClient
.get<{ data: HighestEducationResponse }>(`${this._apiBaseUrl}/education/highest/${id}`, { ...API_HEADERS })
.get<{ data: HighestEducationResponse }>(`${this._apiBaseUrl}/${id}/educationlevels/highest`, { ...API_HEADERS })
.pipe(
map(response => mapResponseToHighestEducation(response.data)),
map(({ data }) => mapResponseToHighestEducation(data)),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of({});
@@ -99,31 +98,28 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchEducations$(id: string): Observable<Education[]> {
return this.httpClient
.get<{ data: EducationsResponse }>(`${this._apiBaseUrl}/education/${id}`, { ...API_HEADERS })
.get<{ data: EducationsResponse }>(`${this._apiBaseUrl}/${id}/educations`, { ...API_HEADERS })
.pipe(
map(response => {
if (response.data.utbildningar) {
return response.data.utbildningar.sort((a, b) =>
sortFromToDates({ from: a.period_from, to: a.period_tom }, { from: b.period_from, to: b.period_tom })
);
}
return [];
}),
map(
educations => educations.map(utbildning => mapResponseToEducation(utbildning)),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of([]);
})
)
map(({ data }) =>
data.utbildningar
? data.utbildningar.sort((a, b) =>
sortFromToDates({ from: a.period_from, to: a.period_tom }, { from: b.period_from, to: b.period_tom })
)
: []
),
map(educations => educations.map(utbildning => mapResponseToEducation(utbildning))),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of([]);
})
);
}
private _fetchTranslator$(id: string): Observable<string> {
return this.httpClient
.get<{ data: TranslatorResponse }>(`${this._apiBaseUrl}/translator/${id}`, { ...API_HEADERS })
.get<{ data: TranslatorResponse }>(`${this._apiBaseUrl}/${id}/translator`, { ...API_HEADERS })
.pipe(
map(response => (response.data.sprak ? response.data.sprak.beskrivning : null)),
map(({ data }) => data.sprak?.beskrivning || null),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of('');
@@ -133,9 +129,9 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchWorkLanguages$(id: string): Observable<string[]> {
return this.httpClient
.get<{ data: WorkLanguagesResponse }>(`${this._apiBaseUrl}/work/languages/${id}`, { ...API_HEADERS })
.get<{ data: WorkLanguagesResponse }>(`${this._apiBaseUrl}/${id}/work/languages`, { ...API_HEADERS })
.pipe(
map(response => (response.data.sprak ? response.data.sprak.map(sprak => sprak.beskrivning) : [])),
map(({ data }) => data?.sprak?.map(sprak => sprak.beskrivning) || []),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of([]);
@@ -145,13 +141,9 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchDisabilities$(id: string): Observable<Disability[]> {
return this.httpClient
.get<{ data: DisabilityResponse[] }>(`${this._apiBaseUrl}/work/disability/${id}`, { ...API_HEADERS })
.get<{ data: DisabilityResponse[] }>(`${this._apiBaseUrl}/${id}/work/disabilities`, { ...API_HEADERS })
.pipe(
map(response =>
response.data.length
? response.data.map(funktionsnedsattning => mapResponseToDisability(funktionsnedsattning))
: []
),
map(({ data }) => data?.map(funktionsnedsattning => mapResponseToDisability(funktionsnedsattning)) || []),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of([]);
@@ -161,16 +153,14 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchWorkExperiences$(id: string): Observable<WorkExperience[]> {
return this.httpClient
.get<{ data: WorkExperiencesResponse }>(`${this._apiBaseUrl}/work/experience/${id}`, { ...API_HEADERS })
.get<{ data: WorkExperiencesResponse }>(`${this._apiBaseUrl}/${id}/work/experiences`, { ...API_HEADERS })
.pipe(
map(response => {
if (response.data.arbetslivserfarenheter) {
return response.data.arbetslivserfarenheter.sort((a, b) =>
map(
({ data }) =>
data?.arbetslivserfarenheter?.sort((a, b) =>
sortFromToDates({ from: a.period_from, to: a.period_tom }, { from: b.period_from, to: b.period_tom })
);
}
return [];
}),
) || []
),
map(workExperiences => workExperiences.map(erfarenhet => mapResponseToWorkExperience(erfarenhet))),
catchError(error => {
this.errorService.add(errorToCustomError(error));
@@ -181,9 +171,9 @@ export class DeltagareService extends UnsubscribeDirective {
private _fetchAvropInformation$(id: string): Observable<Avrop | Partial<Avrop>> {
return this.httpClient
.get<{ data: AvropResponse }>(`${this._apiAvropUrl}/${id}`, { ...API_HEADERS })
.get<{ data: AvropResponse }>(`${this._apiBaseUrl}/${id}/avrop`, { ...API_HEADERS })
.pipe(
map(response => (response.data ? mapAvropResponseToAvrop(response.data) : {})),
map(({ data }) => (data ? mapAvropResponseToAvrop(data) : {})),
catchError(error => {
this.errorService.add(errorToCustomError(error));
return of({});

View File

@@ -1,8 +1,13 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { UnsubscribeDirective } from '@dafa-directives/unsubscribe.directive';
import { environment } from '@dafa-environment';
import { mapUserApiResponseToUser, User, UserApiResponse } from '@dafa-models/user.model';
import { Observable } from 'rxjs';
import { OrganizationResponse } from '@dafa-models/api/organization.response.model';
import { UserInfoResponse } from '@dafa-models/api/user-info.response.model';
import { mapResponseToOrganization, Organization } from '@dafa-models/organization.model';
import { mapResponseToUserInfo, UserInfo } from '@dafa-models/user-info.model';
import { User } from '@dafa-models/user.model';
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
const API_HEADERS = { headers: environment.api.headers };
@@ -10,12 +15,32 @@ const API_HEADERS = { headers: environment.api.headers };
@Injectable({
providedIn: 'root',
})
export class UserService {
private _userApiUrl = `${environment.api.url}/auth`;
public user$: Observable<User> = this.httpClient.get<UserApiResponse>(this._userApiUrl, API_HEADERS).pipe(
filter(response => !!response.data),
map(response => mapUserApiResponseToUser(response.data))
);
export class UserService extends UnsubscribeDirective {
private _authApiUrl = `${environment.api.url}/auth`;
private _user$ = new BehaviorSubject<User>(null);
constructor(private httpClient: HttpClient) {}
public user$: Observable<User> = this._user$.asObservable();
constructor(private httpClient: HttpClient) {
super();
this.unsubscribeOnDestroy(
combineLatest([this._fetchUserInfo$(), this._fetchOrganizations$()]).subscribe(([userInfo, organizations]) => {
this._user$.next({ ...userInfo, organizations });
})
);
}
private _fetchOrganizations$(): Observable<Organization[]> {
return this.httpClient.get<{ data: OrganizationResponse[] }>(`${this._authApiUrl}/organizations`, API_HEADERS).pipe(
filter(response => !!response?.data),
map(({ data }) => data.map(organization => mapResponseToOrganization(organization)))
);
}
private _fetchUserInfo$(): Observable<UserInfo> {
return this.httpClient.get<{ data: UserInfoResponse }>(`${this._authApiUrl}/userinfo`, API_HEADERS).pipe(
filter(response => !!response?.data),
map(({ data }) => mapResponseToUserInfo(data))
);
}
}

View File

@@ -1,10 +1,14 @@
export const environment = {
import { Environment } from '@dafa-models/environment.model';
export const environment: Environment = {
environment: 'api',
clientId: '5d08c2e4-763e-42f6-b858-24e4773bb83d',
loginUrl: 'https://ciam-test.arbetsformedlingen.se:8443/uas/oauth2/authorization?response_type=code&scope=openid',
production: false,
api: {
url: '/api',
headers: {},
headers: {
orgnr: '5568301337', // Until we have an organisation-selector
},
},
};

View File

@@ -1,4 +1,8 @@
export const environment: any = {
import { Environment } from '@dafa-models/environment.model';
export const environment: Environment = {
environment: 'prod',
clientId: '',
loginUrl: 'https://ciam.arbetsformedlingen.se/',
production: true,
api: {

View File

@@ -1,4 +1,6 @@
export const environment: any = {
import { Environment } from '@dafa-models/environment.model';
export const environment: Environment = {
environment: 'local',
clientId: '',
loginUrl: '/mock-login',

View File

@@ -1,6 +1,6 @@
{
"/api": {
"target": "https://mina-sidor-fa-test-api.tocp.arbetsformedlingen.se",
"target": "https://mina-sidor-fa-test.tocp.arbetsformedlingen.se",
"secure": false,
"changeOrigin": true
}

View File

@@ -12,11 +12,8 @@ function generateCurrentUser() {
id: faker.datatype.uuid(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
ssn: `${faker.date.between('1950', '2000').toISOString().split('T')[0].replace(/-/g, '')}-${faker.datatype.number({
min: 1000,
max: 9999,
})}`,
organizations: [ORGANIZATIONS[Math.floor(Math.random() * ORGANIZATIONS.length)]],
roles: ['Admin'],
organizations: chooseRandom(ORGANIZATIONS, ORGANIZATIONS.length),
authorizations: chooseRandom(AUTHORIZATIONS, faker.datatype.number(3)),
};
}

View File

@@ -11,13 +11,13 @@ function generateOrganizations(amount = 10) {
for (let i = 1; i <= amount; ++i) {
organizations.push({
id: faker.datatype.uuid(),
organizationNumber: `${faker.datatype.number({
min: 100000,
max: 999999
})}-${faker.datatype.number({
min: 1000,
max: 9999
})}`,
organizationNumber: `${faker.datatype.number({
min: 100000,
max: 999999,
})}${faker.datatype.number({
min: 1000,
max: 9999,
})}`,
name: faker.company.companyName(),
kaNumber: faker.datatype.number({ min: 100000, max: 999999 }),
address: {

View File

@@ -16,13 +16,14 @@ server.use(
'/employee*': '/employees$1',
'/participants': '/participants?_embed=employees',
'/participant/:id': '/participants/:id?_embed=employees',
'/auth': '/currentUser',
'/customerinfo/*/*': '/deltagare/$2',
'/customerinfo': '/deltagare',
'/auth/userinfo': '/currentUser',
'/auth/organizations': '/currentUser',
'/avrop/tjanster*': '/tjanster$1',
'/avrop/utforandeverksamheter*': '/organizations$1',
'/avrop/kommuner*': '/kommuner$1',
'/avrop/:sokandeId': '/avrop?sokandeId=:sokandeId',
'/deltagare': '/avrop',
'/deltagare/:sokandeId/avrop': '/avrop?sokandeId=:sokandeId',
'/deltagare/:sokandeId/*': '/deltagare/:sokandeId',
'*page=*': '$1_page=$2',
'*limit=*': '$1_limit=$2',
'*sort=*': '$1_sort=$2',
@@ -53,41 +54,50 @@ router.render = (req, res) => {
req.body.createdAt = Date.now();
}
if (pathname.includes('/auth/token')) {
res.jsonp(res.locals.data);
} else {
let data = res.locals.data;
const deltagareRegex = /(?:\/customerinfo\/)(contact|driverlicense|education\/highest|education|translator|work\/disability|work\/languages|work\/experience)/g;
const isDeltagarePath = deltagareRegex.exec(pathname);
const avropRegex = /(?:\/avrop\/)(tjanster|utforandeverksamheter|kommuner|\d)/g;
const isAvropPath = avropRegex.exec(pathname);
let data = res.locals.data;
const deltagareRegex = /(?:\/deltagare\/)(?:\d\/)(contact|driverlicense|educationlevels\/highest|educations|translator|work\/disabilities|work\/languages|work\/experiences)/g;
const isDeltagarePath = deltagareRegex.exec(pathname);
const avropRegex = /(?:\/avrop\/(?:tjanster|utforandeverksamheter|kommuner|\d))|(?:\/deltagare\/\d\/(avrop))/g;
const isAvropPath = avropRegex.exec(pathname);
const authRegex = /(?:\/auth\/)(userinfo|organizations)/g;
const isAuthPath = authRegex.exec(pathname);
if (isDeltagarePath) {
const deltagareSubPath = getDeltagareSubPath(isDeltagarePath[1]);
data = res.locals.data[deltagareSubPath] || {};
if (isAuthPath) {
const authSubPath = isAuthPath[1];
if (authSubPath === 'organizations') {
data = res.locals.data[authSubPath].map(organization => ({
name: organization.name,
organizationnumber: organization.organizationNumber,
}));
}
if (isAvropPath) {
if (params) {
const newData = [];
params.forEach((value, key) => {
if (key === 'kommunCodes') {
value = +value;
}
newData.push(...data.filter(item => item[`related_${key}`].includes(value)));
});
data = newData.filter((value, index, arr) => arr.findIndex(item => item.code === value.code) === index);
} else if (isAvropPath[1]) {
data = data[0];
}
}
res.jsonp({
data,
...appendMetaData(params, res),
});
}
if (isDeltagarePath) {
const deltagareSubPath = getDeltagareSubPath(isDeltagarePath[1]);
data = res.locals.data[deltagareSubPath] || {};
}
if (isAvropPath) {
if (params) {
const newData = [];
params.forEach((value, key) => {
if (key === 'kommunCodes') {
value = +value;
}
newData.push(...data.filter(item => item[`related_${key}`].includes(value)));
});
data = newData.filter((value, index, arr) => arr.findIndex(item => item.code === value.code) === index);
} else if (isAvropPath[1]) {
data = data[0];
}
}
res.jsonp({
data,
...appendMetaData(params, res),
});
};
server.use(router);
@@ -116,13 +126,13 @@ function appendMetaData(params, res) {
function getDeltagareSubPath(path) {
switch (path) {
case 'education/highest':
case 'educationlevels/highest':
return 'highestEducation';
case 'work/disability':
case 'work/disabilities':
return 'disabilities';
case 'work/languages':
return 'workLanguages';
case 'work/experience':
case 'work/experiences':
return 'workExperiences';
default:
return path;