From eeee2c6e62484e6a8521959f21f813f62ac681c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20AUBERT?= Date: Mon, 23 Dec 2024 14:02:07 +0100 Subject: [PATCH 1/4] [FIX]: Fix type coverage --- src/v6y-front-bo/src/commons/hooks/useToken.ts | 10 +++++++--- src/v6y-front/src/commons/hooks/useAuth.tsx | 10 ++++++---- .../src/infrastructure/storage/CookieHelper.ts | 11 +++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/v6y-front-bo/src/commons/hooks/useToken.ts b/src/v6y-front-bo/src/commons/hooks/useToken.ts index 5f6cec7a..649489b9 100644 --- a/src/v6y-front-bo/src/commons/hooks/useToken.ts +++ b/src/v6y-front-bo/src/commons/hooks/useToken.ts @@ -1,10 +1,14 @@ import Cookies from 'js-cookie'; -const useToken = () => { +interface Auth { + token?: string; +} + +const useToken = (): string | undefined => { const auth = Cookies.get('auth'); - const token = JSON.parse(auth || '{}')?.token; + const parsedAuth: Auth = JSON.parse(auth || '{}'); - return token; + return parsedAuth.token; }; export default useToken; diff --git a/src/v6y-front/src/commons/hooks/useAuth.tsx b/src/v6y-front/src/commons/hooks/useAuth.tsx index a2f74038..4d163cc7 100644 --- a/src/v6y-front/src/commons/hooks/useAuth.tsx +++ b/src/v6y-front/src/commons/hooks/useAuth.tsx @@ -1,14 +1,16 @@ +import type { AuthCookie } from '../../infrastructure/storage/CookieHelper'; import { getAuthCookie, removeAuthCookie } from '../../infrastructure/storage/CookieHelper'; -const getAuthToken = () => { - const auth = getAuthCookie(); +const getAuthToken = (): string | undefined => { + const auth: AuthCookie | null = getAuthCookie(); return auth?.token; }; -const useLogin = () => { - const auth = getAuthCookie(); +const useLogin = () => { + const auth: AuthCookie | null = getAuthCookie(); + if (auth) { return { isLoggedIn: true, diff --git a/src/v6y-front/src/infrastructure/storage/CookieHelper.ts b/src/v6y-front/src/infrastructure/storage/CookieHelper.ts index ab31084c..b1f6b74a 100644 --- a/src/v6y-front/src/infrastructure/storage/CookieHelper.ts +++ b/src/v6y-front/src/infrastructure/storage/CookieHelper.ts @@ -1,10 +1,17 @@ import Cookies from 'js-cookie'; -export const getAuthCookie = () => { + +export interface AuthCookie { + token: string; + _id: string; + role: string; +} + +export const getAuthCookie = (): AuthCookie | null => { const auth = Cookies.get('auth'); if (!auth) return null; try { - return JSON.parse(auth); + return JSON.parse(auth) as AuthCookie; } catch (e) { console.error('Failed to parse auth cookie:', e); return null; From 63271a347931f90071fe993dd6e927e2a6e5d80a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20AUBERT?= Date: Tue, 24 Dec 2024 13:20:11 +0100 Subject: [PATCH 2/4] [FIX]: moved app filtering to the db query --- .../application/ApplicationQueries.ts | 59 ++++++++++--------- .../src/database/ApplicationProvider.ts | 35 ++++++++--- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts b/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts index a921b8c7..d3cc4fb2 100644 --- a/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts +++ b/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts @@ -234,21 +234,19 @@ const getApplicationListByPageAndParams = async ( AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] where : ${where}`); AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] sort : ${sort}`); - let appList = await ApplicationProvider.getApplicationListByPageAndParams({ - searchText, - keywords, - offset: offset !== undefined ? offset : start || 0, - limit, - where, - }); - - if (!(user.role === 'ADMIN' || user.role === 'SUPERADMIN')) { - const userApplicationsIds = user.applications || []; - appList = appList.filter((app) => userApplicationsIds.includes(app._id)); - } + const appList = await ApplicationProvider.getApplicationListByPageAndParams( + { + searchText, + keywords, + offset: offset !== undefined ? offset : start || 0, + limit, + where, + }, + user, + ); AppLogger.info( - `[ApplicationQueries - getApplicationListByPageAndParams] appList : ${appList?.length}`, + `[ApplicationQueries - getApplicationListByPageAndParams] Number of apps : ${appList?.length}`, ); return appList; @@ -275,18 +273,16 @@ const getApplicationList = async ( AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] where : ${where}`); AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] sort : ${sort}`); - let appList = await ApplicationProvider.getApplicationListByPageAndParams({ - where, - sort, - }); - - if (!(user.role === 'ADMIN' || user.role === 'SUPERADMIN')) { - const userApplicationsIds = user.applications || []; - appList = appList.filter((app) => userApplicationsIds.includes(app._id)); - } + const appList = await ApplicationProvider.getApplicationListByPageAndParams( + { + where, + sort, + }, + user, + ); AppLogger.info( - `[ApplicationQueries - getApplicationListByPageAndParams] appList : ${appList?.length}`, + `[ApplicationQueries - getApplicationListByPageAndParams] Number of apps : ${appList?.length}`, ); return appList; @@ -331,7 +327,11 @@ const getApplicationStatsByParams = async (_: unknown, args: SearchQueryType) => * @param _ * @param args */ -const getApplicationTotalByParams = async (_: unknown, args: SearchQueryType) => { +const getApplicationTotalByParams = async ( + _: unknown, + args: SearchQueryType, + { user }: { user: AccountType }, +) => { try { const { keywords, searchText } = args || {}; @@ -344,10 +344,13 @@ const getApplicationTotalByParams = async (_: unknown, args: SearchQueryType) => `[ApplicationQueries - getApplicationTotalByParams] searchText : ${searchText}`, ); - const appsTotal = await ApplicationProvider.getApplicationTotalByParams({ - searchText, - keywords, - }); + const appsTotal = await ApplicationProvider.getApplicationTotalByParams( + { + searchText, + keywords, + }, + user, + ); AppLogger.info( `[ApplicationQueries - getApplicationTotalByParams] apps Total : ${appsTotal}`, diff --git a/src/v6y-commons/src/database/ApplicationProvider.ts b/src/v6y-commons/src/database/ApplicationProvider.ts index c560ad07..295592fc 100644 --- a/src/v6y-commons/src/database/ApplicationProvider.ts +++ b/src/v6y-commons/src/database/ApplicationProvider.ts @@ -1,6 +1,7 @@ import { FindOptions, Op, Sequelize } from 'sequelize'; import AppLogger from '../core/AppLogger.ts'; +import { AccountType } from '../types/AccountType.ts'; import { ApplicationInputType, ApplicationType } from '../types/ApplicationType.ts'; import { SearchQueryType } from '../types/SearchQueryType.ts'; import AuditProvider from './AuditProvider.ts'; @@ -416,13 +417,10 @@ const getApplicationDetailsKeywordsByParams = async ({ _id }: ApplicationType) = * @param limit * @param where */ -const getApplicationListByPageAndParams = async ({ - searchText, - keywords, - offset, - limit, - where, -}: SearchQueryType) => { +const getApplicationListByPageAndParams = async ( + { searchText, keywords, offset, limit, where }: SearchQueryType, + user: AccountType, +) => { try { AppLogger.info( `[ApplicationProvider - getApplicationListByPageAndParams] keywords: ${keywords?.join( @@ -445,6 +443,16 @@ const getApplicationListByPageAndParams = async ({ limit, where, }); + + if (user.role !== 'ADMIN' && user.role !== 'SUPERADMIN' && user.applications?.length) { + searchQuery.where = { + ...searchQuery.where, + _id: { + [Op.in]: user.applications, + }, + }; + } + const applications = await ApplicationModelType.findAll(searchQuery); AppLogger.info( `[ApplicationProvider - getApplicationListByPageAndParams] applications: ${applications?.length}`, @@ -462,7 +470,10 @@ const getApplicationListByPageAndParams = async ({ * @param searchText * @param keywords */ -const getApplicationTotalByParams = async ({ searchText, keywords }: SearchQueryType) => { +const getApplicationTotalByParams = async ( + { searchText, keywords }: SearchQueryType, + user: AccountType, +) => { try { AppLogger.info( `[ApplicationProvider - getApplicationTotalByParams] searchText: ${searchText}`, @@ -474,6 +485,14 @@ const getApplicationTotalByParams = async ({ searchText, keywords }: SearchQuery ); const searchQuery = await buildSearchQuery({ searchText, keywords }); + if (user.role !== 'ADMIN' && user.role !== 'SUPERADMIN' && user.applications?.length) { + searchQuery.where = { + ...searchQuery.where, + _id: { + [Op.in]: user.applications, + }, + }; + } const applicationsCount = await ApplicationModelType.count(searchQuery); AppLogger.info( From 584f36ff208d7f72f2edf92cde8c3a35c7603d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20AUBERT?= Date: Tue, 24 Dec 2024 14:34:55 +0100 Subject: [PATCH 3/4] [FIX]: removed duplicated getApplicationList --- .../src/commons/apis/getApplicationList.ts | 14 -------------- .../apis/getApplicationListByPageAndParams.ts | 14 ++++++++++++++ .../components/VitalityAccountCreateView.tsx | 6 +++--- .../components/VitalityAccountEditView.tsx | 6 +++--- .../components/VitalityApplicationListView.tsx | 4 ++-- 5 files changed, 22 insertions(+), 22 deletions(-) delete mode 100644 src/v6y-front-bo/src/commons/apis/getApplicationList.ts create mode 100644 src/v6y-front-bo/src/commons/apis/getApplicationListByPageAndParams.ts diff --git a/src/v6y-front-bo/src/commons/apis/getApplicationList.ts b/src/v6y-front-bo/src/commons/apis/getApplicationList.ts deleted file mode 100644 index 1a1a8d82..00000000 --- a/src/v6y-front-bo/src/commons/apis/getApplicationList.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { gql } from 'graphql-request'; - -const GetApplicationList = gql` - query GetApplicationList($sort: [String]) { - getApplicationList(sort: $sort) { - _id - acronym - name - description - } - } -`; - -export default GetApplicationList; diff --git a/src/v6y-front-bo/src/commons/apis/getApplicationListByPageAndParams.ts b/src/v6y-front-bo/src/commons/apis/getApplicationListByPageAndParams.ts new file mode 100644 index 00000000..2f9c2eef --- /dev/null +++ b/src/v6y-front-bo/src/commons/apis/getApplicationListByPageAndParams.ts @@ -0,0 +1,14 @@ +import { gql } from 'graphql-request'; + +const getApplicationListByPageAndParams = gql` + query GetApplicationListByPageAndParams($sort: String) { + getApplicationListByPageAndParams(sort: $sort) { + _id + acronym + name + description + } + } +`; + +export default getApplicationListByPageAndParams; diff --git a/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountCreateView.tsx b/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountCreateView.tsx index 7c2cc33b..b9faab08 100644 --- a/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountCreateView.tsx +++ b/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountCreateView.tsx @@ -4,7 +4,7 @@ import { ApplicationType } from '@v6y/commons/src/types/ApplicationType'; import { Typography } from 'antd'; import { useEffect, useState } from 'react'; -import GetApplicationList from '../../../commons/apis/getApplicationList'; +import GetApplicationListByPageAndParams from '../../../commons/apis/getApplicationListByPageAndParams'; import VitalityEmptyView from '../../../commons/components/VitalityEmptyView'; import { accountCreateEditItems, @@ -44,8 +44,8 @@ export default function VitalityAccountCreateView() { createQueryParams: {}, }} selectOptions={{ - resource: 'getApplicationList', - query: GetApplicationList, + resource: 'getApplicationListByPageAndParams', + query: GetApplicationListByPageAndParams, }} // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error diff --git a/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountEditView.tsx b/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountEditView.tsx index e31442bf..653755a7 100644 --- a/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountEditView.tsx +++ b/src/v6y-front-bo/src/features/v6y-accounts/components/VitalityAccountEditView.tsx @@ -6,7 +6,7 @@ import { Typography } from 'antd'; import { useEffect, useState } from 'react'; import React from 'react'; -import GetApplicationList from '../../../commons/apis/getApplicationList'; +import GetApplicationListByPageAndParams from '../../../commons/apis/getApplicationListByPageAndParams'; import VitalityEmptyView from '../../../commons/components/VitalityEmptyView'; import { accountCreateEditItems, @@ -59,8 +59,8 @@ export default function VitalityAccountEditView() { }, }} selectOptions={{ - resource: 'getApplicationList', - query: GetApplicationList, + resource: 'getApplicationListByPageAndParams', + query: GetApplicationListByPageAndParams, }} // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error diff --git a/src/v6y-front-bo/src/features/v6y-applications/components/VitalityApplicationListView.tsx b/src/v6y-front-bo/src/features/v6y-applications/components/VitalityApplicationListView.tsx index 02d7c832..0e83016b 100644 --- a/src/v6y-front-bo/src/features/v6y-applications/components/VitalityApplicationListView.tsx +++ b/src/v6y-front-bo/src/features/v6y-applications/components/VitalityApplicationListView.tsx @@ -1,6 +1,6 @@ import { ApplicationType } from '@v6y/commons'; -import GetApplicationList from '../../../commons/apis/getApplicationList'; +import GetApplicationListByPageAndParams from '../../../commons/apis/getApplicationListByPageAndParams'; import VitalityTable from '../../../commons/components/VitalityTable'; import { buildCommonTableColumns, @@ -25,7 +25,7 @@ export default function VitalityApplicationListView() { ]} queryOptions={{ resource: 'getApplicationListByPageAndParams', - query: GetApplicationList, + query: GetApplicationListByPageAndParams, }} renderTable={(dataSource: ApplicationType[]) => ( Date: Tue, 24 Dec 2024 18:22:45 +0100 Subject: [PATCH 4/4] [FIX]: remove old duplicated function --- .../application/ApplicationQueries.ts | 37 ------------------- .../application/ApplicationQueriesType.ts | 1 - 2 files changed, 38 deletions(-) diff --git a/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts b/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts index d3cc4fb2..8b9e025c 100644 --- a/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts +++ b/src/v6y-bff/src/resolvers/application/ApplicationQueries.ts @@ -256,42 +256,6 @@ const getApplicationListByPageAndParams = async ( } }; -/** - * Get application list - * @param _ - * @param args - * @param user - */ -const getApplicationList = async ( - _: unknown, - args: SearchQueryType, - { user }: { user: AccountType }, -) => { - try { - const { where, sort } = args || {}; - - AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] where : ${where}`); - AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] sort : ${sort}`); - - const appList = await ApplicationProvider.getApplicationListByPageAndParams( - { - where, - sort, - }, - user, - ); - - AppLogger.info( - `[ApplicationQueries - getApplicationListByPageAndParams] Number of apps : ${appList?.length}`, - ); - - return appList; - } catch (error) { - AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] error : ${error}`); - return []; - } -}; - /** * Get application stats by params * @param _ @@ -371,7 +335,6 @@ const ApplicationQueries = { getApplicationDetailsKeywordsByParams, getApplicationTotalByParams, getApplicationListByPageAndParams, - getApplicationList, getApplicationStatsByParams, }; diff --git a/src/v6y-bff/src/types/application/ApplicationQueriesType.ts b/src/v6y-bff/src/types/application/ApplicationQueriesType.ts index bcd8504a..bd42f410 100644 --- a/src/v6y-bff/src/types/application/ApplicationQueriesType.ts +++ b/src/v6y-bff/src/types/application/ApplicationQueriesType.ts @@ -1,6 +1,5 @@ const ApplicationQueriesType = ` type Query { - getApplicationList(where: JSON, sort: [String]): [ApplicationType] getApplicationListByPageAndParams(start: Int, offset: Int, limit: Int, keywords: [String], searchText: String, where: JSON, sort: String): [ApplicationType] getApplicationStatsByParams(keywords: [String]): [KeywordStatsType] getApplicationTotalByParams(keywords: [String], searchText: String): Int