Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Fixed bug in scope creation when scope exists (#10260)
Browse files Browse the repository at this point in the history
When creating a scope for a user who already had that scope, existing logic
was throwing errors in feathers about 'Cannot redefine propery: Symbol'. It's
unclear why this was being thrown since it dealt with feathers internals, but
simplifying the checkExistingScopes logic to just remove existing scopes instead
of trying to track which ones already existed, like is already done when patching
a user with new scopes, solved the problem.
  • Loading branch information
barankyle authored May 30, 2024
1 parent 8d5c50a commit b262b3e
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions packages/server-core/src/scope/scope/scope.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { disallow, iff, isProvider } from 'feathers-hooks-common'
import {
ScopeData,
scopeDataValidator,
ScopeID,
scopePath,
scopeQueryValidator,
ScopeTypeInterface
Expand Down Expand Up @@ -60,37 +61,21 @@ const checkExistingScopes = async (context: HookContext<ScopeService>) => {
paginate: false
})) as any as ScopeTypeInterface[]

const existingData: ScopeData[] = []
const createData: ScopeData[] = []
const existingData: ScopeID[] = []

for (const item of data) {
const existingScope = oldScopes && oldScopes.find((el) => el.type === item.type)
if (existingScope) {
existingData.push(existingScope)
} else {
createData.push(item)
}
}

if (createData.length > 0) {
context.data = createData
context.existingData = existingData
} else {
context.result = existingData
if (existingScope) existingData.push(existingScope.id)
}
}

/**
* Append existing scopes with the newly created scopes
* @param context
* @returns
*/
const addExistingScopes = async (context: HookContext<ScopeService>) => {
if (context.existingData?.length > 0) {
let result = (Array.isArray(context.result) ? context.result : [context.result]) as ScopeTypeInterface[]
result = [...result, ...context.existingData]
context.result = result
}
await context.app.service(scopePath).remove(null, {
query: {
id: {
$in: existingData
},
userId: data[0].userId
}
})
}

export default {
Expand All @@ -116,7 +101,7 @@ export default {
all: [],
find: [],
get: [],
create: [addExistingScopes],
create: [],
update: [],
patch: [],
remove: []
Expand Down

0 comments on commit b262b3e

Please sign in to comment.