-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCatalogue.ts
253 lines (218 loc) · 7.4 KB
/
getCatalogue.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import type { Entry, Asset } from "contentful";
import type {
CatalogueAPIResponse,
CatalogueBasicInfo,
CatalogueBottlenoseDolphin,
ContentTypeCatalogueBottlenoseDolphin,
CatalogueMinkeWhale,
ContentTypeCatalogueMinkeWhale,
} from "./types";
import { Catalogues, ContentTypes, CATALOGUE_RESULTS_LIMIT } from "./constants";
import { contentfulDeliveryClient } from "./contentful";
import { flattenImageAssetFields } from "./flattenAssetFields";
/**
* Reduces bottlenose dolphin catalogue entry.
* @param entry Bottlenose dolphin catalogue Contentful entry.
* @returns Simplified data of entry.
*/
const reduceCatalogueItem = (
entry: Entry<ContentTypeCatalogueBottlenoseDolphin | ContentTypeCatalogueMinkeWhale>,
): CatalogueBasicInfo => ({
id: String(entry.fields.id),
reference: entry.fields?.reference ? String(entry.fields.reference) : null,
name: entry.fields?.name ? String(entry.fields.name) : null,
slug: String(entry.fields.slug),
});
interface GetCatalogueListOptions {
page: number;
search?: string;
}
/**
* Gets catalogue entries from Contentful.
* @param catalogue Catalogue type.
* @param options Options.
* @param options.page Number of entries to query.
* @param [options.search] Text to search on `slug` field.
* @returns Catalogue entries.
*/
const getCatalogueList = async (
catalogue: Catalogues,
{ page, search }: GetCatalogueListOptions,
): Promise<CatalogueAPIResponse | null> => {
const query = {
order: ["fields.id"],
limit: CATALOGUE_RESULTS_LIMIT,
skip: CATALOGUE_RESULTS_LIMIT * (page - 1),
};
if (search) {
// @ts-expect-error TBA
query.query = search;
}
let result;
if (catalogue === Catalogues.BottlenoseDolphin) {
result = await contentfulDeliveryClient.getEntries<ContentTypeCatalogueBottlenoseDolphin>({
content_type: ContentTypes.CatalogueBottlenoseDolphin,
...query,
});
}
if (catalogue === Catalogues.MinkeWhale) {
result = await contentfulDeliveryClient.getEntries<ContentTypeCatalogueMinkeWhale>({
content_type: ContentTypes.CatalogueMinkeWhale,
...query,
});
}
const items = result!.items.map((entry) => reduceCatalogueItem(entry));
return {
meta: {
pageSize: CATALOGUE_RESULTS_LIMIT,
currentPage: page,
totalPages: Math.ceil(result!.total / CATALOGUE_RESULTS_LIMIT),
totalItems: result!.total,
},
items,
};
};
/**
* Gets bottlenose dolphin calf catalogue entries by mother's Contentful entry ID.
* @param entryID Contentful entry ID.
* @returns Array of calf catalogue entries.
*/
const getBottlenoseEntryCalves = async (entryID: string): Promise<Array<CatalogueBasicInfo>> => {
const { items } =
await contentfulDeliveryClient.getEntries<ContentTypeCatalogueBottlenoseDolphin>({
content_type: ContentTypes.CatalogueBottlenoseDolphin,
"fields.mother.sys.id": entryID,
order: ["-fields.id"], // Order so 'current calf' is first
});
const data = items.map((entry) => reduceCatalogueItem(entry));
return data;
};
/**
* Gets bottlenose dolphin catalogue item entry from Contentful.
* @param slug Entry `slug` field value.
* @returns Bottlenose dolphin catalogue entry.
*/
const getBottlenoseDolphinCatalogueItem = async (
slug: string,
): Promise<CatalogueBottlenoseDolphin | null> => {
const { items } =
await contentfulDeliveryClient.getEntries<ContentTypeCatalogueBottlenoseDolphin>({
content_type: ContentTypes.CatalogueBottlenoseDolphin,
limit: 1,
"fields.slug": slug,
});
if (!items.length) {
return null;
}
const [entry] = items;
const calves = await getBottlenoseEntryCalves(entry.sys.id);
const data = {
entry: {
id: entry.fields.id,
reference: entry.fields?.reference ?? null,
name: entry.fields?.name ?? null,
slug: entry.fields.slug,
birthYear: entry.fields?.birthYear ?? null,
sex: entry.fields?.sex ?? "UNKNOWN",
totalRecaptures: entry.fields?.totalRecaptures ?? null,
yearsRecaptured: entry.fields?.yearsRecaptured ?? null,
totalCalves: entry.fields.totalCalves ?? null,
leftDorsalFin: entry.fields?.leftDorsalFin
? flattenImageAssetFields(entry.fields.leftDorsalFin)
: null,
rightDorsalFin: entry.fields?.rightDorsalFin
? flattenImageAssetFields(entry.fields.rightDorsalFin)
: null,
otherImages:
entry.fields.otherImages?.map((item) => flattenImageAssetFields(item as Asset)) ?? [],
lastUpdated: entry.sys.updatedAt,
},
mother: entry.fields.mother ? reduceCatalogueItem(entry.fields.mother) : null,
calves,
previous: entry.fields.previousEntry ? reduceCatalogueItem(entry.fields.previousEntry) : null,
next: entry.fields.nextEntry ? reduceCatalogueItem(entry.fields.nextEntry) : null,
};
return data;
};
/**
* Gets slug of bottlenose dolphin entry by ID.
* @param id Entry `id` field value.
* @returns Bottlenose dolphin catalogue entry slug.
*/
const getBottlenoseDolphinItemEntrySlug = async (
id: CatalogueBottlenoseDolphin["entry"]["id"],
): Promise<CatalogueBottlenoseDolphin["entry"]["slug"] | null> => {
const { items } =
await contentfulDeliveryClient.getEntries<ContentTypeCatalogueBottlenoseDolphin>({
content_type: ContentTypes.CatalogueBottlenoseDolphin,
limit: 1,
"fields.id": id,
});
if (!items.length) {
return null;
}
const [entry] = items;
return entry.fields.slug;
};
/**
* Gets minke whale catalogue item entry from Contentful.
* @param slug Entry `slug` field value.
* @returns Minke whale catalogue entry.
*/
const getMinkeWhaleCatalogueItem = async (slug: string): Promise<CatalogueMinkeWhale | null> => {
const { items } = await contentfulDeliveryClient.getEntries<ContentTypeCatalogueMinkeWhale>({
content_type: ContentTypes.CatalogueMinkeWhale,
limit: 1,
"fields.slug": slug,
});
if (!items.length) {
return null;
}
const [entry] = items;
const data = {
entry: {
id: entry.fields.id,
reference: entry.fields?.reference ?? null,
name: entry.fields?.name ?? null,
slug: entry.fields.slug,
totalRecaptures: entry.fields?.totalRecaptures ?? null,
yearsRecaptured: entry.fields?.yearsRecaptured ?? null,
leftDorsalFin: entry.fields?.leftDorsalFin
? flattenImageAssetFields(entry.fields.leftDorsalFin)
: null,
rightDorsalFin: entry.fields?.rightDorsalFin
? flattenImageAssetFields(entry.fields.rightDorsalFin)
: null,
lastUpdated: entry.sys.updatedAt,
},
previous: entry.fields.previousEntry ? reduceCatalogueItem(entry.fields.previousEntry) : null,
next: entry.fields.nextEntry ? reduceCatalogueItem(entry.fields.nextEntry) : null,
};
return data;
};
/**
* Gets slug of minke whale entry by ID.
* @param id Entry `id` field value.
* @returns Bottlenose dolphin catalogue entry slug.
*/
const getMinkeWhaleItemEntrySlug = async (
id: CatalogueMinkeWhale["entry"]["id"],
): Promise<CatalogueMinkeWhale["entry"]["slug"] | null> => {
const { items } = await contentfulDeliveryClient.getEntries<ContentTypeCatalogueMinkeWhale>({
content_type: ContentTypes.CatalogueMinkeWhale,
limit: 1,
"fields.id": id,
});
if (!items.length) {
return null;
}
const [entry] = items;
return entry.fields.slug;
};
export {
getCatalogueList,
getBottlenoseDolphinCatalogueItem,
getBottlenoseDolphinItemEntrySlug,
getMinkeWhaleCatalogueItem,
getMinkeWhaleItemEntrySlug,
};