Skip to content

Commit

Permalink
fix: use juggling null checks instead of strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 15, 2024
1 parent a10a048 commit aeb6e49
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/db/edit/streamListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function testStreamListener (callback?: (change: ChangeStreamDocume
// eslint-disable-next-line @typescript-eslint/no-misused-promises
return (await createChangeStream()).on('change', async (change: ChangeStreamDocument) => {
await onChange(change)
if (callback !== undefined) callback(change)
if (callback != null) callback(change)
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/graphql/organization/OrganizationQueries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type OrganizationDataSource from '../../model/OrganizationDataSource'
import { QueryByIdType, OrganizationGQLFilter, Sort, Context } from '../../types'
import { Context, OrganizationGQLFilter, QueryByIdType, Sort } from '../../types'

const OrganizationQueries = {
organization: async (_: any,
{ muuid }: QueryByIdType,
context: Context, info) => {
const { dataSources } = context
const { organizations }: { organizations: OrganizationDataSource } = dataSources
if (muuid !== undefined) {
if (muuid != null) {
return await organizations.findOneOrganizationByOrgId(muuid)
}
return null
Expand Down
10 changes: 5 additions & 5 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { gql } from 'apollo-server-express'
import { DocumentNode } from 'graphql'

import { CommonResolvers, CommonTypeDef } from './common/index.js'
import { HistoryQueries, HistoryFieldResolvers } from '../graphql/history/index.js'
import { QueryByIdType, GQLFilter, Sort, Context } from '../types'
import { HistoryFieldResolvers, HistoryQueries } from '../graphql/history/index.js'
import { Context, GQLFilter, QueryByIdType, Sort } from '../types'
import { AreaType, CountByDisciplineType } from '../db/AreaTypes.js'
import { ClimbGQLQueryType, ClimbType } from '../db/ClimbTypes.js'
import AreaDataSource from '../model/AreaDataSource.js'
import { MediaMutations, MediaQueries, MediaResolvers } from './media/index.js'
import { PostMutations, PostQueries, PostResolvers } from './posts/index.js'
import { XMediaMutations, XMediaQueries, XMediaResolvers } from './xmedia/index.js'
import { AreaQueries, AreaMutations } from './area/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 { UserQueries, UserMutations, UserResolvers } from './user/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 @@ -74,7 +74,7 @@ const resolvers = {
{ uuid }: QueryByIdType,
{ dataSources }: Context) => {
const { areas } = dataSources
if (uuid !== undefined && uuid !== '') {
if (uuid != null && uuid !== '') {
return await areas.findOneClimbByUUID(muid.from(uuid))
}
return null
Expand Down
16 changes: 10 additions & 6 deletions src/model/BulkImportDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ export default class BulkImportDataSource extends MutableAreaDataSource {
logger.debug('starting bulk import session...')
const session = _session ?? (await mongoose.startSession())
try {
return await withTransaction(session, async () => {
logger.info('starting bulk import...', input)
const bulkImportResult = await withTransaction(session, async () => {
logger.info('starting bulk import...')
logger.debug(input)
return await this._bulkImportJson({ user, input, climbs, session })
}) ?? result
logger.info(`bulk import complete: added ${bulkImportResult.addedAreas.length} new areas, updated ${bulkImportResult.updatedAreas.length} areas, added or updated ${bulkImportResult.addedOrUpdatedClimbs.length} climbs`)
logger.debug(bulkImportResult)
return bulkImportResult
} catch (e) {
logger.error('bulk import failed', e)
throw e
Expand All @@ -70,7 +74,7 @@ export default class BulkImportDataSource extends MutableAreaDataSource {
addedOrUpdatedClimbs: []
}
let area: AreaType | null
if (areaNode.uuid !== undefined && areaNode.uuid !== null) {
if (areaNode.uuid != null) {
area = await this.updateAreaWith({
user,
areaUuid: muuid.from(areaNode.uuid),
Expand All @@ -88,7 +92,7 @@ export default class BulkImportDataSource extends MutableAreaDataSource {
} else {
throw new Error(`area with id ${areaNode.uuid.toUUID().toString()} (${areaNode.areaName ?? 'unknown name'}) not found`)
}
} else if (areaNode.areaName !== undefined) {
} else if (areaNode.areaName != null) {
area = await this.addAreaWith({
user,
areaName: areaNode.areaName,
Expand All @@ -111,15 +115,15 @@ export default class BulkImportDataSource extends MutableAreaDataSource {
} else {
throw new Error('areaName or id is required')
}
if (areaNode.children !== undefined) {
if (areaNode.children != null) {
for (const child of areaNode.children) {
const childResult = await addOrUpdateArea(child, area.metadata.area_id)
result.updatedAreas.push(...childResult.updatedAreas)
result.addedAreas.push(...childResult.addedAreas)
result.addedOrUpdatedClimbs.push(...childResult.addedOrUpdatedClimbs)
}
}
if (areaNode.climbs !== undefined) {
if (areaNode.climbs != null) {
const addedOrUpdatedClimbs = await Promise.all(await climbs?.addOrUpdateClimbsWith({
userId: user,
parentId: area.metadata.area_id,
Expand Down
2 changes: 1 addition & 1 deletion src/model/MutableAreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { createInstance as createExperimentalUserDataSource } from '../model/Exp
import { sanitizeStrict } from '../utils/sanitize.js'
import AreaDataSource from './AreaDataSource.js'
import { changelogDataSource } from './ChangeLogDataSource.js'
import { withTransaction } from '../utils/helpers'
import { withTransaction } from '../utils/helpers.js'

isoCountries.registerLocale(enJson)

Expand Down
2 changes: 1 addition & 1 deletion src/model/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { changelogDataSource } from './ChangeLogDataSource.js'
import ClimbDataSource from './ClimbDataSource.js'
import { createInstance as createExperimentalUserDataSource } from './ExperimentalUserDataSource.js'
import MutableAreaDataSource from './MutableAreaDataSource.js'
import { withTransaction } from '../utils/helpers'
import { withTransaction } from '../utils/helpers.js'

export interface AddOrUpdateClimbsOptions {
userId: MUUID
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { DataSources } from 'apollo-server-core/dist/graphqlOptions'
import UserDataSource from './model/UserDataSource.js'
import express from 'express'
import * as http from 'http'
import BulkImportDataSource from './model/BulkImportDataSource'
import BulkImportDataSource from './model/BulkImportDataSource.js'

export async function createServer (): Promise<{ app: express.Application, server: ApolloServer }> {
const schema = applyMiddleware(
Expand Down

0 comments on commit aeb6e49

Please sign in to comment.