Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Merge pull request #88 from ONSdigital/402-properties-decimals
Browse files Browse the repository at this point in the history
Add properties to answers
  • Loading branch information
samiwel committed Jul 10, 2018
2 parents 78db290 + 0f10410 commit 5048f64
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 273 deletions.
14 changes: 14 additions & 0 deletions migrations/20180621140416_add_properties_to_answers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = function(knex) {
return knex.schema.table("Answers", table => {
table
.jsonb("properties")
.defaultsTo("{}")
.notNullable();
});
};

exports.down = function(knex) {
return knex.schema.table("Answers", table => {
table.dropColumn("properties");
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.up = function(knex) {
return knex.schema.raw(`
UPDATE "Answers"
SET properties = json_build_object(
'required', mandatory
)::jsonb;
`);
};

exports.down = function(knex) {
return knex.schema.raw(`
UPDATE "Answers"
SET mandatory = (properties->>'required')::boolean
`);
};
14 changes: 14 additions & 0 deletions migrations/20180622160307_remove_mandatory_from_answers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = function(knex) {
return knex.schema.table("Answers", function(table) {
table.dropColumn("mandatory");
});
};

exports.down = function(knex) {
return knex.schema.table("Answers", function(table) {
table
.boolean("mandatory")
.notNullable()
.defaultsTo(false);
});
};
20 changes: 20 additions & 0 deletions migrations/20180706134843_add_decimals_data_to_properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exports.up = function(knex) {
return knex.schema.raw(`
UPDATE "Answers"
SET properties = json_build_object(
'required', (properties->>'required')::boolean,
'decimals', 0
)::jsonb
WHERE type IN ('Number', 'Currency');
`);
};

exports.down = function(knex) {
return knex.schema.raw(`
UPDATE "Answers"
SET properties = json_build_object(
'required', (properties->>'required')::boolean
)::jsonb
WHERE type IN ('Number', 'Currency');
`);
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
"colors": "^1.1.2",
"cors": "^2.8.3",
"dotenv": "^6.0.0",
"eq-author-graphql-schema": "0.15.1",
"eq-author-graphql-schema": "0.16.0",
"express": "^4.15.3",
"express-pino-logger": "^3.0.1",
"graphql": "^0.13.2",
"graphql-iso-date": "^3.3.0",
"graphql-relay": "^0.5.2",
"graphql-server-express": "^1.3.2",
"graphql-tools": "^3.0.0",
"graphql-type-json": "^0.2.1",
"jest": "^23.0.0",
"knex": "^0.14.6",
"lodash": "^4.17.4",
Expand Down Expand Up @@ -59,7 +60,7 @@
"eslint": "^4.18.0",
"eslint-config-eq-author": "^1.0.2",
"husky": "^0.14.3",
"lint-staged": "^7.0.0",
"lint-staged": "^7.0.5",
"prettier": "^1.5.3",
"sqlite3": "^4.0.0"
}
Expand Down
67 changes: 50 additions & 17 deletions repositories/AnswerRepository.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
const { head, invert, map } = require("lodash/fp");
const {
flow,
head,
isBoolean,
isObject,
invert,
map,
omit
} = require("lodash/fp");
const { get, merge } = require("lodash");
const db = require("../db");
const Answer = require("../db/Answer");
const mapFields = require("../utils/mapFields");
const mapping = { QuestionPageId: "questionPageId" };
const fromDb = mapFields(mapping);
const toDb = mapFields(invert(mapping));
const db = require("../db");

const handleDeprecatedMandatoryFieldFromDb = answer =>
isObject(answer)
? merge({}, answer, { mandatory: get(answer, "properties.required") })
: answer;

const handleDeprecatedMandatoryFieldToDb = answer =>
isBoolean(answer.mandatory)
? merge({}, answer, { properties: { required: answer.mandatory } })
: answer;

const fromDb = flow(mapFields(mapping), handleDeprecatedMandatoryFieldFromDb);

const toDb = flow(
mapFields(invert(mapping)),
handleDeprecatedMandatoryFieldToDb,
omit("mandatory")
);

const {
createOtherAnswer,
deleteOtherAnswer
Expand Down Expand Up @@ -38,6 +64,7 @@ module.exports.insert = function insert({
qCode,
type,
mandatory,
properties,
questionPageId
}) {
return Answer.create(
Expand All @@ -49,6 +76,7 @@ module.exports.insert = function insert({
qCode,
type,
mandatory,
properties,
questionPageId
})
)
Expand All @@ -64,21 +92,26 @@ module.exports.update = function update({
secondaryLabel,
qCode,
type,
mandatory,
isDeleted,
parentAnswerId
parentAnswerId,
mandatory,
properties
}) {
return Answer.update(id, {
description,
guidance,
label,
secondaryLabel,
qCode,
type,
mandatory,
isDeleted,
parentAnswerId
})
return Answer.update(
id,
toDb({
description,
guidance,
label,
secondaryLabel,
qCode,
type,
isDeleted,
parentAnswerId,
mandatory,
properties
})
)
.then(head)
.then(fromDb);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const findOtherAnswer = async (trx, parentAnswerId) =>
const createAnswer = async (trx, parentAnswerId, type) =>
trx("Answers")
.insert({
mandatory: false,
properties: { required: true },
description: "",
type,
parentAnswerId
Expand Down
22 changes: 19 additions & 3 deletions schema/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { GraphQLDate } = require("graphql-iso-date");
const { includes, isNil } = require("lodash");
const { merge, includes, isNil } = require("lodash");
const GraphQLJSON = require("graphql-type-json");
const formatRichText = require("../utils/formatRichText");

const whereIn = (field, values) => {
Expand All @@ -16,6 +17,17 @@ const assertMultipleChoiceAnswer = answer => {
}
};

const getDefaultAnswerProperties = type => {
switch (type) {
case "Currency":
return { required: false, decimals: 0 };
case "Number":
return { required: false, decimals: 0 };
default:
return { required: false };
}
};

const Resolvers = {
Query: {
questionnaires: (_, args, ctx) => ctx.repositories.Questionnaire.findAll(),
Expand Down Expand Up @@ -92,7 +104,9 @@ const Resolvers = {
ctx.repositories.QuestionPage.undelete(args.input.id),

createAnswer: async (root, args, ctx) => {
const answer = await ctx.repositories.Answer.insert(args.input);
const defaultProperties = getDefaultAnswerProperties(args.input.type);
const input = merge({}, args.input, { properties: defaultProperties });
const answer = await ctx.repositories.Answer.insert(input);

if (answer.type === "Checkbox" || answer.type === "Radio") {
const defaultOptions = [];
Expand Down Expand Up @@ -325,6 +339,8 @@ const Resolvers = {
answer: ({ answerId }, args, ctx) => ctx.repositories.Answer.get(answerId)
},

Date: GraphQLDate
Date: GraphQLDate,

JSON: GraphQLJSON
};
module.exports = Resolvers;
1 change: 0 additions & 1 deletion schema/resolvers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const createNewAnswer = async ({ id: pageId }, type) => {
label: `${type} answer`,
qCode: null,
type: `${type}`,
mandatory: false,
questionPageId: pageId
};

Expand Down
10 changes: 8 additions & 2 deletions tests/fixtures/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"description": "On a scale of 1-5",
"guidance": "1 being liking cheese, 5 being loving cheese. There is no option for not liking cheese",
"type": "Integer",
"mandatory": true,
"properties": {
"required": true,
"decimals": 1
},
"questionPageId": 1
},

Expand All @@ -37,7 +40,10 @@
"description": "If you could only eat one cheese for the rest of your life, what would it be?",
"guidance": "Think hard about this, it's a legally binding contract",
"type": "Integer",
"mandatory": true,
"properties": {
"required": true,
"decimals": 0
},
"questionPageId": 1
}
]
Expand Down
8 changes: 3 additions & 5 deletions tests/fixtures/queries.gql
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ mutation CreateAnswer(
$qCode: String
$label: String
$type: AnswerType!
$mandatory: Boolean!
$questionPageId: Int!
) {
createAnswer(
Expand All @@ -185,7 +184,6 @@ mutation CreateAnswer(
qCode: $qCode
label: $label
type: $type
mandatory: $mandatory
questionPageId: $questionPageId
) {
id
Expand All @@ -199,7 +197,7 @@ mutation UpdateAnswer(
$qCode: String
$label: String
$type: AnswerType
$mandatory: Boolean
$properties: JSON
) {
updateAnswer(
id: $id
Expand All @@ -208,10 +206,10 @@ mutation UpdateAnswer(
qCode: $qCode
label: $label
type: $type
mandatory: $mandatory
properties: $properties
) {
id
mandatory
properties
}
}

Expand Down

0 comments on commit 5048f64

Please sign in to comment.