All files / services entity-creator.ts

100% Statements 70/70
100% Branches 45/45
100% Functions 11/11
100% Lines 70/70

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 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                                90x             34x   3x   2x   3x   8x     3x   2x   4x   4x   4x   1x                 3x                           2x 2x 1x     1x           3x 1x         2x 2x 1x     1x     1x                                     8x 8x 1x     7x 7x 1x     6x 6x 8x 6x   6x                       3x 3x 1x     2x           2x 2x 1x     1x           4x 1x         3x 3x 1x         2x 2x 1x     1x           4x 1x         3x 3x 1x         2x 2x 1x     1x           4x 1x         3x 3x 1x           2x 2x 1x     1x          
// EntityCreator - Maps upload context to entity creation
// Creates the appropriate database entity when an upload is confirmed
import type { Dal } from "../dal";
import type { Upload } from "../db/schema";
import { NotFoundError, ValidationError } from "../lib/errors";
 
/**
 * EntityCreator handles creating the appropriate entity record
 * based on the upload context type.
 *
 * For example:
 * - "project-photo" context creates a project_photos row
 * - "blog-cover" context updates the blog's featuredImageUrl
 * - "room-media" context creates a media row
 */
export class EntityCreator {
	constructor(private dal: Dal) {}
 
	/**
	 * Create the entity for a confirmed upload.
	 * Dispatches to the appropriate handler based on upload.context.
	 */
	async createEntity(upload: Upload): Promise<void> {
		switch (upload.context) {
			case "project-photo":
				return this.createProjectPhoto(upload);
			case "blog-cover":
				return this.updateBlogCover(upload);
			case "blog-image":
				return this.createBlogImage(upload);
			case "room-media":
				return this.createRoomMedia(upload);
			case "profile":
			case "logo":
				return this.updateProLogo(upload);
			case "cover":
				return this.updateProCover(upload);
			case "leadership":
				return this.updateLeadershipPhoto(upload);
			case "certification":
				return this.updateCertificationImage(upload);
			case "testimonial":
				return this.updateTestimonialAvatar(upload);
			default:
				throw new ValidationError(
					`Unknown upload context: ${upload.context}`,
				);
		}
	}
 
	private async createProjectPhoto(_upload: Upload): Promise<void> {
		// Legacy project-photo uploads are no longer supported.
		// project_photos table has been removed. Use room-media context instead.
		throw new ValidationError(
			"Legacy project-photo uploads are no longer supported. Use room-media context instead.",
		);
	}
 
	private async updateBlogCover(upload: Upload): Promise<void> {
		/* v8 ignore start -- defensive guard: contextId validated by caller */
		if (!upload.contextId) {
			throw new ValidationError(
				"contextId (blogId) is required for blog-cover uploads",
			);
		}
		/* v8 ignore stop */
 
		const blog = await this.dal.blogs.findById(upload.contextId);
		if (!blog) {
			throw new NotFoundError("Blog", upload.contextId);
		}
 
		await this.dal.blogs.update(upload.contextId, {
			featuredImageUrl: upload.storageKey,
		});
	}
 
	private async createBlogImage(upload: Upload): Promise<void> {
		if (!upload.contextId) {
			throw new ValidationError(
				"contextId (blogId) is required for blog-image uploads",
			);
		}
 
		const blog = await this.dal.blogs.findById(upload.contextId);
		if (!blog) {
			throw new NotFoundError("Blog", upload.contextId);
		}
 
		const sortOrder = await this.dal.blogImages.getNextSortOrder(
			upload.contextId,
		);
		await this.dal.blogImages.create({
			blogId: upload.contextId,
			sourceType: "upload",
			storageKey: upload.storageKey,
			altText: upload.fileName,
			fileSize: upload.fileSize,
			sortOrder,
		});
	}
 
	private async createRoomMedia(upload: Upload): Promise<void> {
		/* v8 ignore start -- defensive guard: contextId validated by caller */
		if (!upload.contextId) {
			throw new ValidationError(
				"contextId (roomId) is required for room-media uploads",
			);
		}
		/* v8 ignore stop */
 
		const roomId = Number(upload.contextId);
		if (Number.isNaN(roomId)) {
			throw new ValidationError("contextId must be a numeric roomId");
		}
 
		const room = await this.dal.rooms.findById(roomId);
		if (!room) {
			throw new NotFoundError("Room", upload.contextId);
		}
 
		const isImage = upload.contentType.startsWith("image/");
		const mediaType = isImage ? "image" : "video";
		const maxSort = await this.dal.media.getMaxSortOrder(roomId);
		const mediaCount = await this.dal.media.countByRoomId(roomId);
 
		await this.dal.media.create({
			roomId,
			mediaType,
			filename: upload.fileName,
			storageKey: upload.storageKey,
			fileSize: upload.fileSize,
			sortOrder: maxSort + 1,
			isCover: mediaCount === 0,
		});
	}
 
	private async updateProLogo(upload: Upload): Promise<void> {
		const pro = await this.dal.pros.findById(upload.proId);
		if (!pro) {
			throw new NotFoundError("Pro", upload.proId);
		}
 
		await this.dal.pros.update(upload.proId, {
			logoUrl: upload.storageKey,
		});
	}
 
	private async updateProCover(upload: Upload): Promise<void> {
		const pro = await this.dal.pros.findById(upload.proId);
		if (!pro) {
			throw new NotFoundError("Pro", upload.proId);
		}
 
		await this.dal.pros.update(upload.proId, {
			coverImage: upload.storageKey,
		});
	}
 
	private async updateLeadershipPhoto(upload: Upload): Promise<void> {
		if (!upload.contextId) {
			throw new ValidationError(
				"contextId (leadershipId) is required for leadership uploads",
			);
		}
 
		const id = Number(upload.contextId);
		if (Number.isNaN(id)) {
			throw new ValidationError(
				"contextId must be a numeric leadershipId",
			);
		}
 
		const member = await this.dal.companyProfile.getLeadershipById(id);
		if (!member) {
			throw new NotFoundError("Leadership member", upload.contextId);
		}
 
		await this.dal.companyProfile.updateLeadership(id, {
			photoUrl: upload.storageKey,
		});
	}
 
	private async updateCertificationImage(upload: Upload): Promise<void> {
		if (!upload.contextId) {
			throw new ValidationError(
				"contextId (certificationId) is required for certification uploads",
			);
		}
 
		const id = Number(upload.contextId);
		if (Number.isNaN(id)) {
			throw new ValidationError(
				"contextId must be a numeric certificationId",
			);
		}
 
		const cert = await this.dal.companyProfile.getCertificationById(id);
		if (!cert) {
			throw new NotFoundError("Certification", upload.contextId);
		}
 
		await this.dal.companyProfile.updateCertification(id, {
			imageUrl: upload.storageKey,
		});
	}
 
	private async updateTestimonialAvatar(upload: Upload): Promise<void> {
		if (!upload.contextId) {
			throw new ValidationError(
				"contextId (testimonialId) is required for testimonial uploads",
			);
		}
 
		const id = Number(upload.contextId);
		if (Number.isNaN(id)) {
			throw new ValidationError(
				"contextId must be a numeric testimonialId",
			);
		}
 
		const testimonial =
			await this.dal.companyProfile.getTestimonialById(id);
		if (!testimonial) {
			throw new NotFoundError("Testimonial", upload.contextId);
		}
 
		await this.dal.companyProfile.updateTestimonial(id, {
			photoUrl: upload.storageKey,
		});
	}
}