Commit 5ce4f5dd authored by Giannis Kopanas's avatar Giannis Kopanas
Browse files

Merge branch 'develop' into 'master'

Develop

See merge request !52
parents 56f33f71 25d2a865
type Book {
id: ID!
updated: DateTime
#collections: [Collections!]
#divisions: [Division]!
files: [File!]
organisation: Organisation
title: String!
status: String
workflow: String
edition: String
metadata: MetadataBook
settings: SettingsBook
}
type MetadataBook {
issbn: String
}
type SettingsBook {
chapterIndependently: Boolean
multiplePublishedVersions: Boolean
publishPdfDownload: Boolean
eachChapter: Boolean
completeBook: Boolean
}
input SettingsBookInput {
chapterIndependently: Boolean
multiplePublishedVersions: Boolean
publishPdfDownload: Boolean
eachChapter: Boolean
completeBook: Boolean
}
input BookFileInput {
cover: Upload
}
input CreateBookInput {
title: String!
workflow: String!
organisationId: ID!
settings: SettingsBookInput
files: BookFileInput
}
type BookOutput {
metadata: MetadataOutput!
results: [Book!]
}
# input CheckAllBookInput {
# }
extend type Query {
getBook(id: ID!): Book!
getBooks(input: SearchModelInput): BookOutput!
}
extend type Mutation {
createBook(input: CreateBookInput!): Book!
# updateBook(id: ID!, title: String!): Book!
deleteBook(id: [ID]): [ID]
checkAllBook(action: Actions!): String!
}
/* eslint-disable no-shadow */
/* eslint-disable no-param-reassign */
/* eslint-disable no-return-await */
const omitBy = require('lodash/omit')
const isNull = require('lodash/isNull')
const { BookCreateService, SearchService } = require('ncbi-data-services')
const { model: Book } = require('ncbi-data-model/src/book')
const getBooks = async (_, args, ctx) => {
const { input } = args
const searchedBooks = new SearchService(Book, input)
const results = await searchedBooks.search()
return {
metadata: searchedBooks.metadata,
results,
}
}
const createBook = async (_, { input }, ctx) => {
const book = await BookCreateService.create({
...input,
})
return book
}
const getBook = async (_, args, ctx) => {
const { id } = args
const book = await Book.query().findById(id)
return Object.assign({}, book)
}
// const updateBook = async (_, { input }, ctx) => {
// const updatedBook = await Promise.all(
// input.map(async book => {
// const updateBook = await Book.query().findById(book.id)
// Object.assign(updateBook, book)
// await new Book(omitBy(updateBook, isNull)).saveGraph()
// return book.id
// }),
// )
// return await Book.query().whereIn('id', updatedBook)
// }
const deleteBook = async (_, { id }, ctx) => {
await Book.relatedQuery('divisions')
.delete()
.whereIn('book_id', id)
await Book.query()
.delete()
.whereIn('id', id)
return id
}
const checkAllBook = async (_, { action }, ctx) => {
if (action === 'DELETE') {
await Book.relatedQuery('divisions').delete()
await Book.query().delete()
return 'deleted'
}
return true
}
module.exports = {
Book: {
async organisation(book, _, ctx) {
return await ctx.connectors.Organisation.fetchOne(
book.organisationId,
ctx,
)
},
},
Mutation: {
checkAllBook,
createBook,
deleteBook,
// updateBook,
},
Query: {
getBook,
getBooks,
},
}
/* eslint-disable global-require */
const models = require('ncbi-data-model')
module.exports = {
model: models.Organisation,
resolvers: require('./book.resolvers'),
typeDefs: require('../graphqlLoaderUtil')('book/book.graphql'),
}
/* eslint-disable jest/no-disabled-tests */
const {
models: [{ model: Team }],
} = require('@pubsweet/model-team')
const config = require('config')
const { model: User } = require('@pubsweet/model-user')
const { startServer } = require('pubsweet-server')
const supertest = require('supertest')
const { model: Book } = require('ncbi-data-model/src/book')
// const { BookCreateService } = require('ncbi-data-services')
const authentication = require('pubsweet-server/src/authentication')
const dbCleaner = require('../../helpers/db_cleaner')
const fixtures = {
otherUser: {
email: 'some1@example.com',
type: 'user',
username: 'test1',
},
otherUser1: {
email: 'some2@example.com',
type: 'user',
username: 'test2',
},
user: {
email: 'test@example.com',
password: 'test',
type: 'user',
username: 'testuser',
},
}
describe('Book GraphQL', () => {
let token
let user
let server
let graphqlQuery
beforeAll(async () => {
server = await startServer()
await dbCleaner()
user = await new User(fixtures.user).save()
token = authentication.token.create(user)
graphqlQuery = (query, variables) => {
const request = supertest(server)
const req = request.post('/graphql').send({ query, variables })
if (token) req.set('Authorization', `Bearer ${token}`)
return req
}
})
afterAll(done => {
server.close(done)
})
beforeEach(async () => {
await dbCleaner()
})
const createOrganization = async input => {
const { body } = await graphqlQuery(
`mutation($input: CreateOrganisationInput!) {
createOrganisation(input: $input) {
id
name
email
}
}`,
{
input: {
...input,
},
},
)
return body.data.createOrganisation
}
const createBook = async input => {
const { body } = await graphqlQuery(
`mutation($input: CreateBookInput!) {
createBook(input: $input) {
id
organisation {
id
name
}
updated
title
workflow
status
settings {
chapterIndependently
multiplePublishedVersions
publishPdfDownload
completeBook
eachChapter
}
}
}`,
{
input,
},
)
return body
}
it('can get Books', async () => {
const input = {
email: 'org@test.org',
name: 'testorg',
}
const organisation = await createOrganization(input)
await new Book({
organisationId: organisation.id,
title: 'testorg',
}).save()
await new Book({
organisationId: organisation.id,
title: 'testorg1',
}).save()
const { body } = await graphqlQuery(
`query ($input: SearchModelInput)
{ getBooks(input: $input) {
metadata {
total
skip
take
}
results {
id
title
}
}
}`,
{
input: {
skip: 0,
take: 20,
},
},
)
expect(body.data.getBooks.results).toHaveLength(2)
})
it('can create a book', async () => {
const input = {
email: 'org@test.org',
name: 'testorg',
}
const organisation = await createOrganization(input)
const createInput = {
settings: {
chapterIndependently: false,
completeBook: false,
eachChapter: true,
multiplePublishedVersions: true,
publishPdfDownload: false,
},
title: 'test book',
workflow: 'word',
}
const body = await createBook({
...createInput,
organisationId: organisation.id,
})
expect(body).toMatchObject({
data: {
createBook: Object.assign(
{},
{ organisation: { id: organisation.id, name: input.name } },
createInput,
),
},
})
})
// it('can update organisation ', async () => {
// const { id } = await new Organisation({
// email: 'Email',
// name: 'Org',
// }).save()
// const input = [
// {
// email: 'OrgEmail',
// id,
// name: 'OrgName',
// },
// ]
// const { body } = await graphqlQuery(
// `mutation($input: [UpdateOrganisationInput]!) {
// updateOrganisation(input: $input) {
// id
// name
// email
// }
// }`,
// {
// input,
// },
// )
// expect(body).toEqual({
// data: {
// updateOrganisation: input,
// },
// })
// })
it('can delete a book', async () => {
const input = {
email: 'org@test.org',
name: 'testorg',
}
const organisation = await createOrganization(input)
const createInput = {
settings: {
chapterIndependently: false,
completeBook: false,
eachChapter: true,
multiplePublishedVersions: true,
publishPdfDownload: false,
},
title: 'test book',
workflow: 'word',
}
const {
data: { createBook: book },
} = await createBook({
...createInput,
organisationId: organisation.id,
})
const {
data: { createBook: book1 },
} = await createBook({
...createInput,
organisationId: organisation.id,
title: 'test book 1',
})
const { body } = await graphqlQuery(
`mutation($id: [ID]) {
deleteBook(id: $id)
}`,
{ id: [book1.id, book.id] },
token,
)
expect(body).toEqual({
data: { deleteBook: [book1.id, book.id] },
})
})
// it('can get organisation and users Teams', async () => {
// const { id } = await new Organisation({
// email: 'test@test.com',
// name: 'Test',
// }).save()
// const otherUser = await new User(fixtures.otherUser).save()
// const otherUser1 = await new User(fixtures.otherUser1).save()
// const members = [
// { status: 'unverified', user: { id: otherUser1.id } },
// { status: 'enabled', user: { id: otherUser.id } },
// ]
// await new Organisation({ name: 'other Test' }).save()
// await Team.query().upsertGraphAndFetch(
// {
// members,
// name: 'Organisation Admin',
// objectId: id,
// objectType: 'Organisation',
// role: 'OrganisationAdmin',
// },
// {
// relate: true,
// unrelate: true,
// },
// )
// const { body } = await graphqlQuery(
// `query ($id: ID!, $input: SearchModelInput)
// {
// getOrganisation (id: $id, input: $input) {
// id
// name
// email
// teams {
// id
// role
// }
// users {
// metadata {
// total
// skip
// take
// }
// results {
// id
// }
// }
// }
// }`,
// {
// id,
// input: {
// skip: 0,
// take: 10,
// },
// },
// )
// console.log(body)
// expect(body.data.getOrganisation.users.metadata.total).toBe('2')
// })
})
......@@ -38,3 +38,13 @@ input OperatorInput {
notContains: String
beginsWith: String
}
scalar DateTime
enum Actions {
DELETE
UPDATE
VERIFY
REJECT
ASSIGNTEAM
}
type File {
id: ID!
name: String!
source: String!
mimetype: String!
}
input NewFileInput {
name: String
source: String
mimetype: String
}
input UpdateFileInput {
id: ID!
name: String
source: String
mimetype: String
data: String
hashed: String
}
extend type Mutation {
uploadFile(input: NewFileInput!): File
updateFile(input: UpdateFileInput!): File
}
const logger = require('@pubsweet/logger')
const fs = require('fs')
const path = require('path')
const isUndefined = require('lodash/isUndefined')
const omitBy = require('lodash/omitBy')
const config = require('config')
const { models: File } = require('ncbi-data-model/src')
const uploadsPath = config.get('pubsweet-server').uploads
const updateFile = async (_, { input }, ctx) => {
try {
const { id, data, hashed, ...restFile } = input
const result = omitBy(restFile, isUndefined)
const currentFile = await File.query().patchAndFetchById(id, result)
if (data) {
fs.writeFileSync(currentFile.source, data)
if (hashed) {
fs.writeFileSync(
path.join(uploadsPath, 'paged', hashed, 'default.css'),
data,
)
}
}
return currentFile
} catch (e) {
logger.error(e)
throw new Error(e)
}
}
module.exports = {
Mutation: {
updateFile,
},
}
/* eslint-disable global-require */
module.exports = {
resolvers: require('./file.resolvers'),
typeDefs: require('../graphqlLoaderUtil')('file/file.graphql'),
}
......@@ -27,8 +27,11 @@ fs.readdirSync(normalizedPath)
}
}
// eslint-disable-next-line no-empty
} catch (e) {}
} catch (e) {
console.log(e)
}
})
module.exports = {
resolvers: Rslvs,
typeDefs: Defs.join(' '),
......
......@@ -27,6 +27,13 @@ type OrganisationOutput {
results: [Organisation!]
}
input CheckAllOrganisationInput {
id: ID
name: String
email: String