mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2026-03-01 12:44:28 +00:00
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
/* eslint-disable @typescript-eslint/return-await */
|
|
import { UserInputError } from '@graphql/errors'
|
|
|
|
import Resolver from './helpers/Resolver'
|
|
import { queryLocations } from './users/location'
|
|
|
|
import type { Context } from '@src/context'
|
|
|
|
export default {
|
|
Location: {
|
|
...Resolver('Location', {
|
|
undefinedToNull: [
|
|
'nameEN',
|
|
'nameDE',
|
|
'nameFR',
|
|
'nameNL',
|
|
'nameIT',
|
|
'nameES',
|
|
'namePT',
|
|
'namePL',
|
|
'nameRU',
|
|
],
|
|
}),
|
|
distanceToMe: async (parent, _params, context: Context, _resolveInfo) => {
|
|
if (!parent.id) {
|
|
throw new Error('Can not identify selected Location!')
|
|
}
|
|
const session = context.driver.session()
|
|
|
|
const query = session.readTransaction(async (transaction) => {
|
|
const result = await transaction.run(
|
|
`
|
|
MATCH (loc:Location {id: $parent.id})
|
|
MATCH (me:User {id: $user.id})-[:IS_IN]->(meLoc:Location)
|
|
WITH
|
|
point({latitude: loc.lat, longitude: loc.lng}) as locPoint,
|
|
point({latitude: meLoc.lat, longitude: meLoc.lng}) as mePoint
|
|
RETURN round(point.distance(locPoint, mePoint) / 1000) as distance
|
|
`,
|
|
{ parent, user: context.user },
|
|
)
|
|
|
|
return result.records.map((record) => record.get('distance'))[0]
|
|
})
|
|
|
|
try {
|
|
return await query
|
|
} finally {
|
|
await session.close()
|
|
}
|
|
},
|
|
},
|
|
Query: {
|
|
queryLocations: async (_object, args, context: Context, _resolveInfo) => {
|
|
try {
|
|
return queryLocations(args, context)
|
|
} catch (e) {
|
|
throw new UserInputError(e.message)
|
|
}
|
|
},
|
|
},
|
|
}
|