|
| 1 | +import { |
| 2 | + BiDataQueryOptions, |
| 3 | + BiDataTable, |
| 4 | + BiSearch, |
| 5 | + LarkPageData, |
| 6 | + makeSimpleFilter, |
| 7 | + normalizeText, |
| 8 | + TableCellLink, |
| 9 | + TableCellRelation, |
| 10 | + TableCellValue, |
| 11 | + TableRecord, |
| 12 | +} from 'mobx-lark'; |
| 13 | +import { toggle } from 'mobx-restful'; |
| 14 | +import { HTTPError } from 'koajax'; |
| 15 | +import { buildURLData } from 'web-utility'; |
| 16 | + |
| 17 | +import { LarkBase, larkClient } from './Base'; |
| 18 | +import { ActivityTableId, LarkBitableId } from './configuration'; |
| 19 | + |
| 20 | +export type Activity = LarkBase & |
| 21 | + Record< |
| 22 | + | 'name' |
| 23 | + | 'alias' |
| 24 | + | 'type' |
| 25 | + | 'tags' |
| 26 | + | 'summary' |
| 27 | + | 'image' |
| 28 | + | 'cardImage' |
| 29 | + | `${'start' | 'end'}Time` |
| 30 | + | 'city' |
| 31 | + | 'location' |
| 32 | + | 'host' |
| 33 | + | 'link' |
| 34 | + | 'liveLink' |
| 35 | + | `database${'' | 'Schema'}`, |
| 36 | + TableCellValue |
| 37 | + >; |
| 38 | + |
| 39 | +export class ActivityModel extends BiDataTable<Activity>() { |
| 40 | + client = larkClient; |
| 41 | + |
| 42 | + queryOptions: BiDataQueryOptions = { text_field_as_array: false }; |
| 43 | + |
| 44 | + constructor(appId = LarkBitableId, tableId = ActivityTableId) { |
| 45 | + super(appId, tableId); |
| 46 | + } |
| 47 | + |
| 48 | + static getLink = ({ |
| 49 | + id, |
| 50 | + type, |
| 51 | + alias, |
| 52 | + link, |
| 53 | + database, |
| 54 | + }: Pick<Activity, 'id' | 'type' | 'alias' | 'link' | 'database'>) => |
| 55 | + database ? `/${type?.toString().toLowerCase() || 'activity'}/${alias || id}` : link + ''; |
| 56 | + |
| 57 | + extractFields({ |
| 58 | + id, |
| 59 | + fields: { host, city, link, database, databaseSchema, ...fields }, |
| 60 | + }: TableRecord<Activity>) { |
| 61 | + return { |
| 62 | + ...fields, |
| 63 | + id: id!, |
| 64 | + host: (host as TableCellRelation[])?.map(normalizeText), |
| 65 | + city: (city as TableCellRelation[])?.map(normalizeText), |
| 66 | + link: (link as TableCellLink)?.link, |
| 67 | + database: (database as TableCellLink)?.link, |
| 68 | + databaseSchema: databaseSchema && JSON.parse(databaseSchema as string), |
| 69 | + }; |
| 70 | + } |
| 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 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export class SearchActivityModel extends BiSearch<Activity>(ActivityModel) { |
| 101 | + searchKeys = ['name', 'alias', 'type', 'tags', 'summary', 'city', 'location', 'host']; |
| 102 | +} |
0 commit comments