Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Is demo #9

Merged
merged 6 commits into from
Aug 1, 2022
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
11 changes: 10 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ export class AppComponent implements OnDestroy, OnInit {
) { }

ngOnInit(): void {
this.configService.isManaged().pipe(first()).subscribe((v) => ConfigManager.initConfigManager(this.http, this.configService, v));
this.initalRequests();
NotificationService.subscribeToNotification(this, {
whitelist: ['notification_created', 'project_deleted', 'config_updated'],
func: this.handleWebsocketNotification
});
this.initializeNotificationService();
this.initWithConfigManager();
}

initalRequests() {
this.configService.isManaged().pipe(first()).subscribe((v) => ConfigManager.initConfigManager(this.http, this.configService, v));
this.configService.isDemo().pipe(first()).subscribe((v) => ConfigManager.setIsDemo(v));
this.configService.isAdmin().pipe(first()).subscribe((v) => ConfigManager.setIsAdmin(v));
this.configService.getBlackWhiteDemo().pipe(first()).subscribe((v) => ConfigManager.setBlackWhiteListDemo(v));
}


initWithConfigManager() {

if (!ConfigManager.isInit()) {
Expand Down
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { ErrorHandler, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BaseModule } from './base/base.module';
import { ExceptionInterceptor } from './base/interceptors/exception.interceptor';
import { GlobalErrorHandler } from './base/interceptors/global-exception.interceptor';
import { NotificationService } from './base/services/notification.service';
import { DataModule } from './data/data.module';
import { ProjectsModule } from './projects/projects.module';
Expand Down Expand Up @@ -50,6 +51,7 @@ import { RecordIDEModule } from './record-ide/record-ide.module';
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: ExceptionInterceptor, multi: true },
{ provide: ErrorHandler, useClass: GlobalErrorHandler },
NotificationService,
],
bootstrap: [AppComponent],
Expand Down
32 changes: 31 additions & 1 deletion src/app/base/components/header/header.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
<header class="sticky top-0 z-10 w-full">
<div
class="relative z-10 flex-shrink-0 h-16 bg-white border-b border-gray-200 shadow-sm flex justify-between items-center">
<div *ngIf="hideLogout"
class="absolute top-0 left-0 right-0 bottom-0 flex items-center justify-center pointer-events-none">
<span class="inline-flex items-center px-2 py-0.5 rounded font-medium bg-red-100 text-red-800">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-play-card mr-2" width="24"
height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<rect transform="rotate(90 12 12)" x="3" y="5" width="18" height="14" rx="2"></rect>
<line x1="8" y1="6" x2="8.01" y2="6"></line>
<line x1="16" y1="18" x2="16.01" y2="18"></line>
<path d="M12 16l-3 -4l3 -4l3 4z"></path>
</svg>
Demo Playground - Everything will be reset on the hour
</span>
<!-- <div class="font-bold text-red-500">Demo - Everything will be reset on the hour</div> -->
</div>
<div class="flex items-center">
<ng-container [ngSwitch]="page">
<ng-template [ngSwitchCase]="'projects'">
Expand Down Expand Up @@ -58,8 +74,22 @@
</svg>
</a>
</div>
<div class="flex items-center justify-center">
<a href="https://github.com/code-kern-ai/refinery" target="_blank" data-tip="Star us on GitHub"
rel="noopener noreferrer" class="flex mr-6 tooltip tooltip-left">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-star" width="24"
height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path
d="M12 17.75l-6.172 3.245l1.179 -6.873l-5 -4.867l6.9 -1l3.086 -6.253l3.086 6.253l6.9 1l-5 4.867l1.179 6.873z">
</path>
</svg>
</a>

</div>
<div class="flex justify-center overflow-visible">
<div *ngIf="user" class="dropdown dropdown-right dropdown-end mr-4">
<div *ngIf="user && !hideLogout" class="dropdown dropdown-right dropdown-end mr-4">
<a tabindex="0" class="w-full cursor-pointer">
<div data-intercom-target="User Avatar Button" (isMenuOpen)="toggleVisible($event, menuButton)"
appDropdown>
Expand Down
4 changes: 3 additions & 1 deletion src/app/base/components/header/header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class HeaderComponent implements OnInit, OnDestroy {
subscriptions$: Subscription[] = [];

showConfigSettings: boolean = false;
hideLogout: boolean;

constructor(private auth: AuthApiService,
private router: Router) { }
Expand All @@ -33,7 +34,8 @@ export class HeaderComponent implements OnInit, OnDestroy {
timer(250).subscribe(() => this.setShowConfig());
return;
}
this.showConfigSettings = !ConfigManager.getIsManaged()
this.hideLogout = ConfigManager.getIsDemo() && !ConfigManager.getIsAdmin();
this.showConfigSettings = !ConfigManager.getIsManaged();
}

ngOnDestroy(): void {
Expand Down
15 changes: 15 additions & 0 deletions src/app/base/interceptors/DemoError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class DemoError extends Error {
errorQueryType: string;
errorQueryText: string;
constructor(type: string, queryText: string, msg?: string) {
super(msg);
Object.setPrototypeOf(this, DemoError.prototype);
this.errorQueryType = type;
this.errorQueryText = queryText;
}

errorMessage(): string {
if (this.message) return this.message
return "This function isn't part of the demo application.\nIf you want to test this function, don't hesitate to check out our open-source or hosted versions!";
}
}
4 changes: 1 addition & 3 deletions src/app/base/interceptors/exception.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import {
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { NotificationService } from '../services/notification.service';

@Injectable()
export class ExceptionInterceptor implements HttpInterceptor {
constructor(private notificationService: NotificationService) {}
constructor(private notificationService: NotificationService) { }

intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
retry(1),
catchError((error: HttpErrorResponse) => {
let message = '';
if (error.error instanceof ErrorEvent) {
Expand Down
14 changes: 14 additions & 0 deletions src/app/base/interceptors/global-exception.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ErrorHandler, Injectable } from '@angular/core';
import { DemoError } from './DemoError';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor() { }

handleError(error: any) {
if (error instanceof DemoError) {
alert(error.errorMessage());
} else {
console.error(error);
}
}
}
37 changes: 37 additions & 0 deletions src/app/base/services/base/apollo-checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


import { Apollo } from 'apollo-angular';
import { QueryOptions, MutationOptions, ApolloQueryResult, FetchResult } from '@apollo/client/core';
import { EmptyObject, WatchQueryOptions } from 'apollo-angular/types';
import { Observable } from 'rxjs';
import { QueryRef } from 'apollo-angular/query-ref';
import { DemoError } from '../../interceptors/DemoError';
import { ConfigManager } from '../config-service';

export class ApolloChecker {

public apollo: Apollo;
constructor(apollo: Apollo) {
this.apollo = apollo;
}

mutate<T, V = EmptyObject>(options: MutationOptions<T, V>): Observable<FetchResult<T>> {
ApolloChecker.checkBlackWhiteList("mutation", options.mutation.loc?.source.body);
return this.apollo.mutate(options)
}
watchQuery<TData, TVariables = EmptyObject>(options: WatchQueryOptions<TVariables, TData>): QueryRef<TData, TVariables> {
ApolloChecker.checkBlackWhiteList("query", options.query.loc?.source.body);
return this.apollo.watchQuery(options);
}
query<T, V = EmptyObject>(options: QueryOptions<V, T>): Observable<ApolloQueryResult<T>> {
ApolloChecker.checkBlackWhiteList("query", options.query.loc?.source.body);
return this.apollo.query(options);
}

private static checkBlackWhiteList(type: string, queryText: string) {
if (!ConfigManager.getIsDemo()) return;
if (ConfigManager.getIsAdmin()) return;
if (!queryText) throw new Error("Can't find query text");
if (!ConfigManager.checkBlackWhiteList(type, queryText)) throw new DemoError(type, queryText);
}
}
35 changes: 35 additions & 0 deletions src/app/base/services/config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export class ConfigManager {
private static http: HttpClient;
private static configApolloService: ConfigApolloService;
private static isManaged: boolean = true;//differentation between propriatary or not
private static isAdmin: boolean = false;
private static isDemo: boolean = false;
private static blackWhiteListDemo: any;
private static registedUpdateListeners: Map<Object, () => void> = new Map<Object, () => void>();
private static justUpdated = false;

Expand Down Expand Up @@ -80,5 +83,37 @@ export class ConfigManager {
ConfigManager.registedUpdateListeners.delete(caller);
}

public static setIsAdmin(value: boolean) {
ConfigManager.isAdmin = value;
}
public static getIsAdmin() {
return ConfigManager.isAdmin;
}

public static setIsDemo(value: boolean) {
ConfigManager.isDemo = value;
}
public static getIsDemo() {
return ConfigManager.isDemo;
}

public static setBlackWhiteListDemo(value: any) {
ConfigManager.blackWhiteListDemo = value;
}

public static checkBlackWhiteList(type: string, queryText: string): boolean {
if (!ConfigManager.blackWhiteListDemo) return false;
if (type == "query") {
for (const blacklisted of ConfigManager.blackWhiteListDemo["queries"]) {
if (queryText.indexOf(blacklisted) != -1) return false;
}
return true;
} else {
for (const whitelisted of ConfigManager.blackWhiteListDemo["mutations"]) {
if (queryText.indexOf(whitelisted) != -1) return true;
}
return false;
}
}
}

32 changes: 31 additions & 1 deletion src/app/base/services/config/config-apollo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { Apollo } from 'apollo-angular';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApolloChecker } from '../base/apollo-checker';
import { mutations } from './config-mutations';
import { queries } from './config-queries';

Expand All @@ -11,7 +12,8 @@ import { queries } from './config-queries';
})
export class ConfigApolloService {

constructor(private apollo: Apollo) { }
private apollo: ApolloChecker;
constructor(private apolloBase: Apollo) { this.apollo = new ApolloChecker(this.apolloBase); }

isManaged() {
return this.apollo
Expand All @@ -30,4 +32,32 @@ export class ConfigApolloService {
}
});
}

isDemo() {
return this.apollo
.query({
query: queries.IS_DEMO,
fetchPolicy: 'cache-first',
})
.pipe(map((result) => result['data']['isDemo'] != false));
}

isAdmin() {
return this.apollo
.query({
query: queries.IS_AMDIN,
fetchPolicy: 'no-cache',
})
.pipe(map((result) => result['data']['isAdmin'] != false));
}

getBlackWhiteDemo() {
return this.apollo
.query({
query: queries.GET_BLACK_WHITE_DEMO,
fetchPolicy: 'cache-first',
})
.pipe(map((result) => JSON.parse(result['data']['getBlackWhiteDemo'])));
}

}
15 changes: 15 additions & 0 deletions src/app/base/services/config/config-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,20 @@ export const queries = {
query{
isManaged
}
`,
IS_DEMO: gql`
query{
isDemo
}
`,
IS_AMDIN: gql`
query{
isAdmin
}
`,
GET_BLACK_WHITE_DEMO: gql`
{
getBlackWhiteDemo
}
`
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { Injectable } from '@angular/core';
import { mutations } from './knowledge-bases-mutations';
import { queries } from './knowledge-bases-queries';
import { map } from 'rxjs/operators';
import { ApolloChecker } from '../base/apollo-checker';

@Injectable({
providedIn: 'root',
})
export class KnowledgeBasesApolloService {
constructor(private apollo: Apollo) { }
private apollo: ApolloChecker;
constructor(private apolloBase: Apollo) { this.apollo = new ApolloChecker(this.apolloBase); }

createKnowledgeBase(projectId: string) {
return this.apollo.mutate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { Injectable } from '@angular/core';
import { Apollo } from 'apollo-angular';
import { NotificationType } from 'aws-sdk/clients/budgets';
import { map } from 'rxjs/operators';
import { ApolloChecker } from '../base/apollo-checker';
import { mutations } from './notification-mutations';
import { queries } from './notification-queries';

@Injectable({
providedIn: 'root',
})
export class NotificationApolloService {
constructor(private apollo: Apollo) { }
private apollo: ApolloChecker;
constructor(private apolloBase: Apollo) { this.apollo = new ApolloChecker(this.apolloBase); }

getNotificationsByUser() {
const query = this.apollo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Apollo } from 'apollo-angular';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Organization } from '../../entities/organization';
import { ApolloChecker } from '../base/apollo-checker';
import { mutations } from './organization-mutations';
import { queries } from './organization-queries';

Expand All @@ -12,7 +13,8 @@ import { queries } from './organization-queries';
})
export class OrganizationApolloService {

constructor(private apollo: Apollo) { }
private apollo: ApolloChecker;
constructor(private apolloBase: Apollo) { this.apollo = new ApolloChecker(this.apolloBase); }

createOrganization(name: string) {
return this.apollo
Expand Down
4 changes: 3 additions & 1 deletion src/app/base/services/project/project-apollo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { Apollo } from 'apollo-angular';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Project } from '../../entities/project';
import { ApolloChecker } from '../base/apollo-checker';
import { mutations } from './project-mutations';
import { queries } from './project-queries';

@Injectable({
providedIn: 'root',
})
export class ProjectApolloService {
constructor(private apollo: Apollo) { }
private apollo: ApolloChecker;
constructor(private apolloBase: Apollo) { this.apollo = new ApolloChecker(this.apolloBase) }

createProject(projectName: string, description: string) {
return this.apollo
Expand Down
Loading