-
Notifications
You must be signed in to change notification settings - Fork 7
/
landingPages.js
58 lines (50 loc) · 1.69 KB
/
landingPages.js
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
import { respond } from '@tupaia/utils';
import { buildProjectDataForFrontend } from './projects';
async function buildLandingPageDataForFrontend(landingPage, req) {
const {
name,
image_url: imageUrl,
logo_url: logoUrl,
primary_hexcode: primaryHexcode,
secondary_hexcode: secondaryHexcode,
long_bio: longBio,
contact_us: contactUs,
external_link: externalLink,
phone_number: phoneNumber,
website_url: websiteUrl,
include_name_in_header: includeNameInHeader,
extended_title: extendedTitle,
project_codes: projectCodes,
} = landingPage;
// Fetch all the projects with their entity_ids
const allProjects = await req.models.project.getAllProjectDetails();
// Filter to only the projects that are included in the landing page
const applicableProjects = allProjects.filter(project => projectCodes.includes(project.code));
// Build the project data for the frontend, using the method from the projects API, so that we get the same information returned, including permissions
const projects = await Promise.all(
applicableProjects.map(project => buildProjectDataForFrontend(project, req)),
);
return {
name,
imageUrl,
logoUrl,
primaryHexcode,
secondaryHexcode,
longBio,
contactUs,
externalLink,
phoneNumber,
websiteUrl,
includeNameInHeader,
extendedTitle,
projects,
};
}
export async function getLandingPage(req, res) {
const { landingPageUrl } = req.params;
const landingPage = await req.models.landingPage.findOne({ url_segment: landingPageUrl });
if (!landingPage) {
return respond(res, null, 200);
}
return respond(res, await buildLandingPageDataForFrontend(landingPage, req), 200);
}