Files
mantra.press/server/router/projects/index.js

528 lines
22 KiB
JavaScript

const express = require('express')
const XL = require('excel4node');
module.exports = function (options) {
const db = options.db;
var router = express.Router();
router.route('/')
.get(function(request, response, next) {
db.Project.findAll()
.then(projects => {
response.display("projects", {
user: request.user,
pageTitle: "Projects - Mantra",
projects: projects
})
})
})
router.route('/:id')
.get(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.ProjectArtifactVersions,
include: [
{
association: db.ProjectArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
},
{
association: db.Project.ProjectTranslationArtifactVersions,
include: [
{
association: db.ProjectTranslationArtifactVersion.TranslationArtifactVersion,
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
}
]
}
]
}).then(project => {
if (project) {
response.display("project", {
user: request.user,
pageTitle: "Projects - Mantra",
project: project
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/create')
.get(function(request, response, next) {
response.display("project-form", {
user: request.user,
pageTitle: "Projects - Mantra",
project: {
}
})
})
.post(function(request, response, next) {
db.Project.create({
name: request.body.name,
description: request.body.description,
entityId: request.user.individualEntityUser.entityUser.entityId
}).then(project => {
if (project) {
response.redirect(`/projects/${project.id}`)
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/add-artifact')
.get(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.ProjectArtifactVersions
},
{
association: db.Project.ProjectTranslationArtifactVersions
}
]
}).then(async (project) => {
if (project) {
const entries = await db.Artifact.findAll({
// Narrow it down to entries this user has admin ownership off...,
include: [
{
association: db.Artifact.ArtifactVersions,
limit: 1,
required: true
// TODO: Order by version...
}
]
})
response.display("project-add-artifact", {
user: request.user,
pageTitle: "Projects - Mantra",
project: project,
entries: entries
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.ProjectArtifactVersions,
include: [
{
association: db.ProjectArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
},
{
association: db.Project.ProjectTranslationArtifactVersions,
include: [
{
association: db.ProjectTranslationArtifactVersion.TranslationArtifactVersion,
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
}
]
}
]
}).then(async (project) => {
if (project) {
// TODO: Fi
const artifactVersion = await db.ArtifactVersion.findByPk(request.body.artifactVersionId, {
// Narrow it down to entries this user has admin ownership off...,
})
if (artifactVersion) {
const projectArtifactVersion = db.ProjectArtifactVersion.create({
projectId: project.id,
artifactVersionId: artifactVersion.id
})
response.redirect(`/projects/${project.id}`)
} else {
response.redirect(`/projects/${project.id}/add-artifact`)
}
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/add-translation')
.get(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.ProjectArtifactVersions
},
{
association: db.Project.ProjectTranslationArtifactVersions
}
]
}).then(async (project) => {
if (project) {
const translationArtifactVersions = await db.TranslationArtifactVersion.findAll({
// Narrow it down to entries this user has admin ownership off...,
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
required: true,
// TODO: Order by version...
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
})
response.display("project-add-translation", {
user: request.user,
pageTitle: "Projects - Mantra",
project: project,
translationArtifactVersions: translationArtifactVersions
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.ProjectArtifactVersions
},
{
association: db.Project.ProjectTranslationArtifactVersions
}
]
}).then(async (project) => {
if (project) {
// TODO: Fi
const translationArtifactVersion = await db.TranslationArtifactVersion.findByPk(request.body.translationArtifactVersionId, {
// Narrow it down to entries this user has admin ownership off...,
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
required: true
// TODO: Order by version...
}
]
})
if (translationArtifactVersion) {
const projectTranslationArtifactVersion = db.ProjectTranslationArtifactVersion.create({
projectId: project.id,
translationArtifactVersionId: translationArtifactVersion.id
})
response.redirect(`/projects/${project.id}`)
} else {
response.redirect(`/projects/${project.id}/add-translation`)
}
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/campaign')
.get(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.Campaign,
required: true
},
{
association: db.Project.ProjectArtifactVersions,
include: [
{
association: db.ProjectArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
},
{
association: db.Project.ProjectTranslationArtifactVersions,
include: [
{
association: db.ProjectTranslationArtifactVersion.TranslationArtifactVersion,
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact
}
]
}
]
}
]
}
]
}).then(project => {
if (project) {
response.redirect(`/campaigns/${project.campaign.id}`)
} else {
response.redirect(`/projects/${request.params.id}/campaign/create`)
}
})
})
router.route('/:id/campaign/create')
.get(function(request, response, next) {
db.Project.findByPk(request.params.id, {
include: [
{
association: db.Project.Campaign,
}
]
}).then(project => {
if (project) {
if (project.campaign) {
response.redirect(`/campaigns/${project.campaign.id}`)
} else {
response.display("campaign-form", {
user: request.user,
pageTitle: "Campaign - Mantra",
campaign: {
name: `Campaign for ${project.name} project`
},
project: project,
})
}
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/spreadsheet')
.post(function(request, response, next) {
db.Project.findByPk(request.params.id, {
}).then(async (project) => {
if (project) {
db.TranslationArtifactVersion.findAll({
include: [
// TODO: require translationArtifactVersions in ProjectArtifactVersion
{
association: db.TranslationArtifactVersion.ProjectTranslationArtifactVersions,
where: {
projectId: request.params.id
},
required: true
},
{
association: db.TranslationArtifactVersion.ArtifactVersion,
include: [
{
association: db.ArtifactVersion.Artifact,
include: [
{
association: db.Artifact.Dialect
}
]
},
{
association: db.ArtifactVersion.Chapters,
include: [
{
association: db.Chapter.Chunks,
include: [
{
association: db.Chunk.Translation
}
]
}
]
}
]
},
{
association: db.TranslationArtifactVersion.Dialect
}
]
}).then(translationArtifactVersions => {
const workbook = new XL.Workbook();
const cellWidth = 45
const cellHeight = 25
translationArtifactVersions.forEach((translationArtifactVersion, index) => {
const worksheet_name = `${index+1}-${translationArtifactVersion.dialect.countryId}-${translationArtifactVersion.dialect.languageId} - ${translationArtifactVersion.artifactVersion.artifact.name}`.substring(0, 30);
// Don't have [ ] * ? : / \
const worksheet = workbook.addWorksheet(worksheet_name.replace(/[`*?:'\[\]\\\/]/gi, ""))
worksheet.cell(2,2,2,3,true)
.string(translationArtifactVersion.artifactVersion.artifact.name)
.style({
font: {
bold: true,
},
alignment: {
horizontal: 'center',
wrapText: true
}
})
worksheet.cell(4,2)
.string(translationArtifactVersion.artifactVersion.artifact.dialect.name)
.style({
font: {
bold: true,
},
alignment: {
horizontal: 'center',
wrapText: true
}
})
worksheet.cell(4,3)
.string(translationArtifactVersion.dialect.name)
.style({
font: {
bold: true,
},
alignment: {
horizontal: 'center',
wrapText: true
}
})
// Meta data for the translation and artifact
worksheet.cell(4,4)
.string(translationArtifactVersion.artifactVersion.id)
worksheet.cell(4,5)
.string(translationArtifactVersion.id)
var cellIndex = 6
for (const i in translationArtifactVersion.artifactVersion.chapters) {
const chapter = translationArtifactVersion.artifactVersion.chapters[i]
const chunks = chapter.chunks.sort((a,b) => a.index - b.index)
for (const j in chunks) {
const chunk = chunks[j]
worksheet.cell(cellIndex,2)
.string(chunk.text)
.style({
alignment: {
wrapText: true,
vertical: 'justify'
}
})
const translationCell = worksheet.cell(cellIndex,3)
.style({
alignment: {
wrapText: true,
vertical: 'justify'
}
})
if (chunk.translation) {
translationCell.string(chunk.translation.text)
}
const textLength = chunk.text.length
worksheet.row(cellIndex).setHeight(
Math.ceil(Math.max(textLength/cellWidth, 1)*cellHeight)
)
// Meta data...
// ID for chunk
worksheet.cell(cellIndex,4)
.string(chunk.id)
// ID for translation
worksheet.cell(cellIndex,5)
.string(chunk.translation?.id ?? "")
cellIndex++
}
cellIndex++
}
// Format Columns
worksheet.column(2).setWidth(cellWidth);
worksheet.column(3).setWidth(cellWidth);
worksheet.column(4).hide();
worksheet.column(5).hide();
// Format Rows...
worksheet.row(5).freeze()
worksheet.row(1).setHeight(5)
worksheet.row(3).setHeight(5)
worksheet.row(5).setHeight(5)
})
workbook.write(`${project.name}.xlsx`, response);
}).catch(error => {
next(error)
})
} else {
next()
}
})
})
return router;
};