All files / routes/internal/website-build build-helpers.ts

100% Statements 42/42
100% Branches 26/26
100% Functions 15/15
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              19x                 17x   16x                             2x                 2x                   2x                                           21x         21x     21x         13x     13x   13x 1x       12x 12x     12x 15x                   3x       12x     14x 14x     12x       12x 2x 2x 2x 1x         14x                                                               14x             14x   11x 6x   5x 5x   6x 6x     5x       14x    
// Shared helpers for internal website-build routes
import type { Dal } from "../../../dal";
 
/**
 * Build an aggregated company profile for pro-sites consumption.
 * Maps DB field names to the pro-sites CompanyProfile type:
 *   - establishedYear → foundedYear
 *   - proLeadership → teamMembers
 *   - cert.title → cert.name, cert.issuer → cert.issuedBy
 *   - adds testimonials array
 */
export async function buildAggregatedCompanyProfile(
	dal: Dal,
	proId: string,
) {
	const [pro, leadership, certifications, testimonials] = await Promise.all(
		[
			dal.pros.findById(proId),
			dal.companyProfile.getLeadership(proId),
			dal.companyProfile.getCertifications(proId),
			dal.companyProfile.getTestimonials(proId),
		],
	);
 
	if (!pro) return null;
 
	return {
		id: pro.id,
		proId: pro.id,
		tagline: pro.tagline,
		foundedYear: pro.establishedYear,
		processDescription: pro.processDescription ?? null,
		socialLinks: {
			website: pro.websiteUrl,
			instagram: pro.instagramHandle,
			facebook: pro.facebookUrl,
			linkedin: pro.linkedinUrl,
			youtube: pro.youtubeUrl,
		},
		teamMembers:
			leadership.length > 0
				? leadership.map((l) => ({
						name: l.name,
						role: l.role,
						photoUrl: l.photoUrl,
						bio: l.bio,
					}))
				: null,
		certifications:
			certifications.length > 0
				? certifications.map((c) => ({
						name: c.title,
						type: c.type,
						issuedBy: c.issuer,
						year: c.year,
						imageUrl: c.imageUrl,
					}))
				: null,
		testimonials:
			testimonials.length > 0
				? testimonials.map((t) => ({
						customerName: t.customerName,
						customerLocation: t.customerLocation,
						projectType: t.projectType,
						reviewText: t.reviewText,
						rating: t.rating,
					}))
				: null,
		awardsAndRecognition: null,
	};
}
 
/**
 * Fetch all published blogs associated with a pro.
 * A blog is associated with a pro if:
 *   1. The pro is linked via blogPros with attributionType "author", OR
 *   2. The blog's ideaSourceProId matches the pro's ID
 *
 * Only returns published blogs, ordered by publishedAt descending.
 */
export async function fetchProBlogs(dal: Dal, proId: string) {
	// Find blog IDs where the pro is an author via blogPros
	const authorLinks = await dal.blogPros.findAll(
		{ proId, attributionType: "author" },
		0,
		100,
	);
	const authorBlogIds = authorLinks.map((link) => link.blogId);
 
	// Find blogs where ideaSourceProId matches
	const ideaSourceBlogs = await dal.blogs.findAll(
		{ ideaSourceProId: proId, status: "published" },
		0,
		100,
	);
	const ideaSourceBlogIds = ideaSourceBlogs.map((b) => b.id);
 
	// Combine unique blog IDs
	const allBlogIds = [...new Set([...authorBlogIds, ...ideaSourceBlogIds])];
 
	if (allBlogIds.length === 0) {
		return [];
	}
 
	// Fetch full blog data for all matching IDs (returns Map<id, Blog>)
	const allBlogsMap = await dal.blogs.findByIds(allBlogIds);
	const allBlogs = [...allBlogsMap.values()];
 
	// Filter to published only and map to pro-sites BlogPost shape
	const publishedBlogs = allBlogs
		.filter((blog) => blog.status === "published")
		.sort((a, b) => {
			/* v8 ignore start -- V8 artifact: ternary false branch; published blogs always have publishedAt */
			const aTime = a.publishedAt
				? (a.publishedAt instanceof Date ? a.publishedAt.getTime() : new Date(a.publishedAt).getTime())
				: 0;
			const bTime = b.publishedAt
				? (b.publishedAt instanceof Date ? b.publishedAt.getTime() : new Date(b.publishedAt).getTime())
				: 0;
			/* v8 ignore stop */
			return bTime - aTime;
		});
 
	// Fetch categories for blogs that have categoryId
	const categoryIds = [
		...new Set(
			publishedBlogs
				.map((b) => b.categoryId)
				.filter((id): id is string => !!id),
		),
	];
	const categoriesMap = new Map<
		string,
		{ name: string; slug: string }
	>();
	if (categoryIds.length > 0) {
		for (const catId of categoryIds) {
			const cat = await dal.blogCategories.findById(catId);
			if (cat) {
				categoriesMap.set(catId, { name: cat.name, slug: cat.slug });
			}
		}
	}
 
	return publishedBlogs.map((blog) => ({
		id: blog.id,
		slug: blog.slug,
		title: blog.title,
		metaDescription: blog.metaDescription ?? null,
		content: blog.content,
		/* v8 ignore start -- V8 artifact: ?? null fallback */
		featuredImageUrl: blog.featuredImageUrl ?? null,
		featuredImageAlt: blog.featuredImageAlt ?? null,
		/* v8 ignore stop */
		/* v8 ignore start -- V8 artifact: ternary false branch */
		publishedAt: blog.publishedAt
			? (blog.publishedAt instanceof Date
					? blog.publishedAt.toISOString()
					: new Date(blog.publishedAt).toISOString())
			: null,
		/* v8 ignore stop */
		readTimeMinutes: blog.readTimeMinutes ?? null,
		category: blog.categoryId
			? categoriesMap.get(blog.categoryId) ?? null
			: null,
	}));
}
 
/**
 * Fetch all published projects for a pro with their photos, and optionally
 * rooms + media when the project uses the room-based layout.
 *
 * This logic was previously duplicated between /:jobId/pro-data and
 * /preview/:slug — extracting it here removes that duplication.
 */
export async function fetchProjectsWithDetails(dal: Dal, proId: string) {
	const projects = await dal.projects.findAll(
		{ proId, status: "published" },
		0,
		100,
	);
 
	// Attach rooms + media for projects that use the room-based layout
	const projectsWithRooms = await Promise.all(
		projects.map(async (project) => {
			if (!project.useRooms) {
				return { ...project, photos: [] };
			}
			const rooms = await dal.rooms.findByProjectId(project.id);
			const roomsWithMedia = await Promise.all(
				rooms.map(async (room) => {
					const media = await dal.media.findByRoomId(room.id);
					return { ...room, media };
				}),
			);
			return { ...project, photos: [], rooms: roomsWithMedia };
		}),
	);
 
	return projectsWithRooms;
}