From 63ce5cbd2bd2d1af7a30c7edf0eb22ee9b8bf47f Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Tue, 26 Oct 2021 19:08:24 +0100 Subject: [PATCH] Handle existing membership in join --- src/config/sequelize.js | 2 - src/database/models/Survey.js | 8 +++ .../models/{SurveyUsers.js => SurveyUser.js} | 0 src/http/controllers/api/content.js | 2 + src/http/controllers/api/v2/surveys.js | 66 ++++++++++++++----- 5 files changed, 58 insertions(+), 20 deletions(-) rename src/database/models/{SurveyUsers.js => SurveyUser.js} (100%) diff --git a/src/config/sequelize.js b/src/config/sequelize.js index ede7836..99516bf 100644 --- a/src/config/sequelize.js +++ b/src/config/sequelize.js @@ -1,8 +1,6 @@ const { Op } = require('sequelize') const { config } = require('bootstrap') -console.log(config('database')) - module.exports = { development: { username: 'hackerfest', diff --git a/src/database/models/Survey.js b/src/database/models/Survey.js index 054fd31..8595c71 100644 --- a/src/database/models/Survey.js +++ b/src/database/models/Survey.js @@ -24,6 +24,14 @@ class Survey extends BaseModel { meta: this.meta, } } + + asOwnSurvey() { + const value = this.toJSON() + if (this.SurveyUser) { + value.answers = this.SurveyUser.toJSON().properties + } + return value + } } module.exports = (sequelize, DataTypes) => { diff --git a/src/database/models/SurveyUsers.js b/src/database/models/SurveyUser.js similarity index 100% rename from src/database/models/SurveyUsers.js rename to src/database/models/SurveyUser.js diff --git a/src/http/controllers/api/content.js b/src/http/controllers/api/content.js index 1f126df..c688964 100644 --- a/src/http/controllers/api/content.js +++ b/src/http/controllers/api/content.js @@ -144,5 +144,7 @@ exports.getWithin = async ctx => { toDate.toISOString(), ) + + console.log(metrics) ctx.body = { metrics } } diff --git a/src/http/controllers/api/v2/surveys.js b/src/http/controllers/api/v2/surveys.js index 76ceefe..9752c7d 100644 --- a/src/http/controllers/api/v2/surveys.js +++ b/src/http/controllers/api/v2/surveys.js @@ -2,11 +2,20 @@ const moment = require('moment') const UnauthorizedError = require("../../../../core/errors/UnauthorizedError"); const NotFoundError = require("../../../../core/errors/NotFoundError"); const HttpError = require("../../../../core/errors/HttpError"); -const { Survey, SurveyUser, User } = require('database/models') - +const { Survey, SurveyUser, User, sequelize } = require('database/models') +const { QueryTypes } = require('sequelize') exports.list = async ctx => { const { Sequelize, Survey } = require('database/models') + const user = await ctx.services['core.auth'].getUser() + + let includes = user ? [{ + model: SurveyUser, + required: false, + where: { + user_id: user.id, + } + }] : [] const surveys = await Survey.findAll({ where: { @@ -18,12 +27,14 @@ exports.list = async ctx => { }, public: true, } + }, { + include: includes }) console.log(surveys) ctx.body = { - surveys, + surveys: surveys.map(s => s.asOwnSurvey()), } } @@ -37,7 +48,10 @@ exports.joined = async ctx => { } const surveys = await user.getSurveys({ include: [SurveyUser] }) - console.log(surveys) + + ctx.body = { + surveys: surveys.map(s => s.asOwnSurvey()) + } } exports.join = async ctx => { @@ -53,6 +67,8 @@ exports.join = async ctx => { const { survey } = ctx.models const { properties = {} } = ctx.request.body + console.log(survey.properties, properties) + if (Object.values(survey.properties).length !== Object.values(properties).length) { throw new HttpError(400, 'Must provide values for all properties') } @@ -65,24 +81,38 @@ exports.join = async ctx => { surveyUserProperties[key] = properties[key] }) - console.log(survey) - const surveyUser = await user.createSurveyMembership({ - survey, - properties: surveyUserProperties, - }, { - include: [Survey] + const existing = await SurveyUser.findOne({ + where: { + survey_id: survey.id, + user_id: user.id, + } + }) + + if (existing) { + existing.properties = surveyUserProperties + await existing.save() + + ctx.body = { + survey, + answers: existing, + } + return + } + + const [rows, count] = await sequelize.query(` + INSERT INTO "survey_users" ("user_id","properties","survey_id") + VALUES (:user_id,:properties,:survey_id) RETURNING "user_id","survey_id","properties","created_at"; + `, { + replacements: { survey_id: survey.id, user_id: user.id, properties: JSON.stringify(properties) }, + type: QueryTypes.INSERT, }) - // const surveyUser = await SurveyUser.create({ - // survey_id: survey.id, - // user_id: user.id, - // properties: surveyUserProperties, - // }, { - // include: [Survey, User] - // }) + if (count === 0) { + throw new HttpError(500, 'Unable to join survey') + } - console.log(surveyUser) + const surveyUser = SurveyUser.build(rows[0]) ctx.body = { survey, -- GitLab