2021-12-22 21:08:44 +02:00
const express = require ( 'express' )
2021-12-23 00:47:06 +02:00
const { Op } = require ( "sequelize" )
2023-01-09 00:45:42 +02:00
const idGenerator = require ( 'mantra-db-models/src/lib/id-generator' ) ;
2023-01-19 00:40:13 +02:00
const markdownProducer = require ( "../../../lib/markdown-producer" )
2021-12-22 21:08:44 +02:00
module . exports = function ( options ) {
const db = options . db ;
var router = express . Router ( ) ;
router . route ( '/:id' )
. get ( function ( request , response , next ) {
2022-01-05 03:04:43 +02:00
db . TranslationArtifactVersion . findByPk ( request . params . id , {
2021-12-22 21:08:44 +02:00
include : [
2022-01-09 02:33:41 +02:00
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Owner ,
// required: false,
include : [
{
association : db . Owner . OwnerEntities ,
// required: true,
include : [
{
association : db . OwnerEntity . Entity ,
// required: false,
include : [
{
association : db . Entity . EntityUsers ,
required : false ,
where : {
2022-01-09 03:22:40 +02:00
userId : request . user ? . id ? ? null
2022-01-09 02:33:41 +02:00
}
}
]
}
]
}
]
}
]
} ,
{
association : db . ArtifactVersion . Chapters
}
]
} ,
{
association : db . TranslationArtifactVersion . ForkedFrom
} ,
2021-12-22 21:08:44 +02:00
{
2022-01-08 00:59:36 +02:00
association : db . TranslationArtifactVersion . TranslationChapters ,
2022-01-09 02:33:41 +02:00
include : [
{
association : db . TranslationChapter . Chapter
}
]
} ,
{
association : db . TranslationArtifactVersion . Owner ,
// required: false,
include : [
{
association : db . Owner . OwnerEntities ,
// required: true,
include : [
{
association : db . OwnerEntity . Entity ,
// required: false,
include : [
{
association : db . Entity . EntityUsers ,
required : false ,
where : {
2022-01-09 03:22:40 +02:00
userId : request . user ? . id ? ? null
2022-01-09 02:33:41 +02:00
}
}
]
}
]
}
]
2022-06-19 23:31:16 +02:00
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionEditor ,
2021-12-22 21:08:44 +02:00
}
]
2022-01-05 03:04:43 +02:00
} ) . then ( translationArtifactVersion => {
if ( translationArtifactVersion ) {
2022-01-09 02:33:41 +02:00
response . display ( "translation-artifact-version" , {
user : request . user ,
pageTitle : "Translation Artifact - Mantra" ,
translationArtifactVersion : translationArtifactVersion ,
} )
2021-12-22 21:08:44 +02:00
} else {
next ( )
}
2022-01-09 02:33:41 +02:00
} ) . catch ( error => {
next ( error )
2021-12-22 21:08:44 +02:00
} )
} )
router . route ( '/:id/chapter/:chapterId' )
. get ( function ( request , response , next ) {
2022-01-08 00:59:36 +02:00
db . TranslationChapter . findByPk ( request . params . chapterId , {
2021-12-22 21:08:44 +02:00
include : [
2022-06-19 23:31:16 +02:00
{
association : db . TranslationChapter . Chapter ,
} ,
{
association : db . TranslationChapter . TranslationChapterTranslators ,
2022-06-22 02:25:56 +02:00
include : [
{
association : db . TranslationChapterTranslator . Translator ,
include : [
{
association : db . Entity . EntityEmail ,
include : [
{
association : db . EntityEmail . Email
}
]
}
]
}
]
2022-06-19 23:31:16 +02:00
} ,
{
association : db . TranslationChapter . TranslationChapterProofReaders ,
} ,
2021-12-22 21:08:44 +02:00
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChapter . TranslationChunks ,
2021-12-22 21:08:44 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChunk . Translation
2021-12-22 21:08:44 +02:00
}
]
} ,
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChapter . TranslationArtifactVersion ,
2021-12-22 21:08:44 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationArtifactVersion . ArtifactVersion ,
2021-12-25 01:35:32 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
2021-12-25 01:35:32 +02:00
}
]
2022-01-08 02:42:23 +02:00
} ,
{
association : db . TranslationArtifactVersion . Dialect
} ,
{
association : db . TranslationArtifactVersion . BackTranslationFrom
2022-06-19 23:31:16 +02:00
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionEditor ,
2021-12-22 21:08:44 +02:00
}
]
}
]
2022-01-08 00:59:36 +02:00
} ) . then ( translationChapter => {
if ( translationChapter ) {
2021-12-22 21:08:44 +02:00
response . display ( "translate-chapter" , {
user : request . user ,
2022-01-08 00:59:36 +02:00
pageTitle : ` Translate Chapter ${ translationChapter . name } ` ,
translationChapter : translationChapter
2022-01-07 23:18:02 +02:00
} )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
router . route ( '/:id/chapter/:chapterId/view' )
. get ( function ( request , response , next ) {
2022-01-08 00:59:36 +02:00
db . TranslationChapter . findByPk ( request . params . chapterId , {
2022-01-07 23:18:02 +02:00
include : [
2023-01-09 00:45:42 +02:00
{
association : db . TranslationChapter . Chapter ,
required : true
} ,
2022-01-07 23:18:02 +02:00
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChapter . TranslationChunks ,
2022-01-07 23:18:02 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChunk . Translation
2022-01-07 23:18:02 +02:00
}
]
} ,
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChapter . TranslationArtifactVersion ,
2022-01-07 23:18:02 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationArtifactVersion . ArtifactVersion ,
2022-01-07 23:18:02 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
2022-01-07 23:18:02 +02:00
}
]
}
]
}
]
2022-01-08 00:59:36 +02:00
} ) . then ( translationChapter => {
if ( translationChapter ) {
2023-01-19 00:40:13 +02:00
2022-01-07 23:18:02 +02:00
response . display ( "translate-chapter-view" , {
user : request . user ,
2022-01-08 00:59:36 +02:00
pageTitle : ` Translate Chapter ${ translationChapter . name } ` ,
2023-01-19 00:40:13 +02:00
translatedChapterOutput : markdownProducer . produceHtml ( translationChapter ) ,
2022-01-08 00:59:36 +02:00
translationChapter : translationChapter
2021-12-22 21:08:44 +02:00
} )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2023-01-09 00:45:42 +02:00
router . route ( '/:id/chapter/:chapterId/html' )
. get ( function ( request , response , next ) {
db . TranslationChapter . findByPk ( request . params . chapterId , {
include : [
{
association : db . TranslationChapter . Chapter ,
required : true
} ,
{
association : db . TranslationChapter . TranslationChunks ,
include : [
{
association : db . TranslationChunk . Translation
}
]
} ,
{
association : db . TranslationChapter . TranslationArtifactVersion ,
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
}
]
}
]
}
]
} ) . then ( translationChapter => {
if ( translationChapter ) {
response . contentType = "text/plain"
2023-01-19 00:40:13 +02:00
response . send ( markdownProducer . produceHtml ( translationChapter ) )
2023-01-09 00:45:42 +02:00
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
router . route ( '/:id/chapter/:chapterId/markdown' )
. get ( function ( request , response , next ) {
db . TranslationChapter . findByPk ( request . params . chapterId , {
include : [
{
association : db . TranslationChapter . Chapter ,
required : true
} ,
{
association : db . TranslationChapter . TranslationChunks ,
include : [
{
association : db . TranslationChunk . Translation
}
]
} ,
{
association : db . TranslationChapter . TranslationArtifactVersion ,
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
}
]
}
]
}
]
} ) . then ( translationChapter => {
if ( translationChapter ) {
response . contentType = "text/plain"
2023-01-19 00:40:13 +02:00
response . send ( markdownProducer . produceMarkdownString ( tokens ) )
2023-01-09 00:45:42 +02:00
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2021-12-22 22:34:58 +02:00
2021-12-23 00:47:06 +02:00
router . route ( '/:id/chapter/:chapterId/t/:chunkIndex' )
2021-12-22 22:34:58 +02:00
. get ( function ( request , response , next ) {
2021-12-23 00:47:06 +02:00
const previousIndex = Number ( request . params . chunkIndex ) - 1
const nextIndex = Number ( request . params . chunkIndex ) + 1
2022-01-08 00:59:36 +02:00
db . TranslationChunk . findAll ( {
2021-12-23 00:47:06 +02:00
where : {
2022-01-08 00:59:36 +02:00
translationChapterId : request . params . chapterId ,
2021-12-23 00:47:06 +02:00
index : {
[ Op . between ] : [
previousIndex ,
nextIndex
] ,
}
} ,
limit : 3 ,
2021-12-22 22:34:58 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChunk . Translation
2021-12-22 22:34:58 +02:00
} ,
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChunk . TranslationChapter ,
2021-12-23 00:47:06 +02:00
required : true ,
2021-12-22 22:34:58 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChapter . TranslationArtifactVersion ,
2021-12-22 22:34:58 +02:00
required : true ,
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationArtifactVersion . ArtifactVersion ,
2021-12-25 01:35:32 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
2021-12-25 01:35:32 +02:00
}
]
2021-12-22 22:34:58 +02:00
}
]
}
]
}
]
2022-01-08 00:59:36 +02:00
} ) . then ( translationChunks => {
if ( translationChunks . length > 0 ) {
const translationChunk = translationChunks . find ( translationChunk => translationChunk . index == Number ( request . params . chunkIndex ) )
if ( translationChunk ) {
2021-12-27 00:14:48 +02:00
response . display ( "translate-chunk" , {
user : request . user ,
2022-01-08 00:59:36 +02:00
pageTitle : ` Translate ${ translationChunk . translationChapter . translationArtifactVersion . artifactVersion . artifact . name } ` ,
translationChunk : translationChunk ,
previousTranslationChunk : translationChunks . find ( translationChunk => translationChunk . index == previousIndex ) ,
nextTranslationChunk : translationChunks . find ( translationChunk => translationChunk . index == nextIndex )
2021-12-27 00:14:48 +02:00
} )
} else {
response . redirect ( ` /translate/ ${ request . params . id } /chapter/ ${ request . params . chapterId } ` )
}
2021-12-22 22:34:58 +02:00
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2021-12-25 02:03:56 +02:00
. post ( function ( request , response , next ) {
2022-01-08 00:59:36 +02:00
db . TranslationChunk . findOne ( {
2021-12-25 02:03:56 +02:00
where : {
2022-01-08 00:59:36 +02:00
translationChapterId : request . params . chapterId ,
2021-12-25 02:03:56 +02:00
index : request . params . chunkIndex
} ,
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChunk . Translation
2021-12-25 02:03:56 +02:00
} ,
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChunk . TranslationChapter ,
2021-12-25 02:03:56 +02:00
required : true ,
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationChapter . TranslationArtifactVersion ,
2021-12-25 02:03:56 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . TranslationArtifactVersion . ArtifactVersion ,
2021-12-25 02:03:56 +02:00
include : [
{
2022-01-08 00:59:36 +02:00
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
2021-12-25 02:03:56 +02:00
}
]
}
]
}
]
}
]
2022-01-08 00:59:36 +02:00
} ) . then ( async ( translationChunk ) => {
if ( translationChunk ) {
if ( translationChunk . translation ) {
translationChunk . translation . text = request . body . translatedText
await translationChunk . translation . save ( )
2021-12-25 02:03:56 +02:00
} else {
translation = await db . Translation . create ( {
2022-01-07 00:27:14 +02:00
creatorId : request . user . id ,
2022-01-08 00:59:36 +02:00
translationChunkId : translationChunk . id ,
2021-12-25 02:03:56 +02:00
text : request . body . translatedText ,
2022-01-05 03:04:43 +02:00
translationArtifactVersionId : request . params . id
2021-12-25 02:03:56 +02:00
} )
}
2021-12-27 00:14:48 +02:00
response . redirect ( ` /translate/ ${ request . params . id } /chapter/ ${ request . params . chapterId } /t/ ${ Number ( request . params . chunkIndex ) + 1 } ` )
2021-12-25 02:03:56 +02:00
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2021-12-27 00:00:36 +02:00
router . route ( '/:id/back' )
. post ( function ( request , response , next ) {
2022-01-05 03:04:43 +02:00
db . TranslationArtifactVersion . findByPk ( request . params . id , {
2021-12-27 00:00:36 +02:00
include : [
{
2022-01-08 02:42:23 +02:00
association : db . TranslationArtifactVersion . Dialect ,
} ,
{
association : db . TranslationArtifactVersion . TranslationChapters ,
2021-12-27 00:00:36 +02:00
include : [
{
2022-01-08 02:42:23 +02:00
association : db . TranslationChapter . TranslationChunks ,
2021-12-27 00:00:36 +02:00
include : [
{
2022-01-08 02:42:23 +02:00
association : db . TranslationChunk . Translation ,
2021-12-27 00:00:36 +02:00
}
]
2022-01-08 02:42:23 +02:00
}
]
} ,
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
2021-12-27 00:00:36 +02:00
{
2022-01-05 03:04:43 +02:00
association : db . ArtifactVersion . Artifact ,
2021-12-27 00:00:36 +02:00
include : [
{
2022-01-05 03:04:43 +02:00
association : db . Artifact . Dialect
2021-12-27 00:00:36 +02:00
}
]
}
]
2022-01-08 03:34:51 +02:00
} ,
{
association : db . TranslationArtifactVersion . BackTranslation ,
} ,
2022-01-09 01:44:02 +02:00
{
association : db . TranslationArtifactVersion . Owner ,
required : true ,
include : [
{
association : db . Owner . OwnerEntities ,
required : true ,
include : [
{
association : db . OwnerEntity . Entity ,
required : true ,
include : [
{
association : db . Entity . EntityUsers ,
required : true ,
where : {
2022-01-09 03:22:40 +02:00
userId : request . user ? . id ? ? null
2022-01-09 01:44:02 +02:00
}
}
]
}
]
}
]
}
2021-12-27 00:00:36 +02:00
]
2022-01-05 03:04:43 +02:00
} ) . then ( ( translationArtifactVersion ) => {
if ( translationArtifactVersion ) {
2022-01-08 03:34:51 +02:00
if ( translationArtifactVersion . backTranslation ) {
// translationArtifactVersion has a back translation...xx§
return translationArtifactVersion . backTranslation
} else {
return db . TranslationArtifactVersion . create ( {
creatorId : request . user . id ,
name : translationArtifactVersion . artifactVersion . artifact . dialect . name ,
artifactVersionId : translationArtifactVersion . artifactVersionId ,
dialectId : translationArtifactVersion . dialect . id ,
2022-06-17 22:55:52 +02:00
visibility : translationArtifactVersion . visibility ,
2022-01-08 03:34:51 +02:00
backTranslationFromId : translationArtifactVersion . id ,
2022-01-08 21:22:51 +02:00
owner : {
ownerEntities : [
{
entityId : request . user . individualEntityUser . entityUser . entityId ,
}
]
} ,
2022-01-08 03:34:51 +02:00
translationChapters : translationArtifactVersion . translationChapters . map ( translationChapter => {
return {
creatorId : request . user . id ,
chapterId : translationChapter . chapterId ,
2022-01-15 00:59:30 +02:00
index : translationChapter . index ,
2022-01-08 03:34:51 +02:00
translationChunks : translationChapter . translationChunks . map ( translationChunk => {
return {
creatorId : request . user . id ,
chunkId : translationChunk . chunkId ,
text : translationChunk . translation . text ,
index : translationChunk . index
}
} )
}
} )
} , {
include : [
{
association : db . TranslationArtifactVersion . TranslationChapters ,
include : [
{
association : db . TranslationChapter . TranslationChunks
}
]
2022-01-08 21:22:51 +02:00
} ,
{
association : db . TranslationArtifactVersionCampaign . Owner ,
include : [
{
association : db . Owner . OwnerEntities
}
]
2022-01-08 03:34:51 +02:00
}
]
2022-01-08 02:42:23 +02:00
} )
2022-01-08 03:34:51 +02:00
}
2021-12-27 00:00:36 +02:00
} else {
return null
}
2022-01-08 02:42:23 +02:00
} ) . then ( translationArtifactVersion => {
if ( translationArtifactVersion ) {
response . redirect ( ` /translate/ ${ translationArtifactVersion . id } ` )
2021-12-27 00:00:36 +02:00
} else {
2022-01-08 03:34:51 +02:00
next ( )
2021-12-27 00:00:36 +02:00
}
} ) . catch ( error => {
next ( error )
} )
} )
2021-12-27 02:25:03 +02:00
2022-01-06 19:44:39 +02:00
router . route ( '/:id/campaigns' )
2021-12-27 02:25:03 +02:00
. get ( function ( request , response , next ) {
2022-01-05 03:04:43 +02:00
db . TranslationArtifactVersion . findByPk ( request . params . id , {
2021-12-27 02:25:03 +02:00
include : [
{
2022-01-05 03:04:43 +02:00
association : db . TranslationArtifactVersion . TranslationArtifactVersionCampaigns ,
2021-12-27 02:25:03 +02:00
include : [
{
2022-01-05 03:04:43 +02:00
association : db . TranslationArtifactVersionCampaign . Campaign ,
2022-01-09 01:44:02 +02:00
}
]
} ,
{
association : db . TranslationArtifactVersion . Owner ,
required : true ,
include : [
{
association : db . Owner . OwnerEntities ,
required : true ,
include : [
{
association : db . OwnerEntity . Entity ,
required : true ,
include : [
{
association : db . Entity . EntityUsers ,
required : true ,
where : {
2022-01-09 03:22:40 +02:00
userId : request . user ? . id ? ? null
2022-01-09 01:44:02 +02:00
}
}
]
}
]
2021-12-27 02:25:03 +02:00
}
]
}
]
2022-01-05 03:04:43 +02:00
} ) . then ( translationArtifactVersion => {
if ( translationArtifactVersion ) {
2022-01-09 01:44:02 +02:00
if ( translationArtifactVersion . translationArtifactVersionCampaigns . length == 0 ) {
response . redirect ( ` /translate/ ${ request . params . id } /campaigns/create ` )
} else if ( translationArtifactVersion . translationArtifactVersionCampaigns . length == 1 ) {
2022-01-06 19:44:39 +02:00
response . redirect ( ` /campaigns/ ${ translationArtifactVersion . translationArtifactVersionCampaigns [ 0 ] . campaignId } ` )
2021-12-27 02:25:03 +02:00
} else {
2022-01-05 03:04:43 +02:00
// TODO: Display translation artifact campaign...
2021-12-27 02:25:03 +02:00
next ( )
}
} else {
2022-01-09 01:44:02 +02:00
next ( )
2021-12-27 02:25:03 +02:00
}
} )
} )
2022-01-06 19:44:39 +02:00
router . route ( '/:id/campaigns/create' )
2021-12-27 02:25:03 +02:00
. get ( function ( request , response , next ) {
2022-01-05 03:04:43 +02:00
db . TranslationArtifactVersion . findByPk ( request . params . id , {
2021-12-27 02:25:03 +02:00
include : [
{
2022-01-05 03:04:43 +02:00
association : db . TranslationArtifactVersion . ArtifactVersion ,
2021-12-27 02:25:03 +02:00
include : [
{
2022-01-05 03:04:43 +02:00
association : db . ArtifactVersion . Artifact
2021-12-27 02:25:03 +02:00
}
]
} ,
{
2022-01-05 03:04:43 +02:00
association : db . TranslationArtifactVersion . TranslationArtifactVersionCampaigns ,
2021-12-27 02:25:03 +02:00
include : [
{
2022-01-05 03:04:43 +02:00
association : db . TranslationArtifactVersionCampaign . Campaign ,
2022-01-09 01:44:02 +02:00
}
]
} ,
{
association : db . TranslationArtifactVersion . Owner ,
required : true ,
include : [
{
association : db . Owner . OwnerEntities ,
required : true ,
include : [
{
association : db . OwnerEntity . Entity ,
required : true ,
include : [
{
association : db . Entity . EntityUsers ,
required : true ,
where : {
2022-01-09 03:22:40 +02:00
userId : request . user ? . id ? ? nulls
2022-01-09 01:44:02 +02:00
}
}
]
}
]
2021-12-27 02:25:03 +02:00
}
]
}
]
2022-01-05 03:04:43 +02:00
} ) . then ( translationArtifactVersion => {
if ( translationArtifactVersion ) {
2022-01-09 01:44:02 +02:00
// TODO: Check if campaign exits...
2021-12-27 02:25:03 +02:00
response . display ( "campaign-form" , {
user : request . user ,
pageTitle : "Campaign - Mantra" ,
campaign : {
2022-01-05 03:04:43 +02:00
name : ` Campaign for ${ translationArtifactVersion . name } translation of ${ translationArtifactVersion . artifactVersion . artifact . name } `
2021-12-27 02:25:03 +02:00
} ,
2022-01-05 03:04:43 +02:00
translationArtifactVersions : [ translationArtifactVersion ]
2021-12-27 02:25:03 +02:00
} )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2022-01-06 02:00:49 +02:00
. post ( function ( request , response , next ) {
db . TranslationArtifactVersion . findByPk ( request . params . id , {
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact
}
]
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionCampaigns ,
include : [
{
association : db . TranslationArtifactVersionCampaign . Campaign ,
2022-01-09 01:44:02 +02:00
}
]
} ,
{
association : db . TranslationArtifactVersion . Owner ,
required : true ,
include : [
{
association : db . Owner . OwnerEntities ,
required : true ,
include : [
{
association : db . OwnerEntity . Entity ,
required : true ,
include : [
{
association : db . Entity . EntityUsers ,
required : true ,
where : {
2022-01-09 03:22:40 +02:00
userId : request . user ? . id ? ? null
2022-01-09 01:44:02 +02:00
}
}
]
}
]
2022-01-06 02:00:49 +02:00
}
]
}
]
} ) . then ( async ( translationArtifactVersion ) => {
if ( translationArtifactVersion ) {
// TODO: Create campaign...
const campaign = await db . Campaign . create ( {
2022-01-07 00:27:14 +02:00
creatorId : request . user . id ,
2022-01-06 02:00:49 +02:00
name : request . body . name ,
description : request . body . description ,
// defaultSatoshis: [request.body.satoshis].flat()[0],
defaultSatoshis : request . body . satoshis ,
2022-01-08 21:22:51 +02:00
owner : {
ownerEntities : [
{
entityId : request . user . individualEntityUser . entityUser . entityId ,
}
]
} ,
2022-01-06 02:00:49 +02:00
translationArtifactVersionCampaigns : [
{
2022-01-07 02:15:44 +02:00
creatorId : request . user . id ,
2022-01-06 02:00:49 +02:00
translationArtifactVersionId : translationArtifactVersion . id ,
2022-01-08 21:22:51 +02:00
owner : {
ownerEntities : [
{
entityId : request . user . individualEntityUser . entityUser . entityId ,
}
]
} ,
2022-01-06 02:00:49 +02:00
satoshis : request . body . satoshis
}
]
} , {
include : [
{
2022-01-08 21:22:51 +02:00
association : db . Campaign . TranslationArtifactVersionCampaigns ,
include : [
{
association : db . TranslationArtifactVersionCampaign . Owner ,
include : [
{
association : db . Owner . OwnerEntities
}
]
}
]
} ,
{
association : db . Campaign . Owner ,
include : [
{
association : db . Owner . OwnerEntities
}
]
2022-01-06 02:00:49 +02:00
}
]
} )
response . redirect ( ` /campaigns/ ${ campaign . id } ` )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2021-12-27 02:25:03 +02:00
2022-01-06 19:44:39 +02:00
router . route ( '/:id/pledges' )
. get ( function ( request , response , next ) {
db . TranslationArtifactVersion . findByPk ( request . params . id , {
include : [
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionPledges ,
required : true ,
include : [
{
association : db . TranslationArtifactVersionPledge . Pledge ,
required : true
}
]
}
]
} ) . then ( translationArtifactVersion => {
if ( translationArtifactVersion ) {
if ( translationArtifactVersion . translationArtifactVersionPledges . length == 1 ) {
response . redirect ( ` /pledges/ ${ translationArtifactVersion . translationArtifactVersionPledges [ 0 ] . campaignId } ` )
} else {
// TODO: Display translation artifact campaign...
next ( )
}
} else {
response . redirect ( ` /translate/ ${ request . params . id } /pledges/create ` )
}
} )
} )
router . route ( '/:id/pledges/create' )
. get ( function ( request , response , next ) {
db . TranslationArtifactVersion . findByPk ( request . params . id , {
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact
}
]
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionPledges ,
required : false ,
include : [
{
association : db . TranslationArtifactVersionPledge . Pledge ,
required : false
}
]
}
]
} ) . then ( translationArtifactVersion => {
if ( translationArtifactVersion ) {
response . display ( "pledge-form" , {
user : request . user ,
pageTitle : "Pledge - Mantra" ,
campaign : {
name : ` Pledge for ${ translationArtifactVersion . name } translation of ${ translationArtifactVersion . artifactVersion . artifact . name } ` ,
} ,
translationArtifactVersions : [ translationArtifactVersion ]
} )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
. post ( function ( request , response , next ) {
db . TranslationArtifactVersion . findByPk ( request . params . id , {
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact
}
]
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionPledges ,
required : false ,
include : [
{
association : db . TranslationArtifactVersionPledge . Pledge ,
required : false
}
]
}
]
} ) . then ( async ( translationArtifactVersion ) => {
if ( translationArtifactVersion ) {
// TODO: Create campaign...
const pledge = await db . Pledge . create ( {
2022-01-07 00:27:14 +02:00
creatorId : request . user . id ,
2022-01-06 19:44:39 +02:00
message : request . body . message ,
2022-01-08 21:22:51 +02:00
owner : {
ownerEntities : [
{
entityId : request . user . individualEntityUser . entityUser . entityId
}
]
} ,
2022-01-06 19:44:39 +02:00
translationArtifactVersionPledges : [
{
2022-01-07 02:15:44 +02:00
creatorId : request . user . id ,
2022-01-06 19:44:39 +02:00
translationArtifactVersionId : translationArtifactVersion . id ,
2022-01-08 21:22:51 +02:00
owner : {
ownerEntities : [
{
entityId : request . user . individualEntityUser . entityUser . entityId ,
}
]
} ,
2022-01-06 19:44:39 +02:00
satoshis : request . body . satoshis
}
]
} , {
include : [
{
2022-01-08 21:22:51 +02:00
association : db . Pledge . TranslationArtifactVersionPledges ,
include : [
{
association : db . TranslationArtifactVersionPledge . Owner ,
include : [
{
association : db . Owner . OwnerEntities
}
]
}
]
} ,
{
association : db . Pledge . Owner ,
include : [
{
association : db . Owner . OwnerEntities
}
]
2022-01-06 19:44:39 +02:00
}
]
} )
response . redirect ( ` /pledges/ ${ pledge . id } ` )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2022-06-22 02:25:56 +02:00
router . route ( '/:id/chapter/:chapterId/translator' )
. get ( function ( request , response , next ) {
db . TranslationChapter . findByPk ( request . params . chapterId , {
include : [
{
association : db . TranslationChapter . Chapter ,
} ,
{
association : db . TranslationChapter . TranslationChapterTranslators ,
2022-06-22 03:39:36 +02:00
include : [
{
association : db . TranslationChapterTranslator . Translator ,
include : [
{
association : db . Entity . EntityEmail ,
include : [
{
association : db . EntityEmail . Email
}
]
}
]
}
]
2022-06-22 02:25:56 +02:00
} ,
{
association : db . TranslationChapter . TranslationChapterProofReaders ,
} ,
{
association : db . TranslationChapter . TranslationChunks ,
include : [
{
association : db . TranslationChunk . Translation
}
]
} ,
{
association : db . TranslationChapter . TranslationArtifactVersion ,
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
}
]
} ,
{
association : db . TranslationArtifactVersion . Dialect
} ,
{
association : db . TranslationArtifactVersion . BackTranslationFrom
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionEditor ,
}
]
}
]
} ) . then ( translationChapter => {
if ( translationChapter ) {
response . display ( "translate-chapter-translator-form" , {
user : request . user ,
pageTitle : ` Translate Chapter ${ translationChapter . name } ` ,
translationChapter : translationChapter
} )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
. post ( function ( request , response , next ) {
db . TranslationChapter . findByPk ( request . params . chapterId , {
include : [
{
association : db . TranslationChapter . Chapter ,
} ,
{
association : db . TranslationChapter . TranslationChapterTranslators ,
} ,
{
association : db . TranslationChapter . TranslationChapterProofReaders ,
} ,
{
association : db . TranslationChapter . TranslationChunks ,
include : [
{
association : db . TranslationChunk . Translation
}
]
} ,
{
association : db . TranslationChapter . TranslationArtifactVersion ,
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
include : [
{
association : db . ArtifactVersion . Artifact ,
include : [
{
association : db . Artifact . Dialect
}
]
}
]
} ,
{
association : db . TranslationArtifactVersion . Dialect
} ,
{
association : db . TranslationArtifactVersion . BackTranslationFrom
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionEditor ,
}
]
}
]
} ) . then ( async ( translationChapter ) => {
if ( translationChapter ) {
// TODO: Check if translationChapter has a TranslationChapterTranslator with this email...
// TODO: FindOrCreate Email
const [ email , created ] = await db . Email . findOrCreate ( {
where : {
address : request . body . translator
} ,
include : [
{
association : db . Email . EntityEmails ,
required : false ,
where : {
creatorId : request . user . id
}
} ,
{
association : db . Email . UserEmail ,
include : [
{
association : db . UserEmail . User ,
include : [
{
association : db . User . IndividualEntityUser ,
include : [
{
association : db . IndividualEntityUser . EntityUser
}
]
}
]
}
]
}
] ,
defaults : {
address : request . body . translator
}
} )
if ( email . userEmail ) {
// if email has user associated with it tie that users individual entity to a new TranslationChapterTranslator object
await db . TranslationChapterTranslator . create ( {
translationChapterId : translationChapter . id ,
creatorId : request . user . id ,
translatorId : email . userEmail . user . individualEntityUser . entityUser . entityId
} )
} else if ( email . entityEmails ? . length > 0 ) {
// TODO: If email has an EntityEmail (which has this user as a creator) tie that entityEmail to a new TranslationChapterTranslator
await db . TranslationChapterTranslator . create ( {
translationChapterId : translationChapter . id ,
creatorId : request . user . id ,
translatorId : email . entityEmails [ 0 ] . entityId
} )
} else {
// if email doesn't have a user/entityEmail associated with it (newly created) create an EntityEmail and tie it to a new TranslationChapterTranslator object
await db . TranslationChapterTranslator . create ( {
translationChapterId : translationChapter . id ,
creatorId : request . user . id ,
translator : {
name : idGenerator . generateRandomAlphanumeric ( ) ,
type : "individual" ,
entityEmail : {
creatorId : request . user . id ,
emailAddress : email . address ,
type : 'translator'
}
}
} , {
include : [
{
association : db . TranslationChapterTranslator . Translator ,
include : [
{
association : db . Entity . EntityEmail
}
]
}
]
} )
}
response . redirect ( ` /translate/ ${ translationChapter . translationArtifactVersion . id } /chapter/ ${ translationChapter . id } ` )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2022-06-22 03:39:36 +02:00
router . route ( '/:id/chapter/:chapterId/translator/:translatorId' )
. get ( function ( request , response , next ) {
db . TranslationChapterTranslator . findByPk ( request . params . translatorId , {
include : [
{
association : db . TranslationChapterTranslator . TranslationChapter ,
required : true ,
include : [
{
association : db . TranslationChapter . TranslationArtifactVersion ,
required : true ,
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
required : true ,
include : [
{
association : db . ArtifactVersion . Artifact ,
required : true ,
include : [
{
association : db . Artifact . Dialect
} ,
{
association : db . Artifact . Owner ,
required : true ,
include : [
{
association : db . Owner . OwnerEntities ,
required : true ,
include : [
{
association : db . OwnerEntity . Entity ,
required : true ,
include : [
{
association : db . Entity . EntityUsers ,
required : true ,
where : {
userId : request . user ? . id ? ? null
}
}
]
}
]
}
]
}
]
}
]
} ,
{
association : db . TranslationArtifactVersion . Dialect
} ,
{
association : db . TranslationArtifactVersion . BackTranslationFrom
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionEditor ,
}
]
}
]
} ,
{
association : db . TranslationChapterTranslator . Translator ,
include : [
{
association : db . Entity . EntityEmail ,
include : [
{
association : db . EntityEmail . Email
}
]
}
]
}
]
} ) . then ( translationChapterTranslator => {
if ( translationChapterTranslator ) {
response . display ( "translate-chapter-translator-remove" , {
user : request . user ,
pageTitle : ` Translator ${ translationChapterTranslator . name } ` ,
translationChapterTranslator : translationChapterTranslator
} )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
. post ( function ( request , response , next ) {
db . TranslationChapterTranslator . findByPk ( request . params . translatorId , {
include : [
{
association : db . TranslationChapterTranslator . TranslationChapter ,
required : true ,
include : [
{
association : db . TranslationChapter . TranslationArtifactVersion ,
required : true ,
include : [
{
association : db . TranslationArtifactVersion . ArtifactVersion ,
required : true ,
include : [
{
association : db . ArtifactVersion . Artifact ,
required : true ,
include : [
{
association : db . Artifact . Dialect
} ,
{
association : db . Artifact . Owner ,
required : true ,
include : [
{
association : db . Owner . OwnerEntities ,
required : true ,
include : [
{
association : db . OwnerEntity . Entity ,
required : true ,
include : [
{
association : db . Entity . EntityUsers ,
required : true ,
where : {
userId : request . user ? . id ? ? null
}
}
]
}
]
}
]
}
]
}
]
} ,
{
association : db . TranslationArtifactVersion . Dialect
} ,
{
association : db . TranslationArtifactVersion . BackTranslationFrom
} ,
{
association : db . TranslationArtifactVersion . TranslationArtifactVersionEditor ,
}
]
}
]
} ,
{
association : db . TranslationChapterTranslator . Translator ,
include : [
{
association : db . Entity . EntityEmail ,
include : [
{
association : db . EntityEmail . Email
}
]
}
]
}
]
} ) . then ( async ( translationChapterTranslator ) => {
if ( translationChapterTranslator ) {
await translationChapterTranslator . destroy ( )
response . redirect ( ` /translate/ ${ translationChapterTranslator . translationChapter . translationArtifactVersion . id } /chapter/ ${ translationChapterTranslator . translationChapter . id } ` )
} else {
next ( )
}
} ) . catch ( error => {
next ( error )
} )
} )
2021-12-22 21:08:44 +02:00
return router ;
} ;