Skip to content

Commit

Permalink
fix(mis): 修复平台管理页面用户列表的用户统计错误 (#1013)
Browse files Browse the repository at this point in the history
### 现状:
1.
平台管理页面下的AllUserTable里的数据接口是后端分页,但是前端对三个tab的数据统计是根据该分页接口的该页数据过滤得到的,是不准确的。
2. 之前的GetPlatformUsersCounts是用来适配GetAllUsers后端分页接口,但在此处#905
被删去,原因在于当用户填入了筛选项时,数据统计的接口并不是筛选后的情况。


### 改动
1. 恢复AllUserTable里的GetPlatformUsersCounts接口,确保统计数据准确,
2. 同时给该接口增加过滤参数,确保用户输入过滤项时的数据实时更新。
  • Loading branch information
ZihanChen821 authored Nov 29, 2023
1 parent afad0c5 commit 1a1189a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-trees-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@scow/mis-server": patch
"@scow/mis-web": patch
---

管理系统 AllUserTable 恢复计数接口并且新增筛选参数
5 changes: 5 additions & 0 deletions .changeset/itchy-trains-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scow/grpc-api": patch
---

GetPlatformUsersCounts 新增 id_or_name 参数
20 changes: 15 additions & 5 deletions apps/mis-server/src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,23 @@ export const userServiceServer = plugin((server) => {
}];
},

getPlatformUsersCounts: async ({ em }) => {

const totalCount = await em.count(User);
getPlatformUsersCounts: async ({ request, em }) => {
const { idOrName } = request;
const idOrNameQuery = idOrName ? {
$and: [
{
$or: [
{ userId: { $like: `%${idOrName}%` } },
{ name: { $like: `%${idOrName}%` } },
],
},
],
} : {};
const totalCount = await em.count(User, idOrNameQuery);
const totalAdminCount = await em.count(User,
{ platformRoles: { $like: `%${PlatformRole.PLATFORM_ADMIN}%` } });
{ platformRoles: { $like: `%${PlatformRole.PLATFORM_ADMIN}%` }, ...idOrNameQuery });
const totalFinanceCount = await em.count(User,
{ platformRoles: { $like: `%${PlatformRole.PLATFORM_FINANCE}%` } });
{ platformRoles: { $like: `%${PlatformRole.PLATFORM_FINANCE}%` }, ...idOrNameQuery });

return [{
totalCount: totalCount,
Expand Down
29 changes: 13 additions & 16 deletions apps/mis-web/src/pageComponents/admin/AllUsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DEFAULT_PAGE_SIZE } from "@scow/lib-web/build/utils/pagination";
import { PlatformUserInfo } from "@scow/protos/build/server/user";
import { Static } from "@sinclair/typebox";
import { App, Button, Divider, Form, Input, Space, Table } from "antd";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import React, { useCallback, useMemo, useState } from "react";
import { useAsync } from "react-async";
import { api } from "src/apis";
import { ChangePasswordModalLink } from "src/components/ChangePasswordModal";
Expand Down Expand Up @@ -68,7 +68,6 @@ export const AllUsersTable: React.FC<Props> = ({ refreshToken, user }) => {
const [pageInfo, setPageInfo] = useState<PageInfo>({ page: 1, pageSize: DEFAULT_PAGE_SIZE });
const [sortInfo, setSortInfo] = useState<SortInfo>({ sortField: undefined, sortOrder: undefined });
const [currentPlatformRole, setCurrentPlatformRole] = useState<PlatformRole | undefined>(undefined);
const [allUsers, setAllUsers] = useState<PlatformUserInfo[] | undefined>(undefined);

const promiseFn = useCallback(async () => {

Expand All @@ -83,30 +82,27 @@ export const AllUsersTable: React.FC<Props> = ({ refreshToken, user }) => {
}, [query, pageInfo, sortInfo, currentPlatformRole]);
const { data, isLoading, reload: reloadAllUsers } = useAsync({ promiseFn, watch: refreshToken });

useEffect(() => {
if (currentPlatformRole === undefined) {
setAllUsers(data?.platformUsers);
}
}, [data]);

const { data: platformUsersCounts, isLoading: isCountLoading, reload: reloadUsersCounts } = useAsync({
promiseFn: useCallback(
async () => await api.getPlatformUsersCounts({ query:{ idOrName: query.idOrName } }), [query, refreshToken],
),
});

const roleChangedHandlers = useMemo(() => ({
"ALL_USERS": {
setCurrentPlatformRole: () => setCurrentPlatformRole(undefined),
count: allUsers?.length ?? 0,
count: platformUsersCounts?.totalCount ?? 0,
},
"PLATFORM_ADMIN": {
setCurrentPlatformRole: () => setCurrentPlatformRole(PlatformRole.PLATFORM_ADMIN),
count: allUsers?.filter((user) => {
return user.platformRoles.includes(PlatformRole.PLATFORM_ADMIN);
}).length ?? 0,
count: platformUsersCounts?.totalAdminCount ?? 0,
},
"PLATFORM_FINANCE": {
setCurrentPlatformRole: () => setCurrentPlatformRole(PlatformRole.PLATFORM_FINANCE),
count: allUsers?.filter((user) => {
return user.platformRoles.includes(PlatformRole.PLATFORM_FINANCE);
}).length ?? 0,
count: platformUsersCounts?.totalFinanceCount ?? 0,
},
}), [allUsers]);
}), [platformUsersCounts]);

const handleFilterRoleChange = (role: FilteredRole) => {
roleChangedHandlers[role].setCurrentPlatformRole();
Expand All @@ -116,6 +112,7 @@ export const AllUsersTable: React.FC<Props> = ({ refreshToken, user }) => {

const reload = () => {
reloadAllUsers();
reloadUsersCounts();
};

return (
Expand Down Expand Up @@ -156,7 +153,7 @@ export const AllUsersTable: React.FC<Props> = ({ refreshToken, user }) => {
setPageInfo={setPageInfo}
sortInfo={sortInfo}
setSortInfo={setSortInfo}
isLoading={isLoading}
isLoading={isLoading || isCountLoading}
reload={reload}
user={user}
/>
Expand Down
10 changes: 8 additions & 2 deletions apps/mis-web/src/pages/api/admin/getPlatformUsersCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export type GetPlatformUsersCountsResponse = Static<typeof GetPlatformUsersCount
export const GetPlatformUsersCountsSchema = typeboxRouteSchema({
method: "GET",

query: Type.Object({
idOrName: Type.Optional(Type.String()),
}),

responses: {
200: GetPlatformUsersCountsResponse,
},
Expand All @@ -42,10 +46,12 @@ export default typeboxRoute(GetPlatformUsersCountsSchema,
if (!info) {
return;
}

const { idOrName } = req.query;
const client = getClient(UserServiceClient);

const result = await asyncClientCall(client, "getPlatformUsersCounts", {});
const result = await asyncClientCall(client, "getPlatformUsersCounts", {
idOrName,
});

return {
200: result,
Expand Down
1 change: 1 addition & 0 deletions protos/server/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ message GetUsersByIdsResponse {
}

message GetPlatformUsersCountsRequest {
optional string id_or_name = 1;
}

message GetPlatformUsersCountsResponse {
Expand Down

0 comments on commit 1a1189a

Please sign in to comment.