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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 118x 132x 14x 22x 22x 22x 22x 22x 22x 22x 22x 118x 118x 115x 3x 2x 1x 22x 6x 6x 22x 5x 5x 2x 3x 2x 1x 22x 12x 12x 7x 5x 3x 2x 1x 1x 22x 6x 6x 2x 4x 2x 2x 1x 1x | import { z } from 'zod';
import type { App } from '../../../infrastructure/http/build-app';
import { Baby } from '../../../domain/baby/baby';
import { CreateBabyUseCase } from '../../../application/baby/create-baby.use-case';
import { ListUserBabiesUseCase } from '../../../application/baby/list-user-babies.use-case';
import { GetBabyByIdUseCase } from '../../../application/baby/get-baby-by-id.use-case';
import { UpdateBabyUseCase } from '../../../application/baby/update-baby.use-case';
import { DeleteBabyUseCase } from '../../../application/baby/delete-baby.use-case';
import { BabyNotFoundError } from '../../../application/baby/errors/baby-not-found.error';
import { GuardianForbiddenError } from '../../../application/baby/errors/guardian-forbidden.error';
import { DomainError } from '../../../shared/errors/domain-error';
import { PrismaBabyRepository } from '../../../infrastructure/database/repositories/prisma-baby.repository';
import { PrismaBabyGuardianRepository } from '../../../infrastructure/database/repositories/prisma-baby-guardian.repository';
import { prisma } from '../../../infrastructure/database/prisma-client';
import { authenticate } from '../plugins/authenticate';
import { authErrorResponseSchema } from '../schemas/auth.schema';
import {
babyListResponseSchema,
babyParamsSchema,
babyResponseSchema,
createBabyBodySchema,
updateBabyBodySchema,
} from '../schemas/baby.schema';
function toBirthDate(dateOnly: string): Date {
return new Date(`${dateOnly}T00:00:00.000Z`);
}
function toResponse(baby: Baby) {
return {
measurements: baby.measurements.map((measurement) => ({ ...measurement, measuredOn: measurement.measuredOn.toISOString().slice(0, 10) })),
id: baby.id,
userId: baby.userId,
name: baby.name,
birthDate: baby.birthDate.toISOString().slice(0, 10),
sexAtBirth: baby.sexAtBirth,
// Espelho depreciado — ver o comentário em `babyResponseSchema`.
bloodType: baby.bloodType,
allergies: baby.allergies,
healthPlanName: baby.healthPlanName,
healthPlanNumber: baby.healthPlanNumber,
avatarUrl: baby.avatarUrl,
avatarColor: baby.avatarColor,
createdAt: baby.createdAt.toISOString(),
};
}
export async function babyRoutes(app: App) {
const babyRepository = new PrismaBabyRepository(prisma);
const babyGuardianRepository = new PrismaBabyGuardianRepository(prisma);
const createBabyUseCase = new CreateBabyUseCase(babyRepository, babyGuardianRepository);
const listUserBabiesUseCase = new ListUserBabiesUseCase(babyRepository, babyGuardianRepository);
const getBabyByIdUseCase = new GetBabyByIdUseCase(babyRepository, babyGuardianRepository);
const updateBabyUseCase = new UpdateBabyUseCase(babyRepository, babyGuardianRepository);
const deleteBabyUseCase = new DeleteBabyUseCase(babyRepository, babyGuardianRepository);
app.route({
method: 'POST',
url: '/babies',
preHandler: authenticate,
schema: {
tags: ['Babies'],
summary: 'Create a baby profile',
description: 'Creates a new baby profile owned by the authenticated user.',
body: createBabyBodySchema,
response: {
201: babyResponseSchema,
400: authErrorResponseSchema,
401: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
try {
const baby = await createBabyUseCase.execute({
measurement: request.body.measurement,
userId: request.userId,
name: request.body.name,
birthDate: toBirthDate(request.body.birthDate),
sexAtBirth: request.body.sexAtBirth,
bloodType: request.body.bloodType,
allergies: request.body.allergies,
healthPlanName: request.body.healthPlanName,
healthPlanNumber: request.body.healthPlanNumber,
avatarUrl: request.body.avatarUrl,
avatarColor: request.body.avatarColor,
});
return reply.status(201).send(toResponse(baby));
} catch (error) {
if (error instanceof DomainError) {
return reply.status(400).send({ status: 'error', message: error.message });
}
throw error;
}
},
});
app.route({
method: 'GET',
url: '/babies',
preHandler: authenticate,
schema: {
tags: ['Babies'],
summary: "List the authenticated user's baby profiles",
response: {
200: babyListResponseSchema,
401: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
const babies = await listUserBabiesUseCase.execute(request.userId);
return reply.status(200).send(babies.map(toResponse));
},
});
app.route({
method: 'GET',
url: '/babies/:babyId',
preHandler: authenticate,
schema: {
tags: ['Babies'],
summary: 'Get a baby profile by id',
params: babyParamsSchema,
response: {
200: babyResponseSchema,
401: authErrorResponseSchema,
404: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
try {
const baby = await getBabyByIdUseCase.execute({
babyId: request.params.babyId,
requestingUserId: request.userId,
});
return reply.status(200).send(toResponse(baby));
} catch (error) {
if (error instanceof BabyNotFoundError) {
return reply.status(404).send({ status: 'error', message: error.message });
}
throw error;
}
},
});
app.route({
method: 'PATCH',
url: '/babies/:babyId',
preHandler: authenticate,
schema: {
tags: ['Babies'],
summary: 'Update a baby profile',
params: babyParamsSchema,
body: updateBabyBodySchema,
response: {
200: babyResponseSchema,
400: authErrorResponseSchema,
401: authErrorResponseSchema,
404: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
try {
const baby = await updateBabyUseCase.execute({
measurement: request.body.measurement,
babyId: request.params.babyId,
requestingUserId: request.userId,
name: request.body.name,
birthDate: request.body.birthDate ? toBirthDate(request.body.birthDate) : undefined,
sexAtBirth: request.body.sexAtBirth,
bloodType: request.body.bloodType,
allergies: request.body.allergies,
healthPlanName: request.body.healthPlanName,
healthPlanNumber: request.body.healthPlanNumber,
avatarUrl: request.body.avatarUrl,
avatarColor: request.body.avatarColor,
});
return reply.status(200).send(toResponse(baby));
} catch (error) {
if (error instanceof BabyNotFoundError) {
return reply.status(404).send({ status: 'error', message: error.message });
}
if (error instanceof DomainError) {
return reply.status(400).send({ status: 'error', message: error.message });
}
throw error;
}
},
});
app.route({
method: 'DELETE',
url: '/babies/:babyId',
preHandler: authenticate,
schema: {
tags: ['Babies'],
summary: 'Delete a baby profile',
description:
'Deletes a baby profile, cascading to its vaccine records, appointments, milestones and ' +
'notifications. Only the OWNER guardian can delete the profile — other guardians get 403.',
params: babyParamsSchema,
response: {
204: z.null().describe('Baby profile deleted successfully'),
401: authErrorResponseSchema,
403: authErrorResponseSchema,
404: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
try {
await deleteBabyUseCase.execute({
babyId: request.params.babyId,
requestingUserId: request.userId,
});
return reply.status(204).send(null);
} catch (error) {
if (error instanceof BabyNotFoundError) {
return reply.status(404).send({ status: 'error', message: error.message });
}
if (error instanceof GuardianForbiddenError) {
return reply.status(403).send({ status: 'error', message: error.message });
}
throw error;
}
},
});
}
|