Skip to content

Commit

Permalink
chore: add route to get observation using docId
Browse files Browse the repository at this point in the history
  • Loading branch information
luandro committed Feb 9, 2025
1 parent 296d3f0 commit addd85a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/routes/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,69 @@ export default async function observationRoutes(
fastify,
{ serverBearerToken },
) {
fastify.get('/projects/:projectPublicId/observation/:docId', {
schema: {
params: Type.Object({
projectPublicId: Type.String(),
docId: Type.String(),
}),
response: {
200: Type.Object({
data: schemas.observationResult,
}),
404: schemas.errorResponse,
'4xx': schemas.errorResponse,
},
},
preHandler: async (req, reply) => {
const { projectPublicId } = /** @type {ProjectRequest} */ (req).params
try {
await verifyProjectAuth(req, serverBearerToken, projectPublicId)
} catch {
reply.code(401)
throw errors.invalidBearerToken()
}
await ensureProjectExists(fastify, /** @type {ProjectRequest} */ (req))
},
handler: async (req) => {
const { projectPublicId, docId } =
/** @type {import('fastify').FastifyRequest<{ Params: { projectPublicId: string, docId: string } }>} */ (
req
).params
const project = await fastify.comapeo.getProject(projectPublicId)

const observation = await project.observation.getByDocId(docId)
if (!observation) {
throw errors.notFoundError('Observation not found')
}

return {
data: {
docId: observation.docId,
createdAt: observation.createdAt,
updatedAt: observation.updatedAt,
deleted: observation.deleted,
lat: observation.lat,
lon: observation.lon,
attachments: observation.attachments
.filter((attachment) =>
SUPPORTED_ATTACHMENT_TYPES.has(
/** @type {import('../schemas.js').Attachment['type']} */ (
attachment.type
),
),
)
.map((attachment) => ({
url: new URL(
`projects/${projectPublicId}/attachments/${attachment.driveDiscoveryId}/${attachment.type}/${attachment.name}`,
req.baseUrl,
).href,
})),
tags: observation.tags,
},
}
},
})
fastify.get('/projects/:projectPublicId/observations', {
schema: {
params: Type.Object({
Expand Down
7 changes: 7 additions & 0 deletions test/test_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ OBSERVATION_ID=$(echo "${RESPONSE}" | jq -r '.data[0].docId')
echo "Observation to delete: ${OBSERVATION_ID}"
echo

echo "GET /projects/${PROJECT_ID}/observation/${OBSERVATION_ID}"
echo "--------------------------------------"
RESPONSE=$(curl -s -f -H "Authorization: Bearer ${BEARER_TOKEN}" "${HOST}/projects/${PROJECT_ID}/observation/${OBSERVATION_ID}") || (echo "❌ Failed to fetch observation by docId" && exit 1)
echo "Response: ${RESPONSE}"
echo "✅ Passed: Fetched observation by docId"
echo

# Test DELETE /projects/:projectId/observations/:observationId
echo "DELETE /projects/${PROJECT_ID}/observations/${OBSERVATION_ID}"
echo "--------------------------------------"
Expand Down

0 comments on commit addd85a

Please sign in to comment.