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 } ] }).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\r?\n/) db.Chapter.create({ name: chunks[0].trim(), artifactVersionId: artifactVersion.id, chunks: chunks.map((chunk,index) => { return { text: chunk, 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.TranslationArtifactVersion.ForkedFrom } ] }).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 } ] }).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({ name: dialect.name, artifactVersionId: artifactVersion.id, dialectId: dialect.id, entityId: request.user.individualEntityUser.entityUser.entityId }) if (translationArtifactVersion) { return response.redirect(`/v/${artifactVersion.id}/translations/${translationArtifactVersion.id}`) } } } } next() }).catch(error => { next(error) }) }) router.route('/:id/campaign/create') .get(function(request, response, next) { db.ArtifactVersion.findByPk(request.params.id, { include: [ { association: db.ArtifactVersion.Artifact, }, { association: db.ArtifactVersion.TranslationArtifactVersions } ] }).then(artifactVersion => { if (artifactVersion) { response.display("campaign-form", { user: request.user, pageTitle: "Campaign - Mantra", campaign: { name: `Campaign for ${artifactVersion.translationArtifactVersions.length} translations of ${artifactVersion.artifact.name}` }, translationArtifactVersions: artifactVersion.translationArtifactVersions }) } else { next() } }).catch(error => { next(error) }) }) return router; };