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

feat: use relations to link files and their owners

parent 7bc9afdc
No related branches found
No related tags found
No related merge requests found
const BaseModel = require('@pubsweet/base-model')
const logger = require('@pubsweet/logger')
class File extends BaseModel {
static get tableName() {
......@@ -11,6 +10,29 @@ class File extends BaseModel {
this.type = 'file'
}
static get relationMappings() {
const { Manuscript, ReviewComment } = require('@pubsweet/models')
return {
manuscript: {
relation: BaseModel.BelongsToOneRelation,
modelClass: Manuscript,
join: {
from: 'manuscripts.id',
to: 'files.manuscript_id',
},
},
reviewComment: {
relation: BaseModel.BelongsToOneRelation,
modelClass: ReviewComment,
join: {
from: 'review_comments.id',
to: 'files.review_comments_id',
},
},
}
}
static get schema() {
return {
properties: {
......@@ -20,25 +42,11 @@ class File extends BaseModel {
fileType: { type: ['string', 'null'] },
filename: { type: ['string', 'null'] },
size: { type: ['integer', 'null'] },
object: { type: ['string', 'null'] },
objectId: { type: 'string', format: 'uuid' },
reviewCommentId: { type: ['string', 'null'], format: 'uuid' },
manuscriptId: { type: ['string', 'null'], format: 'uuid' },
},
}
}
static async findByObject({ object, object_id }) {
logger.debug('Finding Files by Object')
const results = await this.query()
.where('object', object)
.andWhere('object_id', object_id)
return results
}
// async $beforeDelete() {
// await Team.deleteAssociated(this.data.type, this.id)
// }
}
File.type = 'file'
......
const resolvers = require('./resolvers')
const typeDefs = require('./typeDefs')
const model = require('./file')
module.exports = {
model,
modelName: 'File',
models: [{ modelName: 'File', model: require('./file') }],
resolvers,
typeDefs,
}
......@@ -2,13 +2,23 @@ CREATE TABLE files (
id UUID PRIMARY KEY,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp,
updated TIMESTAMP WITH TIME ZONE,
object TEXT,
object_id UUID,
-- object_type TEXT,
-- object_id UUID,
label TEXT,
file_type TEXT,
filename TEXT,
url TEXT,
mime_type TEXT,
size INTEGER,
type TEXT NOT NULL
type TEXT NOT NULL,
-- Things that can have files
manuscript_id UUID REFERENCES manuscripts(id) ON DELETE CASCADE,
review_comment_id UUID REFERENCES review_comments(id) ON DELETE CASCADE,
constraint exactly_one_file_owner check(
(
(manuscript_id is not null)::integer +
(review_comment_id is not null)::integer
) = 1
)
);
\ No newline at end of file
......@@ -32,6 +32,7 @@ const resolvers = {
const path = await upload(file)
meta.url = `/static/${path}`
const data = await new File(meta).save()
return data
},
},
......
......@@ -8,8 +8,8 @@ const typeDefs = `
fileType: String
filename: String
mimeType: String
object: String
objectId: ID!
manuscriptId: ID
reviewCommentId: ID
label: String
size: Int
}
......@@ -18,8 +18,8 @@ const typeDefs = `
id: ID!
created: DateTime!
updated: DateTime
object: String
objectId: ID!
manuscriptId: ID
reviewCommentId: ID
label: String
fileType: String
filename: String
......
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