const express = require('express') const { Op } = require("sequelize") module.exports = function (options) { const db = options.db; var router = express.Router(); router.route('/:id') .get(function(request, response, next) { db.TranslationEntryVersion.findByPk(request.params.id, { include: [ { association: db.TranslationEntryVersion.EntryVersion, required: true, include: [ { association: db.EntryVersion.Chapters, required: true, limit: 1, // TODO: Order by chapter index } ] } ] }).then(translationEntryVersion => { if (translationEntryVersion) { response.redirect(`/translate/${translationEntryVersion.id}/chapter/${translationEntryVersion.entryVersion.chapters[0].id}`) } else { next() } }) }) router.route('/:id/chapter/:chapterId') .get(function(request, response, next) { db.Chapter.findByPk(request.params.chapterId, { include: [ { association: db.Chapter.Chunks, include: [ { association: db.Chunk.Translation } ] }, { association: db.Chapter.EntryVersion, required: true, include: [ { association: db.EntryVersion.TranslationEntryVersions, required: true, where: { id: request.params.id } }, { association: db.EntryVersion.Entry, include: [ { association: db.Entry.Dialect } ] } ] } ] }).then(chapter => { if (chapter) { response.display("translate-chapter", { user: request.user, pageTitle: `Translate Chapter ${chapter.name}`, chapter: chapter }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/chapter/:chapterId/t/:chunkIndex') .get(function(request, response, next) { const previousIndex = Number(request.params.chunkIndex)-1 const nextIndex = Number(request.params.chunkIndex)+1 db.Chunk.findAll({ where: { chapterId: request.params.chapterId, index: { [Op.between]: [ previousIndex, nextIndex ], } }, limit: 3, include: [ { association: db.Chunk.Translation }, { association: db.Chunk.Chapter, required: true, include: [ { association: db.Chapter.EntryVersion, required: true, include: [ { association: db.EntryVersion.TranslationEntryVersions, required: true, where: { id: request.params.id } }, { association: db.EntryVersion.Entry, include: [ { association: db.Entry.Dialect } ] } ] } ] } ] }).then(chunks => { if (chunks.length > 0) { const chunk = chunks.find(chunk => chunk.index == Number(request.params.chunkIndex)) if (chunk) { response.display("translate-chunk", { user: request.user, pageTitle: `Translate ${chunk.chapter.entryVersion.entry.name}`, chunk: chunk, previousChunk: chunks.find(chunk => chunk.index == previousIndex), nextChunk: chunks.find(chunk => chunk.index == nextIndex) }) } else { response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}`) } } else { next() } }).catch(error => { next(error) }) }) .post(function(request, response, next) { db.Chunk.findOne({ where: { chapterId: request.params.chapterId, index: request.params.chunkIndex }, include: [ { association: db.Chunk.Translation }, { association: db.Chunk.Chapter, required: true, include: [ { association: db.Chapter.EntryVersion, required: true, include: [ { association: db.EntryVersion.TranslationEntryVersions, required: true, where: { id: request.params.id } }, { association: db.EntryVersion.Entry, include: [ { association: db.Entry.Dialect } ] } ] } ] } ] }).then(async (chunk) => { if (chunk) { if (chunk.translation) { chunk.translation.text = request.body.translatedText await chunk.translation.save() } else { translation = await db.Translation.create({ chunkId: chunk.id, text: request.body.translatedText, translationEntryVersionId: request.params.id }) } response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}/t/${Number(request.params.chunkIndex)+1}`) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/back') .post(function(request, response, next) { db.TranslationEntryVersion.findByPk(request.params.id, { include: [ { association: db.TranslationEntryVersion.EntryVersion, required: true, include: [ { association: db.EntryVersion.Chapters, required: true, include: [ { association: db.Chapter.Chunks, required: true, include: [ { association: db.Chunk.Translation, required: true, // TODO: Where text is not null } ] } ] }, { association: db.EntryVersion.Entry, include: [ { association: db.Entry.Dialect } ] } ] } ] }).then((translationEntryVersion) => { if (translationEntryVersion) { // TODO: Create a backTranslation... backTranslatedEntry = {} return db.BackTranslation.create({ translationEntryVersionId: translationEntryVersion.id, entry: { name: translationEntryVersion.name + translationEntryVersion.entryVersion.entry.name, url: `/translate/${request.params.id}`, dialectId: translationEntryVersion.dialectId, licenseId: "copyright", // Should be closed because it's a back translation not meant to be published entryVersions: [ { tag: translationEntryVersion.entryVersion.tag, chapters: translationEntryVersion.entryVersion.chapters.map(chapter => { return { name: chapter.chunks[0].translation.text, // TODO: This isn't important... chunks: chapter.chunks.map(chunk => { return { text: chunk.translation.text, index: chunk.index } }) } }), translationEntryVersions: [ { dialectId: translationEntryVersion.entryVersion.entry.dialect.id, name: translationEntryVersion.entryVersion.entry.dialect.name } ] } ] } }, { include: [ { association: db.BackTranslation.Entry, include: [ { association: db.Entry.EntryVersions, include: [ { association: db.EntryVersion.Chapters, include: [ { association: db.Chapter.Chunks } ] }, { association: db.EntryVersion.TranslationEntryVersions } ] } ] } ] }) } else { return null } }).then(backTranslation => { if (backTranslation) { response.redirect(`/library/${backTranslation.entryId}`) } else { // TODO: Let user know we cannot back translate response.redirect(`/translate/${request.params.id}`) } }).catch(error => { next(error) }) }) router.route('/:id/campaign') .get(function(request, response, next) { db.TranslationEntryVersion.findByPk(request.params.id, { include: [ { association: db.TranslationEntryVersion.TranslationEntryVersionCampaigns, required: true, include: [ { association: db.TranslationEntryVersionCampaign.Campaign, required: true } ] } ] }).then(translationEntryVersion => { if (translationEntryVersion) { if (translationEntryVersion.translationEntryVersionCampaings.length == 1) { response.redirect(`/campaigns/${translationEntryVersion.translationEntryVersionCampaings[0].campaignId}`) } else { // TODO: Display translation entry campaign... next() } } else { response.redirect(`/translate/${request.params.id}/campaign/create`) } }) }) router.route('/:id/campaign/create') .get(function(request, response, next) { db.TranslationEntryVersion.findByPk(request.params.id, { include: [ { association: db.TranslationEntryVersion.EntryVersion, include: [ { association: db.EntryVersion.Entry } ] }, { association: db.TranslationEntryVersion.TranslationEntryVersionCampaigns, required: false, include: [ { association: db.TranslationEntryVersionCampaign.Campaign, required: false } ] } ] }).then(translationEntryVersion => { if (translationEntryVersion) { response.display("campaign-form", { user: request.user, pageTitle: "Campaign - Mantra", campaign: { name: `Campaign for ${translationEntryVersion.name} translation of ${translationEntryVersion.entryVersion.entry.name}` }, translationEntryVersions: [translationEntryVersion] }) } else { next() } }).catch(error => { next(error) }) }) return router; };