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 | 36x 3x 3x 3x 3x 16x 16x 2x 14x 2x 2x 1x 1x 1x 5x 5x 4x 1x 3x 3x 1x 3x 3x 5x 3x 12x 11x 11x 12x 12x 6x 6x 1x 11x 11x 1x 10x 5x 5x 5x 2x 2x 2x 2x 10x 2x 2x 1x 1x 2x 2x 1x 1x 3x 3x 3x 2x 2x 1x 2x 1x 1x | // Project Service - Business Logic Layer
import type { Dal } from "../dal";
import type { Project } from "../db/schema";
import type { ProjectFilters } from "../dal/projects.dal";
import { NotFoundError, ForbiddenError, ValidationError } from "../lib/errors";
import { generateId, generateSlug, generateUniqueSlug } from "../lib/utils";
import {
generateRoomSlug,
generateRoomSlugWithSuffix,
} from "../lib/room-slug";
import type { CreateProjectInput, UpdateProjectInput } from "./project/types";
import {
validateProjectInput,
validateUpdateInput,
} from "./project/validation";
// Re-export types for backward compatibility
export type { CreateProjectInput, UpdateProjectInput } from "./project/types";
export class ProjectService {
constructor(private dal: Dal) {}
async list(
filters: ProjectFilters,
page: number,
limit: number,
): Promise<{ projects: Project[]; total: number }> {
const offset = (page - 1) * limit;
const { includePhotoCount: _ignored, ...countFilters } = filters;
const [projects, total] = await Promise.all([
this.dal.projects.findAll(filters, offset, limit),
this.dal.projects.count(countFilters),
]);
return { projects, total };
}
async getById(id: string): Promise<Project> {
const project = await this.dal.projects.findById(id);
if (!project) {
throw new NotFoundError("Project", id);
}
return project;
}
async getBySlug(slug: string): Promise<Project> {
const project = await this.dal.projects.findBySlug(slug);
if (!project) {
throw new NotFoundError("Project");
}
return project;
}
async getByProId(proId: string): Promise<Project[]> {
return this.dal.projects.findByProId(proId);
}
async create(input: CreateProjectInput, userId: string): Promise<Project> {
validateProjectInput(input);
// Verify pro exists
const pro = await this.dal.pros.findById(input.proId);
if (!pro) {
throw new NotFoundError("Pro", input.proId);
}
// Generate unique slug
let slug = generateSlug(input.title);
if (await this.dal.projects.slugExists(slug)) {
slug = generateUniqueSlug(input.title);
}
const projectId = generateId();
const useRooms = input.useRooms ?? false;
const project = await this.dal.projects.create({
id: projectId,
proId: input.proId,
title: input.title,
slug,
description: input.description,
status: (input.status as Project["status"]) ?? "draft",
sort: input.sort ?? 0,
// New portfolio fields
scope: input.scope as Project["scope"],
workedAreaIds: input.workedAreaIds,
localityId: input.localityId,
propertyType: input.propertyType as Project["propertyType"],
propertySize: input.propertySize,
projectAreaSqft: input.projectAreaSqft,
budgetRange: input.budgetRange as Project["budgetRange"],
duration: input.duration,
materialTagIds: input.materialTagIds,
isBeforeAfter: input.isBeforeAfter ?? false,
clientTestimonial: input.clientTestimonial,
yearCompleted: input.yearCompleted,
useRooms,
userCreated: userId,
userUpdated: userId,
});
return project;
}
async update(
id: string,
input: UpdateProjectInput,
userId: string,
): Promise<Project> {
// Verify project exists and get current values
const project = await this.getById(id);
// Validate update input
validateUpdateInput(input);
// If title changed, update slug
const titleChanged = !!(input.title && input.title !== project.title);
let slug = project.slug;
if (titleChanged) {
slug = generateSlug(input.title as string);
if (await this.dal.projects.slugExists(slug, id)) {
slug = generateUniqueSlug(input.title as string);
}
}
// Use save() so the quality score recomputes after content changes (1B).
const updated = await this.dal.projects.save(id, {
...input,
slug,
status: input.status as Project["status"],
propertyType: input.propertyType as Project["propertyType"],
budgetRange: input.budgetRange as Project["budgetRange"],
scope: input.scope as Project["scope"],
userUpdated: userId,
});
if (!updated) {
throw new NotFoundError("Project", id);
}
// Note: useRooms is a display-only flag.
// Regenerate room slugs when project title changes
if (titleChanged && input.title) {
const newTitle = input.title;
const rooms = await this.dal.rooms.findByProjectId(id);
await Promise.all(
rooms.map(async (room) => {
const roomType = await this.dal.roomTypes.findByCode(room.roomType);
const roomTypeDisplayName = roomType?.displayName ?? room.roomType;
let roomSlug = generateRoomSlug(roomTypeDisplayName, newTitle);
/* v8 ignore start -- defensive guard: slug collision unlikely */
if (await this.dal.rooms.slugExists(roomSlug, room.id)) {
/* v8 ignore stop */
roomSlug = generateRoomSlugWithSuffix(
roomTypeDisplayName,
newTitle,
);
}
await this.dal.rooms.update(room.id, { slug: roomSlug });
}),
);
}
return updated;
}
async delete(id: string): Promise<void> {
const exists = await this.dal.projects.findById(id);
if (!exists) {
throw new NotFoundError("Project", id);
}
// Photos will cascade delete due to FK
await this.dal.projects.delete(id);
}
async verifyProOwnership(
projectId: string,
proId: string,
): Promise<Project> {
const project = await this.getById(projectId);
if (project.proId !== proId) {
throw new ForbiddenError("Project does not belong to this pro");
}
return project;
}
async publish(id: string, userId: string): Promise<Project> {
// Check rooms media
const rooms = await this.dal.rooms.findByProjectId(id);
const roomIds = rooms.map((r) => r.id);
if (roomIds.length > 0) {
const media = await this.dal.media.findByRoomIds(roomIds);
if (media.length > 0) {
return this.update(id, { status: "published" }, userId);
}
}
throw new ValidationError(
"Cannot publish a project without at least one photo",
);
}
async archive(id: string, userId: string): Promise<Project> {
return this.update(id, { status: "archived" }, userId);
}
async unarchive(id: string, userId: string): Promise<Project> {
return this.update(id, { status: "draft" }, userId);
}
/**
* Increment view count for a project (called from marketplace API)
*/
async incrementViewCount(id: string): Promise<void> {
await this.dal.projects.incrementViewCount(id);
}
}
|