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

feat(model-team): simplify teams around manuscripts

parent 66675630
No related branches found
No related tags found
No related merge requests found
CREATE TABLE teams (
id UUID PRIMARY KEY,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp,
updated TIMESTAMP WITH TIME ZONE,
object JSONB,
name TEXT,
role TEXT NOT NULL,
members JSONB,
owners JSONB,
global BOOLEAN,
type TEXT NOT NULL
);
\ No newline at end of file
exports.up = knex =>
knex.schema.createTable('team_members', table => {
table.uuid('id').primary()
table.timestamp('created').defaultTo(knex.fn.now())
table.timestamp('updated').defaultTo(knex.fn.now())
table.string('status')
table
.uuid('team_id')
.references('id')
.inTable('teams')
.onDelete('CASCADE')
.onUpdate('CASCADE')
table
.uuid('user_id')
.references('id')
.inTable('users')
.onDelete('CASCADE')
.onUpdate('CASCADE')
table.index(['team_id', 'user_id'])
})
exports.up = async knex => {
const { Team, TeamMember } = require('@pubsweet/models')
const teams = await Team.query()
const saves = []
teams.forEach(team => {
if (team.members) {
team.members.forEach(member => {
saves.push(new TeamMember({ userId: member, teamId: team.id }).save())
})
}
})
await Promise.all(saves)
return knex.schema.table('teams', table => {
table.dropColumn('members')
})
}
exports.up = async knex => {
const { Team } = require('@pubsweet/models')
const teams = await Team.query()
const saves = []
await knex.schema.table('teams', table => {
table.uuid('object_id')
table.string('object_type')
table.index(['object_id', 'object_type'])
})
teams.forEach(team => {
if (team.object && team.object.objectId && team.object.objectType) {
team.objectId = team.object.objectId
team.objectType = team.object.objectType
delete team.object
saves.push(team.save())
}
})
await Promise.all(saves)
return knex.schema.table('teams', table => {
table.dropColumn('object')
})
}
exports.up = async knex => {
await knex.schema.createTable('aliases', table => {
table.uuid('id').primary()
table.timestamp('created').defaultTo(knex.fn.now())
table.timestamp('updated').defaultTo(knex.fn.now())
table.string('name')
table.string('email')
table.string('aff')
})
await knex.schema.table('team_members', table => {
table
.uuid('alias_id')
.references('id')
.inTable('aliases')
})
}
CREATE TABLE teams (
id UUID PRIMARY KEY,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp,
updated TIMESTAMP WITH TIME ZONE,
name TEXT,
role TEXT NOT NULL,
members JSONB,
owners JSONB,
global BOOLEAN,
type TEXT NOT NULL,
manuscript_id UUID REFERENCES manuscripts(id) ON DELETE CASCADE
);
CREATE INDEX ON teams (manuscript_id);
CREATE TABLE aliases (
id uuid NOT NULL PRIMARY KEY,
created timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
updated timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
name TEXT,
email TEXT,
aff TEXT
);
CREATE TABLE team_members (
id uuid NOT NULL PRIMARY KEY,
created timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
updated timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
status character varying(255),
team_id uuid REFERENCES teams(id) ON UPDATE CASCADE ON DELETE CASCADE,
user_id uuid REFERENCES users(id) ON DELETE CASCADE,
alias_id uuid REFERENCES aliases(id)
);
CREATE INDEX ON team_members (team_id, user_id);
\ No newline at end of file
......@@ -12,7 +12,7 @@ class Team extends BaseModel {
}
static get relationMappings() {
const { Alias, TeamMember, User } = require('@pubsweet/models')
const { Alias, TeamMember, User, Manuscript } = require('@pubsweet/models')
return {
members: {
......@@ -49,14 +49,21 @@ class Team extends BaseModel {
to: 'aliases.id',
},
},
manuscript: {
relation: BaseModel.BelongsToOneRelation,
modelClass: Manuscript,
join: {
from: 'manuscripts.id',
to: 'teams.manuscript_id',
},
},
}
}
static get schema() {
return {
properties: {
objectId: { type: ['string', 'null'], format: 'uuid' },
objectType: { type: ['string', 'null'] },
manuscriptId: { type: ['string', 'null'], format: 'uuid' },
name: { type: 'string' },
role: { type: ['string'] },
owners: {
......
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