"use strict"; angular.module("vocabulary") .service("memberWordService", ["HttpStatus", "MessageBox", "messageHandler", "memberWordApi", "lessonService", "$q", function (HttpStatus, MessageBox, messageHandler, memberWordApi, lessonService, $q) { var wordRegex = /^[-.a-zA-Z\d ]+$/; var generalRegex = /^[-.a-zA-Z\d ?!<>,^&`$%'"*;+:()_[]+$/; var blankRegex = /()/; var blankAndGeneralRegex = /^[-.a-zA-Z\d ?!<>,^&`$%'"*;+:()_[]*()+[-.a-zA-Z\d ?!<>,^&`$%'"*;+:()_[]*$/; function getRegex() { return {word: wordRegex, general: generalRegex, blankAndGeneralRegex: blankAndGeneralRegex}; } function stringContainsBlank(text) { return blankRegex.test(text); } function stringContainsValidCharacters(text) { return generalRegex.test(text); } function getMemberWords() { return memberWordApi.get(); } function getMemberWord(wordId) { return memberWordApi.getById(wordId); } function deleteWord(word) { return confirmDelete() .then(function () { return memberWordApi.deleteWord(word.wordId) .then(function () { lessonService.discardWord(word); var message = "Your word has been deleted."; messageHandler.publishSuccess(message); }) .catch(function () { var message = "There was an error."; messageHandler.publishError(message); }); }); } function updateWord(word) { var wordCopy = angular.copy(word); return memberWordApi.updateWord(formatWord(wordCopy)) .then(function () { lessonService.replaceWordIfChosen(word); var message = "Your word has been saved."; messageHandler.publishSuccess(message) }) .catch(function (response) { var message = response.status === HttpStatus.CONFLICT ? "You have already created a word by this name." : "There was an error."; messageHandler.publishError(message); }); } function createWord(word) { var wordCopy = angular.copy(word); return memberWordApi.createWord(formatWord(wordCopy)) .then(lessonService.toggleChosenWord) .then(function () { var message = "Your word has been created."; messageHandler.publishSuccess(message); }) .catch(function (response) { var message = response.status === HttpStatus.CONFLICT ? "You have already created a word by this name." : "There was an error."; messageHandler.publishError(message); }); } function attemptWordUpdate(word) { return validateWord(word) .then(function() { return updateWord(word); }); } function attemptWordCreation(word) { return validateWord(word) .then(function() { return createWord(word); }); } function confirmDelete() { var message = "Are you sure you want to delete this word?"; return MessageBox.show({ message: message, responses: [{subtle: true, action: false, label: 'Cancel'}, {action: true, label: 'Delete Word'}] }).then(function(response){ if(!response || !response.action){ throw "Delete Aborted"; } }); } function formatWord(word) { var assessment = word.assessmentQuestionsAndAnswers[0]; var correctAnswerIndex = assessment.correctAnswerIndex.toString(); word.partOfSpeech = word.partOfSpeechFull.slice(); assessment.question = assessment.question.slice(); assessment.answers[correctAnswerIndex].isCorrect = true; return word; } function validateWord(word) { var isValid = true; if (!word.wordName || word.wordName.length > 30 || !wordRegex.test(word.wordName)) { isValid = false; } if (!word.partOfSpeechFull) { isValid = false; } if (!word.definition || word.definition.length > 255 || !generalRegex.test(word.definition)) { isValid = false; } if (!word.sentenceStarter || word.sentenceStarter.length > 255 || !generalRegex.test(word.sentenceStarter) || !blankRegex.test(word.sentenceStarter)) { isValid = false; } if (!word.clozeSentence || word.clozeSentence.length > 255 || !generalRegex.test(word.clozeSentence) || !blankRegex.test(word.clozeSentence)) { isValid = false; } if ((!word.contextSentences[0].text || word.contextSentences[0].text.length > 255 || !generalRegex.test(word.contextSentences[0].text)) || (word.contextSentences[1].text && (word.contextSentences[1].text.length > 255 || !generalRegex.test(word.contextSentences[1].text))) || (word.contextSentences[2].text && (word.contextSentences[2].text.length > 255 || !generalRegex.test(word.contextSentences[2].text)))) { isValid = false; } if (!word.assessmentQuestionsAndAnswers[0].question || word.assessmentQuestionsAndAnswers[0].question.length > 100 || !generalRegex.test(word.assessmentQuestionsAndAnswers[0].question) || !blankRegex.test(word.assessmentQuestionsAndAnswers[0].question)) { isValid = false; } if (!word.assessmentQuestionsAndAnswers[0].answers[0].text || word.assessmentQuestionsAndAnswers[0].answers[0].length > 40 || !word.assessmentQuestionsAndAnswers[0].answers[1].text || word.assessmentQuestionsAndAnswers[0].answers[1].length > 40 || !word.assessmentQuestionsAndAnswers[0].answers[2].text || word.assessmentQuestionsAndAnswers[0].answers[2].length > 40) { isValid = false; } if (word.assessmentQuestionsAndAnswers[0].correctAnswerIndex === null) { isValid = false; } if (!isValid) { return $q.reject("INVALID WORD"); } return $q.resolve(); } function isMemberWord(word) { return !word.hasOwnProperty('topicId'); } function wordHasCorrectAnswerSet(word) { return word.assessmentQuestionsAndAnswers[0].correctAnswerIndex != null; } return { stringContainsBlank: stringContainsBlank, stringContainsValidCharacters: stringContainsValidCharacters, getMemberWords: getMemberWords, getMemberWord: getMemberWord, attemptWordUpdate: attemptWordUpdate, attemptWordCreation: attemptWordCreation, deleteWord: deleteWord, getRegex: getRegex, isMemberWord: isMemberWord, wordHasCorrectAnswerSet: wordHasCorrectAnswerSet }; }]);