Skip to content
Snippets Groups Projects
Commit a36b9dfd authored by Jure's avatar Jure
Browse files

feat: use objection.js relationships

parent eb1fa495
No related branches found
No related tags found
No related merge requests found
......@@ -147,18 +147,13 @@ const resolvers = {
const update = merge({}, manuscript, data)
return ctx.connectors.Manuscript.update(id, update, ctx)
},
async submitManuscript(_, { id, input }, ctx) {
const data = JSON.parse(input)
async makeDecision(_, { id, decision }, ctx) {
const manuscript = await ctx.connectors.Manuscript.fetchOne(id, ctx)
const update = merge({}, manuscript, data)
// eslint-disable-next-line
const previousVersion = await new ctx.connectors.Manuscript.model(
update,
).createNewVersion()
manuscript.decision = decision
const manuscriptVersion = await previousVersion.save()
return manuscriptVersion
manuscript.status = decision
return manuscript.save()
},
},
Query: {
......@@ -167,7 +162,7 @@ const resolvers = {
const manuscript = await Manuscript.query()
.findById(id)
.eager('channels')
.eager('[teams, channels, reviews]')
if (!manuscript.meta) {
manuscript.meta = {}
......@@ -188,9 +183,6 @@ const resolvers = {
object_id: manuscript.id,
})
// TODO: Do this with eager loading relations
manuscript.teams = await manuscript.getTeams()
manuscript.reviews = await manuscript.getReviews()
manuscript.manuscriptVersions = await manuscript.getManuscriptVersions()
// manuscript.channel = await ctx.connectors.Channel.model.find(
// manuscript.channelId,
......@@ -199,7 +191,7 @@ const resolvers = {
},
async manuscripts(_, { where }, ctx) {
return ctx.connectors.Manuscript.fetchAll(where, ctx, {
eager: '[teams]',
eager: '[teams, reviews]',
})
},
async paginatedManuscripts(_, { sort, offset, limit, filter }, ctx) {
......@@ -244,7 +236,12 @@ const resolvers = {
// change our queries if the submission form changes. We still want to store it as JSONB
// so that we can easily search through the information within.
Manuscript: {
submission(parent, args, ctx) {
submission(parent) {
return JSON.stringify(parent.submission)
},
},
ManuscriptVersion: {
submission(parent) {
return JSON.stringify(parent.submission)
},
},
......@@ -278,7 +275,7 @@ const typeDefs = `
extend type Mutation {
createManuscript(input: ManuscriptInput): Manuscript!
updateManuscript(id: ID!, input: String): Manuscript!
submitManuscript(id: ID!, input: String): Manuscript!
makeDecision(id: ID!, decision: String): Manuscript!
deleteManuscript(id: ID!): ID!
reviewerResponse(currentUserId: ID, action: String, teamId: ID! ): Team
assignTeamEditor(id: ID!, input: String): [Team]
......
......@@ -75,8 +75,12 @@ class Manuscript extends BaseModel {
const { File } = require('@pubsweet/models')
const id = this.parentId || this.id
const manuscripts = await Manuscript.findByField('parent_id', id)
const firstManuscript = await Manuscript.findOneByField('id', id)
const manuscripts = await Manuscript.query()
.where('parent_id', id)
.eager('[teams, teams.members, reviews]')
const firstManuscript = await Manuscript.query()
.findById(id)
.eager('[teams, teams.members, reviews]')
manuscripts.push(firstManuscript)
const manuscriptVersionsArray = manuscripts.filter(
......@@ -92,8 +96,6 @@ class Manuscript extends BaseModel {
await Promise.all(
manuscriptVersions.map(async manuscript => {
manuscript.reviews = await manuscript.getReviews()
manuscript.teams = await manuscript.getTeams()
manuscript.files = await File.findByObject({
object: 'Manuscript',
object_id: manuscript.id,
......@@ -108,8 +110,10 @@ class Manuscript extends BaseModel {
async createNewVersion() {
const { Team, File } = require('@pubsweet/models')
const manuscriptReviews = await this.getReviews()
const manuscriptTeams = await this.getTeams()
const manuscriptReviews = (await this.$query().eager('reviews')).reviews
const manuscriptTeams = (
await this.$query().eager('[teams, teams.members]')
).teams
const teams = manuscriptTeams.filter(
team =>
team.role === 'author' ||
......@@ -163,7 +167,7 @@ class Manuscript extends BaseModel {
}
static get relationMappings() {
const { Channel, User, Team } = require('@pubsweet/models')
const { Channel, User, Team, Review } = require('@pubsweet/models')
return {
submitter: {
......@@ -194,6 +198,22 @@ class Manuscript extends BaseModel {
to: 'teams.objectId',
},
},
reviews: {
relation: BaseModel.HasManyRelation,
modelClass: Review,
join: {
from: 'manuscripts.id',
to: 'reviews.manuscriptId',
},
},
parent: {
relation: BaseModel.HasOneRelation,
modelClass: Manuscript,
join: {
from: 'manuscripts.id',
to: 'manuscripts.parentId',
},
},
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment