Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { editoriaDataModel } = require('editoria-data-model')
const map = require('lodash/map')
const get = require('lodash/get')
const assign = require('lodash/assign')
const { models } = editoriaDataModel
const config = require('config')
const {
Book,
BookComponent,
BookComponentTranslation,
Division,
BookComponentState,
} = models
const createBookComponents = async () => {
try {
const books = await Book.all()
const bookId = books[0].id
const componentType = 'component'
const bookBuilder = get(config, 'bookBuilder')
const workflowStages = get(bookBuilder, 'stages')
await Promise.all(
map(books[0].divisions, async divisionId => {
const newBookComponent = {
bookId,
componentType,
divisionId,
archived: false,
deleted: false,
}
const createdBookComponent = await new BookComponent(
newBookComponent,
).save()
await new BookComponentTranslation({
bookComponentId: createdBookComponent.id,
languageIso: 'en',
title: `title-${createdBookComponent.id}`,
}).save()
await Division.query().patchAndFetchById(divisionId, {
bookComponents: [createdBookComponent.id],
})
const bookComponentWorkflowStages = {
workflowStages: map(workflowStages, stage => ({
type: stage.type,
label: stage.title,
value: -1,
})),
}
await new BookComponentState(
assign(
{},
{
bookComponentId: createdBookComponent.id,
trackChangesEnabled: false,
uploading: false,
},
bookComponentWorkflowStages,
),
).save()
}),
)
} catch (e) {
throw new Error(e)
}
}
module.exports = createBookComponents
createBookComponents()