Skip to content

Commit

Permalink
Merge pull request #1452 from serlo/Xr3ZO-kulla-2024-05-02-19-38
Browse files Browse the repository at this point in the history
refactor(notification): Remove fetchScopeOfNotificationEvent()
  • Loading branch information
kulla authored May 3, 2024
2 parents 3af58dd + 1b894b7 commit 004d8e0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
17 changes: 0 additions & 17 deletions packages/server/src/schema/authorization/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,6 @@ export async function fetchScopeOfUuid(
return Scope.Serlo
}

export async function fetchScopeOfNotificationEvent(
{
id,
}: {
id: number
},
context: Context,
): Promise<Scope> {
const event = await context.dataSources.model.serlo.getNotificationEvent({
id,
})
if (event === null)
throw new UserInputError('Notification event does not exist.')

return await fetchScopeOfUuid({ id: event.objectId }, context)
}

export function resolveScopedRoles(user: Model<'User'>): Model<'ScopedRole'>[] {
return user.roles.map(legacyRoleToRole).filter(isDefined)
}
Expand Down
10 changes: 6 additions & 4 deletions packages/server/src/schema/notifications/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
createNamespace,
} from '~/internals/graphql'
import { NotificationDecoder } from '~/model/decoder'
import { fetchScopeOfNotificationEvent } from '~/schema/authorization/utils'
import { fetchScopeOfUuid } from '~/schema/authorization/utils'
import { resolveConnection } from '~/schema/connection/utils'
import { Resolvers } from '~/types'

Expand Down Expand Up @@ -151,18 +151,20 @@ export const resolvers: Resolvers = {
{ userId },
context,
)
const eventIds = ids.map((id) => {
const events = ids.map((id) => {
const notification = notifications.find((n) => n.id === id)
if (!notification) {
throw new ForbiddenError(
'You are only allowed to set your own notification states.',
)
}
return notification.event.id
return notification.event
})

const scopes = await Promise.all(
eventIds.map((id) => fetchScopeOfNotificationEvent({ id }, context)),
events.map(({ objectId }) =>
fetchScopeOfUuid({ id: objectId }, context),
),
)

await assertUserIsAuthorized({
Expand Down

0 comments on commit 004d8e0

Please sign in to comment.