Skip to content

Commit

Permalink
Merge pull request #6 from Luisotee/main
Browse files Browse the repository at this point in the history
Observation Improvements
  • Loading branch information
luandro authored Feb 12, 2025
2 parents 0b0fa85 + 0ae4b95 commit a45001b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ export default async function authRoutes(fastify, opts) {

// Check if coordinator already exists
const existingCoordinator = fastify.db.findCoordinatorByPhone(phoneNumber)
if (existingCoordinator?.token) {
fastify.log.warn(`Coordinator already exists for phone: ${phoneNumber}`)
throw errors.conflictError('Phone number already registered')
if (existingCoordinator) {
fastify.log.info(
`Coordinator exists with project: ${existingCoordinator.projectName}, will be removed`,
)
// Delete existing coordinator
fastify.db.deleteCoordinatorByPhone(phoneNumber)
fastify.log.info(`Deleted existing coordinator: ${phoneNumber}`)
}

// Check if project name already exists
Expand Down
56 changes: 41 additions & 15 deletions src/routes/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ensureProjectExists, verifyProjectAuth } from './utils.js'
/** @typedef {import('../schemas.js').ObservationToAdd} ObservationToAdd */
/** @typedef {import('../schemas.js').observationToUpdate} ObservationToUpdate */
/** @typedef {import('../schemas.js').AttachmentQuerystring} AttachmentQuerystring */
/** @typedef {import('../schemas.js').Attachment} Attachment */

/**
* Routes for handling observations
Expand Down Expand Up @@ -206,29 +207,56 @@ export default async function observationRoutes(
effectiveVersionId = detailedObs.versionId
}

// Fix for the docId type error by adding null check
if (effectiveVersionId) {
// Update existing observation
const body = /** @type {Record<string, any>} */ (req.body)

// eslint-disable-next-line no-undefined
if (docId === undefined) {
throw errors.badRequestError('docId is required for updates')
}

// Explicitly reject lat/lon in updates
if ('lat' in body || 'lon' in body) {
throw errors.badRequestError(
'Cannot update lat/lon of existing observation',
)
}

const observationData = {
schemaName: /** @type {const} */ ('observation'),
attachments: (body.attachments || []).map(
(/** @type {import('../schemas.js').Attachment} */ attachment) => ({
// Retrieve the existing observation to merge tags/attachments
const existingObs = await project.observation.getByDocId(docId)
if (!existingObs) {
throw errors.notFoundError(
'Observation with provided docId not found',
)
}

const mergedTags = {
...existingObs.tags, // keep current tags
...(preset ? preset.tags : {}), // preset tags if any
...(body.tags || {}), // override with provided tags
}

const mergedAttachments = [
...existingObs.attachments, // keep current attachments
...(body.attachments || []).map(
(/** @type {Attachment} */ attachment) => ({
...attachment,
hash: '',
}),
),
tags: {
...(preset ? preset.tags : {}),
...(body.tags || {}),
},
]

// Fix location overwrite issue in updates
const updateData = {
schemaName: /** @type {'observation'} */ ('observation'),
// Keep original lat/lon values
lat: existingObs.lat,
lon: existingObs.lon,
// Merge existing and new attachments
attachments: mergedAttachments,
// Merge existing and new tags
tags: mergedTags,
...(preset && {
presetRef: {
docId: preset.docId,
Expand All @@ -237,10 +265,7 @@ export default async function observationRoutes(
}),
}

return await project.observation.update(
effectiveVersionId,
observationData,
)
return await project.observation.update(effectiveVersionId, updateData)
}

// Create new observation
Expand All @@ -252,12 +277,13 @@ export default async function observationRoutes(
)
}

// Fix for the attachment parameter typing
const observationData = {
schemaName: /** @type {const} */ ('observation'),
schemaName: /** @type {'observation'} */ ('observation'),
lat: body.lat,
lon: body.lon,
attachments: (body.attachments || []).map(
(/** @type {import('../schemas.js').Attachment} */ attachment) => ({
(/** @type {Attachment} */ attachment) => ({
...attachment,
hash: '',
}),
Expand Down

0 comments on commit a45001b

Please sign in to comment.