123 lines
5.1 KiB
JavaScript
123 lines
5.1 KiB
JavaScript
const express = require('express');
|
|
|
|
module.exports = function (options) {
|
|
const db = options.db;
|
|
var router = express.Router();
|
|
|
|
router.route('/t/:id')
|
|
.post(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
}).then(async (translationArtifactVersion) => {
|
|
if (translationArtifactVersion) {
|
|
// fork...
|
|
const forkedTranslationArtifactVersion = await db.TranslationArtifactVersion.create({
|
|
creatorId: request.user.id,
|
|
name: translationArtifactVersion.name,
|
|
artifactVersionId: translationArtifactVersion.artifactVersionId,
|
|
userId: request.user.id,
|
|
dialectId: translationArtifactVersion.dialectId,
|
|
forkedFromId: translationArtifactVersion.id,
|
|
entityId: request.user.individualEntityUser.entityUser.entityId
|
|
})
|
|
if (forkedTranslationArtifactVersion) {
|
|
return response.redirect(`/v/${forkedTranslationArtifactVersion.artifactVersionId}/translations/${forkedTranslationArtifactVersion.id}`)
|
|
}
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/e/:id')
|
|
.get(function(request, response, next) {
|
|
db.ArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact
|
|
}
|
|
]
|
|
}).then(artifactVersion => {
|
|
if (artifactVersion) {
|
|
response.display("fork-artifact-version", {
|
|
user: request.user,
|
|
pageTitle: "Add translation - Mantra",
|
|
artifactVersion: artifactVersion
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
.post(async function(request, response, next) {
|
|
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 artifactVersion = await db.ArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact
|
|
},
|
|
{
|
|
association: db.ArtifactVersion.TranslationArtifactVersions,
|
|
required: false,
|
|
where: {
|
|
dialectId: dialect.id,
|
|
}
|
|
}
|
|
]
|
|
})
|
|
|
|
if (artifactVersion) {
|
|
const forkedTranslationArtifactVersion = await forkArtifactVersion(
|
|
artifactVersion,
|
|
dialect,
|
|
request.user.id,
|
|
request.user.individualEntityUser.entityUser.entityId
|
|
)
|
|
if (forkedTranslationArtifactVersion) {
|
|
return response.redirect(`/v/${forkedTranslationArtifactVersion.artifactVersionId}/translations/${forkedTranslationArtifactVersion.id}`)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
next()
|
|
})
|
|
|
|
const forkArtifactVersion = (artifactVersion, dialect, userId, entityId) => {
|
|
if (artifactVersion.translationArtifactVersions.length == 0) {
|
|
// create a translationArtifactVersion with a new dialect...
|
|
return db.TranslationArtifactVersion.create({
|
|
creatorId: userId,
|
|
name: dialect.name,
|
|
artifactVersionId: artifactVersion.id,
|
|
dialectId: dialect.id,
|
|
userId: userId,
|
|
entityId: entityId
|
|
})
|
|
} else {
|
|
return db.TranslationArtifactVersion.create({
|
|
creatorId: userId,
|
|
name: artifactVersion.translationArtifactVersions[0].name,
|
|
artifactVersionId: artifactVersion.translationArtifactVersions[0].artifactVersionId,
|
|
userId: userId,
|
|
dialectId: artifactVersion.translationArtifactVersions[0].dialectId,
|
|
forkedFromId: artifactVersion.translationArtifactVersions[0].id
|
|
})
|
|
}
|
|
}
|
|
return router;
|
|
}; |