From 19909a7b5e404bb9950a903b4a4003609d48e09b Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sun, 9 Jan 2022 03:41:14 +0200 Subject: [PATCH] Project ownership handling --- server/router/projects/index.js | 205 +++++++++++++++++++++++++++++++- server/views/project.pug | 20 +++- 2 files changed, 214 insertions(+), 11 deletions(-) diff --git a/server/router/projects/index.js b/server/router/projects/index.js index 4ef82f9..40dbc45 100644 --- a/server/router/projects/index.js +++ b/server/router/projects/index.js @@ -82,19 +82,16 @@ module.exports = function (options) { }, { association: db.Project.Owner, - required: true, include: [ { association: db.Owner.OwnerEntities, - required: true, include: [ { association: db.OwnerEntity.Entity, - required: true, include: [ { association: db.Entity.EntityUsers, - required: true, + required: false, where: { userId: request.user?.id ?? null } @@ -174,6 +171,31 @@ module.exports = function (options) { }, { association: db.Project.ProjectTranslationArtifactVersions + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(async (project) => { @@ -239,6 +261,31 @@ module.exports = function (options) { ] } ] + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(async (project) => { @@ -292,6 +339,31 @@ module.exports = function (options) { }, { association: db.Project.ProjectTranslationArtifactVersions + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(async (project) => { @@ -332,6 +404,31 @@ module.exports = function (options) { }, { association: db.Project.ProjectTranslationArtifactVersions + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(async (project) => { @@ -375,6 +472,31 @@ module.exports = function (options) { { association: db.Project.Campaign, required: true + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(project => { @@ -423,6 +545,31 @@ module.exports = function (options) { ] } ] + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(project => { @@ -483,6 +630,31 @@ module.exports = function (options) { ] } ] + }, + { + association: db.Project.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 + } + } + ] + } + ] + } + ] } ] }).then(async (project) => { @@ -559,7 +731,30 @@ module.exports = function (options) { router.route('/:id/spreadsheet') .post(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) { db.TranslationArtifactVersion.findAll({ diff --git a/server/views/project.pug b/server/views/project.pug index f738684..7a4d49a 100644 --- a/server/views/project.pug +++ b/server/views/project.pug @@ -1,6 +1,12 @@ extend templates/layout.pug block content + - + const isOwnedByUser = project.owner.ownerEntities.some(ownerEntity => { + return ownerEntity.entity.entityUsers.some(entityUser => { + return entityUser.userId == user?.id + }) + }) .container .center h1= project.name @@ -19,15 +25,17 @@ block content h2 Entries each projectArtifactVersion in project.projectArtifactVersions p.flow-text= projectArtifactVersion.artifactVersion.artifact.name - .row - .col.s12 - a.btn.black(href=`/projects/${project.id}/add-artifact`) add from library + if isOwnedByUser + .row + .col.s12 + a.btn.black(href=`/projects/${project.id}/add-artifact`) add from library .divider h2 Translation each projectTranslationArtifactVersion in project.projectTranslationArtifactVersions p.flow-text #{projectTranslationArtifactVersion.translationArtifactVersion.name} - #{projectTranslationArtifactVersion.translationArtifactVersion.artifactVersion.artifact.name} - .row - .col.s12 - a.btn.black(href=`/projects/${project.id}/add-translation`) add translation + if isOwnedByUser + .row + .col.s12 + a.btn.black(href=`/projects/${project.id}/add-translation`) add translation //- TODO: Show project translation artifact versions //- TODO: Show project artifact versions \ No newline at end of file