diff --git a/spec/src/modules/quizzes.js b/spec/src/modules/quizzes.js index c5c4bb69..669ac355 100644 --- a/spec/src/modules/quizzes.js +++ b/spec/src/modules/quizzes.js @@ -61,8 +61,9 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { return quizzes.getQuizNextQuestion(validQuizId, {}).then((res) => { const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); expect(res).to.have.property('next_question').to.be.an('object'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res.next_question.id).to.equal(1); expect(res.next_question.options[0].id).to.equal(1); expect(fetchSpy).to.have.been.called; @@ -84,29 +85,34 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { return quizzes.getQuizNextQuestion(validQuizId, { section }).then((res) => { const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); expect(res).to.have.property('next_question').to.be.an('object'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res.next_question.id).to.equal(1); expect(res.next_question.options[0].id).to.equal(1); expect(requestedUrlParams).to.have.property('section').to.equal(section); }); }); - it('Should return a result provided a valid apiKey, quizId and versionId', () => { - const versionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d'; + it('Should return a result provided a valid apiKey, quizId and quizVersionId, quizSessionId', () => { + const quizVersionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d'; + const quizSessionId = '123;'; const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return quizzes.getQuizNextQuestion(validQuizId, { versionId }).then((res) => { + return quizzes.getQuizNextQuestion(validQuizId, { quizVersionId, quizSessionId }).then((res) => { const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); - expect(res).to.have.property('version_id').to.be.an('string').to.equal(versionId); + expect(res).to.have.property('quiz_version_id').to.be.an('string').to.equal(quizVersionId); expect(res).to.have.property('next_question').to.be.an('object'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res.next_question.id).to.equal(1); expect(res.next_question.options[0].id).to.equal(1); - expect(requestedUrlParams).to.have.property('version_id').to.equal(versionId); + expect(requestedUrlParams).to.have.property('quiz_version_id').to.equal(quizVersionId); + expect(requestedUrlParams).to.have.property('quiz_session_id').to.equal(quizSessionId); + }); }); @@ -121,8 +127,9 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { return quizzes.getQuizNextQuestion(validQuizId, {}).then((res) => { const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); expect(res).to.have.property('next_question').to.be.an('object'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res.next_question.id).to.equal(1); expect(res.next_question.options[0].id).to.equal(1); expect(requestedUrlParams).to.have.property('ui').to.equal(userId); @@ -140,8 +147,9 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { return quizzes.getQuizNextQuestion(validQuizId, {}).then((res) => { const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); expect(res).to.have.property('next_question').to.be.an('object'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res.next_question.id).to.equal(1); expect(res.next_question.options[0].id).to.equal(1); expect(requestedUrlParams).to.have.property('us').to.deep.equal(segments); @@ -155,37 +163,38 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { }); return quizzes.getQuizNextQuestion(validQuizId, { answers: validAnswers }).then((res) => { - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); expect(res).to.have.property('next_question').to.be.an('object'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res.next_question.id).to.equal(4); }); }); - it('Should be rejected if an invalid quizId is provided', () => { + it('Should be rejected if an invalid quizVersionId is provided', () => { const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return expect(quizzes.getQuizNextQuestion('invalidQuizId', {})).to.eventually.be.rejected; + return expect(quizzes.getQuizNextQuestion(validQuizId, { quizVersionId: 'foo' })).to.eventually.be.rejected; }); - it('Should be rejected if no quizId is provided', () => { + it('Should be rejected if an invalid quizId is provided', () => { const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return expect(quizzes.getQuizNextQuestion(null, {})).to.eventually.be.rejected; + return expect(quizzes.getQuizNextQuestion('invalidQuizId', {})).to.eventually.be.rejected; }); - it('Should be rejected if an invalid versionId is provided', () => { + it('Should be rejected if no quizId is provided', () => { const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return expect(quizzes.getQuizNextQuestion(validQuizId, { versionId: 'foo' })).to.eventually.be.rejected; + return expect(quizzes.getQuizNextQuestion(null, {})).to.eventually.be.rejected; }); if (!skipNetworkTimeoutTests) { @@ -231,8 +240,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); - expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; expect(res.response).to.have.property('results').to.be.an('array'); @@ -257,30 +266,35 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; expect(requestedUrlParams).to.have.property('section').to.equal(section); }); }); - it('Should return a result provided a valid apiKey, quizId and versionId', () => { - const versionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d'; + it('Should return a result provided a valid apiKey, quizId, quizVersionId and quizSessionId', () => { + const quizVersionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d'; + const quizSessionId = '12345'; const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return quizzes.getQuizResults(validQuizId, { answers: validAnswers, versionId }).then((res) => { + // eslint-disable-next-line max-len + return quizzes.getQuizResults(validQuizId, { answers: validAnswers, quizVersionId, quizSessionId }).then((res) => { const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string').to.equal(quizVersionId); + expect(res).to.have.property('quiz_session_id').to.be.an('string').to.equal(quizSessionId); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; - expect(requestedUrlParams).to.have.property('version_id').to.equal(versionId); + expect(requestedUrlParams).to.have.property('quiz_version_id').to.equal(quizVersionId); + expect(requestedUrlParams).to.have.property('quiz_session_id').to.equal(quizSessionId); }); }); @@ -297,10 +311,11 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); - expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; + expect(requestedUrlParams).to.have.property('ui').to.equal(userId); }); }); @@ -318,8 +333,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); - expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; expect(requestedUrlParams).to.have.property('us').to.deep.equal(segments); @@ -339,7 +354,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; expect(requestedUrlParams).to.have.property('page').to.equal(page.toString()); @@ -359,7 +375,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; expect(requestedUrlParams).to.have.property('num_results_per_page').to.equal(resultsPerPage.toString()); @@ -379,7 +396,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { expect(res).to.have.property('request').to.be.an('object'); expect(res).to.have.property('response').to.be.an('object'); expect(res).to.have.property('result_id').to.be.an('string'); - expect(res).to.have.property('version_id').to.be.an('string'); + expect(res).to.have.property('quiz_version_id').to.be.an('string'); + expect(res).to.have.property('quiz_session_id').to.be.an('string'); expect(res).to.have.property('quiz_id').to.be.an('string'); expect(fetchSpy).to.have.been.called; expect(requestedUrlParams).to.have.property('filters'); @@ -387,31 +405,31 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => { }); }); - it('Should be rejected if no quizId is provided', () => { + it('Should be rejected if an invalid quizVersionId is provided', () => { const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return expect(quizzes.getQuizResults(null, { answers: validAnswers })).to.eventually.be.rejected; + return expect(quizzes.getQuizResults(validQuizId, { answers: validAnswers, quizVersionId: 'foo' })).to.eventually.be.rejected; }); - it('Should be rejected if an invalid quizId is provided', () => { + it('Should be rejected if no quizId is provided', () => { const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return expect(quizzes.getQuizResults('invalidQuizId', { answers: validAnswers })).to.eventually.be.rejected; + return expect(quizzes.getQuizResults(null, { answers: validAnswers })).to.eventually.be.rejected; }); - it('Should be rejected if an invalid versionId is provided', () => { + it('Should be rejected if an invalid quizId is provided', () => { const { quizzes } = new ConstructorIO({ apiKey: quizApiKey, fetch: fetchSpy, }); - return expect(quizzes.getQuizResults(validQuizId, { answers: validAnswers, versionId: 'foo' })).to.eventually.be.rejected; + return expect(quizzes.getQuizResults('invalidQuizId', { answers: validAnswers })).to.eventually.be.rejected; }); it('Should be rejected if an invalid apiKey is provided', () => { diff --git a/src/modules/quizzes.js b/src/modules/quizzes.js index 8b7a4ed0..d4021968 100644 --- a/src/modules/quizzes.js +++ b/src/modules/quizzes.js @@ -40,16 +40,21 @@ function createQuizUrl(quizId, parameters, options, path) { } if (parameters) { - const { section, answers, versionId, page, resultsPerPage, filters } = parameters; + const { section, answers, quizSessionId, quizVersionId, page, resultsPerPage, filters } = parameters; // Pull section from parameters if (section) { queryParams.section = section; } - // Pull version_id from parameters - if (versionId) { - queryParams.version_id = versionId; + // Pull quiz_version_id from parameters + if (quizVersionId) { + queryParams.quiz_version_id = quizVersionId; + } + + // Pull quiz_session_id from parameters + if (quizSessionId) { + queryParams.quiz_session_id = quizSessionId; } // Pull a (answers) from parameters and transform @@ -102,7 +107,8 @@ class Quizzes { * @param {string} [parameters] - Additional parameters to refine result set * @param {string} [parameters.section] - Product catalog section * @param {array} [parameters.answers] - An array of answers in the format [[1,2],[1]] - * @param {string} [parameters.versionId] - Version identifier for the quiz + * @param {string} [parameters.quizVersionId] - Version identifier for the quiz. Version ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-versioning + * @param {string} [parameters.quizSessionId] - Session identifier for the quiz. Session ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-sessions * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {Promise} @@ -111,7 +117,8 @@ class Quizzes { * constructorio.quizzes.getQuizNextQuestion('quizId', { * answers: [[1,2],[1]], * section: '123', - * versionId: '123' + * quizVersionId: '123', + * quizSessionId: '1234', * }); */ getQuizNextQuestion(id, parameters, networkParameters = {}) { @@ -138,7 +145,7 @@ class Quizzes { return helpers.throwHttpErrorFromResponse(new Error(), response); }) .then((json) => { - if (json.version_id) { + if (json.quiz_version_id) { this.eventDispatcher.queue('quizzes.getQuizNextQuestion.completed', json); return json; @@ -157,7 +164,8 @@ class Quizzes { * @param {string} parameters - Additional parameters to refine result set * @param {array} parameters.answers - An array of answers in the format [[1,2],[1]] * @param {string} [parameters.section] - Product catalog section - * @param {string} [parameters.versionId] - Specific version identifier for the quiz + * @param {string} [parameters.quizVersionId] - Version identifier for the quiz. Version ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-versioning + * @param {string} [parameters.quizSessionId] - Session identifier for the quiz. Session ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-sessions * @param {number} [parameters.page] - The page number of the results * @param {number} [parameters.resultsPerPage] - The number of results per page to return * @param {object} [parameters.filters] - Key / value mapping (dictionary) of filters used to refine results @@ -169,7 +177,8 @@ class Quizzes { * constructorio.quizzes.getQuizResults('quizId', { * answers: [[1,2],[1]], * section: '123', - * versionId: '123' + * quizVersionId: '123', + * quizSessionId: '234' * }); */ getQuizResults(id, parameters, networkParameters = {}) { @@ -196,7 +205,7 @@ class Quizzes { return helpers.throwHttpErrorFromResponse(new Error(), response); }) .then((json) => { - if (json.version_id) { + if (json.quiz_version_id) { this.eventDispatcher.queue('quizzes.getQuizResults.completed', json); return json; diff --git a/src/types/quizzes.d.ts b/src/types/quizzes.d.ts index 011acbdb..af8f85af 100644 --- a/src/types/quizzes.d.ts +++ b/src/types/quizzes.d.ts @@ -17,7 +17,8 @@ export default Quizzes; export interface QuizzesParameters { section?: string; answers?: any[]; - versionId?: string; + quizVersionId?: string; + quizSessionId?: string; } export interface QuizzesResultsParameters extends QuizzesParameters { @@ -50,6 +51,8 @@ export interface NextQuestionResponse extends Record { next_question: Question; is_last_question?: boolean; version_id?: string; + quiz_id?: string; + quiz_session_id?: string; } export interface QuizResultsResponse extends Record { @@ -77,7 +80,7 @@ export interface QuizResultsResponse extends Record { features: Partial[]; }; result_id?: string; - version_id: string; + quiz_version_id: string; quiz_session_id: string; quiz_id: string; }