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

[FIX]: moved filtering of apps and fix duplicates #67

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
78 changes: 22 additions & 56 deletions src/v6y-bff/src/resolvers/application/ApplicationQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,59 +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));
}

AppLogger.info(
`[ApplicationQueries - getApplicationListByPageAndParams] appList : ${appList?.length}`,
const appList = await ApplicationProvider.getApplicationListByPageAndParams(
{
searchText,
keywords,
offset: offset !== undefined ? offset : start || 0,
limit,
where,
},
user,
);

return appList;
} catch (error) {
AppLogger.info(`[ApplicationQueries - getApplicationListByPageAndParams] error : ${error}`);
return [];
}
};

/**
* 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}`);

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));
}

AppLogger.info(
`[ApplicationQueries - getApplicationListByPageAndParams] appList : ${appList?.length}`,
`[ApplicationQueries - getApplicationListByPageAndParams] Number of apps : ${appList?.length}`,
);

return appList;
Expand Down Expand Up @@ -331,7 +291,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 || {};

Expand All @@ -344,10 +308,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}`,
Expand All @@ -368,7 +335,6 @@ const ApplicationQueries = {
getApplicationDetailsKeywordsByParams,
getApplicationTotalByParams,
getApplicationListByPageAndParams,
getApplicationList,
getApplicationStatsByParams,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 27 additions & 8 deletions src/v6y-commons/src/database/ApplicationProvider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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}`,
Expand All @@ -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}`,
Expand All @@ -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(
Expand Down
14 changes: 0 additions & 14 deletions src/v6y-front-bo/src/commons/apis/getApplicationList.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
10 changes: 7 additions & 3 deletions src/v6y-front-bo/src/commons/hooks/useToken.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,7 +25,7 @@ export default function VitalityApplicationListView() {
]}
queryOptions={{
resource: 'getApplicationListByPageAndParams',
query: GetApplicationList,
query: GetApplicationListByPageAndParams,
}}
renderTable={(dataSource: ApplicationType[]) => (
<VitalityTable
Expand Down
10 changes: 6 additions & 4 deletions src/v6y-front/src/commons/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AuthCookie } from '../../infrastructure/storage/CookieHelper';
import { useState } from 'react';
import { z } from 'zod';

Expand All @@ -22,15 +23,16 @@ export type LoginAccountFormType = {

const loginSchemaValidator = z.string().email(VitalityTerms.VITALITY_APP_LOGIN_FORM_EMAIL_WARNING);

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,
Expand Down
11 changes: 9 additions & 2 deletions src/v6y-front/src/infrastructure/storage/CookieHelper.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading