Added behavior journal, adjusted layout, and added some colors
This commit is contained in:
1 parent
b1b89ad334
commit
3237cd7601
31 files changed
+2135
-275
No files matched your search
+142
-214
@@ -1,3 +1,43 @@
|
||||
import { unassignedBirdMatchSchema, claimBirdSchema } from './birdMembershipSchemas.js';
|
||||
import { hasUnassignedBird, removeBirdFromFlock, claimUnassignedBird } from './repositories/birdMembershipRepository.js';
|
||||
import { behaviorTypeSchema, behaviorOccurrenceSchema, behaviorRangeSchema } from './behaviorSchemas.js';
|
||||
import { listBehaviorTypes, createBehaviorType, listBehaviorOccurrences, saveBehaviorOccurrence, deleteBehaviorOccurrence } from './repositories/behaviorRepository.js';
|
||||
import {
|
||||
dateStringSchema,
|
||||
chartColorSchema,
|
||||
photoDataUrlSchema,
|
||||
photoUrlSchema,
|
||||
magicLinkRequestSchema,
|
||||
switchWorkspaceSchema,
|
||||
workspaceTypeSchema,
|
||||
workspaceRoleSchema,
|
||||
billingPlanSchema,
|
||||
billingIntervalSchema,
|
||||
integrationTokenScopeSchema,
|
||||
birdGenderSchema,
|
||||
rescueVerificationStatusSchema,
|
||||
rescueOnboardingSchema,
|
||||
workspaceSchema,
|
||||
createWorkspaceSchema,
|
||||
workspaceMemberSchema,
|
||||
flockTransferSchema,
|
||||
lostBirdReportSchema,
|
||||
publicProfileCodeSchema,
|
||||
birdTransferCodeSchema,
|
||||
birdProfileListSchema,
|
||||
verifiedLocationDetailsSchema,
|
||||
locationSearchSchema,
|
||||
birdSchema,
|
||||
memorializeBirdSchema,
|
||||
memorialReminderPreferenceSchema,
|
||||
birdTimelineEventSchema,
|
||||
weightSchema,
|
||||
vetVisitSchema,
|
||||
medicationSchema,
|
||||
medicationAdministrationSchema,
|
||||
flockNoteSchema,
|
||||
} from './schemas.js';
|
||||
import { mountApiDocs } from './docs/router.js';
|
||||
import { buildEmailLayout } from './emails/emailLayout.js';
|
||||
import { buildWeightReminderEmail } from './emails/weightReminderEmail.js';
|
||||
import type { WeightReminder } from './reminders/weightReminders.js';
|
||||
@@ -48,7 +88,6 @@ import {
|
||||
findBirdsByBandId,
|
||||
createVetVisitForBird,
|
||||
createWeightForBird,
|
||||
deleteBird,
|
||||
deleteMedicationForBird,
|
||||
deleteVetVisitForBird,
|
||||
getBirdById,
|
||||
@@ -207,163 +246,6 @@ const allowedOrigins = Array.from(
|
||||
),
|
||||
);
|
||||
|
||||
const dateStringSchema = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);
|
||||
const chartColorSchema = z.string().regex(/^#[0-9a-fA-F]{6}$/);
|
||||
const photoDataUrlSchema = z
|
||||
.string()
|
||||
.regex(/^data:image\/(?:png|jpeg|jpg|webp|gif);base64,[A-Za-z0-9+/=]+$/)
|
||||
.max(1_500_000);
|
||||
const photoUrlSchema = z.string().trim().url().max(2000);
|
||||
|
||||
const magicLinkRequestSchema = z.object({
|
||||
name: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
email: z.string().trim().email().max(255),
|
||||
redirectTo: z.string().trim().url().max(2000).optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const switchWorkspaceSchema = z.object({
|
||||
workspaceId: z.coerce.number().int().positive(),
|
||||
});
|
||||
|
||||
const workspaceTypeSchema = z.enum(['standard', 'rescue']);
|
||||
const workspaceRoleSchema = z.enum(['owner', 'assistant', 'caregiver', 'viewer']);
|
||||
const billingPlanSchema = z.enum(['household_basic', 'household_plus', 'household_macaw', 'household_hyacinth_macaw']);
|
||||
const billingIntervalSchema = z.enum(['monthly', 'yearly']);
|
||||
const integrationTokenScopeSchema = z.enum(['read_only', 'read_write']);
|
||||
const birdGenderSchema = z.enum(['unknown', 'male', 'female', 'male_dna', 'female_dna']);
|
||||
const rescueVerificationStatusSchema = z.enum(['pending', 'approved', 'rejected']);
|
||||
const rescueOnboardingSchema = z.object({
|
||||
name: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
city: z.string().trim().max(120).optional().or(z.literal('')),
|
||||
state: z.string().trim().max(80).optional().or(z.literal('')),
|
||||
ein: z.string().trim().max(32).optional().or(z.literal('')),
|
||||
website: z.string().trim().url().max(2000).optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const workspaceSchema = z.object({
|
||||
name: z.string().trim().min(1).max(160),
|
||||
workspaceType: workspaceTypeSchema,
|
||||
billingEmail: z.string().trim().email().max(255).optional().or(z.literal('')),
|
||||
billingPlan: billingPlanSchema.optional(),
|
||||
billingInterval: billingIntervalSchema.optional(),
|
||||
rescueOnboarding: rescueOnboardingSchema.optional(),
|
||||
});
|
||||
|
||||
const createWorkspaceSchema = z.object({
|
||||
name: z.string().trim().min(1).max(160),
|
||||
workspaceType: workspaceTypeSchema,
|
||||
billingEmail: z.string().trim().email().max(255).optional().or(z.literal('')),
|
||||
billingPlan: billingPlanSchema.optional(),
|
||||
billingInterval: billingIntervalSchema.optional(),
|
||||
rescueOnboarding: rescueOnboardingSchema.optional(),
|
||||
});
|
||||
|
||||
const workspaceMemberSchema = z.object({
|
||||
name: z.string().trim().min(1).max(160),
|
||||
email: z.string().trim().email().max(255),
|
||||
role: workspaceRoleSchema,
|
||||
});
|
||||
|
||||
const flockTransferSchema = z.object({
|
||||
destinationOwnerEmail: z.string().trim().email().max(255),
|
||||
});
|
||||
|
||||
const lostBirdReportSchema = z.object({
|
||||
tagId: z.string().trim().min(1).max(80),
|
||||
finderName: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
finderEmail: z.string().trim().email().max(255).optional().or(z.literal('')),
|
||||
foundLocation: z.string().trim().max(255).optional().or(z.literal('')),
|
||||
message: z.string().trim().max(1000).optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const publicProfileCodeSchema = z.string().trim().regex(/^[A-Za-z0-9_-]{8,32}$/);
|
||||
const birdTransferCodeSchema = z.string().trim().regex(/^[A-Za-z0-9_-]{12,32}$/);
|
||||
const birdProfileListSchema = z
|
||||
.string()
|
||||
.trim()
|
||||
.max(1000)
|
||||
.refine(
|
||||
(value) => value.split(/\r?\n/).map((item) => item.trim()).filter(Boolean).length <= 3,
|
||||
'Use no more than three list items.',
|
||||
)
|
||||
.optional()
|
||||
.or(z.literal(''));
|
||||
|
||||
const verifiedLocationDetailsSchema = z
|
||||
.object({
|
||||
label: z.string().trim().max(220).optional().or(z.literal('')),
|
||||
city: z.string().trim().max(120).optional().or(z.literal('')),
|
||||
region: z.string().trim().max(120).optional().or(z.literal('')),
|
||||
country: z.string().trim().max(120).optional().or(z.literal('')),
|
||||
countryCode: z.string().trim().max(2).optional().or(z.literal('')),
|
||||
latitude: z.coerce.number().min(-90).max(90).optional().nullable().or(z.literal('')),
|
||||
longitude: z.coerce.number().min(-180).max(180).optional().nullable().or(z.literal('')),
|
||||
precision: z.enum(['city', 'region', 'country']).optional(),
|
||||
provider: z.literal('mapbox').optional().nullable(),
|
||||
providerPlaceId: z.string().trim().max(220).optional().or(z.literal('')),
|
||||
})
|
||||
.optional()
|
||||
.nullable();
|
||||
|
||||
const locationSearchSchema = z.object({
|
||||
q: z.string().trim().min(3).max(120),
|
||||
});
|
||||
|
||||
const birdSchema = z.object({
|
||||
name: z.string().trim().min(1).max(120),
|
||||
tagId: z.string().trim().max(80).optional().or(z.literal('')),
|
||||
species: z.string().trim().min(1).max(120),
|
||||
motivators: birdProfileListSchema,
|
||||
demotivators: birdProfileListSchema,
|
||||
favoriteSnack: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
locationLabel: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
locationDetails: verifiedLocationDetailsSchema,
|
||||
vetClinicName: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
vetClinicAddress: z.string().trim().max(500).optional().or(z.literal('')),
|
||||
vetAccountNumber: z.string().trim().max(120).optional().or(z.literal('')),
|
||||
vetDoctorName: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
gender: birdGenderSchema.optional(),
|
||||
hatchDay: dateStringSchema.optional().or(z.literal('')),
|
||||
dateOfBirth: dateStringSchema.optional().or(z.literal('')),
|
||||
gotchaDay: dateStringSchema.optional().or(z.literal('')),
|
||||
chartColor: chartColorSchema.optional(),
|
||||
photoDataUrl: z.union([photoDataUrlSchema, photoUrlSchema, z.literal('')]).optional(),
|
||||
notifyOnDob: z.boolean().optional(),
|
||||
notifyOnGotchaDay: z.boolean().optional(),
|
||||
publicProfileEnabled: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const memorializeBirdSchema = z.object({
|
||||
memorializedOn: dateStringSchema,
|
||||
memorialNote: z.string().trim().max(1000).optional().or(z.literal('')),
|
||||
notifyOnMemorialDay: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const memorialReminderPreferenceSchema = z.object({
|
||||
notifyOnMemorialDay: z.boolean(),
|
||||
});
|
||||
|
||||
const birdTimelineEventSchema = z
|
||||
.object({
|
||||
eventType: z.enum(['location_updated', 'owner_changed', 'manual_note']),
|
||||
eventDate: dateStringSchema.optional().or(z.literal('')),
|
||||
locationLabel: z.string().trim().max(160).optional().or(z.literal('')),
|
||||
locationDetails: verifiedLocationDetailsSchema,
|
||||
note: z.string().trim().max(500).optional().or(z.literal('')),
|
||||
})
|
||||
.refine(
|
||||
(value) =>
|
||||
value.eventType === 'owner_changed' ||
|
||||
Boolean(
|
||||
value.locationLabel?.trim() ||
|
||||
value.note?.trim() ||
|
||||
value.locationDetails?.city?.trim() ||
|
||||
value.locationDetails?.region?.trim() ||
|
||||
value.locationDetails?.country?.trim(),
|
||||
),
|
||||
'Add a location or note for this timeline item.',
|
||||
);
|
||||
|
||||
type VerifiedLocationDetailsInput = z.infer<typeof verifiedLocationDetailsSchema>;
|
||||
|
||||
const normalizeVerifiedLocationDetails = (value: VerifiedLocationDetailsInput) => {
|
||||
@@ -483,57 +365,6 @@ const normalizeMapboxLocationFeature = (feature: MapboxGeocodeFeature): Verified
|
||||
});
|
||||
};
|
||||
|
||||
const weightSchema = z.object({
|
||||
weightGrams: z.coerce.number().positive().max(10000),
|
||||
recordedOn: dateStringSchema,
|
||||
notes: z.string().trim().max(280).optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const vetVisitSchema = z.object({
|
||||
visitedOn: dateStringSchema,
|
||||
clinicName: z.string().trim().min(1).max(160),
|
||||
reason: z.string().trim().min(1).max(160),
|
||||
notes: z.string().trim().max(1000).optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const medicationSchema = z
|
||||
.object({
|
||||
name: z.string().trim().min(1).max(160),
|
||||
dosage: z.string().trim().min(1).max(160),
|
||||
frequency: z.enum(['once_daily', 'twice_daily', 'every_8_hours', 'every_6_hours', 'as_needed']),
|
||||
doseSchedule: z
|
||||
.array(
|
||||
z.object({
|
||||
key: z.string().trim().min(1).max(80),
|
||||
label: z.string().trim().min(1).max(80),
|
||||
time: z.string().trim().regex(/^$|^\d{2}:\d{2}$/),
|
||||
}),
|
||||
)
|
||||
.min(1)
|
||||
.max(8),
|
||||
route: z.string().trim().max(80).optional().or(z.literal('')),
|
||||
startDate: dateStringSchema,
|
||||
endDate: dateStringSchema.optional().or(z.literal('')),
|
||||
notes: z.string().trim().max(1000).optional().or(z.literal('')),
|
||||
remindersEnabled: z.boolean().optional(),
|
||||
})
|
||||
.refine((value) => !value.endDate || value.endDate >= value.startDate, {
|
||||
message: 'End date must be on or after start date.',
|
||||
path: ['endDate'],
|
||||
});
|
||||
|
||||
const medicationAdministrationSchema = z.object({
|
||||
administeredOn: dateStringSchema,
|
||||
administrationSlot: z.string().trim().min(1).max(80).default('dose-1'),
|
||||
status: z.enum(['administered', 'missed']),
|
||||
notes: z.string().trim().max(500).optional().or(z.literal('')),
|
||||
});
|
||||
|
||||
const flockNoteSchema = z.object({
|
||||
birdId: z.string().uuid().optional().nullable().or(z.literal('')),
|
||||
body: z.string().trim().min(1).max(5000),
|
||||
});
|
||||
|
||||
const integrationTokenCreateSchema = z.object({
|
||||
name: z.string().trim().min(1).max(160),
|
||||
scope: integrationTokenScopeSchema.default('read_write'),
|
||||
@@ -1035,6 +866,7 @@ const oauthProviders = {
|
||||
};
|
||||
|
||||
app.disable('x-powered-by');
|
||||
mountApiDocs(app);
|
||||
app.use(helmet({ crossOriginResourcePolicy: false }));
|
||||
app.use(
|
||||
cors({
|
||||
@@ -3694,6 +3526,27 @@ app.get('/api/locations/search', requireAuth, locationSearchLimiter, async (req:
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/birds/unassigned-match', requireAuth, requireWorkspaceRole(['owner', 'assistant', 'caregiver']), async (req: Request, res: Response, next: NextFunction) => {
|
||||
const parsed = unassignedBirdMatchSchema.safeParse(req.query);
|
||||
if (!parsed.success) { res.status(400).json({ error: 'Enter a known band ID.' }); return; }
|
||||
try {
|
||||
res.json({ available: await hasUnassignedBird(parsed.data.tagId) });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
|
||||
app.post('/api/birds/claim', requireAuth, requireWriteAccess, requireWorkspaceRole(['owner', 'assistant', 'caregiver']), async (req: Request, res: Response, next: NextFunction) => {
|
||||
const parsed = claimBirdSchema.safeParse(req.body);
|
||||
if (!parsed.success) { res.status(400).json({ error: 'A known band ID and confirmation to bring in existing history are required.' }); return; }
|
||||
try {
|
||||
const birdId = await claimUnassignedBird(parsed.data.tagId, req.auth!.workspace.id, req.auth!.user.id);
|
||||
if (!birdId) { res.status(409).json({ error: 'No unassigned bird is available with that band ID. It may already have joined a flock.' }); return; }
|
||||
const bird = await getBirdById(birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(409).json({ error: 'The bird is no longer in this flock. Refresh the flock to continue.' }); return; }
|
||||
await writeAuditLog(req.auth!, 'bird.claimed', 'bird', bird.id, bird.name, { tagId: bird.tag_id });
|
||||
res.status(200).json({ bird: normalizeBird(bird), historyImported: true });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
|
||||
app.get('/api/birds', requireAuth, async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const [birds, memorializedBirds] = await Promise.all([
|
||||
@@ -3842,6 +3695,11 @@ app.post('/api/birds', requireAuth, requireWriteAccess, requireWorkspaceRole(['o
|
||||
let uploadedObjectKeyToCleanup: string | null = null;
|
||||
|
||||
try {
|
||||
const tagId = normalizeBandId(parsed.data.tagId);
|
||||
if (tagId && await hasUnassignedBird(tagId)) {
|
||||
res.status(409).json({ code: 'bird_history_available', error: 'An unassigned bird has this band ID. Confirm adding the existing bird with its history instead of creating a new profile.' });
|
||||
return;
|
||||
}
|
||||
const birdId = crypto.randomUUID();
|
||||
const photoStorage = await resolveBirdPhotoStorage({
|
||||
birdId,
|
||||
@@ -4298,16 +4156,15 @@ app.delete('/api/birds/:birdId', requireAuth, requireWriteAccess, requireWorkspa
|
||||
return;
|
||||
}
|
||||
|
||||
const deleted = await deleteBird(req.params.birdId, req.auth!.workspace.id);
|
||||
const removed = await removeBirdFromFlock(req.params.birdId, req.auth!.workspace.id, req.auth!.user.id);
|
||||
|
||||
if (!deleted) {
|
||||
res.status(404).json({ error: 'Bird not found.' });
|
||||
if (!removed) {
|
||||
res.status(404).json({ error: 'Bird not found or no longer removable.' });
|
||||
return;
|
||||
}
|
||||
|
||||
await writeAuditLog(req.auth!, 'bird.deleted', 'bird', bird.id, bird.name);
|
||||
await writeAuditLog(req.auth!, 'bird.removed_from_flock', 'bird', bird.id, bird.name);
|
||||
res.status(204).send();
|
||||
await deleteBirdPhotoObjectIfNeeded(bird.photo_object_key);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
@@ -4384,6 +4241,77 @@ app.patch('/api/birds/:birdId/memorial-reminders', requireAuth, requireWriteAcce
|
||||
}
|
||||
});
|
||||
|
||||
// Behavior types are flock-scoped; occurrences follow their bird across flock transfers.
|
||||
app.get('/api/birds/:birdId/behavior-types', requireAuth, async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
if (!z.string().uuid().safeParse(req.params.birdId).success) { res.status(400).json({ error: 'Invalid bird ID.' }); return; }
|
||||
const bird = await getBirdById(req.params.birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(404).json({ error: 'Bird not found.' }); return; }
|
||||
res.json({ behaviorTypes: await listBehaviorTypes(req.auth!.workspace.id, bird.id) });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
app.post('/api/birds/:birdId/behavior-types', requireAuth, requireWriteAccess, requireWorkspaceRole(['owner', 'assistant', 'caregiver']), async (req: Request, res: Response, next: NextFunction) => {
|
||||
const parsed = behaviorTypeSchema.safeParse(req.body);
|
||||
if (!parsed.success || !z.string().uuid().safeParse(req.params.birdId).success) { res.status(400).json({ error: 'Invalid behavior type payload.' }); return; }
|
||||
try {
|
||||
const bird = await getBirdById(req.params.birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(404).json({ error: 'Bird not found.' }); return; }
|
||||
if (!ensureBirdWritable(bird, res)) return;
|
||||
const behaviorType = await createBehaviorType(req.auth!.workspace.id, parsed.data.name, parsed.data.description);
|
||||
await writeAuditLog(req.auth!, 'behavior_type.created', 'behavior_type', behaviorType.id, parsed.data.name);
|
||||
res.status(201).json({ behaviorType });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
app.get('/api/birds/:birdId/behaviors', requireAuth, async (req: Request, res: Response, next: NextFunction) => {
|
||||
const parsed = behaviorRangeSchema.safeParse(req.query);
|
||||
if (!parsed.success || !z.string().uuid().safeParse(req.params.birdId).success) { res.status(400).json({ error: 'Invalid behavior date range or bird ID.' }); return; }
|
||||
try {
|
||||
const bird = await getBirdById(req.params.birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(404).json({ error: 'Bird not found.' }); return; }
|
||||
res.json({ occurrences: await listBehaviorOccurrences(bird.id, req.auth!.workspace.id, parsed.data.from, parsed.data.to, parsed.data.behaviorTypeId) });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
app.post('/api/birds/:birdId/behaviors', requireAuth, requireWriteAccess, requireWorkspaceRole(['owner', 'assistant', 'caregiver']), async (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!z.string().uuid().safeParse(req.params.birdId).success) { res.status(400).json({ error: 'Invalid ID.' }); return; }
|
||||
const parsed = behaviorOccurrenceSchema.safeParse(req.body);
|
||||
if (!parsed.success) { res.status(400).json({ error: 'Invalid behavior payload.', details: parsed.error.flatten() }); return; }
|
||||
try {
|
||||
const bird = await getBirdById(req.params.birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(404).json({ error: 'Bird not found.' }); return; }
|
||||
if (!ensureBirdWritable(bird, res)) return;
|
||||
const occurrence = await saveBehaviorOccurrence(bird.id, req.auth!.workspace.id, parsed.data);
|
||||
if (!occurrence) { res.status(404).json({ error: 'Occurrence or behavior type not found, or bird is no longer editable.' }); return; }
|
||||
await writeAuditLog(req.auth!, 'behavior.created', 'behavior_occurrence', occurrence.id, bird.name, { birdId: bird.id });
|
||||
res.status(201).json({ occurrence });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
app.put('/api/birds/:birdId/behaviors/:occurrenceId', requireAuth, requireWriteAccess, requireWorkspaceRole(['owner', 'assistant', 'caregiver']), async (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!z.string().uuid().safeParse(req.params.birdId).success || !z.string().uuid().safeParse(req.params.occurrenceId).success) { res.status(400).json({ error: 'Invalid ID.' }); return; }
|
||||
const parsed = behaviorOccurrenceSchema.safeParse(req.body);
|
||||
if (!parsed.success) { res.status(400).json({ error: 'Invalid behavior payload.', details: parsed.error.flatten() }); return; }
|
||||
try {
|
||||
const bird = await getBirdById(req.params.birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(404).json({ error: 'Bird not found.' }); return; }
|
||||
if (!ensureBirdWritable(bird, res)) return;
|
||||
const occurrence = await saveBehaviorOccurrence(bird.id, req.auth!.workspace.id, parsed.data, req.params.occurrenceId);
|
||||
if (!occurrence) { res.status(404).json({ error: 'Occurrence or behavior type not found, or bird is no longer editable.' }); return; }
|
||||
await writeAuditLog(req.auth!, 'behavior.updated', 'behavior_occurrence', occurrence.id, bird.name, { birdId: bird.id });
|
||||
res.status(200).json({ occurrence });
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
app.delete('/api/birds/:birdId/behaviors/:occurrenceId', requireAuth, requireWriteAccess, requireWorkspaceRole(['owner', 'assistant', 'caregiver']), async (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!z.string().uuid().safeParse(req.params.birdId).success || !z.string().uuid().safeParse(req.params.occurrenceId).success) { res.status(400).json({ error: 'Invalid ID.' }); return; }
|
||||
try {
|
||||
const bird = await getBirdById(req.params.birdId, req.auth!.workspace.id);
|
||||
if (!bird) { res.status(404).json({ error: 'Bird not found.' }); return; }
|
||||
if (!ensureBirdWritable(bird, res)) return;
|
||||
const occurrence = await deleteBehaviorOccurrence(bird.id, req.auth!.workspace.id, req.params.occurrenceId);
|
||||
if (!occurrence) { res.status(404).json({ error: 'Occurrence or behavior type not found, or bird is no longer editable.' }); return; }
|
||||
await writeAuditLog(req.auth!, 'behavior.deleted', 'behavior_occurrence', occurrence.id, bird.name, { birdId: bird.id });
|
||||
res.status(204).send();
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
|
||||
app.get('/api/birds/:birdId/weights', requireAuth, async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const days = Math.min(Math.max(Number(req.query.days ?? 30), 1), 425);
|
||||
|
||||
Reference in new issue
Block a user