Commit 0b948814 authored by Alexandros Georgantas's avatar Alexandros Georgantas

test(app): seed script

parent e43e166f
const logger = require('@pubsweet/logger')
const { editoriaDataModel } = require('editoria-data-model/')
const keys = require('lodash/keys')
const map = require('lodash/map')
const pick = require('lodash/pick')
const { models } = editoriaDataModel
const config = require('config')
const {
User,
Book,
BookTranslation,
Team,
BookTranslation,
BookCollection,
BookCollectionTranslation,
} = models
// const { setupDb } = require('@pubsweet/db-manager')
export async function createBooksWithUsersAndTeams() {
const collections = await BookCollection.all()
const globalTeams = await Team.query().where('global', true)
let globalTeam
let collection
if (collections.length === 0) {
collection = await new BookCollection().save()
await new BookCollectionTranslation({
collectionId: collection.id,
languageIso: 'en',
title: 'Books',
}).save()
} else {
collection = collections[0]
}
let authorUser = await User.findByUsername('author')
let productionEditorUser = await User.findByUsername('productionEditor')
let globalProductionEditorUser = await User.findByUsername(
'globalProductionEditor',
)
let copyEditorUser = await User.findByUsername('copyEditor')
if (!authorUser) {
authorUser = await new User({
username: 'author',
email: 'author@example.com',
password: 'password',
givenName: 'AUTH',
surname: 'AUTH',
admin: false,
}).save()
}
if (!productionEditorUser) {
productionEditorUser = await new User({
username: 'productionEditorUser',
email: 'productionEditorUser@example.com',
password: 'password',
givenName: 'PE',
surname: 'PE',
admin: false,
}).save()
}
if (!globalProductionEditorUser) {
globalProductionEditorUser = await new User({
username: 'globalProductionEditorUser',
email: 'globalProductionEditorUser@example.com',
password: 'password',
givenName: 'GPE',
surname: 'GPE',
admin: false,
}).save()
}
if (!copyEditorUser) {
copyEditorUser = await new User({
username: 'copyEditorUser',
email: 'copyEditorUser@example.com',
password: 'password',
givenName: 'CP',
surname: 'CP',
admin: false,
}).save()
}
if (globalTeams.length === 0) {
const newTeam = await new Team({
name: 'Production Editor',
role: 'productionEditor',
deleted: false,
global: true,
}).save()
const userMember = pick(globalProductionEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
const member = { members: [{ user: [userMember] }] }
globalTeam = await Team.query().patchAndFetchById(newTeam.id, member)
} else {
const tempGlobal = globalTeams[0]
if (tempGlobal.members.length === 0) {
const userMember = pick(globalProductionEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
const member = { members: [{ user: [userMember] }] }
globalTeam = await Team.query().patchAndFetchById(tempGlobal.id, member)
} else {
globalTeam = globalTeams[0]
}
}
const books = await Book.all()
let authorBook
let productionEditorBook
let copyEditorBook
if (books.length === 0) {
authorBook = new Book({
collectionId: collection.id,
}).save()
await new BookTranslation({
title: 'Author Book',
bookId: authorBook.id,
languageIso: 'en',
})
const roles = keys(config.authsome.teams)
await Promise.all(
map(roles, async role => {
if (role === 'author') {
const userMember = pick(authorUser, [
'id',
'email',
'username',
'admin',
'type',
])
await new Team({
name: config.authsome.teams[role].name,
objectId: authorBook.id,
objectType: 'book',
role,
members: [{ user: [userMember] }],
deleted: false,
global: false,
}).save()
} else {
await new Team({
name: config.authsome.teams[role].name,
objectId: authorBook.id,
objectType: 'book',
role,
deleted: false,
global: false,
}).save()
}
}),
)
productionEditorBook = new Book({
collectionId: collection.id,
}).save()
await new BookTranslation({
title: 'Production Editor Book',
bookId: productionEditorBook.id,
languageIso: 'en',
})
const roles = keys(config.authsome.teams)
await Promise.all(
map(roles, async role => {
if (role === 'productionEditor') {
const userMember = pick(productionEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
await new Team({
name: config.authsome.teams[role].name,
objectId: productionEditorBook.id,
members: [{ user: [userMember] }],
objectType: 'book',
role,
deleted: false,
global: false,
}).save()
} else {
await new Team({
name: config.authsome.teams[role].name,
objectId: productionEditorBook.id,
objectType: 'book',
role,
deleted: false,
global: false,
}).save()
}
}),
)
copyEditorBook = new Book({
collectionId: collection.id,
}).save()
await new BookTranslation({
title: 'Copy Editor Book',
bookId: copyEditorBook.id,
languageIso: 'en',
})
const roles = keys(config.authsome.teams)
await Promise.all(
map(roles, async role => {
if (role === 'copyEditor') {
const userMember = pick(copyEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
await new Team({
name: config.authsome.teams[role].name,
objectId: copyEditorBook.id,
objectType: 'book',
members: [{ user: [userMember] }],
role,
deleted: false,
global: false,
}).save()
} else {
await new Team({
name: config.authsome.teams[role].name,
objectId: copyEditorBook.id,
objectType: 'book',
role,
deleted: false,
global: false,
}).save()
}
}),
)
} else {
const authorBookTranslation = await BookTranslation.query()
.where('title', 'Author Book')
.andWhere('languageIso', 'en')
authorBook = await Book.findById(authorBookTranslation[0].bookId)
authorBookTeams = await Team.query().where('objectId', authorBook.id)
const productionEditorBookTranslation = await BookTranslation.query()
.where('title', 'Production Editor Book')
.andWhere('languageIso', 'en')
productionEditorBook = await Book.findById(
productionEditorBookTranslation[0].bookId,
)
productionEditorBookTeams = await Team.query().where(
'objectId',
productionEditorBook.id,
)
const copyEditorBookTranslation = await BookTranslation.query()
.where('title', 'Copy Editor Book')
.andWhere('languageIso', 'en')
copyEditorBook = await Book.findById(copyEditorBookTranslation[0].bookId)
copyEditorBookTeams = await Team.query().where(
'objectId',
copyEditorBook.id,
)
}
logger.info('Seeding complete.')
}
Markdown is supported
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