Skip to content

Commit 7dc6595

Browse files
committed
[add] Hackathon types & models based on MobX-Lark
1 parent c6bb7c0 commit 7dc6595

File tree

5 files changed

+336
-442
lines changed

5 files changed

+336
-442
lines changed

models/Activity.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1217
import { LarkBase, larkClient } from './Base';
1318
import { 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

63100
export class SearchActivityModel extends BiSearch<Activity>(ActivityModel) {

models/Base.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ export const makeGithubSearchCondition = (queryMap: DataObject) =>
5151
.map(([key, value]) => `${key}:${value}`)
5252
.join(' ');
5353

54-
export type LarkBase = Record<'id' | 'createdAt' | 'updatedAt', TableCellValue>;
54+
export type LarkBase = Record<
55+
'id' | `created${'At' | 'By'}` | `updated${'At' | 'By'}`,
56+
TableCellValue
57+
>;
5558

5659
export const larkClient = new HTTPClient({
5760
baseURI: LARK_API_HOST,

0 commit comments

Comments
 (0)