690 files changed
+12045
-33943
No files matched your search
@@ -0,0 +1,86 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { GalleryPlacement } from "@/generated/prisma/enums";
|
||||
import {
|
||||
addGalleryImage,
|
||||
deleteGalleryImage,
|
||||
listGalleryImages,
|
||||
reorderGalleryImages,
|
||||
updateGalleryImage,
|
||||
} from "@/services/gallery/gallery.service";
|
||||
import { cleanupTestBusiness, createTestBusiness, prisma } from "@/services/scheduling/__tests__/fixtures";
|
||||
import type { TestBusiness } from "@/services/scheduling/__tests__/fixtures";
|
||||
|
||||
describe("Gallery service (integration)", () => {
|
||||
let business: TestBusiness;
|
||||
|
||||
beforeEach(async () => {
|
||||
business = await createTestBusiness();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await cleanupTestBusiness(business);
|
||||
});
|
||||
|
||||
it("keeps HERO and PORTFOLIO images in separate lists for the same business", async () => {
|
||||
await addGalleryImage(prisma, { businessId: business.businessId, placement: GalleryPlacement.HERO, imageUrl: "/media/gallery/hero-1.png" });
|
||||
await addGalleryImage(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, imageUrl: "/media/gallery/portfolio-1.png", category: "Fades" });
|
||||
|
||||
const hero = await listGalleryImages(prisma, { businessId: business.businessId, placement: GalleryPlacement.HERO });
|
||||
const portfolio = await listGalleryImages(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO });
|
||||
|
||||
expect(hero).toHaveLength(1);
|
||||
expect(portfolio).toHaveLength(1);
|
||||
expect(portfolio[0]?.category).toBe("Fades");
|
||||
});
|
||||
|
||||
it("assigns increasing displayOrder on add, and reorder fully replaces it", async () => {
|
||||
const a = await addGalleryImage(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, imageUrl: "/media/a.png" });
|
||||
const b = await addGalleryImage(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, imageUrl: "/media/b.png" });
|
||||
expect(a.displayOrder).toBe(0);
|
||||
expect(b.displayOrder).toBe(1);
|
||||
|
||||
await reorderGalleryImages(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, orderedIds: [b.id, a.id] });
|
||||
|
||||
const reordered = await listGalleryImages(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO });
|
||||
expect(reordered.map((i) => i.id)).toEqual([b.id, a.id]);
|
||||
});
|
||||
|
||||
it("updates category/caption/isActive, and activeOnly filtering respects isActive", async () => {
|
||||
const image = await addGalleryImage(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, imageUrl: "/media/a.png" });
|
||||
await updateGalleryImage(prisma, { id: image.id, businessId: business.businessId, category: "Beards", caption: "Client X", isActive: false });
|
||||
|
||||
const all = await listGalleryImages(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO });
|
||||
expect(all[0]?.category).toBe("Beards");
|
||||
expect(all[0]?.caption).toBe("Client X");
|
||||
expect(all[0]?.isActive).toBe(false);
|
||||
|
||||
const activeOnly = await listGalleryImages(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, activeOnly: true });
|
||||
expect(activeOnly).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("never lets one business update or delete another business's gallery image", async () => {
|
||||
const otherBusiness = await createTestBusiness();
|
||||
try {
|
||||
const image = await addGalleryImage(prisma, { businessId: otherBusiness.businessId, placement: GalleryPlacement.PORTFOLIO, imageUrl: "/media/other.png" });
|
||||
|
||||
const updateResult = await updateGalleryImage(prisma, { id: image.id, businessId: business.businessId, caption: "hijacked" });
|
||||
expect(updateResult.success).toBe(false);
|
||||
|
||||
const deleteResult = await deleteGalleryImage(prisma, { id: image.id, businessId: business.businessId });
|
||||
expect(deleteResult.success).toBe(false);
|
||||
|
||||
const stillThere = await prisma.businessGalleryImage.findUnique({ where: { id: image.id } });
|
||||
expect(stillThere).not.toBeNull();
|
||||
expect(stillThere?.caption).not.toBe("hijacked");
|
||||
} finally {
|
||||
await cleanupTestBusiness(otherBusiness);
|
||||
}
|
||||
});
|
||||
|
||||
it("deletes an image it owns", async () => {
|
||||
const image = await addGalleryImage(prisma, { businessId: business.businessId, placement: GalleryPlacement.PORTFOLIO, imageUrl: "/media/a.png" });
|
||||
const result = await deleteGalleryImage(prisma, { id: image.id, businessId: business.businessId });
|
||||
expect(result.success).toBe(true);
|
||||
expect(await prisma.businessGalleryImage.findUnique({ where: { id: image.id } })).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
import { GalleryPlacement } from "@/generated/prisma/enums";
|
||||
|
||||
/**
|
||||
* Owner-managed image galleries — one model/service backs both the Home
|
||||
* page hero carousel (placement: HERO) and the Portfolio tab (placement:
|
||||
* PORTFOLIO) rather than two near-duplicate "gallery" features. See
|
||||
* docs/phase-9-summary.md, "Home Screen Redesign".
|
||||
*/
|
||||
|
||||
export interface GalleryImageSummary {
|
||||
id: string;
|
||||
imageUrl: string;
|
||||
category: string | null;
|
||||
caption: string | null;
|
||||
displayOrder: number;
|
||||
isActive: boolean;
|
||||
}
|
||||
|
||||
export async function listGalleryImages(
|
||||
prisma: PrismaClient,
|
||||
params: { businessId: string; placement: GalleryPlacement; activeOnly?: boolean },
|
||||
): Promise<GalleryImageSummary[]> {
|
||||
const images = await prisma.businessGalleryImage.findMany({
|
||||
where: {
|
||||
businessId: params.businessId,
|
||||
placement: params.placement,
|
||||
...(params.activeOnly ? { isActive: true } : {}),
|
||||
},
|
||||
orderBy: { displayOrder: "asc" },
|
||||
});
|
||||
return images.map((i) => ({
|
||||
id: i.id,
|
||||
imageUrl: i.imageUrl,
|
||||
category: i.category,
|
||||
caption: i.caption,
|
||||
displayOrder: i.displayOrder,
|
||||
isActive: i.isActive,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function addGalleryImage(
|
||||
prisma: PrismaClient,
|
||||
params: { businessId: string; placement: GalleryPlacement; imageUrl: string; mediaAssetId?: string; category?: string },
|
||||
): Promise<GalleryImageSummary> {
|
||||
const maxOrder = await prisma.businessGalleryImage.aggregate({
|
||||
where: { businessId: params.businessId, placement: params.placement },
|
||||
_max: { displayOrder: true },
|
||||
});
|
||||
|
||||
const image = await prisma.businessGalleryImage.create({
|
||||
data: {
|
||||
businessId: params.businessId,
|
||||
placement: params.placement,
|
||||
imageUrl: params.imageUrl,
|
||||
mediaAssetId: params.mediaAssetId,
|
||||
category: params.category?.trim() || null,
|
||||
displayOrder: (maxOrder._max.displayOrder ?? -1) + 1,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
id: image.id,
|
||||
imageUrl: image.imageUrl,
|
||||
category: image.category,
|
||||
caption: image.caption,
|
||||
displayOrder: image.displayOrder,
|
||||
isActive: image.isActive,
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateGalleryImage(
|
||||
prisma: PrismaClient,
|
||||
params: { id: string; businessId: string; category?: string | null; caption?: string | null; isActive?: boolean },
|
||||
): Promise<{ success: boolean }> {
|
||||
const result = await prisma.businessGalleryImage.updateMany({
|
||||
where: { id: params.id, businessId: params.businessId },
|
||||
data: {
|
||||
...(params.category !== undefined ? { category: params.category?.trim() || null } : {}),
|
||||
...(params.caption !== undefined ? { caption: params.caption?.trim() || null } : {}),
|
||||
...(params.isActive !== undefined ? { isActive: params.isActive } : {}),
|
||||
},
|
||||
});
|
||||
return { success: result.count > 0 };
|
||||
}
|
||||
|
||||
export async function deleteGalleryImage(
|
||||
prisma: PrismaClient,
|
||||
params: { id: string; businessId: string },
|
||||
): Promise<{ success: boolean }> {
|
||||
const result = await prisma.businessGalleryImage.deleteMany({ where: { id: params.id, businessId: params.businessId } });
|
||||
return { success: result.count > 0 };
|
||||
}
|
||||
|
||||
/** Full replacement of display order — same "send the whole ordered id list" pattern as reorderCategories/reorderServices in service-catalogue.service.ts. */
|
||||
export async function reorderGalleryImages(
|
||||
prisma: PrismaClient,
|
||||
params: { businessId: string; placement: GalleryPlacement; orderedIds: string[] },
|
||||
): Promise<void> {
|
||||
await prisma.$transaction(
|
||||
params.orderedIds.map((id, index) =>
|
||||
prisma.businessGalleryImage.updateMany({
|
||||
where: { id, businessId: params.businessId, placement: params.placement },
|
||||
data: { displayOrder: index },
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user