-
Notifications
You must be signed in to change notification settings - Fork 37
feat: Trail Condition Reporting with GPS, photos, offline support, and trust scoring #1907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
8
commits into
development
Choose a base branch
from
copilot/update-trail-condition-reporting
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58234c6
Initial plan
Copilot 5479584
feat: implement trail condition reporting with GPS, photos, offline s…
Copilot dcef629
fix: address CodeRabbit review feedback on trail conditions feature
Copilot ad1c59e
fix: address Copilot review — migration, self-vote prevention, markHe…
Copilot 76ab938
fix: atomic-update-first pattern for verify and markHelpful routes
Copilot 10d7f6c
fix: address CodeRabbit feedback — migration timestamp, empty/mock st…
Copilot cab3e33
fix: resolve TypeScript errors in trail conditions feature
claude 04ab56d
Merge origin/development into copilot/update-trail-condition-reporting
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './useCreateTrailConditionReport'; | ||
| export * from './useTrailConditions'; | ||
| export * from './useVerifyTrailConditionReport'; |
25 changes: 25 additions & 0 deletions
25
apps/expo/features/trail-conditions/hooks/useCreateTrailConditionReport.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
| import axiosInstance, { handleApiError } from 'expo-app/lib/api/client'; | ||
| import type { TrailCondition, TrailConditionInput } from '../types'; | ||
|
|
||
| const createTrailConditionReport = async (input: TrailConditionInput): Promise<TrailCondition> => { | ||
| try { | ||
| const res = await axiosInstance.post('/api/trail-conditions', input); | ||
| return res.data; | ||
| } catch (error) { | ||
| const { message } = handleApiError(error); | ||
| console.error('Failed to create trail condition report:', error); | ||
| throw new Error(message); | ||
| } | ||
| }; | ||
|
|
||
| export function useCreateTrailConditionReport() { | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: createTrailConditionReport, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ queryKey: ['trailConditions'] }); | ||
| }, | ||
| }); | ||
| } |
27 changes: 27 additions & 0 deletions
27
apps/expo/features/trail-conditions/hooks/useTrailConditions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import axiosInstance, { handleApiError } from 'expo-app/lib/api/client'; | ||
| import { useAuthenticatedQueryToolkit } from 'expo-app/lib/hooks/useAuthenticatedQueryToolkit'; | ||
| import type { TrailCondition } from '../types'; | ||
|
|
||
| export const fetchTrailConditions = async (): Promise<TrailCondition[]> => { | ||
| try { | ||
| const res = await axiosInstance.get('/api/trail-conditions'); | ||
| return res.data?.items ?? res.data ?? []; | ||
| } catch (error) { | ||
| const { message } = handleApiError(error); | ||
| console.error('Failed to fetch trail conditions:', error); | ||
| throw new Error(message); | ||
| } | ||
| }; | ||
|
|
||
| export function useTrailConditions() { | ||
| const { isQueryEnabledWithAccessToken } = useAuthenticatedQueryToolkit(); | ||
|
|
||
| return useQuery({ | ||
| queryKey: ['trailConditions'], | ||
| enabled: isQueryEnabledWithAccessToken, | ||
| queryFn: fetchTrailConditions, | ||
| staleTime: 1000 * 60 * 5, // 5 minutes | ||
| refetchOnWindowFocus: false, | ||
| }); | ||
| } |
25 changes: 25 additions & 0 deletions
25
apps/expo/features/trail-conditions/hooks/useVerifyTrailConditionReport.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
| import axiosInstance, { handleApiError } from 'expo-app/lib/api/client'; | ||
| import type { TrailCondition } from '../types'; | ||
|
|
||
| const verifyTrailConditionReport = async (reportId: string): Promise<TrailCondition> => { | ||
| try { | ||
| const res = await axiosInstance.post(`/api/trail-conditions/${reportId}/verify`); | ||
| return res.data; | ||
| } catch (error) { | ||
| const { message } = handleApiError(error); | ||
| console.error('Failed to verify trail condition report:', error); | ||
| throw new Error(message); | ||
| } | ||
| }; | ||
|
|
||
| export function useVerifyTrailConditionReport() { | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: verifyTrailConditionReport, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ queryKey: ['trailConditions'] }); | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './hooks'; | ||
| export * from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| export type TrailConditionValue = 'excellent' | 'good' | 'fair' | 'poor' | 'closed'; | ||
|
|
||
| export interface TrailConditionLocation { | ||
| latitude: number; | ||
| longitude: number; | ||
| name?: string; | ||
| } | ||
|
|
||
| export interface TrailCondition { | ||
| id: string; | ||
| userId: number; | ||
| trailName: string; | ||
| location?: TrailConditionLocation | null; | ||
| condition: TrailConditionValue; | ||
| details: string; | ||
| photos?: string[] | null; | ||
| trustScore: number; | ||
| verifiedCount: number; | ||
| helpfulCount: number; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| } | ||
|
|
||
| export type TrailConditionInput = Omit< | ||
| TrailCondition, | ||
| 'id' | 'userId' | 'trustScore' | 'verifiedCount' | 'helpfulCount' | 'createdAt' | 'updatedAt' | ||
| > & { | ||
| id: string; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| CREATE TYPE "trail_condition" AS ENUM ('excellent', 'good', 'fair', 'poor', 'closed');--> statement-breakpoint | ||
| CREATE TABLE "trail_conditions" ( | ||
| "id" text PRIMARY KEY NOT NULL, | ||
| "user_id" integer NOT NULL, | ||
| "trail_name" text NOT NULL, | ||
| "location" jsonb, | ||
| "condition" "trail_condition" NOT NULL, | ||
| "details" text NOT NULL, | ||
| "photos" jsonb DEFAULT '[]', | ||
| "trust_score" real DEFAULT 0.5 NOT NULL, | ||
| "verified_count" integer DEFAULT 0 NOT NULL, | ||
| "helpful_count" integer DEFAULT 0 NOT NULL, | ||
| "created_at" timestamp DEFAULT now() NOT NULL, | ||
| "updated_at" timestamp DEFAULT now() NOT NULL | ||
| ); | ||
| --> statement-breakpoint | ||
| ALTER TABLE "trail_conditions" ADD CONSTRAINT "trail_conditions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint | ||
| CREATE INDEX "trail_conditions_user_id_idx" ON "trail_conditions" USING btree ("user_id");--> statement-breakpoint | ||
| CREATE INDEX "trail_conditions_created_at_idx" ON "trail_conditions" USING btree ("created_at"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { OpenAPIHono } from '@hono/zod-openapi'; | ||
| import type { Env } from '@packrat/api/types/env'; | ||
| import type { Variables } from '@packrat/api/types/variables'; | ||
| import { trailConditionListRoutes } from './list'; | ||
| import { trailConditionRoutes } from './report'; | ||
|
|
||
| const trailConditionsRoutes = new OpenAPIHono<{ Bindings: Env; Variables: Variables }>(); | ||
|
|
||
| trailConditionsRoutes.route('/', trailConditionListRoutes); | ||
| trailConditionsRoutes.route('/', trailConditionRoutes); | ||
|
|
||
| export { trailConditionsRoutes }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi'; | ||
| import { createDb } from '@packrat/api/db'; | ||
| import { trailConditions } from '@packrat/api/db/schema'; | ||
| import { ErrorResponseSchema } from '@packrat/api/schemas/catalog'; | ||
| import { | ||
| CreateTrailConditionRequestSchema, | ||
| TrailConditionListResponseSchema, | ||
| TrailConditionSchema, | ||
| } from '@packrat/api/schemas/trailConditions'; | ||
| import type { Env } from '@packrat/api/types/env'; | ||
| import type { Variables } from '@packrat/api/types/variables'; | ||
| import { count, desc, eq, sql } from 'drizzle-orm'; | ||
|
|
||
| const trailConditionListRoutes = new OpenAPIHono<{ Bindings: Env; Variables: Variables }>(); | ||
|
|
||
| // ------------------------------ | ||
| // List Trail Conditions Route | ||
| // ------------------------------ | ||
| const listTrailConditionsRoute = createRoute({ | ||
| method: 'get', | ||
| path: '/', | ||
| tags: ['Trail Conditions'], | ||
| summary: 'List trail conditions', | ||
| description: 'Get trail condition reports ordered by most recent', | ||
| security: [{ bearerAuth: [] }], | ||
| request: { | ||
| query: z.object({ | ||
| limit: z.coerce.number().int().positive().max(100).default(100).optional(), | ||
| offset: z.coerce.number().int().min(0).default(0).optional(), | ||
| }), | ||
| }, | ||
| responses: { | ||
| 200: { | ||
| description: 'Trail conditions retrieved successfully', | ||
| content: { 'application/json': { schema: TrailConditionListResponseSchema } }, | ||
| }, | ||
| 500: { | ||
| description: 'Internal server error', | ||
| content: { 'application/json': { schema: ErrorResponseSchema } }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| trailConditionListRoutes.openapi(listTrailConditionsRoute, async (c) => { | ||
| try { | ||
| const db = createDb(c); | ||
| const { limit = 100, offset = 0 } = c.req.valid('query'); | ||
| const [items, countResult] = await Promise.all([ | ||
| db | ||
| .select() | ||
| .from(trailConditions) | ||
| .orderBy(desc(trailConditions.createdAt)) | ||
| .limit(limit) | ||
| .offset(offset), | ||
| db.select({ total: count() }).from(trailConditions), | ||
| ]); | ||
| const total = countResult[0]?.total ?? 0; | ||
|
|
||
| return c.json({ items, total }, 200); | ||
| } catch (error) { | ||
| console.error('Error fetching trail conditions:', error); | ||
| return c.json({ error: 'Failed to fetch trail conditions' }, 500); | ||
| } | ||
| }); | ||
|
|
||
| // ------------------------------ | ||
| // Create Trail Condition Route | ||
| // ------------------------------ | ||
| const createTrailConditionRoute = createRoute({ | ||
| method: 'post', | ||
| path: '/', | ||
| tags: ['Trail Conditions'], | ||
| summary: 'Create a trail condition report', | ||
| description: 'Submit a new trail condition report with optional photos and GPS location', | ||
| security: [{ bearerAuth: [] }], | ||
| request: { | ||
| body: { | ||
| content: { 'application/json': { schema: CreateTrailConditionRequestSchema } }, | ||
| required: true, | ||
| }, | ||
| }, | ||
| responses: { | ||
| 200: { | ||
| description: 'Trail condition report created successfully', | ||
| content: { 'application/json': { schema: TrailConditionSchema } }, | ||
| }, | ||
| 400: { | ||
| description: 'Bad request', | ||
| content: { 'application/json': { schema: ErrorResponseSchema } }, | ||
| }, | ||
| 500: { | ||
| description: 'Internal server error', | ||
| content: { 'application/json': { schema: ErrorResponseSchema } }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| trailConditionListRoutes.openapi(createTrailConditionRoute, async (c) => { | ||
| const auth = c.get('user'); | ||
| const db = createDb(c); | ||
| const data = c.req.valid('json'); | ||
|
|
||
| try { | ||
| // Compute initial trust score based on reporter history | ||
| const countRows = await db | ||
| .select({ count: sql<number>`count(*)::int` }) | ||
| .from(trailConditions) | ||
| .where(eq(trailConditions.userId, auth.userId)); | ||
| const reportCount = countRows[0]?.count ?? 0; | ||
|
|
||
| // Trust score starts at 0.5 for new reporters, increasing with more reports | ||
| const baseScore = Math.min(0.5 + reportCount * 0.05, 0.9); | ||
|
|
||
| const [newReport] = await db | ||
| .insert(trailConditions) | ||
| .values({ | ||
| id: data.id, | ||
| userId: auth.userId, | ||
| trailName: data.trailName, | ||
| location: data.location ?? null, | ||
| condition: data.condition, | ||
| details: data.details, | ||
| photos: data.photos ?? [], | ||
| trustScore: baseScore, | ||
| verifiedCount: 0, | ||
| helpfulCount: 0, | ||
| }) | ||
| .returning(); | ||
|
|
||
| if (!newReport) return c.json({ error: 'Failed to create trail condition report' }, 400); | ||
|
|
||
| return c.json(newReport, 200); | ||
| } catch (error) { | ||
| console.error('Error creating trail condition:', error); | ||
| return c.json({ error: 'Failed to create trail condition report' }, 500); | ||
| } | ||
| }); | ||
|
|
||
| export { trailConditionListRoutes }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Drizzle migration file was generated for the new
trail_conditionstable andtrail_conditionenum. The schema changes in this file won't take effect in the database without a corresponding migration underpackages/api/drizzle/. You need to runbunx drizzle-kit generate(or the equivalent command for this project) to produce the migration SQL file.