Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,24 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { KnowledgeBaseTranslationsWordpress } from '@wordpress/collections/translations/translations.wordpress.model';
import { ImageWordpress } from '@wordpress/collections/system/image.wordpress.model';
import { RolesWordpress } from '@wordpress/collections/roles/roles.wordpress.model';

@ObjectType()
class KnowledgeBaseParentWordpress {
class KnowledgeBaseRolesWordpress {
@Field(() => [RolesWordpress])
nodes: RolesWordpress[];
}

@ObjectType()
export class KnowledgeBaseParentWordpress {
@Field()
databaseId: number;

@Field()
informationAccessRestriction: 'ALLOW' | 'DISALLOW' | 'NONE';

@Field(() => KnowledgeBaseRolesWordpress)
informationRoles: KnowledgeBaseRolesWordpress;
}

@ObjectType()
Expand Down Expand Up @@ -84,6 +97,12 @@ export class KnowledgeBaseWordpress {
@Field()
informationPosition: number;

@Field()
informationAccessRestriction: 'ALLOW' | 'DISALLOW' | 'NONE';

@Field(() => KnowledgeBaseRolesWordpress)
informationRoles: KnowledgeBaseRolesWordpress;

@Field(() => [KnowledgeBaseTranslationsWordpress])
translations: KnowledgeBaseTranslationsWordpress[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ import { KnowledgeBaseTranslationsWordpress } from '@wordpress/collections/trans
import { ValidateMapping } from '@common/decorators/validate-mapping.decorator';
import { KnowledgeBaseSchema } from '@common/validation/schemas/knowledge-base.schema';
import { normalizeEmptyStringToNull } from '@common/utils/normalize';
import { KnowledgeBaseWordpress } from '@wordpress/collections/knowledge-base/knowledge-base.wordpress.model';
import {
KnowledgeBaseParentWordpress,
KnowledgeBaseWordpress,
} from '@wordpress/collections/knowledge-base/knowledge-base.wordpress.model';
import { KnowledgeBase } from '@common/models/knowledge-base.model';
import { Authorization } from '@common/models/authorization.model';

// TODO: Move FRENCH_CODE to .env and rename it to DEFAULT_LANGUAGE_CODE
const FRENCH_CODE = 'FR';
Expand Down Expand Up @@ -94,6 +98,7 @@ export class KnowledgeBaseWordpressService {
childDisplay: knowledgeBase.informationChildDisplay || null,
link: normalizeEmptyStringToNull(knowledgeBase.informationLink),
position: knowledgeBase.informationPosition || 0,
authorization: this.createAuthorization(knowledgeBase),
translations,
parentId:
knowledgeBase.informationParent?.node?.databaseId?.toString() || null,
Expand All @@ -106,6 +111,26 @@ export class KnowledgeBaseWordpressService {
};
}

// creation of authorizations based on the parents' rights if they exist, otherwise classic creation
private createAuthorization(
knowledgeBase: KnowledgeBaseWordpress,
): Authorization | null {
const parentAuthorisation = this.getAuthorization(
knowledgeBase.informationParent?.node,
);
return parentAuthorisation ?? this.getAuthorization(knowledgeBase);
}

private getAuthorization(
knowledgeBase?: KnowledgeBaseWordpress | KnowledgeBaseParentWordpress,
): Authorization | null {
const roles =
knowledgeBase?.informationRoles?.nodes?.map((role) => role.roleCode) ||
[];
const type = knowledgeBase?.informationAccessRestriction;
return type && type !== 'NONE' ? { type, roles } : null;
}

async getKnowledgeBase(): Promise<KnowledgeBase[]> {
const data = await this.wordpressService.executeGraphQLQuery(`
query {
Expand All @@ -118,6 +143,14 @@ export class KnowledgeBaseWordpressService {
informationChildDisplay
informationLink
informationPosition
informationAccessRestriction
informationRoles(first: 100) {
nodes {
databaseId
roleCode
roleDescription
}
}
informationSearchKeywords
informationPhone
informationAddress
Expand All @@ -133,8 +166,14 @@ export class KnowledgeBaseWordpressService {
informationParent {
node {
databaseId
informationTitle
informationContent
informationAccessRestriction
informationRoles(first: 100) {
nodes {
databaseId
roleCode
roleDescription
}
}
}
}
translations {
Expand All @@ -152,6 +191,6 @@ export class KnowledgeBaseWordpressService {
}
}
`);
return data.knowledgeBases.nodes.map(this.mapToMultiModel);
return data.knowledgeBases.nodes.map(this.mapToMultiModel.bind(this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import { Field, ObjectType } from '@nestjs/graphql';
import { KnowledgeBaseTranslations } from '@common/models/translations.model';
import { Authorization } from '@common/models/authorization.model';

@ObjectType()
export class KnowledgeBase {
Expand All @@ -56,6 +57,9 @@ export class KnowledgeBase {
@Field({ nullable: true })
position: number;

@Field(() => Authorization, { nullable: true })
authorization: Authorization | null;

@Field(() => [KnowledgeBaseTranslations], { nullable: true })
translations: KnowledgeBaseTranslations[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import { z } from 'zod';
import { IdSchema } from '@common/validation/schemas/base-type.schema';
import { KnowledgeBaseTranslationsSchema } from '@common/validation/schemas/translations.schema';
import { AuthorizationSchema } from '@common/validation/schemas/authorization.schema';

export const KnowledgeBaseSchema = z.object({
id: IdSchema,
type: z.enum(['content', 'internal_link', 'external_link']),
childDisplay: z.enum(['card', 'list']).nullable(),
link: z.string().min(1, 'Information link cannot be empty string').nullable(),
position: z.number().int().default(0),
authorization: AuthorizationSchema.nullable(),
translations: z
.array(KnowledgeBaseTranslationsSchema)
.min(1, 'At least one translation is required for Features'),
Expand Down
34 changes: 26 additions & 8 deletions dev/user-backend-nest/main/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,32 @@ export class AppController {
);
}

@Get('/knowledge-base')
knowledgeBase() {
return this.knowledgeBaseClient.send(
{
cmd: 'knowledgeBase',
},
{},
);
@Post('/knowledge-base')
knowledgeBase(@Body() body) {
return this.authClient
.send(
{
cmd: 'getUser',
},
body,
)
.pipe(
concatMap((user) => {
const roles = user ? user.roles : ['anonymous'];
return this.knowledgeBaseClient
.send(
{
cmd: 'knowledgeBase',
},
roles,
)
.pipe(
map((knowledgeBase) => {
return new AuthorizationHelper(roles).filter(knowledgeBase);
}),
);
}),
);
}

@Get('/version')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
* termes.
*/

export interface Authorization {
type: 'ALLOW' | 'DISALLOW';
roles: string[];
}

export interface KnowledgeBaseQueryDto {
login: string;
}
Expand Down Expand Up @@ -64,6 +69,7 @@ export interface KnowledgeBaseDto {
translations?: KnowledgeBaseTranslation[];
childDisplay?: ChildDisplay;
position: number;
authorization?: Authorization;
}

export interface KnowledgeBaseTranslation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export class KnowledgeBaseService {
link
childDisplay
position
authorization {
type
roles
}
translations {
languagesCode
searchKeywords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MultiTenantService } from '@multi/shared';
import { Observable } from 'rxjs';
import {tap} from "rxjs/operators";
import { getAuthToken, NetworkService, MultiTenantService } from '@multi/shared';
import {from, Observable} from 'rxjs';
import {filter, switchMap, take, tap} from "rxjs/operators";
import {KnowledgeBaseRepository} from "./knowledge-base.repository";
import {KnowledgeBaseItem} from "./knowledge-base.repository";

Expand All @@ -54,14 +54,24 @@ export class KnowledgeBaseService {
private multiTenantService: MultiTenantService,
private knowledgeBaseRepository: KnowledgeBaseRepository,
private http: HttpClient,
private networkService: NetworkService,
) {}

public loadAndStoreKnowledgeBase(): Observable<KnowledgeBaseItem[]> {
const url = `${this.multiTenantService.getApiEndpoint()}/knowledge-base`;

return this.http.get<KnowledgeBaseItem[]>(url).pipe(
return from(this.networkService.getConnectionStatus()).pipe(
filter(status => status.connected),
switchMap(() => getAuthToken()),
take(1),
switchMap(authToken => this.getKnowledgeBase(authToken)),
tap(knowledgeBases => this.knowledgeBaseRepository.setKnowledgeBases(knowledgeBases)),
);
}

private getKnowledgeBase(authToken: string): Observable<KnowledgeBaseItem[]> {
const url = `${this.multiTenantService.getApiEndpoint()}/knowledge-base`;
const data = {
authToken
};
return this.http.post<KnowledgeBaseItem[]>(url, data);
}
}
Loading