diff --git a/backend/src/graphql/types/type/User.gql b/backend/src/graphql/types/type/User.gql index f1a2bcc15..751931d5b 100644 --- a/backend/src/graphql/types/type/User.gql +++ b/backend/src/graphql/types/type/User.gql @@ -252,6 +252,6 @@ type Mutation { # Get a JWT Token for the given Email and password login(email: String!, password: String!): String! - setTrophyBadgeSelected(slot: Int!, badgeId: ID!): User + setTrophyBadgeSelected(slot: Int!, badgeId: ID): User resetTrophyBadgesSelected: User } diff --git a/backend/src/schema/resolvers/users.spec.ts b/backend/src/schema/resolvers/users.spec.ts index 0a74d46d3..8d082b682 100644 --- a/backend/src/schema/resolvers/users.spec.ts +++ b/backend/src/schema/resolvers/users.spec.ts @@ -76,7 +76,7 @@ const updateOnlineStatus = gql` ` const setTrophyBadgeSelected = gql` - mutation ($slot: Int!, $badgeId: ID!) { + mutation ($slot: Int!, $badgeId: ID) { setTrophyBadgeSelected(slot: $slot, badgeId: $badgeId) { badgeTrophiesCount badgeTrophiesSelected { @@ -1294,6 +1294,53 @@ describe('setTrophyBadgeSelected', () => { }), ) }) + + describe('set badge to null', () => { + it('returns the user with no badge set on the selected slot', async () => { + await mutate({ + mutation: setTrophyBadgeSelected, + variables: { slot: 0, badgeId: 'trophy_bear' }, + }) + await mutate({ + mutation: setTrophyBadgeSelected, + variables: { slot: 5, badgeId: 'trophy_panda' }, + }) + + await expect( + mutate({ + mutation: setTrophyBadgeSelected, + variables: { slot: 5, badgeId: null }, + }), + ).resolves.toEqual( + expect.objectContaining({ + data: { + setTrophyBadgeSelected: { + badgeTrophiesCount: 2, + badgeTrophiesSelected: [ + { + id: 'trophy_bear', + }, + null, + null, + null, + null, + null, + null, + null, + null, + ], + badgeTrophiesUnused: [ + { + id: 'trophy_panda', + }, + ], + badgeTrophiesUnusedCount: 1, + }, + }, + }), + ) + }) + }) }) }) diff --git a/backend/src/schema/resolvers/users.ts b/backend/src/schema/resolvers/users.ts index 4f1fb6d5b..913427085 100644 --- a/backend/src/schema/resolvers/users.ts +++ b/backend/src/schema/resolvers/users.ts @@ -404,17 +404,25 @@ export default { const session = context.driver.session() const query = session.writeTransaction(async (transaction) => { - const result = await transaction.run( - ` + const queryBadge = ` MATCH (user:User {id: $userId})<-[:REWARDED]-(badge:Badge {id: $badgeId}) OPTIONAL MATCH (user)-[badgeRelation:SELECTED]->(badge) OPTIONAL MATCH (user)-[slotRelation:SELECTED{slot: $slot}]->(:Badge) DELETE badgeRelation, slotRelation MERGE (user)-[:SELECTED{slot: toInteger($slot)}]->(badge) RETURN user {.*} - `, - { userId, badgeId, slot }, - ) + ` + const queryNull = ` + MATCH (user:User {id: $userId}) + OPTIONAL MATCH (user)-[slotRelation:SELECTED {slot: $slot}]->(:Badge) + DELETE slotRelation + RETURN user {.*} + ` + const result = await transaction.run(badgeId ? queryBadge : queryNull, { + userId, + badgeId, + slot, + }) return result.records.map((record) => record.get('user'))[0] }) try {