Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few adjustments to allow to use the server properly from the fronten… #1

Merged
merged 7 commits into from
Dec 19, 2023
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
31 changes: 31 additions & 0 deletions electron-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions electron-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"license": "MIT",
"dependencies": {
"compression": "^1.7.4",
"cors": "^2.8.5",
"electron-squirrel-startup": "^1.0.0",
"express": "^4.18.2",
"rxjs": "^7.8.1",
Expand All @@ -41,6 +42,7 @@
"@electron-forge/plugin-auto-unpack-natives": "^6.4.2",
"@electron-forge/publisher-github": "^6.4.2",
"@types/compression": "^1.7.5",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.12.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { Blob as FSBlob } from 'buffer';
import * as fs from 'fs';
import path from 'path';
import { Observable, forkJoin, map, of, switchMap } from 'rxjs';
import {
ConnectorParameters,
FileStatus,
IConnector,
Link,
SearchResults,
SyncItem,
} from 'src/logic/connector/domain/connector';
import { ConnectorParameters, FileStatus, IConnector, Link, SearchResults, SyncItem } from '../../domain/connector';
import { SourceConnectorDefinition } from '../factory';

const FILES_TO_IGNORE = ['.DS_Store', 'Thumbs.db'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class GDriveImpl extends OAuthBaseConnector implements IConnector {
if (!params?.token) {
return false;
}
if (!params?.refresh_token) {
if (!params?.refresh) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Test validate gdrive params', () => {
expect(
sourceConnector.areParametersValid({
incorrect: 'test',
refresh_token: 'test',
refresh: 'test',
}),
).toBe(false);
});
Expand All @@ -88,7 +88,7 @@ describe('Test validate gdrive params', () => {
expect(
sourceConnector.areParametersValid({
token: '',
refresh_token: '',
refresh: '',
}),
).toBe(false);
});
Expand All @@ -97,7 +97,7 @@ describe('Test validate gdrive params', () => {
expect(
sourceConnector.areParametersValid({
token: 'test',
refresh_token: 'test',
refresh: 'test',
}),
).toBe(true);
});
Expand Down
4 changes: 0 additions & 4 deletions electron-app/src/logic/sync/domain/dto/create-sync.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export class CreateSyncDto {
if (!connectorDefinition) {
return ['Connector definition is not defined'];
}
const sourceConnector = connectorDefinition.factory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create a sync object without validate the conector data?

if (!sourceConnector.areParametersValid(props.connector.parameters)) {
return [`Connector ${props.connector.name} parameters are not valid`];
}

if (!props.kb) {
return ['The Knowledge Box info is mandatory'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Create Sync dto tests', () => {
expect(dto).toBeUndefined();
});

test('should not create a valid dto - connector params are not valid', () => {
test('should not create a valid dto - connector params are incomplete', () => {
const [error, dto] = CreateSyncDto.create({
...props,
id: undefined,
Expand All @@ -52,8 +52,8 @@ describe('Create Sync dto tests', () => {
},
});

expect(error).toEqual('Connector folder parameters are not valid');
expect(dto).toBeUndefined();
expect(error).toBeUndefined();
expect(dto).toBeDefined();
});

test('should not create a valid dto - kb params are not valid', () => {
Expand Down
2 changes: 1 addition & 1 deletion electron-app/src/logic/sync/domain/sync.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getConnector } from '../../connector/infrastructure/factory';

export type Connector = {
name: 'gdrive' | 'folder';
logo: string;
// logo: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameters: { [key: string]: any };
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CustomError } from '../../../errors';
import { ISyncRepository } from '../sync.repository';
import { getConnector } from '../../../connector/infrastructure/factory';

export interface GetSyncAuthUseCase {
execute(id: string): Promise<boolean>;
}

export class GetSyncAuth implements GetSyncAuthUseCase {
constructor(private readonly repository: ISyncRepository) {}

async execute(id: string) {
const data = await this.repository.getSync(id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create an SyncEntity and get the sourceConector from there to get the auth data. it Is better because we will have only one responsible to manage this, the entity

if (data === null) {
throw new CustomError(`Sync with id ${id} not found`, 404);
}
const connectorDefinition = getConnector(data.connector?.name || '');
if (!connectorDefinition) {
return false;
}
const sourceConnector = connectorDefinition.factory();
sourceConnector.setParameters(data.connector?.parameters ?? {});
return sourceConnector.hasAuthData();
}
}
11 changes: 11 additions & 0 deletions electron-app/src/logic/sync/presentation/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GetSync } from '../domain/use-cases/get-sync.use-case';
import { UpdateSync } from '../domain/use-cases/update-sync.use-case';
import { FileSystemSyncDatasource } from '../infrastructure/file-system.sync.datasource';
import { SyncRepository } from '../infrastructure/sync.repository';
import { GetSyncAuth } from '../domain/use-cases/get-sync-auth.use-case';

export class SyncFileSystemRoutes {
private readonly basePath: string;
Expand Down Expand Up @@ -73,6 +74,16 @@ export class SyncFileSystemRoutes {
}
});

router.get('/:id/auth', async (req, res) => {
const { id } = req.params;
try {
const data = await new GetSyncAuth(syncRepository).execute(id);
res.status(200).send({ hasAuth: data });
} catch (error) {
this.handleError(res, error);
}
});

router.get('/:id/folders', async (req, res) => {
const { id } = req.params;
try {
Expand Down
15 changes: 13 additions & 2 deletions electron-app/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import compression from 'compression';
import express, { Router } from 'express';
import cors from 'cors';
import http from 'http';
import { EVENTS, EventEmitter } from './events/events';

Expand Down Expand Up @@ -46,20 +47,30 @@ export class Server {
}

async start() {
//* CORS
this.app.use(
cors({
origin: '*',
}),
);
//* Middlewares
this.app.use(express.json()); // raw
this.app.use(express.urlencoded({ extended: true })); // x-www-form-urlencoded
this.app.use(compression());
this.app.use((req, res, next) => {
res.setHeader('Content-Type', 'application/json');
next();
});

//* Routes
this.app.use(this.routes);

this.app.get('/', async (_req, res) => {
res.status(200).send('Server is running');
res.status(200).send(JSON.stringify('Server is running'));
});

this.app.get('/status', async (_req, res) => {
res.status(200).send('Server is running');
res.status(200).send(JSON.stringify({ running: true }));
});

this.app.post('/stop', async (_req, res) => {
Expand Down