-
Notifications
You must be signed in to change notification settings - Fork 0
[codex] report visibility wiring #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae5a9b9
fix(citizen-pwa): normalize live report timeline
Exc1D 50a413a
fix(functions): publish verified reports
Exc1D d923f9b
docs: record report visibility wiring
Exc1D e27479a
fix(citizen-pwa): restore lookup and submission wiring
Exc1D d4e49bf
fix(citizen-pwa,functions): address CodeRabbit PR 100 review comments
Exc1D 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
There are no files selected for viewing
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
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 |
|---|---|---|
| @@ -1,71 +1,124 @@ | ||
| import type { ReportStatus } from '@bantayog/shared-types' | ||
| import type { ReportData } from '../hooks/useReport' | ||
|
|
||
| export function mapReportFromFirestore(data: Record<string, unknown>): ReportData { | ||
| if (!data.id || !data.status || !Array.isArray(data.timeline)) { | ||
| throw new Error('Invalid report data: missing required fields'); | ||
| const VALID_STATUSES: Set<string> = new Set([ | ||
| 'draft_inbox', | ||
| 'new', | ||
| 'awaiting_verify', | ||
| 'verified', | ||
| 'assigned', | ||
| 'acknowledged', | ||
| 'en_route', | ||
| 'on_scene', | ||
| 'resolved', | ||
| 'closed', | ||
| 'reopened', | ||
| 'rejected', | ||
| 'cancelled', | ||
| 'cancelled_false_report', | ||
| 'merged_as_duplicate', | ||
| ]) | ||
|
|
||
| function toMillis(value: unknown): number | undefined { | ||
| if (typeof value === 'number') { | ||
| return value | ||
| } | ||
| if ( | ||
| value && | ||
| typeof value === 'object' && | ||
| 'toMillis' in value && | ||
| typeof value.toMillis === 'function' | ||
| ) { | ||
| const millis = value.toMillis() | ||
| return typeof millis === 'number' ? millis : undefined | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| function mapTimelineEvent(rawEvt: unknown, index: number) { | ||
| if (!rawEvt || typeof rawEvt !== 'object' || Array.isArray(rawEvt)) { | ||
| throw new Error(`Invalid timeline event at index ${index}`) | ||
| } | ||
| const evt = rawEvt as Record<string, unknown> | ||
| const timestamp = toMillis(evt.timestamp) | ||
| if (typeof evt.event !== 'string' || timestamp === undefined) { | ||
| throw new Error(`Invalid timeline event fields at index ${index}`) | ||
| } | ||
| return { | ||
| event: evt.event, | ||
| timestamp, | ||
| ...(typeof evt.actor === 'string' && { actor: evt.actor }), | ||
| ...(typeof evt.note === 'string' && { note: evt.note }), | ||
| } | ||
| } | ||
|
|
||
| export function mapReportFromFirestore( | ||
| data: Record<string, unknown>, | ||
| docId?: string, | ||
| ): ReportData { | ||
| if (typeof data.status !== 'string' || !VALID_STATUSES.has(data.status)) { | ||
| throw new Error('Invalid report data: missing required fields') | ||
| } | ||
|
|
||
| const status = data.status as ReportStatus | ||
|
|
||
| const createdAt = toMillis(data.createdAt) ?? toMillis(data.submittedAt) | ||
| const updatedAt = toMillis(data.updatedAt) ?? toMillis(data.lastStatusAt) | ||
| if (data.timeline !== undefined && !Array.isArray(data.timeline)) { | ||
| throw new Error('Invalid report data: missing required fields') | ||
| } | ||
| const timeline = Array.isArray(data.timeline) | ||
| ? data.timeline.map(mapTimelineEvent) | ||
| : [ | ||
| ...(typeof createdAt === 'number' ? [{ event: 'new', timestamp: createdAt }] : []), | ||
| ...(data.status !== 'new' && typeof updatedAt === 'number' | ||
| ? [{ event: data.status, timestamp: updatedAt }] | ||
| : []), | ||
| ] | ||
|
|
||
| const result: ReportData = { | ||
| id: data.id as string, | ||
| status: data.status as ReportStatus, | ||
| timeline: (data.timeline as unknown[]).map((rawEvt, index) => { | ||
| if (!rawEvt || typeof rawEvt !== 'object' || Array.isArray(rawEvt)) { | ||
| throw new Error(`Invalid timeline event at index ${index}`) | ||
| } | ||
| const evt = rawEvt as Record<string, unknown> | ||
| if (typeof evt.event !== 'string' || typeof evt.timestamp !== 'number') { | ||
| throw new Error(`Invalid timeline event fields at index ${index}`) | ||
| } | ||
| return { | ||
| event: evt.event, | ||
| timestamp: evt.timestamp, | ||
| ...(typeof evt.actor === 'string' && { actor: evt.actor }), | ||
| ...(typeof evt.note === 'string' && { note: evt.note }), | ||
| } | ||
| }), | ||
| }; | ||
| id: typeof data.id === 'string' ? data.id : docId ?? 'unknown', | ||
| status, | ||
| timeline, | ||
| } | ||
|
|
||
| if (data.type !== undefined) { | ||
| result.type = data.type as string; | ||
| result.type = data.type as string | ||
| } | ||
| if (data.reportType !== undefined) { | ||
| result.reportType = data.reportType as string; | ||
| result.reportType = data.reportType as string | ||
| } | ||
| if (data.severity !== undefined) { | ||
| result.severity = data.severity as string; | ||
| result.severity = data.severity as string | ||
| } | ||
| if (data.createdAt !== undefined) { | ||
| result.createdAt = data.createdAt as number; | ||
| if (createdAt !== undefined) { | ||
| result.createdAt = createdAt | ||
| } | ||
| if (data.updatedAt !== undefined) { | ||
| result.updatedAt = data.updatedAt as number; | ||
| if (updatedAt !== undefined) { | ||
| result.updatedAt = updatedAt | ||
| } | ||
| if ( | ||
| data.location !== undefined && | ||
| data.location !== null && | ||
| typeof data.location === 'object' && | ||
| !Array.isArray(data.location) | ||
| ) { | ||
| const loc = data.location as Record<string, unknown>; | ||
| const rawLocation = | ||
| data.location !== undefined && data.location !== null ? data.location : data.publicLocation | ||
| if (rawLocation !== undefined && rawLocation !== null && typeof rawLocation === 'object' && !Array.isArray(rawLocation)) { | ||
| const loc = rawLocation as Record<string, unknown> | ||
| result.location = { | ||
| ...(loc.address !== undefined && { address: loc.address as string }), | ||
| ...(loc.lat !== undefined && { lat: loc.lat as number }), | ||
| ...(loc.lng !== undefined && { lng: loc.lng as number }), | ||
| }; | ||
| ...(typeof loc.address === 'string' && { address: loc.address }), | ||
| ...(typeof loc.lat === 'number' && { lat: loc.lat }), | ||
| ...(typeof loc.lng === 'number' && { lng: loc.lng }), | ||
| } | ||
| } | ||
| if (data.reporterName !== undefined) { | ||
| result.reporterName = data.reporterName as string; | ||
| result.reporterName = data.reporterName as string | ||
| } | ||
| if (data.reporterPhone !== undefined) { | ||
| result.reporterPhone = data.reporterPhone as string; | ||
| result.reporterPhone = data.reporterPhone as string | ||
| } | ||
| if (data.resolutionNote !== undefined) { | ||
| result.resolutionNote = data.resolutionNote as string; | ||
| result.resolutionNote = data.resolutionNote as string | ||
| } | ||
| if (data.closedBy !== undefined) { | ||
| result.closedBy = data.closedBy as string; | ||
| result.closedBy = data.closedBy as string | ||
| } | ||
|
|
||
| return result; | ||
| return result | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.