@@ -2,12 +2,17 @@ import {
22 BiDataQueryOptions ,
33 BiDataTable ,
44 BiSearch ,
5+ LarkPageData ,
6+ makeSimpleFilter ,
57 normalizeText ,
68 TableCellLink ,
79 TableCellRelation ,
810 TableCellValue ,
911 TableRecord ,
1012} from 'mobx-lark' ;
13+ import { toggle } from 'mobx-restful' ;
14+ import { HTTPError } from 'koajax' ;
15+ import { buildURLData } from 'web-utility' ;
1116
1217import { LarkBase , larkClient } from './Base' ;
1318import { ActivityTableId , LarkBitableId } from './configuration' ;
@@ -42,22 +47,54 @@ export class ActivityModel extends BiDataTable<Activity>() {
4247
4348 static getLink = ( {
4449 id,
50+ type,
4551 alias,
4652 link,
4753 database,
48- } : Pick < Activity , 'id' | 'alias' | 'link' | 'database' > ) =>
49- database ? `/activity/${ alias || id } ` : link + '' ;
54+ } : Pick < Activity , 'id' | 'type' | ' alias' | 'link' | 'database' > ) =>
55+ database ? `/${ type ?. toString ( ) . toLowerCase ( ) || ' activity' } /${ alias || id } ` : link + '' ;
5056
51- extractFields ( { id, fields : { host, city, link, database, ...fields } } : TableRecord < Activity > ) {
57+ extractFields ( {
58+ id,
59+ fields : { host, city, link, database, databaseSchema, ...fields } ,
60+ } : TableRecord < Activity > ) {
5261 return {
5362 ...fields ,
5463 id : id ! ,
5564 host : ( host as TableCellRelation [ ] ) ?. map ( normalizeText ) ,
5665 city : ( city as TableCellRelation [ ] ) ?. map ( normalizeText ) ,
5766 link : ( link as TableCellLink ) ?. link ,
5867 database : ( database as TableCellLink ) ?. link ,
68+ databaseSchema : databaseSchema && JSON . parse ( databaseSchema as string ) ,
5969 } ;
6070 }
71+
72+ @toggle ( 'downloading' )
73+ async getOneByAlias ( alias : string ) {
74+ const path = `${ this . baseURI } ?${ buildURLData ( { filter : makeSimpleFilter ( { alias } , '=' ) } ) } ` ;
75+
76+ const { body } = await this . client . get < LarkPageData < TableRecord < Activity > > > ( path ) ;
77+
78+ const [ item ] = body ! . data ! . items || [ ] ;
79+
80+ if ( ! item )
81+ throw new HTTPError (
82+ `Activity "${ alias } " is not found` ,
83+ { method : 'GET' , path } ,
84+ { status : 404 , statusText : 'Not found' , headers : { } } ,
85+ ) ;
86+ return ( this . currentOne = this . extractFields ( item ) ) ;
87+ }
88+
89+ @toggle ( 'downloading' )
90+ async getOne ( id : string ) {
91+ try {
92+ await super . getOne ( id ) ;
93+ } catch {
94+ await this . getOneByAlias ( id ) ;
95+ }
96+ return this . currentOne ;
97+ }
6198}
6299
63100export class SearchActivityModel extends BiSearch < Activity > ( ActivityModel ) {
0 commit comments