integratedhub/routes/api/retrieve/property_media.ts
2026-01-20 09:02:26 -06:00

96 lines
2.7 KiB
TypeScript

// routes/api/retrieve/property_media.ts
import { define } from "../../../utils.ts";
import {
getDocumentsByProperty,
getMediaByProperty,
getMediaByPropertyAndType,
getMediaCountByProperty,
getPhotosByProperty,
getPrimaryPhoto,
} from "../../../services/propertyMediaService.ts";
import { SessionCookie } from "../../auth/callback.ts";
export const handler = define.handlers({
async GET(ctx) {
try {
const stateData: SessionCookie = ctx.state.data;
const url = new URL(ctx.req.url);
const property_id = url.searchParams.get("property_id");
const media_type = url.searchParams.get("media_type");
const primary_only = url.searchParams.get("primary_only") === "true";
const count_only = url.searchParams.get("count_only") === "true";
if (!property_id) {
return new Response(
JSON.stringify({
error: "Property ID is required",
}),
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
let result;
if (count_only) {
// Get media count statistics
result = await getMediaCountByProperty(property_id);
return new Response(
JSON.stringify({
success: true,
count: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else if (primary_only) {
// Get primary photo only
result = await getPrimaryPhoto(property_id);
return new Response(
JSON.stringify({
success: true,
primary_photo: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} else if (media_type) {
// Get media by specific type
if (media_type === "photo") {
result = await getPhotosByProperty(property_id);
} else if (media_type === "document") {
result = await getDocumentsByProperty(property_id);
} else {
result = await getMediaByPropertyAndType(property_id, media_type);
}
} else {
// Get all media for property
result = await getMediaByProperty(property_id);
}
return new Response(
JSON.stringify({
success: true,
media: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (e) {
return new Response(JSON.stringify({ error: e }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
},
});