From 2f14e479a07c3f807ec9c1f144dc2774103782d5 Mon Sep 17 00:00:00 2001 From: kngako Date: Mon, 9 Jan 2023 02:19:32 +0200 Subject: [PATCH] Add ability to add language to project entities --- server/router/projects/index.js | 226 ++++++++++++++++++++++++- server/views/project-language-form.pug | 22 +++ server/views/project-tracker.pug | 9 +- 3 files changed, 254 insertions(+), 3 deletions(-) create mode 100644 server/views/project-language-form.pug diff --git a/server/router/projects/index.js b/server/router/projects/index.js index 41eebb8..b156b36 100644 --- a/server/router/projects/index.js +++ b/server/router/projects/index.js @@ -447,7 +447,7 @@ module.exports = function (options) { }) if (translationArtifactVersion) { - const projectTranslationArtifactVersion = db.ProjectTranslationArtifactVersion.create({ + const projectTranslationArtifactVersion = await db.ProjectTranslationArtifactVersion.create({ creatorId: request.user.id, projectId: project.id, translationArtifactVersionId: translationArtifactVersion.id @@ -1536,5 +1536,229 @@ module.exports = function (options) { }) }) + router.route('/:id/languages/add') + .get(function(request, response, next) { + db.Project.findByPk(request.params.id, { + include: [ + { + association: db.Project.Owner, + include: [ + { + association: db.Owner.OwnerEntities, + include: [ + { + association: db.OwnerEntity.Entity, + include: [ + { + association: db.Entity.EntityUsers, + required: false, + where: { + userId: request.user?.id ?? null + } + } + ] + } + ] + } + ] + } + ] + }).then(async (project) => { + if (project) { + // TODO: Add language to + response.display("project-language-form", { + user: request.user, + pageTitle: "Project Langauges - Mantra", + project: project + }) + } else { + next() + } + }).catch(error => { + next(error) + }) + }) + .post(async function(request, response, next) { + console.log("Request: ", request.body) + + // TODO: Find dialectId + 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) { + db.Project.findByPk(request.params.id, { + include: [ + { + association: db.Project.ProjectArtifactVersions, + include: [ + { + association: db.ProjectArtifactVersion.ArtifactVersion, + include: [ + { + association: db.ArtifactVersion.Artifact + }, + { + association: db.ArtifactVersion.TranslationArtifactVersions, + where: { + backTranslationFromId: null, + // forkedFromId: null + dialectId: dialect.id + }, + required: false, + include: [ + { + association: db.TranslationArtifactVersion.ProjectTranslationArtifactVersions, + where: { + projectId: request.params.id + }, + required: false + } + ] + }, + { + association: db.ArtifactVersion.Chapters, + include: [ + { + association: db.Chapter.Chunks + } + ] + } + ] + }, + + ] + }, + { + association: db.Project.Owner, + include: [ + { + association: db.Owner.OwnerEntities, + include: [ + { + association: db.OwnerEntity.Entity, + include: [ + { + association: db.Entity.EntityUsers, + required: false, // TODO: Make required... + where: { + userId: request.user?.id ?? null + } + } + ] + } + ] + } + ] + } + ] + }).then(async (project) => { + if (project) { + project.projectArtifactVersions + for (let i = 0; i < project.projectArtifactVersions.length; i++) { + const artifactVersion = project.projectArtifactVersions[i].artifactVersion; + + if (artifactVersion.translationArtifactVersions.length == 0) { + console.log("Add this new language: ", dialect.toJSON()) + const translationArtifactVersion = await db.TranslationArtifactVersion.create({ + creatorId: request.user.id, + name: dialect.name, + artifactVersionId: artifactVersion.id, + visibility: artifactVersion.artifact.visibility, + dialectId: dialect.id, + owner: { + ownerEntities: [ + { + entityId: request.user.individualEntityUser.entityUser.entityId, + } + ] + }, + translationChapters: artifactVersion.chapters.map(chapter => { + return { + creatorId: request.user.id, + chapterId: chapter.id, + index: chapter.index, + translationChunks: chapter.chunks.map(chunk => { + return { + creatorId: request.user.id, + chunkId: chunk.id, + text: chunk.text, + index: chunk.index + } + }) + } + }), + projectTranslationArtifactVersions: [ + { + creatorId: request.user.id, + projectId: project.id + } + ] + }, { + include: [ + { + association: db.TranslationArtifactVersion.TranslationChapters, + include: [ + { + association: db.TranslationChapter.TranslationChunks + } + ] + }, + { + association: db.TranslationArtifactVersion.ProjectTranslationArtifactVersions, + }, + { + association: db.TranslationArtifactVersion.Owner, + include: [ + { + association: db.Owner.OwnerEntities + } + ] + } + ] + }) + + // TODO: Associate in the project... + + } else { + console.log("Language already supported") + const translationArtifactVersion = artifactVersion.translationArtifactVersions[0] + + // Associate in the project... if association doesn't exists + if (translationArtifactVersion.projectTranslationArtifactVersions == 0) { + const projectTranslationArtifactVersion = await db.ProjectTranslationArtifactVersion.create({ + creatorId: request.user.id, + projectId: project.id, + translationArtifactVersionId: translationArtifactVersion.id + }) + } + + } + } + // TODO: Load all the entries associated with this project and translate them... + return response.redirect(`/projects/${project.id}`) + } else { + next() + } + }).catch(error => { + next(error) + }) + } else { + // TODO: Show error + next() + } + } else { + // TODO: Show error... + next() + } + }) return router; }; \ No newline at end of file diff --git a/server/views/project-language-form.pug b/server/views/project-language-form.pug new file mode 100644 index 0000000..ffd8a0b --- /dev/null +++ b/server/views/project-language-form.pug @@ -0,0 +1,22 @@ +extend templates/layout.pug + +block content + .container + .center + h1 Languages of #{project.name} + + .row + .col.s12 + form.row(action=`/projects/${project.id}/languages/add`, method="post") + + .col.s12.input-field + i.material-icons.prefix record_voice_over + input#dialect-autocomplete.autocomplete(type="text", name="dialect", required, autocomplete="off") + label(for="dialect-autocomplete") Dialect which this projects translations will be made in. + .col.s12 + button.btn.black(type="submit") add language + + +block additionalScripts + script + include js/init-dialect-autocomplete.js \ No newline at end of file diff --git a/server/views/project-tracker.pug b/server/views/project-tracker.pug index 1b76d63..06d98cb 100644 --- a/server/views/project-tracker.pug +++ b/server/views/project-tracker.pug @@ -37,7 +37,12 @@ block content a.btn.black(href=`/projects/${project.id}/import`) i.material-icons.left file_upload span import spreadsheets - + + .row + .col.s12 + a.btn.black(href=`/projects/${project.id}/languages/add`) + i.material-icons.left record_voice_over + span add language .divider table.highlight @@ -50,7 +55,7 @@ block content span th Name th - a.dropdown-trigger language ▼ + a.dropdown-trigger language (#{project.get("languageCount")}) ▼ th Artifact th Chapter th