Skip to content

Commit 03434de

Browse files
committed
consolidated event types into file
1 parent 2dee014 commit 03434de

File tree

8 files changed

+152
-196
lines changed

8 files changed

+152
-196
lines changed

packages/openneuro-app/src/scripts/dataset/components/dataset-event-item.tsx

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,12 @@ import { useMutation, useQuery } from "@apollo/client"
33
import { toast } from "react-toastify"
44
import ToastContent from "../../common/partials/toast-content.jsx"
55
import * as Sentry from "@sentry/react"
6+
import { Event } from "../../types/event-types"
67
import styles from "./scss/dataset-events.module.scss"
78
import { PROCESS_CONTRIBUTOR_REQUEST_MUTATION } from "../../queries/datasetEvents.js"
89
import { Username } from "../../users/username.js"
910
import { GET_USER } from "../../queries/user.js"
1011

11-
interface Event {
12-
id: string
13-
timestamp: string
14-
note?: string
15-
event: {
16-
type: string
17-
targetUserId?: string
18-
status?: string
19-
requestId?: string
20-
message?: string
21-
reason?: string
22-
datasetId?: string
23-
resolutionStatus?: string
24-
target?: {
25-
id: string
26-
name?: string
27-
email?: string
28-
orcid?: string
29-
}
30-
}
31-
user?: { name?: string; email?: string; id?: string; orcid?: string }
32-
hasBeenRespondedTo?: boolean
33-
responseStatus?: string
34-
}
35-
3612
interface DatasetEventItemProps {
3713
event: Event
3814
datasetId: string

packages/openneuro-app/src/scripts/dataset/components/dataset-event-list.tsx

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
import React from "react"
2+
import { Event } from "../../types/event-types"
23
import { DatasetEventItem } from "./dataset-event-item"
34

4-
interface ProcessedDatasetEvent {
5-
id: string
6-
note: string
7-
timestamp: string
8-
user: {
9-
id: string
10-
name: string
11-
email: string
12-
orcid: string
13-
}
14-
event: {
15-
type: string
16-
requestId?: string
17-
status?: string
18-
}
19-
success: boolean
20-
hasBeenRespondedTo?: boolean
21-
responseStatus?: string
22-
}
23-
245
interface DatasetEventListProps {
25-
events: ProcessedDatasetEvent[]
6+
events: Event[]
267
datasetId: string
278
editingNoteId: string | null
289
updatedNote: string
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { User } from "./user-types"
2+
3+
export type RequestStatus = "pending" | "accepted" | "denied"
4+
5+
export interface EventDescription {
6+
type: string
7+
targetUserId?: string
8+
status?: RequestStatus
9+
requestId?: string
10+
message?: string
11+
reason?: string
12+
datasetId?: string
13+
resolutionStatus?: RequestStatus
14+
target?: User
15+
version?: string
16+
public?: boolean
17+
level?: string
18+
ref?: string
19+
}
20+
21+
export interface Event {
22+
id: string
23+
timestamp: string
24+
note?: string
25+
success?: boolean
26+
event: EventDescription
27+
user?: User
28+
hasBeenRespondedTo?: boolean
29+
responseStatus?: string
30+
dataset?: {
31+
id: string
32+
name?: string
33+
}
34+
datasetId?: string
35+
status?: "unread" | "saved" | "archived"
36+
}
37+
38+
export interface MappedNotification {
39+
id: string
40+
title: string
41+
content: string
42+
status: "unread" | "saved" | "archived"
43+
type: "general" | "approval" | "response"
44+
approval?: "pending" | "accepted" | "denied"
45+
originalNotification: Event
46+
datasetId?: string
47+
requestId?: string
48+
targetUserId?: string
49+
requesterUser?: User
50+
adminUser?: User
51+
reason?: string
52+
}
53+
54+
/**
55+
* @param rawNotification The raw Event object.
56+
* @returns A MappedNotification object with properties populated from the raw data.
57+
*/
58+
export const mapRawEventToMappedNotification = (
59+
rawNotification: Event,
60+
): MappedNotification => {
61+
const { event, note, user, dataset, datasetId: rawDatasetId } =
62+
rawNotification
63+
const {
64+
type,
65+
resolutionStatus,
66+
status: eventStatus,
67+
requestId,
68+
targetUserId,
69+
reason,
70+
} = event
71+
72+
let title = note || "General Notification"
73+
let mappedType: MappedNotification["type"] = "general"
74+
let approval: MappedNotification["approval"]
75+
let requesterUser: User | undefined
76+
let adminUser: User | undefined
77+
78+
switch (type) {
79+
case "contributorRequest":
80+
title = "Contributor Request for Dataset"
81+
mappedType = "approval"
82+
approval = resolutionStatus ?? "pending"
83+
requesterUser = user
84+
break
85+
case "contributorResponse":
86+
title = `Contributor ${eventStatus} for Dataset`
87+
mappedType = "response"
88+
approval = eventStatus as "accepted" | "denied"
89+
adminUser = user
90+
break
91+
case "note":
92+
title = note || "Admin Note on Dataset"
93+
break
94+
default:
95+
title = note || `Dataset ${type || "Unknown Type"}`
96+
break
97+
}
98+
99+
const datasetId = dataset?.id || rawDatasetId || event.datasetId || ""
100+
101+
return {
102+
id: rawNotification.id,
103+
title,
104+
content: note || "",
105+
status: rawNotification.status ?? "unread",
106+
type: mappedType,
107+
approval,
108+
datasetId,
109+
requestId: requestId,
110+
targetUserId: targetUserId || user?.id,
111+
originalNotification: rawNotification,
112+
requesterUser,
113+
adminUser,
114+
reason: reason,
115+
}
116+
}
Lines changed: 24 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Event, MappedNotification } from "./event-types"
2+
13
export interface User {
24
id: string
35
name: string
@@ -15,146 +17,7 @@ export interface User {
1517
provider?: string
1618
modified?: string
1719
githubSynced?: Date
18-
notifications?: DatasetEventGraphQL[]
19-
}
20-
21-
export interface DatasetEventDescriptionGraphQL {
22-
type?: string
23-
version?: string
24-
public?: boolean
25-
level?: string
26-
ref?: string
27-
message?: string
28-
requestId?: string
29-
targetUserId?: string
30-
status?: string
31-
reason?: string
32-
datasetId?: string
33-
resolutionStatus?: "pending" | "accepted" | "denied"
34-
}
35-
36-
export interface DatasetEventGraphQL {
37-
id: string
38-
timestamp: string // GraphQL DateTime is a string
39-
note?: string
40-
success?: boolean
41-
user?: User
42-
event: DatasetEventDescriptionGraphQL
43-
dataset?: {
44-
id: string
45-
name?: string
46-
}
47-
datasetId?: string
48-
status?: "unread" | "saved" | "archived"
49-
}
50-
51-
export type OutletContextType = {
52-
notifications: MappedNotification[]
53-
handleUpdateNotification: (
54-
id: string,
55-
updates: Partial<MappedNotification>,
56-
) => void
57-
}
58-
59-
export interface MappedNotification {
60-
id: string
61-
title: string
62-
content: string
63-
status: "unread" | "saved" | "archived"
64-
type: "general" | "approval" | "response"
65-
approval?: "pending" | "accepted" | "denied"
66-
originalNotification: DatasetEventGraphQL
67-
datasetId?: string
68-
requestId?: string
69-
targetUserId?: string
70-
requesterUser?: User
71-
adminUser?: User
72-
reason?: string
73-
}
74-
75-
export const mapRawDatasetEventToMappedNotification = (
76-
rawNotification: DatasetEventGraphQL,
77-
): MappedNotification => {
78-
const event = rawNotification.event
79-
let title = "General Notification"
80-
81-
const status: MappedNotification["status"] = rawNotification.status ??
82-
"unread"
83-
84-
let mappedType: MappedNotification["type"] = "general"
85-
let approval: MappedNotification["approval"]
86-
87-
const datasetId = rawNotification.dataset?.id ||
88-
rawNotification.datasetId ||
89-
event.datasetId ||
90-
""
91-
92-
let requestIdForMutation: string | undefined
93-
let targetUserIdForMutation: string | undefined
94-
95-
let requesterUser: User | undefined
96-
let adminUser: User | undefined
97-
let eventReason: string | undefined
98-
switch (event?.type) {
99-
case "contributorRequest":
100-
title = `Contributor Request for Dataset`
101-
mappedType = "approval"
102-
approval = event.resolutionStatus ?? "pending"
103-
requestIdForMutation = event.requestId
104-
targetUserIdForMutation = rawNotification.user?.id
105-
requesterUser = rawNotification.user
106-
break
107-
108-
case "contributorResponse":
109-
title = `Contributor ${event.status} for Dataset`
110-
mappedType = "response"
111-
approval = event.status as "accepted" | "denied"
112-
requestIdForMutation = event.requestId
113-
targetUserIdForMutation = event.targetUserId
114-
adminUser = rawNotification.user
115-
eventReason = event.reason
116-
break
117-
118-
case "note":
119-
title = `Admin Note on Dataset`
120-
break
121-
122-
default:
123-
title = rawNotification.note ||
124-
`Dataset ${rawNotification.event.type || "Unknown Type"}` ||
125-
`${datasetId}: ${event}` ||
126-
`${rawNotification.event}`
127-
break
128-
}
129-
130-
return {
131-
id: rawNotification.id,
132-
title: title,
133-
content: rawNotification.note || "",
134-
status: status,
135-
type: mappedType,
136-
approval: approval,
137-
datasetId: datasetId,
138-
requestId: requestIdForMutation,
139-
targetUserId: targetUserIdForMutation,
140-
originalNotification: rawNotification,
141-
requesterUser: requesterUser,
142-
adminUser: adminUser,
143-
reason: eventReason,
144-
}
145-
}
146-
147-
export interface UserRoutesProps {
148-
orcidUser: User
149-
hasEdit: boolean
150-
isUser: boolean
151-
}
152-
export interface UserCardProps {
153-
orcidUser: User
154-
}
155-
156-
export interface UserAccountViewProps {
157-
orcidUser: User
20+
notifications?: Event[]
15821
}
15922

16023
export interface Dataset {
@@ -188,6 +51,19 @@ export interface Dataset {
18851
}
18952
}
19053

54+
export interface UserRoutesProps {
55+
orcidUser: User
56+
hasEdit: boolean
57+
isUser: boolean
58+
}
59+
export interface UserCardProps {
60+
orcidUser: User
61+
}
62+
63+
export interface UserAccountViewProps {
64+
orcidUser: User
65+
}
66+
19167
export interface DatasetCardProps {
19268
dataset: Dataset
19369
hasEdit: boolean
@@ -203,3 +79,11 @@ export interface AccountContainerProps {
20379
hasEdit: boolean
20480
isUser: boolean
20581
}
82+
83+
export type OutletContextType = {
84+
notifications: MappedNotification[]
85+
handleUpdateNotification: (
86+
id: string,
87+
updates: Partial<MappedNotification>,
88+
) => void
89+
}

packages/openneuro-app/src/scripts/users/__tests__/user-routes.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { vi } from "vitest"
44
import { MemoryRouter, Outlet } from "react-router-dom"
55
import { MockedProvider } from "@apollo/client/testing"
66
import { UserRoutes } from "../user-routes"
7+
import type { Event } from "../../types/event-types"
78
import type {
8-
DatasetEventGraphQL,
99
MappedNotification,
1010
OutletContextType,
1111
User,
@@ -159,7 +159,7 @@ vi.mock("./user-notifications-tab-content", () => ({
159159

160160
// Mock the UserNotificationsView
161161
vi.mock("./user-notifications-view", () => {
162-
const baseDatasetEvent: DatasetEventGraphQL = {
162+
const baseDatasetEvent: Event = {
163163
id: "1",
164164
timestamp: "2023-01-01T12:00:00Z",
165165
event: { type: "published", message: "A dataset has been published." },

packages/openneuro-app/src/scripts/users/user-notification-accordion-actions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useCallback } from "react"
22
import * as Sentry from "@sentry/react"
33
import { toast } from "react-toastify"
44
import { useMutation } from "@apollo/client"
5-
import type { MappedNotification } from "../types/user-types"
5+
import type { MappedNotification } from "../types/event-types"
66
import { UPDATE_NOTIFICATION_STATUS_MUTATION } from "../queries/datasetEvents"
77
import { StatusActionButton } from "./components/status-action-buttons"
88
import iconUnread from "../../assets/icon-unread.png"

0 commit comments

Comments
 (0)