Commit 7bba7224 authored by Alexandros Georgantas's avatar Alexandros Georgantas

fix(app): seed script fixed

parent d1c12e7c
const logger = require('@pubsweet/logger')
const { editoriaDataModel } = require('editoria-data-model')
const { TeamMember } = require('@pubsweet/models')
const keys = require('lodash/keys')
const map = require('lodash/map')
const pick = require('lodash/pick')
const { models } = editoriaDataModel
const config = require('config')
......@@ -16,9 +16,6 @@ const {
BookCollectionTranslation,
} = models
console.log('models', models)
// const { setupDb } = require('@pubsweet/db-manager')
const createBooksWithUsersAndTeams = async () => {
try {
const collections = await BookCollection.all()
......@@ -43,7 +40,6 @@ const createBooksWithUsersAndTeams = async () => {
)
let copyEditorUser = await User.findByUsername('copyEditor')
if (!authorUser) {
console.log('here atuh')
authorUser = await new User({
username: 'author',
email: 'author@example.com',
......@@ -54,7 +50,6 @@ const createBooksWithUsersAndTeams = async () => {
}).save()
}
if (!productionEditorUser) {
console.log('here atuh')
productionEditorUser = await new User({
username: 'productionEditor',
email: 'productionEditorUser@example.com',
......@@ -91,29 +86,18 @@ const createBooksWithUsersAndTeams = async () => {
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)
await new TeamMember({
userId: globalProductionEditorUser.id,
teamId: newTeam.id,
}).save()
} else {
const tempGlobal = globalTeams[0]
console.log('here', tempGlobal)
if (tempGlobal.members === undefined) {
const userMember = pick(globalProductionEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
const member = { members: [{ user: [userMember] }] }
globalTeam = await Team.query().patchAndFetchById(tempGlobal.id, member)
console.log('globallllll', globalTeam)
await new TeamMember({
userId: globalProductionEditorUser.id,
teamId: tempGlobal.id,
}).save()
} else {
globalTeam = globalTeams[0]
}
......@@ -137,22 +121,18 @@ const createBooksWithUsersAndTeams = async () => {
await Promise.all(
map(roles, async role => {
if (role === 'author') {
const userMember = pick(authorUser, [
'id',
'email',
'username',
'admin',
'type',
])
await new Team({
const newTeam = await new Team({
name: config.authsome.teams[role].name,
objectId: authorBook.id,
objectType: 'book',
role,
members: [{ user: [userMember] }],
deleted: false,
global: false,
}).save()
await new TeamMember({
userId: authorUser.id,
teamId: newTeam.id,
}).save()
} else {
await new Team({
name: config.authsome.teams[role].name,
......@@ -177,22 +157,18 @@ const createBooksWithUsersAndTeams = async () => {
await Promise.all(
map(roles, async role => {
if (role === 'productionEditor') {
const userMember = pick(productionEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
await new Team({
const newTeam = await new Team({
name: config.authsome.teams[role].name,
objectId: productionEditorBook.id,
members: [{ user: [userMember] }],
objectType: 'book',
role,
deleted: false,
global: false,
}).save()
await new TeamMember({
userId: productionEditorUser.id,
teamId: newTeam.id,
}).save()
} else {
await new Team({
name: config.authsome.teams[role].name,
......@@ -218,22 +194,18 @@ const createBooksWithUsersAndTeams = async () => {
await Promise.all(
map(roles, async role => {
if (role === 'copyEditor') {
const userMember = pick(copyEditorUser, [
'id',
'email',
'username',
'admin',
'type',
])
await new Team({
const newTeam = await new Team({
name: config.authsome.teams[role].name,
objectId: copyEditorBook.id,
objectType: 'book',
members: [{ user: [userMember] }],
role,
deleted: false,
global: false,
}).save()
await new TeamMember({
userId: copyEditorUser.id,
teamId: newTeam.id,
}).save()
} else {
await new Team({
name: config.authsome.teams[role].name,
......@@ -247,13 +219,11 @@ const createBooksWithUsersAndTeams = async () => {
}),
)
} else {
console.log('here too')
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')
......@@ -261,20 +231,10 @@ const createBooksWithUsersAndTeams = async () => {
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')
console.log('3', copyEditorBookTranslation)
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