Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ticks field to climb query #431

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { MediaMutations, MediaQueries, MediaResolvers } from './media/index.js'
import { AreaMutations, AreaQueries } from './area/index.js'
import { ClimbMutations } from './climb/index.js'
import { OrganizationMutations, OrganizationQueries } from './organization/index.js'
import { TickMutations, TickQueries } from './tick/index.js'
import { TickMutations, TickQueries, TickResolvers } from './tick/index.js'
import { UserMutations, UserQueries, UserResolvers } from './user/index.js'
import { getAuthorMetadataFromBaseNode } from '../db/utils/index.js'
import { geojsonPointToLatitude, geojsonPointToLongitude } from '../utils/helpers.js'
Expand Down Expand Up @@ -118,6 +118,7 @@ const resolvers = {
...MediaResolvers,
...HistoryFieldResolvers,
...UserResolvers,
...TickResolvers,
JSONObject: GraphQLJSONObject,

Climb: {
Expand Down Expand Up @@ -188,7 +189,13 @@ const resolvers = {
}
: node.content,

authorMetadata: getAuthorMetadataFromBaseNode
authorMetadata: getAuthorMetadataFromBaseNode,

ticks: async (node: ClimbGQLQueryType, _: any, { dataSources }: GQLContext) => {
const { ticks } = dataSources
return await ticks.ticksByUserIdAndClimb(node._id.toUUID().toString())
}

},

Area: {
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/schema/Climb.gql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type Climb {

"Metadata about creation & update of this climb"
authorMetadata: AuthorMetadata!

"User ticks of this climb"
ticks: [TickType]
}

type ClimbMetadata {
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/schema/Tick.gql
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type TickType {
"""
grade: String
source: TickSource

"""User public profile"""
user: UserPublicProfile!
}

"The tick sources that openbeta supports."
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/tick/TickQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TickQueries = {
userTicksByClimbId: async (_, input, { dataSources }): Promise<TickType[] | null> => {
const { ticks }: { ticks: TickDataSource } = dataSources
const { climbId, userId } = input
return await ticks.ticksByUserIdAndClimb(userId, climbId)
return await ticks.ticksByUserIdAndClimb(climbId, userId)
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/graphql/tick/TickResolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import muuid from 'uuid-mongodb'
import { TickType } from '../../db/TickTypes.js'
import { GQLContext } from '../../types.js'

export const TickResolvers = {
TickType: {
user: async (node: TickType, args: any, { dataSources }: GQLContext) => {
const { users } = dataSources
return await users.getUserPublicProfileByUuid(muuid.from(node.userId))
}
}
}
3 changes: 2 additions & 1 deletion src/graphql/tick/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TickMutations from './TickMutations.js'
import TickQueries from './TickQueries.js'
import { TickResolvers } from './TickResolvers.js'

export { TickMutations, TickQueries }
export { TickMutations, TickQueries, TickResolvers }
9 changes: 7 additions & 2 deletions src/model/TickDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ export default class TickDataSource extends MongoDataSource<TickType> {
return await this.tickModel.find({ userId: userIdObject._id.toUUID().toString() })
}

async ticksByUserIdAndClimb (userId: string, climbId: string): Promise<TickType[]> {
return await this.tickModel.find({ userId, climbId })
/**
* Get all ticks by climb uuid and optional user uuid
* @param userId Optional user uuid
* @param climbId climb uuid
*/
async ticksByUserIdAndClimb (climbId: string, userId?: string): Promise<TickType[]> {
return await this.tickModel.find({ ...(userId != null && { userId }), climbId }).sort({ dateClimbed: -1 }).lean()
}

static instance: TickDataSource
Expand Down
2 changes: 1 addition & 1 deletion src/model/__tests__/ticks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Ticks', () => {
if (tick == null || tick2 == null) {
fail('Should add a new tick')
}
const userClimbTicks = await ticks.ticksByUserIdAndClimb(userId.toUUID().toString(), climbId)
const userClimbTicks = await ticks.ticksByUserIdAndClimb(climbId, userId.toUUID().toString())
expect(userClimbTicks.length).toEqual(1)
})

Expand Down
Loading