-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathActivity.ts
More file actions
105 lines (92 loc) · 2.63 KB
/
Activity.ts
File metadata and controls
105 lines (92 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
BiDataQueryOptions,
BiDataTable,
BiSearch,
BiTableSchema,
LarkPageData,
makeSimpleFilter,
normalizeText,
TableCellLink,
TableCellRelation,
TableCellValue,
TableRecord,
} from 'mobx-lark';
import { toggle } from 'mobx-restful';
import { HTTPError } from 'koajax';
import { buildURLData } from 'web-utility';
import { LarkBase, larkClient } from './Base';
import { ActivityTableId, LarkBitableId } from './configuration';
export type Activity = LarkBase &
Record<
| 'name'
| 'alias'
| 'type'
| 'tags'
| 'summary'
| 'image'
| 'cardImage'
| `${'start' | 'end'}Time`
| 'city'
| 'location'
| 'host'
| 'link'
| 'liveLink'
| `database${'' | 'Schema'}`,
TableCellValue
> & {
databaseSchema: BiTableSchema;
};
export class ActivityModel extends BiDataTable<Activity>() {
client = larkClient;
queryOptions: BiDataQueryOptions = { text_field_as_array: false };
constructor(appId = LarkBitableId, tableId = ActivityTableId) {
super(appId, tableId);
}
static getLink = ({
id,
type,
alias,
link,
database,
}: Pick<Activity, 'id' | 'type' | 'alias' | 'link' | 'database'>) =>
database ? `/${type?.toString().toLowerCase() || 'activity'}/${alias || id}` : link?.toString();
extractFields({
id,
fields: { host, city, link, database, databaseSchema, ...fields },
}: TableRecord<Activity>) {
return {
...fields,
id: id!,
host: (host as TableCellRelation[])?.map(normalizeText),
city: (city as TableCellRelation[])?.map(normalizeText),
link: (link as TableCellLink)?.link,
database: (database as TableCellLink)?.link,
databaseSchema: databaseSchema && JSON.parse(databaseSchema as string),
};
}
@toggle('downloading')
async getOneByAlias(alias: string) {
const path = `${this.baseURI}?${buildURLData({ filter: makeSimpleFilter({ alias }, '=') })}`;
const { body } = await this.client.get<LarkPageData<TableRecord<Activity>>>(path);
const [item] = body!.data!.items || [];
if (!item)
throw new HTTPError(
`Activity "${alias}" is not found`,
{ method: 'GET', path },
{ status: 404, statusText: 'Not found', headers: {} },
);
return (this.currentOne = this.extractFields(item));
}
@toggle('downloading')
async getOne(id: string) {
try {
await super.getOne(id);
} catch {
await this.getOneByAlias(id);
}
return this.currentOne;
}
}
export class SearchActivityModel extends BiSearch<Activity>(ActivityModel) {
searchKeys = ['name', 'alias', 'type', 'tags', 'summary', 'city', 'location', 'host'];
}