@@ -2,8 +2,19 @@ import { LanguageTranslationMap } from '@/constants/common';
2
2
import { ResponseGetType } from '@/interfaces/database/base' ;
3
3
import { IToken } from '@/interfaces/database/chat' ;
4
4
import { ITenantInfo } from '@/interfaces/database/knowledge' ;
5
- import { ISystemStatus , IUserInfo } from '@/interfaces/database/user-setting' ;
6
- import userService from '@/services/user-service' ;
5
+ import {
6
+ ISystemStatus ,
7
+ ITenant ,
8
+ ITenantUser ,
9
+ IUserInfo ,
10
+ } from '@/interfaces/database/user-setting' ;
11
+ import userService , {
12
+ addTenantUser ,
13
+ agreeTenant ,
14
+ deleteTenantUser ,
15
+ listTenant ,
16
+ listTenantUser ,
17
+ } from '@/services/user-service' ;
7
18
import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
8
19
import { Modal , message } from 'antd' ;
9
20
import DOMPurify from 'dompurify' ;
@@ -215,3 +226,125 @@ export const useCreateSystemToken = () => {
215
226
216
227
return { data, loading, createToken : mutateAsync } ;
217
228
} ;
229
+
230
+ export const useListTenantUser = ( ) => {
231
+ const { data : tenantInfo } = useFetchTenantInfo ( ) ;
232
+ const tenantId = tenantInfo . tenant_id ;
233
+ const {
234
+ data,
235
+ isFetching : loading ,
236
+ refetch,
237
+ } = useQuery < ITenantUser [ ] > ( {
238
+ queryKey : [ 'listTenantUser' , tenantId ] ,
239
+ initialData : [ ] ,
240
+ gcTime : 0 ,
241
+ enabled : ! ! tenantId ,
242
+ queryFn : async ( ) => {
243
+ const { data } = await listTenantUser ( tenantId ) ;
244
+
245
+ return data ?. data ?? [ ] ;
246
+ } ,
247
+ } ) ;
248
+
249
+ return { data, loading, refetch } ;
250
+ } ;
251
+
252
+ export const useAddTenantUser = ( ) => {
253
+ const { data : tenantInfo } = useFetchTenantInfo ( ) ;
254
+ const queryClient = useQueryClient ( ) ;
255
+ const {
256
+ data,
257
+ isPending : loading ,
258
+ mutateAsync,
259
+ } = useMutation ( {
260
+ mutationKey : [ 'addTenantUser' ] ,
261
+ mutationFn : async ( email : string ) => {
262
+ const { data } = await addTenantUser ( tenantInfo . tenant_id , email ) ;
263
+ if ( data . retcode === 0 ) {
264
+ queryClient . invalidateQueries ( { queryKey : [ 'listTenantUser' ] } ) ;
265
+ }
266
+ return data ?. retcode ;
267
+ } ,
268
+ } ) ;
269
+
270
+ return { data, loading, addTenantUser : mutateAsync } ;
271
+ } ;
272
+
273
+ export const useDeleteTenantUser = ( ) => {
274
+ const { data : tenantInfo } = useFetchTenantInfo ( ) ;
275
+ const queryClient = useQueryClient ( ) ;
276
+ const { t } = useTranslation ( ) ;
277
+
278
+ const {
279
+ data,
280
+ isPending : loading ,
281
+ mutateAsync,
282
+ } = useMutation ( {
283
+ mutationKey : [ 'deleteTenantUser' ] ,
284
+ mutationFn : async ( {
285
+ userId,
286
+ tenantId,
287
+ } : {
288
+ userId : string ;
289
+ tenantId ?: string ;
290
+ } ) => {
291
+ const { data } = await deleteTenantUser ( {
292
+ tenantId : tenantId ?? tenantInfo . tenant_id ,
293
+ userId,
294
+ } ) ;
295
+ if ( data . retcode === 0 ) {
296
+ message . success ( t ( 'message.deleted' ) ) ;
297
+ queryClient . invalidateQueries ( { queryKey : [ 'listTenantUser' ] } ) ;
298
+ queryClient . invalidateQueries ( { queryKey : [ 'listTenant' ] } ) ;
299
+ }
300
+ return data ?. data ?? [ ] ;
301
+ } ,
302
+ } ) ;
303
+
304
+ return { data, loading, deleteTenantUser : mutateAsync } ;
305
+ } ;
306
+
307
+ export const useListTenant = ( ) => {
308
+ const { data : tenantInfo } = useFetchTenantInfo ( ) ;
309
+ const tenantId = tenantInfo . tenant_id ;
310
+ const {
311
+ data,
312
+ isFetching : loading ,
313
+ refetch,
314
+ } = useQuery < ITenant [ ] > ( {
315
+ queryKey : [ 'listTenant' , tenantId ] ,
316
+ initialData : [ ] ,
317
+ gcTime : 0 ,
318
+ enabled : ! ! tenantId ,
319
+ queryFn : async ( ) => {
320
+ const { data } = await listTenant ( ) ;
321
+
322
+ return data ?. data ?? [ ] ;
323
+ } ,
324
+ } ) ;
325
+
326
+ return { data, loading, refetch } ;
327
+ } ;
328
+
329
+ export const useAgreeTenant = ( ) => {
330
+ const queryClient = useQueryClient ( ) ;
331
+ const { t } = useTranslation ( ) ;
332
+
333
+ const {
334
+ data,
335
+ isPending : loading ,
336
+ mutateAsync,
337
+ } = useMutation ( {
338
+ mutationKey : [ 'agreeTenant' ] ,
339
+ mutationFn : async ( tenantId : string ) => {
340
+ const { data } = await agreeTenant ( tenantId ) ;
341
+ if ( data . retcode === 0 ) {
342
+ message . success ( t ( 'message.operated' ) ) ;
343
+ queryClient . invalidateQueries ( { queryKey : [ 'listTenant' ] } ) ;
344
+ }
345
+ return data ?. data ?? [ ] ;
346
+ } ,
347
+ } ) ;
348
+
349
+ return { data, loading, agreeTenant : mutateAsync } ;
350
+ } ;
0 commit comments