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 BaseModel = require('@pubsweet/base-model')
const logger = require('@pubsweet/logger')
class File extends BaseModel { class File extends BaseModel {
static get tableName() { static get tableName() {
...@@ -11,6 +10,29 @@ class File extends BaseModel { ...@@ -11,6 +10,29 @@ class File extends BaseModel {
this.type = 'file' 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() { static get schema() {
return { return {
properties: { properties: {
...@@ -20,25 +42,11 @@ class File extends BaseModel { ...@@ -20,25 +42,11 @@ class File extends BaseModel {
fileType: { type: ['string', 'null'] }, fileType: { type: ['string', 'null'] },
filename: { type: ['string', 'null'] }, filename: { type: ['string', 'null'] },
size: { type: ['integer', 'null'] }, size: { type: ['integer', 'null'] },
object: { type: ['string', 'null'] }, reviewCommentId: { type: ['string', 'null'], format: 'uuid' },
objectId: { type: 'string', 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' File.type = 'file'
......
const resolvers = require('./resolvers') const resolvers = require('./resolvers')
const typeDefs = require('./typeDefs') const typeDefs = require('./typeDefs')
const model = require('./file')
module.exports = { module.exports = {
model, models: [{ modelName: 'File', model: require('./file') }],
modelName: 'File',
resolvers, resolvers,
typeDefs, typeDefs,
} }
...@@ -2,13 +2,23 @@ CREATE TABLE files ( ...@@ -2,13 +2,23 @@ CREATE TABLE files (
id UUID PRIMARY KEY, id UUID PRIMARY KEY,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp, created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp,
updated TIMESTAMP WITH TIME ZONE, updated TIMESTAMP WITH TIME ZONE,
object TEXT, -- object_type TEXT,
object_id UUID, -- object_id UUID,
label TEXT, label TEXT,
file_type TEXT, file_type TEXT,
filename TEXT, filename TEXT,
url TEXT, url TEXT,
mime_type TEXT, mime_type TEXT,
size INTEGER, 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 = { ...@@ -32,6 +32,7 @@ const resolvers = {
const path = await upload(file) const path = await upload(file)
meta.url = `/static/${path}` meta.url = `/static/${path}`
const data = await new File(meta).save() const data = await new File(meta).save()
return data return data
}, },
}, },
......
...@@ -8,8 +8,8 @@ const typeDefs = ` ...@@ -8,8 +8,8 @@ const typeDefs = `
fileType: String fileType: String
filename: String filename: String
mimeType: String mimeType: String
object: String manuscriptId: ID
objectId: ID! reviewCommentId: ID
label: String label: String
size: Int size: Int
} }
...@@ -18,8 +18,8 @@ const typeDefs = ` ...@@ -18,8 +18,8 @@ const typeDefs = `
id: ID! id: ID!
created: DateTime! created: DateTime!
updated: DateTime updated: DateTime
object: String manuscriptId: ID
objectId: ID! reviewCommentId: ID
label: String label: String
fileType: String fileType: String
filename: 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