Skip to content

Commit

Permalink
feat: add updating observation and adding category
Browse files Browse the repository at this point in the history
  • Loading branch information
luandro committed Feb 5, 2025
1 parent 42ec06f commit a2fc494
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
24 changes: 23 additions & 1 deletion src/routes/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export default async function observationRoutes(
params: Type.Object({
projectPublicId: Type.String(),
}),
querystring: Type.Object({
versionId: Type.Optional(Type.String()),
category: Type.Optional(Type.String()),
}),
body: schemas.observationToAdd,
response: {
201: Type.Literal(''),
Expand All @@ -90,9 +94,19 @@ export default async function observationRoutes(
},
handler: async (req) => {
const { projectPublicId } = /** @type {ProjectRequest} */ (req).params
const { versionId, category } =
/** @type {import('fastify').FastifyRequest<{Querystring: {versionId?: string, category?: string}}>} */ (
req
).query
const body = /** @type {ObservationToAdd} */ (req.body)
const project = await fastify.comapeo.getProject(projectPublicId)

let preset
if (category) {
const presets = await project.preset.getMany()
preset = presets.find((p) => p.name === category)
}
console.log('category', category, preset)
const observationData = {
schemaName: /** @type {const} */ ('observation'),
lat: body.lat,
Expand All @@ -101,6 +115,9 @@ export default async function observationRoutes(
...attachment,
hash: '', // Required by schema but not used
})),
presetRef: preset
? { docId: preset.docId, versionId: preset.versionId }
: null,
tags: body.tags || {},
metadata: body.metadata || {
manualLocation: false,
Expand All @@ -115,7 +132,12 @@ export default async function observationRoutes(
},
}

const response = await project.observation.create(observationData)
let response
if (versionId) {
response = await project.observation.update(versionId, observationData)
} else {
response = await project.observation.create(observationData)
}
return response
},
})
Expand Down
29 changes: 27 additions & 2 deletions test/test_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ PROJECT_ID=${FIRST_PROJECT_ID}
echo "Using project ID: ${PROJECT_ID}"
echo

# Test PUT /projects/:projectId/observation
echo "PUT /projects/${PROJECT_ID}/observation"
# Test PUT /projects/:projectId/observation - create
echo "PUT /projects/${PROJECT_ID}/observation - create"
echo "-------------------------------------"
RESPONSE=$(curl -s -f -X PUT \
-H "Authorization: Bearer ${BEARER_TOKEN}" \
Expand All @@ -158,6 +158,31 @@ echo "Response: ${RESPONSE}"
echo "✅ Passed"
echo

# Get versionId from response
# Parse response JSON to extract versionId
VERSION_ID=$(echo "${RESPONSE}" | jq -r '.versionId')
echo "Using version ID: ${VERSION_ID}"

# Test PUT /projects/:projectId/observation - update
echo "PUT /projects/${PROJECT_ID}/observation - update"
echo "-------------------------------------"
RESPONSE=$(curl -s -f -X PUT \
-H "Authorization: Bearer ${BEARER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"lat": 0,
"lon": 0,
"tags": {
"notes": "Updated observation",
"category": "test"
},
"attachments": []
}' \
"${HOST}/projects/${PROJECT_ID}/observation?versionId=${VERSION_ID}") || (echo "❌ Failed" && exit 1)
echo "Response: ${RESPONSE}"
echo "✅ Passed"
echo

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

0 comments on commit a2fc494

Please sign in to comment.