Render adoption reports in worker
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import path from 'path';
|
||||
|
||||
import {
|
||||
getBirdById,
|
||||
listVetVisitsForBird,
|
||||
listWeightsForBird,
|
||||
} from '../repositories/birdRepository.js';
|
||||
import { listFlockNotes } from '../repositories/auditRepository.js';
|
||||
import { getS3ImageStorageConfig } from '../storage/imageStorageConfig.js';
|
||||
import { getSignedS3ObjectUrl } from '../storage/s3Client.js';
|
||||
import type { BirdRow } from '../types.js';
|
||||
import { renderAdoptionReportPdf } from './adoptionReport.js';
|
||||
|
||||
const adoptionReportWeightHistoryDays = 425;
|
||||
|
||||
const loadBirdReportPhotoBuffer = async (bird: BirdRow) => {
|
||||
if (!bird.photo_object_key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const s3Config = getS3ImageStorageConfig();
|
||||
|
||||
if (!s3Config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const signedUrl = getSignedS3ObjectUrl({
|
||||
config: s3Config,
|
||||
objectKey: bird.photo_object_key,
|
||||
expiresInSeconds: 5 * 60,
|
||||
});
|
||||
const imageResponse = await fetch(signedUrl);
|
||||
|
||||
if (!imageResponse.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentType = imageResponse.headers.get('content-type') || bird.photo_content_type || '';
|
||||
|
||||
if (!/^image\/(?:png|jpe?g)$/i.test(contentType)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Buffer.from(await imageResponse.arrayBuffer());
|
||||
};
|
||||
|
||||
export const renderAdoptionReportForBird = async ({
|
||||
birdId,
|
||||
workspaceId,
|
||||
transferCode,
|
||||
printFriendly,
|
||||
}: {
|
||||
birdId: string;
|
||||
workspaceId: number;
|
||||
transferCode: string;
|
||||
printFriendly: boolean;
|
||||
}) => {
|
||||
const bird = await getBirdById(birdId, workspaceId);
|
||||
|
||||
if (!bird) {
|
||||
throw new Error('Bird not found.');
|
||||
}
|
||||
|
||||
const [weights, vetVisits, notes, birdPhotoBuffer] = await Promise.all([
|
||||
listWeightsForBird(bird.id, workspaceId, adoptionReportWeightHistoryDays),
|
||||
listVetVisitsForBird(bird.id, workspaceId),
|
||||
listFlockNotes(workspaceId),
|
||||
loadBirdReportPhotoBuffer(bird),
|
||||
]);
|
||||
const birdNotes = notes.filter((note) => note.bird_id === bird.id);
|
||||
|
||||
return renderAdoptionReportPdf({
|
||||
bird,
|
||||
weights,
|
||||
vetVisits,
|
||||
notes: birdNotes,
|
||||
transferCode,
|
||||
birdPhotoBuffer,
|
||||
printFriendly,
|
||||
assets: {
|
||||
logoPath: path.join(process.cwd(), 'assets', 'flockpal-logo.png'),
|
||||
wordmarkPath: path.join(process.cwd(), 'assets', 'flockpal-text.png'),
|
||||
defaultBirdPhotoPath: path.join(process.cwd(), 'assets', 'yoda-default.png'),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user