From 742aa61b7c7e8de53fc28c8dcbbb1ed0aa3e43fd Mon Sep 17 00:00:00 2001 From: nael Date: Thu, 30 Nov 2023 19:02:27 +0100 Subject: [PATCH] :sparkles: Backend integration webapp --- .../organisations/organisations.controller.ts | 7 +- .../organisations/organisations.service.ts | 4 + .../src/@core/projects/projects.controller.ts | 7 +- .../src/@core/projects/projects.service.ts | 4 + packages/webapp/src/hooks/useApiKeys.tsx | 44 +++++----- packages/webapp/src/hooks/useConnections.tsx | 44 +++++----- .../webapp/src/hooks/useFieldMappings.tsx | 81 ++++++++++++++----- packages/webapp/src/hooks/useJobs.tsx | 44 +++++----- packages/webapp/src/hooks/useLinkedUsers.tsx | 45 ++++++----- .../webapp/src/hooks/useOrganisations.tsx | 44 +++++----- packages/webapp/src/hooks/useProjects.tsx | 44 +++++----- packages/webapp/src/types/index.ts | 4 + 12 files changed, 235 insertions(+), 137 deletions(-) create mode 100644 packages/webapp/src/types/index.ts diff --git a/packages/api/src/@core/organisations/organisations.controller.ts b/packages/api/src/@core/organisations/organisations.controller.ts index 0979ef348..bdf72f0e6 100644 --- a/packages/api/src/@core/organisations/organisations.controller.ts +++ b/packages/api/src/@core/organisations/organisations.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Post } from '@nestjs/common'; +import { Body, Controller, Get, Post } from '@nestjs/common'; import { OrganisationsService } from './organisations.service'; import { LoggerService } from '../logger/logger.service'; import { CreateOrganizationDto } from './dto/create-organization.dto'; @@ -12,6 +12,11 @@ export class OrganisationsController { this.logger.setContext(OrganisationsController.name); } + @Get() + getProjects() { + return this.organizationsService.getOrganisations(); + } + @Post('create') createOrg(@Body() orgCreateDto: CreateOrganizationDto) { return this.organizationsService.createOrganization(orgCreateDto); diff --git a/packages/api/src/@core/organisations/organisations.service.ts b/packages/api/src/@core/organisations/organisations.service.ts index 719f5c3ed..aeec015dc 100644 --- a/packages/api/src/@core/organisations/organisations.service.ts +++ b/packages/api/src/@core/organisations/organisations.service.ts @@ -9,6 +9,10 @@ export class OrganisationsService { constructor(private prisma: PrismaService, private logger: LoggerService) { this.logger.setContext(OrganisationsService.name); } + + async getOrganisations() { + return await this.prisma.organizations.findMany(); + } async createOrganization(data: CreateOrganizationDto) { const res = await this.prisma.organizations.create({ data: { diff --git a/packages/api/src/@core/projects/projects.controller.ts b/packages/api/src/@core/projects/projects.controller.ts index fd96c7103..937416da8 100644 --- a/packages/api/src/@core/projects/projects.controller.ts +++ b/packages/api/src/@core/projects/projects.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Post } from '@nestjs/common'; +import { Body, Controller, Get, Post } from '@nestjs/common'; import { ProjectsService } from './projects.service'; import { LoggerService } from '../logger/logger.service'; import { CreateProjectDto } from './dto/create-project.dto'; @@ -12,6 +12,11 @@ export class ProjectsController { this.logger.setContext(ProjectsController.name); } + @Get() + getProjects() { + return this.projectsService.getProjects(); + } + @Post('create') createProject(@Body() projectCreateDto: CreateProjectDto) { return this.projectsService.createProject(projectCreateDto); diff --git a/packages/api/src/@core/projects/projects.service.ts b/packages/api/src/@core/projects/projects.service.ts index d443c7041..5ef3703d0 100644 --- a/packages/api/src/@core/projects/projects.service.ts +++ b/packages/api/src/@core/projects/projects.service.ts @@ -9,6 +9,10 @@ export class ProjectsService { constructor(private prisma: PrismaService, private logger: LoggerService) { this.logger.setContext(ProjectsService.name); } + + async getProjects() { + return await this.prisma.projects.findMany(); + } async createProject(data: CreateProjectDto) { const { id_organization, ...rest } = data; const res = await this.prisma.projects.create({ diff --git a/packages/webapp/src/hooks/useApiKeys.tsx b/packages/webapp/src/hooks/useApiKeys.tsx index d153d0aa5..411b67422 100644 --- a/packages/webapp/src/hooks/useApiKeys.tsx +++ b/packages/webapp/src/hooks/useApiKeys.tsx @@ -1,5 +1,7 @@ + +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -13,31 +15,35 @@ export interface Job { date: string; } -const useApiKeys = () => { +export interface ApiKeysReturnType extends HookBaseReturn { + apiKeys: Job[]; + fetchApiKeys: () => Promise; +} + +type ApiKeysReturnFunction = () => ApiKeysReturnType; + + +const useApiKeys: ApiKeysReturnFunction = () => { const [apiKeys, setApiKeys] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - useEffect(() => { - async function loadApiKeys() { - try { - const response = await fetch(`${config.API_URL}/apiKeys`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setApiKeys(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchApiKeys = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/auth/api-keys`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setApiKeys(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadApiKeys(); - }, []); - - return { apiKeys, isLoading, error }; + return { apiKeys, fetchApiKeys, isLoading, error }; }; export default useApiKeys; diff --git a/packages/webapp/src/hooks/useConnections.tsx b/packages/webapp/src/hooks/useConnections.tsx index b532a9436..d8cb92daa 100644 --- a/packages/webapp/src/hooks/useConnections.tsx +++ b/packages/webapp/src/hooks/useConnections.tsx @@ -1,5 +1,7 @@ + +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -13,31 +15,35 @@ export interface Job { date: string; } -const useConnections = () => { +export interface ConnectionsReturnType extends HookBaseReturn { + connections: Job[]; + fetchConnections: () => Promise; +} + +type ConnectionsReturnFunction = () => ConnectionsReturnType; + + +const useConnections: ConnectionsReturnFunction = () => { const [connections, setConnections] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - useEffect(() => { - async function loadConnections() { - try { - const response = await fetch(`${config.API_URL}/connections`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setConnections(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchConnections = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/connections`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setConnections(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadConnections(); - }, []); - - return { connections, isLoading, error }; + return { connections, fetchConnections, isLoading, error }; }; export default useConnections; diff --git a/packages/webapp/src/hooks/useFieldMappings.tsx b/packages/webapp/src/hooks/useFieldMappings.tsx index 6697dc537..1ce27b9dc 100644 --- a/packages/webapp/src/hooks/useFieldMappings.tsx +++ b/packages/webapp/src/hooks/useFieldMappings.tsx @@ -1,5 +1,6 @@ +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -12,32 +13,72 @@ export interface Job { organisation: string; date: string; } + +export interface FMReturnType extends HookBaseReturn { + mappings: Job[]; + standardObjects: string[]; + fetchMappings: () => Promise; + fetchStandardObjects: () => Promise; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + getMapping: (id: string) => Promise; +} + +type FMReturnFunction = () => FMReturnType; + -const useFieldMappings = () => { - const [fieldMappings, setFieldMappings] = useState([]); - const [isLoading, setIsLoading] = useState(true); +const useFieldMappings: FMReturnFunction = () => { + const [mappings, setMappings] = useState([]); + const [standardObjects, setStandardObjects] = useState([]); + + const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); - useEffect(() => { - async function loadFieldMappings() { - try { - const response = await fetch(`${config.API_URL}/fieldMappings`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setFieldMappings(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchMappings = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/field-mapping/attribute`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setMappings(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadFieldMappings(); - }, []); + const fetchStandardObjects = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/field-mapping/entities`); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + const data = await response.json(); + setStandardObjects(data); + } catch (err) { + setError(err as Error); + } + setIsLoading(false); + }; + + //TODO + const getMapping = async (id: string) => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/field-mapping/${id}`); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return await response.json(); + } catch (err) { + setError(err as Error); + } + setIsLoading(false); + }; - return { fieldMappings, isLoading, error }; + return { mappings, isLoading, error, fetchMappings, standardObjects, fetchStandardObjects, getMapping }; }; export default useFieldMappings; diff --git a/packages/webapp/src/hooks/useJobs.tsx b/packages/webapp/src/hooks/useJobs.tsx index a61a51900..c1812efe5 100644 --- a/packages/webapp/src/hooks/useJobs.tsx +++ b/packages/webapp/src/hooks/useJobs.tsx @@ -1,5 +1,7 @@ + +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -13,31 +15,35 @@ export interface Job { date: string; } -const useJobs = () => { +export interface JobsReturnType extends HookBaseReturn { + jobs: Job[]; + fetchJobs: () => Promise; +} + +type JobsReturnFunction = () => JobsReturnType; + + +const useJobs: JobsReturnFunction = () => { const [jobs, setJobs] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - useEffect(() => { - async function loadJobs() { - try { - const response = await fetch(`${config.API_URL}/jobs`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setJobs(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchJobs = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/jobs`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setJobs(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadJobs(); - }, []); - - return { jobs, isLoading, error }; + return { jobs, fetchJobs, isLoading, error }; }; export default useJobs; diff --git a/packages/webapp/src/hooks/useLinkedUsers.tsx b/packages/webapp/src/hooks/useLinkedUsers.tsx index b75ed5adf..dadde4c9b 100644 --- a/packages/webapp/src/hooks/useLinkedUsers.tsx +++ b/packages/webapp/src/hooks/useLinkedUsers.tsx @@ -1,6 +1,7 @@ +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -14,31 +15,35 @@ export interface Job { date: string; } -const useLinkedUsers = () => { - const [linkedAccounts, setLinkedAccounts] = useState([]); +export interface LUReturnType extends HookBaseReturn { + linkedUsers: Job[]; + fetchLinkedUsers: () => Promise; +} + +type LUReturnFunction = () => LUReturnType; + + +const useLinkedUsers: LUReturnFunction = () => { + const [linkedUsers, setLinkedUsers] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - useEffect(() => { - async function loadConnections() { - try { - const response = await fetch(`${config.API_URL}/linkedUsers`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setLinkedAccounts(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchLinkedUsers = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/linked-users`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setLinkedUsers(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadConnections(); - }, []); - - return { linkedAccounts, isLoading, error }; + return { linkedUsers, fetchLinkedUsers, isLoading, error }; }; export default useLinkedUsers; diff --git a/packages/webapp/src/hooks/useOrganisations.tsx b/packages/webapp/src/hooks/useOrganisations.tsx index 08146df4c..3a7a275ee 100644 --- a/packages/webapp/src/hooks/useOrganisations.tsx +++ b/packages/webapp/src/hooks/useOrganisations.tsx @@ -1,5 +1,7 @@ + +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -13,31 +15,35 @@ export interface Job { date: string; } -const useOrganisations = () => { +export interface OrganisationsReturnType extends HookBaseReturn { + organisations: Job[]; + fetchOrganisations: () => Promise; +} + +type OrgansiationsReturnFunction = () => OrganisationsReturnType; + + +const useOrganisations: OrgansiationsReturnFunction = () => { const [organisations, setOrganisations] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - useEffect(() => { - async function loadOrganisations() { - try { - const response = await fetch(`${config.API_URL}/organisations`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setOrganisations(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchOrganisations = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/organisations`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setOrganisations(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadOrganisations(); - }, []); - - return { organisations, isLoading, error }; + return { organisations, fetchOrganisations, isLoading, error }; }; export default useOrganisations; diff --git a/packages/webapp/src/hooks/useProjects.tsx b/packages/webapp/src/hooks/useProjects.tsx index fb4f38d70..ccbcc7e67 100644 --- a/packages/webapp/src/hooks/useProjects.tsx +++ b/packages/webapp/src/hooks/useProjects.tsx @@ -1,5 +1,7 @@ + +import { HookBaseReturn } from '@/types'; import config from '@/utils/config'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; //TODO: import from shared type @@ -13,31 +15,35 @@ export interface Job { date: string; } -const useProjects = () => { +export interface ProjectsReturnType extends HookBaseReturn { + projects: Job[]; + fetchProjects: () => Promise; +} + +type ProjectsReturnFunction = () => ProjectsReturnType; + + +const useProjects: ProjectsReturnFunction = () => { const [projects, setProjects] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - useEffect(() => { - async function loadProjects() { - try { - const response = await fetch(`${config.API_URL}/projects`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - setProjects(data); - } catch (err) { - setError(err as Error); - } finally { - setIsLoading(false); + const fetchProjects = async () => { + setIsLoading(true); + try { + const response = await fetch(`${config.API_URL}/projects`); + if (!response.ok) { + throw new Error('Network response was not ok'); } + const data = await response.json(); + setProjects(data); + } catch (err) { + setError(err as Error); } + setIsLoading(false); + }; - loadProjects(); - }, []); - - return { projects, isLoading, error }; + return { projects, fetchProjects, isLoading, error }; }; export default useProjects; diff --git a/packages/webapp/src/types/index.ts b/packages/webapp/src/types/index.ts new file mode 100644 index 000000000..a02296a5f --- /dev/null +++ b/packages/webapp/src/types/index.ts @@ -0,0 +1,4 @@ +export interface HookBaseReturn { + isLoading: boolean; + error: Error | null; +} \ No newline at end of file