-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOrganization.ts
166 lines (141 loc) · 4.24 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { observable } from 'mobx';
import {
BiDataQueryOptions,
BiDataTable,
BiSearch,
makeSimpleFilter,
normalizeText,
TableCellLink,
TableCellRelation,
TableCellValue,
TableRecord,
} from 'mobx-lark';
import { Filter, NewData } from 'mobx-restful';
import { groupBy, 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 };
@observable
accessor tagMap: Record<string, Organization[]> = {};
normalize({
fields: { type, tags, link, codeLink, ...fields },
...meta
}: TableRecord<Organization>) {
return {
...meta,
...fields,
type: (type as TableCellRelation[])?.map(normalizeText),
tags: (tags as TableCellRelation[])
?.map(normalizeText)
.toString()
.split(','),
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('&&');
}
async groupAllByTags() {
await this.getAll();
return (this.tagMap = groupBy(
this.allItems,
({ tags }) => tags as string[],
));
}
}
export type OrganizationStatisticItem = Record<
'name' | `${'organization' | 'activity'}Count`,
TableCellValue
>;
export const OSC_YEAR_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_OSC_YEAR_STATISTIC_TABLE_ID!,
OSC_CITY_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_OSC_CITY_STATISTIC_TABLE_ID!,
OSC_TYPE_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_OSC_TYPE_STATISTIC_TABLE_ID!,
OSC_TAG_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_OSC_TAG_STATISTIC_TABLE_ID!;
export const NGO_YEAR_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_NGO_YEAR_STATISTIC_TABLE_ID!,
NGO_CITY_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_NGO_CITY_STATISTIC_TABLE_ID!,
NGO_TYPE_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_NGO_TYPE_STATISTIC_TABLE_ID!,
NGO_TAG_STATISTIC_TABLE_ID =
process.env.NEXT_PUBLIC_NGO_TAG_STATISTIC_TABLE_ID!;
export class OrganizationStatisticModel extends BiDataTable<OrganizationStatisticItem>() {
client = larkClient;
requiredKeys = ['name'] as const;
queryOptions = { text_field_as_array: false };
countAll = async ([
key = 'organizationCount',
]: (keyof OrganizationStatisticItem)[] = []) => {
const list = await this.getAll();
const group = list
.map(({ name, [key]: count }) => count && [name, count])
.filter(Boolean) as [string, number][];
return Object.fromEntries(group);
};
}
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();