const express = require('express'); const req = require('express/lib/request'); module.exports = function (options) { const db = options.db; var router = express.Router(); router.route('/:id') .get(function(request, response, next) { db.ArtifactVersion.findByPk(request.params.id, { include: [ { association: db.ArtifactVersion.Artifact, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.Chapters }, { association: db.ArtifactVersion.TranslationArtifactVersions, where: { backTranslationFromId: null }, required: false } ] }).then(artifactVersion => { if (artifactVersion) { response.display("artifact-version", { user: request.user, pageTitle: "Artifact - Mantra", artifactVersion: artifactVersion }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/chapters/add') .get(function(request, response, next) { db.ArtifactVersion.findByPk(request.params.id, { include: [ { association: db.ArtifactVersion.Artifact, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.Chapters } ] }).then(artifactVersion => { if (artifactVersion) { response.display("chapter-form", { user: request.user, pageTitle: "Library - Mantra", artifactVersion: artifactVersion }) } else { next() } }).catch(error => { next(error) }) }) .post(function(request, response, next) { db.ArtifactVersion.findByPk(request.params.id, { include: [ { association: db.ArtifactVersion.Artifact, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.Chapters } ] }).then(artifactVersion => { const isString = typeof request.body.text === "string" || request.body.text instanceof String if (artifactVersion && isString) { const chunks = request.body.text.trim().split(/\r?\n/).filter(text => { if (text) { return true } else { return false } }) db.Chapter.create({ creatorId: request.user.id, name: chunks[0].trim(), artifactVersionId: artifactVersion.id, chunks: chunks.map((chunk,index) => { return { creatorId: request.user.id, text: chunk.trim(), index: index } }) }, { include: [ { association: db.Chapter.Chunks } ] }).then(chapter => { if (chapter) { response.redirect(`/v/${chapter.artifactVersionId}/chapters/${chapter.id}`) } else { response.display("chapter-form", { user: request.user, pageTitle: "Library - Mantra", artifactVersion: artifactVersion }) } }).catch(error => { next(error) }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/chapters/:chapterId') .get(function(request, response, next) { db.Chapter.findByPk(request.params.chapterId, { include: [ { association: db.Chapter.Chunks }, { association: db.Chapter.ArtifactVersion, include: [ { association: db.ArtifactVersion.Artifact } ] } ] }).then(chapter => { if (chapter) { response.display("chapter", { user: request.user, pageTitle: "Chapter - Mantra", chapter: chapter }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/translations/:translationArtifactVersionId') .get(function(request, response, next) { db.TranslationArtifactVersion.findByPk(request.params.translationArtifactVersionId, { include: [ { association: db.TranslationArtifactVersion.ArtifactVersion, include: [ { association: db.ArtifactVersion.Artifact }, { association: db.ArtifactVersion.Chapters } ] }, { association: db.TranslationArtifactVersion.ForkedFrom }, { association: db.TranslationArtifactVersion.TranslationChapters, include: [ { association: db.TranslationChapter.Chapter } ] } ] }).then(translationArtifactVersion => { if (translationArtifactVersion) { response.display("translation-artifact-version", { user: request.user, pageTitle: "Translation Artifact - Mantra", translationArtifactVersion: translationArtifactVersion, }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/translations/add') .get(function(request, response, next) { db.ArtifactVersion.findByPk(request.params.id, { include: [ { association: db.ArtifactVersion.Artifact, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.TranslationArtifactVersions } ] }).then(artifactVersion => { if (artifactVersion) { response.display("translation-form", { user: request.user, pageTitle: "Library - Mantra", artifactVersion: artifactVersion }) } else { next() } }).catch(error => { next(error) }) }) .post(function(request, response, next) { db.ArtifactVersion.findByPk(request.params.id, { include: [ { association: db.ArtifactVersion.Artifact, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.TranslationArtifactVersions }, { association: db.ArtifactVersion.Chapters, include: [ { association: db.Chapter.Chunks } ] } ] }).then(async (artifactVersion) => { if (artifactVersion) { const dialectTokens = request.body.dialect?.split(":") if (dialectTokens?.length == 2) { const countryId = dialectTokens[0].split("-")[0] const languageId = dialectTokens[0].split("-")[1] const dialect = await db.Dialect.findOne({ where: { countryId: countryId, languageId: languageId } }) if (dialect) { const translationArtifactVersion = await db.TranslationArtifactVersion.create({ creatorId: request.user.id, name: dialect.name, artifactVersionId: artifactVersion.id, dialectId: dialect.id, entityId: request.user.individualEntityUser.entityUser.entityId, translationChapters: artifactVersion.chapters.map(chapter => { return { creatorId: request.user.id, chapterId: chapter.id, translationChunks: chapter.chunks.map(chunk => { return { creatorId: request.user.id, chunkId: chunk.id, text: chunk.text, index: chunk.index } }) } }) }, { include: [ { association: db.TranslationArtifactVersion.TranslationChapters, include: [ { association: db.TranslationChapter.TranslationChunks } ] } ] }) if (translationArtifactVersion) { return response.redirect(`/v/${artifactVersion.id}/translations/${translationArtifactVersion.id}`) } } } } next() }).catch(error => { next(error) }) }) return router; };