Skip to content
Snippets Groups Projects
Verified Commit 63ce5cbd authored by Louis's avatar Louis :fire:
Browse files

Handle existing membership in join

parent c73c82cf
No related branches found
No related tags found
No related merge requests found
const { Op } = require('sequelize')
const { config } = require('bootstrap')
console.log(config('database'))
module.exports = {
development: {
username: 'hackerfest',
......
......@@ -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) => {
......
......@@ -144,5 +144,7 @@ exports.getWithin = async ctx => {
toDate.toISOString(),
)
console.log(metrics)
ctx.body = { metrics }
}
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment