Skip to content

Commit

Permalink
chore(server): Migrate resolvers to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
nellh committed Nov 1, 2023
1 parent 596da34 commit ecca596
Show file tree
Hide file tree
Showing 46 changed files with 52 additions and 58 deletions.
6 changes: 3 additions & 3 deletions packages/openneuro-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import Keyv from 'keyv'
import KeyvRedis from '@keyv/redis'
import cookieParser from 'cookie-parser'
import * as jwt from './libs/authentication/jwt'
import * as auth from './libs/authentication/states.js'
import { sitemapHandler } from './handlers/sitemap.js'
import { setupPassportAuth } from './libs/authentication/passport.js'
import * as auth from './libs/authentication/states'
import { sitemapHandler } from './handlers/sitemap'
import { setupPassportAuth } from './libs/authentication/passport'
import { redis } from './libs/redis'
import { version } from './lerna.json'
export { Express } from 'express-serve-static-core'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { vi } from 'vitest'
import * as changelog from '../changelog.js'
import * as changelog from '../changelog'

vi.mock('ioredis')
vi.mock('../../config.ts')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { vi } from 'vitest'
import request from 'superagent'
import { createDataset, datasetsFilter, testBlacklist } from '../dataset'
import { getDatasetWorker } from '../../libs/datalad-service.js'
import { getDatasetWorker } from '../../libs/datalad-service'
import { connect } from 'mongoose'

// Mock requests to Datalad service
vi.mock('superagent')
vi.mock('ioredis')
vi.mock('../../libs/redis.js')
vi.mock('../../libs/redis')
vi.mock('../../config.ts')
vi.mock('../../libs/notifications.js')
vi.mock('../../libs/notifications')

describe('dataset model operations', () => {
describe('createDataset()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getDescriptionObject,
repairDescriptionTypes,
appendSeniorAuthor,
} from '../description.js'
} from '../description'

// Mock requests to Datalad service
vi.mock('ioredis')
Expand Down
2 changes: 1 addition & 1 deletion packages/openneuro-server/src/datalad/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import objectHash from 'object-hash'
import { Readable } from 'stream'
import * as Mongoose from 'mongoose'
import config from '../config'
import * as subscriptions from '../handlers/subscriptions.js'
import * as subscriptions from '../handlers/subscriptions'
import { generateDataladCookie } from '../libs/authentication/jwt'
import { redis } from '../libs/redis'
import CacheItem, { CacheType } from '../cache/item'
Expand Down
4 changes: 2 additions & 2 deletions packages/openneuro-server/src/datalad/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import config from '../config'
import request from 'superagent'
import { redis } from '../libs/redis.js'
import { commitFiles } from './dataset.js'
import { redis } from '../libs/redis'
import { commitFiles } from './dataset'
import { fileUrl } from './files'
import { generateDataladCookie } from '../libs/authentication/jwt'
import { getDatasetWorker } from '../libs/datalad-service'
Expand Down
5 changes: 1 addition & 4 deletions packages/openneuro-server/src/datalad/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import request from 'superagent'
import Dataset from '../models/dataset'
import publishDraftUpdate from '../graphql/utils/publish-draft-update.js'
import { getDatasetWorker } from '../libs/datalad-service'

export const getDraftRevision = async datasetId => {
Expand All @@ -19,9 +18,7 @@ export const updateDatasetRevision = (datasetId, gitRef) => {
/**
* Update the revision modified time in a draft on changes
*/
return Dataset.updateOne({ id: datasetId }, { modified: new Date() })
.exec()
.then(() => publishDraftUpdate(datasetId, gitRef))
return Dataset.updateOne({ id: datasetId }, { modified: new Date() }).exec()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`resolver permissions helpers > checkDatasetAdmin() > resolves to false for anonymous users 1`] = `"You do not have admin access to this dataset."`;

exports[`resolver permissions helpers > checkDatasetWrite() > resolves to false for anonymous users 1`] = `"You do not have access to modify this dataset."`;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import {
datasetReadQuery,
checkPermissionLevel,
Expand Down Expand Up @@ -95,12 +96,17 @@ describe('resolver permissions helpers', () => {
describe('checkDatasetAdmin()', () => {
it('resolves to false for anonymous users', () => {
return expect(
checkDatasetAdmin('ds000001', null, null, false),
checkDatasetAdmin('ds000001', null, null, { checkExists: false }),
).rejects.toThrowErrorMatchingSnapshot()
})
it('resolves to true for admins', () => {
return expect(
checkDatasetAdmin('ds000001', '1234', { admin: true }, false),
checkDatasetAdmin(
'ds000001',
'1234',
{ admin: true },
{ checkExists: false },
),
).resolves.toBe(true)
})
})
Expand Down
5 changes: 0 additions & 5 deletions packages/openneuro-server/src/graphql/pubsub.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { vi } from 'vitest'
import {
encodeCursor,
decodeCursor,
elasticRelayConnection,
} from '../dataset-search'

vi.mock('ioredis')
vi.mock('../../../elasticsearch/elastic-client.js')
vi.mock('../../../elasticsearch/elastic-client.ts')
vi.mock('../../../config.ts')

describe('dataset search resolvers', () => {
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('dataset search resolvers', () => {
hasPreviousPage: false,
},
}
// @ts-expect-error Mock version does not use all arguments
const connection = await elasticRelayConnection(emptyApiResponse, {
dataset: vi.fn(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe('dataset resolvers', () => {
})
it('sorts array of objects by the "created" property as strings', () => {
const testArray = [
{ id: 2, created: '2018-11-20T00:05:43.473Z' },
{ id: 1, created: '2018-11-19T00:05:43.473Z' },
{ id: 3, created: '2018-11-23T00:05:43.473Z' },
{ id: 2, created: '2018-11-20T00:05:43.473Z', tag: '2.0.0' },
{ id: 1, created: '2018-11-19T00:05:43.473Z', tag: '1.0.0' },
{ id: 3, created: '2018-11-23T00:05:43.473Z', tag: '3.0.0' },
]
const sorted = testArray.sort(ds.snapshotCreationComparison)
expect(sorted[0].id).toBe(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import { updatePermissions } from '../permissions'

vi.mock('ioredis')
Expand All @@ -22,7 +23,7 @@ describe('permissions resolvers', () => {
await updatePermissions(
{},
{ datasetId: 'ds01234', userEmail: 'fake@test.com' },
{},
{ user: '1234', userInfo: { id: '1234' } },
)
} catch (err) {
error = err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import { matchKnownObjects, filterLatestSnapshot } from '../snapshots'

vi.mock('ioredis')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import { users } from '../user.js'

vi.mock('ioredis')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const datasetSearchConnection = async (
const searchId = hashObject({ q })
const requestBody = {
sort: [{ _score: 'asc', id: 'desc' }],
search_after: undefined,
}
if (after) {
try {
Expand Down Expand Up @@ -228,6 +229,7 @@ export const advancedDatasetSearchConnection = async (
query: allDatasets
? query
: await parseQuery(query, datasetType, datasetStatus, user),
search_after: undefined,
}
if (after) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as datalad from '../../datalad/dataset'
import { removeDatasetSearchDocument } from '../../graphql/resolvers/dataset-search.js'
import { snapshots, latestSnapshot } from './snapshots.js'
import { description } from './description.js'
import { removeDatasetSearchDocument } from './dataset-search'
import { snapshots, latestSnapshot } from './snapshots'
import { description } from './description'
import {
checkDatasetRead,
checkDatasetWrite,
checkDatasetAdmin,
} from '../permissions'
import { user } from './user.js'
import { user } from './user'
import { permissions } from './permissions'
import { datasetComments } from './comment.js'
import { datasetComments } from './comment'
import { metadata } from './metadata'
import { history } from './history.js'
import { history } from './history'
import * as dataladAnalytics from '../../datalad/analytics'
import DatasetModel from '../../models/dataset'
import Deletion from '../../models/deletion'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const githubDerivative = async (
}
}

interface DatasetDerivatives {
export interface DatasetDerivatives {
name: string
local: boolean
s3Url: URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Resolver implementation for dataset_description.json
*/
import { setDescription } from '../../datalad/description'
import { checkDatasetWrite } from '../permissions.js'
import { checkDatasetWrite } from '../permissions'
export { description } from '../../datalad/description'

export const updateDescription = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This stub is here in case draft.readme or snapshot.readme needs future extension
*/
import { setReadme } from '../../datalad/readme'
import { checkDatasetWrite } from '../permissions.js'
import { checkDatasetWrite } from '../permissions'
export { readme } from '../../datalad/readme'

export const updateReadme = (obj, { datasetId, value }, { user, userInfo }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as datalad from '../../datalad/snapshots'
import { dataset, analytics, snapshotCreationComparison } from './dataset.js'
import { onBrainlife } from './brainlife'
import { checkDatasetRead, checkDatasetWrite } from '../permissions.js'
import { checkDatasetRead, checkDatasetWrite } from '../permissions'
import { readme } from './readme.js'
import { description } from './description.js'
import { summary } from './summary'
import { snapshotIssues } from './issues.js'
import { getFiles } from '../../datalad/files'
import Summary from '../../models/summary'
import DatasetModel from '../../models/dataset'
import { filterRemovedAnnexObjects } from '../utils/file.js'
import { filterRemovedAnnexObjects } from '../utils/file'
import DeprecatedSnapshot from '../../models/deprecatedSnapshot'
import { redis } from '../../libs/redis'
import CacheItem, { CacheType } from '../../cache/item'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import config from '../../config'
import { generateDataladCookie } from '../../libs/authentication/jwt'
import { getDatasetWorker } from '../../libs/datalad-service'
import Issue from '../../models/issue'
import publishDraftUpdate from '../utils/publish-draft-update.js'
import { redlock } from '../../libs/redis'

/**
Expand All @@ -23,10 +22,7 @@ export const updateValidation = (obj, args) => {
},
)
.exec()
.then(() => {
publishDraftUpdate(args.validation.datasetId, args.validation.id)
return true
})
.then(() => true)
}

export const validationUrl = (datasetId, ref) => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import User from '../../../models/user'
import { addJWT } from '../jwt'

Expand Down
4 changes: 2 additions & 2 deletions packages/openneuro-server/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import express from 'express'
import * as users from './handlers/users'
import * as datalad from './handlers/datalad'
import * as comments from './handlers/comments'
import { clientConfig } from './handlers/config.js'
import { clientConfig } from './handlers/config'
import * as subscriptions from './handlers/subscriptions'
import verifyUser from './libs/authentication/verifyUser'
import * as google from './libs/authentication/google'
import * as orcid from './libs/authentication/orcid'
import * as jwt from './libs/authentication/jwt'
import * as auth from './libs/authentication/states'
import * as doi from './handlers/doi'
import { sitemapHandler } from './handlers/sitemap.js'
import { sitemapHandler } from './handlers/sitemap'
import { reviewerHandler } from './handlers/reviewer'

const noCache = (req, res, next) => {
Expand Down

0 comments on commit ecca596

Please sign in to comment.