Skip to content

Commit

Permalink
Merge pull request #1796 from serlo/staging
Browse files Browse the repository at this point in the history
Deployment
  • Loading branch information
hugotiburtino authored Nov 9, 2024
2 parents 55e1da5 + d034a1f commit 773c5a5
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push-migration-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Build and push Docker image
run: |
cd packages/db-migrations
DOCKER_REGISTRY=ghcr.io DOCKER_REPOSITORY=$(echo $GITHUB_REPOSITORY)/db-migration lerna run --stream push-image
DOCKER_REGISTRY=ghcr.io DOCKER_REPOSITORY=$(echo $GITHUB_REPOSITORY)/db-migration yarn push-image
env:
DOCKER_REGISTRY: ghcr.io
DOCKER_REPOSITORY: ${{ github.repository }}/db-migration
20 changes: 20 additions & 0 deletions __tests__/schema/user/add-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ test('updates the cache', async () => {
})
})

test('Is successful even when user has already role', async () => {
await uuidQuery.shouldReturnData({
uuid: {
roles: {
nodes: [{ role: Role.Login, scope: Scope.Serlo }],
},
},
})
await mutation
.withInput({ username: regularUser.username, role: Role.Login })
.execute()
await uuidQuery.shouldReturnData({
uuid: {
roles: {
nodes: [{ role: Role.Login, scope: Scope.Serlo }],
},
},
})
})

test('fails when user is not authenticated', async () => {
await mutation.forUnauthenticatedUser().shouldFailWithError('UNAUTHENTICATED')
})
Expand Down
23 changes: 16 additions & 7 deletions packages/server/src/schema/uuid/user/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,29 @@ export const resolvers: Resolvers = {
throw new UserInputError('no user with given username')
}

const userHasAlreadyRole = await database.fetchOptional(
`
SELECT 1
FROM role_user
WHERE user_id = ?
AND role_id = ( SELECT id
FROM role
WHERE name = ?)`,
[id, generateRole(role, instance)],
)

if (userHasAlreadyRole) {
return { success: true, query: {} }
}

await database.mutate(
`
INSERT INTO role_user (user_id, role_id)
SELECT ?, role.id
FROM role
WHERE role.name = ?
AND NOT EXISTS (
SELECT 1
FROM role_user
WHERE role_user.user_id = ?
AND role_user.role_id = role.id
)
`,
[id, generateRole(role, instance), id],
[id, generateRole(role, instance)],
)

await UuidResolver.removeCacheEntry({ id }, context)
Expand Down
Loading

0 comments on commit 773c5a5

Please sign in to comment.