-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOrganization.ts
135 lines (113 loc) · 3.12 KB
/
Organization.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { observable } from 'mobx';
import {
BiDataQueryOptions,
BiDataTable,
BiSearch,
makeSimpleFilter,
TableCellLink,
TableCellValue,
TableRecord,
} from 'mobx-lark';
import { Filter, NewData } from 'mobx-restful';
import { cache, countBy, groupBy, Hour, isEmpty } from 'web-utility';
import { larkClient } from '../Base';
import { COMMUNITY_BASE_ID } from './index';
export type Organization = Record<
| 'id'
| 'verified'
| 'name'
| 'type'
| 'tags'
| 'startDate'
| 'city'
| 'email'
| 'link'
| 'codeLink'
| 'wechatName'
| 'logos'
| 'summary',
TableCellValue
>;
export type OrganizationStatistic = Record<
'type' | 'tag' | 'year' | 'city',
Record<string, number>
>;
export const ORGANIZATION_TABLE_ID =
process.env.NEXT_PUBLIC_ORGANIZATION_TABLE_ID!,
NGO_BASE_ID = process.env.NEXT_PUBLIC_NGO_BASE_ID!,
NGO_TABLE_ID = process.env.NEXT_PUBLIC_NGO_TABLE_ID!;
export const sortStatistic = (data: Record<string, number>, sortValue = true) =>
Object.entries(data)
.map(([key, count]) => [key, count] as const)
.sort(([kX, vX], [kY, vY]) => (sortValue ? vY - vX : kY.localeCompare(kX)));
export class OrganizationModel extends BiDataTable<Organization>() {
client = larkClient;
constructor(appId = COMMUNITY_BASE_ID, tableId = ORGANIZATION_TABLE_ID) {
super(appId, tableId);
}
requiredKeys = ['name', 'type', 'tags', 'city', 'logos', 'summary'] as const;
queryOptions: BiDataQueryOptions = { text_field_as_array: false };
declare statistic: OrganizationStatistic;
@observable
accessor tagMap: Record<string, Organization[]> = {};
normalize({
id,
fields: { link, codeLink, ...fields },
}: TableRecord<Organization>) {
return {
...fields,
id: id!,
link: (link as TableCellLink)?.link,
codeLink: (codeLink as TableCellLink)?.link,
};
}
makeFilter(filter: NewData<Organization>) {
return [
'CurrentValue.[verified]=1',
!isEmpty(filter) && makeSimpleFilter(filter),
]
.filter(Boolean)
.join('&&');
}
getStatistic = cache(async clean => {
const list = await this.getAll();
setTimeout(clean, Hour / 2);
return (this.statistic = {
type: countBy(list, 'type'),
tag: countBy(list, 'tags'),
year: countBy(list, ({ startDate }) =>
new Date(startDate as number).getFullYear(),
),
city: countBy(list, 'city'),
});
}, 'Organization Statistic');
async groupAllByTags() {
await this.getAll();
return (this.tagMap = groupBy(
this.allItems,
({ tags }) => tags as string[],
));
}
}
export class SearchOrganizationModel extends BiSearch(OrganizationModel) {
searchKeys = [
'name',
'summary',
'city',
'email',
'link',
'codeLink',
'wechatName',
] as const;
makeFilter(filter: Filter<Organization>) {
return ['CurrentValue.[verified]=1', super.makeFilter(filter)]
.filter(Boolean)
.join('&&');
}
}
export class SearchNGOModel extends SearchOrganizationModel {
constructor(appId = NGO_BASE_ID, tableId = NGO_TABLE_ID) {
super(appId, tableId);
}
}
export default new OrganizationModel();