private static CallNote JTokenToCallNote(JToken callNoteJObject)
        {
            if (callNoteJObject == null)
            {
                return(null);
            }

            var callNote = new CallNote
            {
                NoteTime       = DateTime.ParseExact((string)callNoteJObject["NoteTime"], Constants.DateTimeFormat, null),
                NoteContent    = (string)callNoteJObject["NoteContent"],
                ChildCallNotes = new List <ChildCallNote>()
            };

            var jArray = (JArray)callNoteJObject["ChildCallNotes"];

            foreach (var jToken in jArray)
            {
                var childCallNote = new ChildCallNote
                {
                    NoteTime    = DateTime.ParseExact((string)jToken["NoteTime"], Constants.DateTimeFormat, null),
                    NoteContent = (string)jToken["NoteContent"]
                };
                callNote.ChildCallNotes.Add(childCallNote);
            }

            return(callNote);
        }
        public ActionResult AddChildNotePost(int id, string userName, ChildCallNote childCallNote)
        {
            _logger.Info("[CustomersController::AddChildNote] Adding ChildCallNote. Id: {}, UserName {}", id, userName);

            try
            {
                var note = new ChildCallNote()
                {
                    NoteTime    = DateTime.Now,
                    NoteContent = childCallNote.NoteContent
                };

                try
                {
                    _callNotesService.WriteChildCallNote(userName, note);
                }
                catch (FileNotFoundException)
                {
                    // Create notes file if not found and the user is valid
                    var customer = _customersService.SelectById(id);
                    if (customer != null && string.Equals(customer.UserName, userName))
                    {
                        _callNotesService.CreateCallNotesFile(userName);
                        // add as a call note since there is no call note
                        _callNotesService.WriteCallNote(userName, new CallNote
                        {
                            NoteTime       = DateTime.Now,
                            NoteContent    = childCallNote.NoteContent,
                            ChildCallNotes = new List <ChildCallNote>()
                        });
                    }
                }

                _logger.Info("[CustomersController::AddChildNote] Adding ChildCallNote Successfully. Id: {}, UserName: {}, Note: {}", id, userName, note);
                return(RedirectToAction("NoteDetails", new { id, userName }));
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersController::AddChildNote] Adding ChildCallNote Error. Id: {}, UserName: {}, ChildCallNote: {}", id, userName, childCallNote);
                return(View("Error"));
            }
        }
        public void TestInitialize()
        {
            _customersFilePath = Path.Combine(TestConstants.DataSourcePath, TestConstants.DataSourcePathSegment);
            _customersFileName = TestConstants.CustomerFileName;

            _filePath = Path.Combine(TestConstants.DataSourcePath, TestConstants.DataSourcePathSegment, TestConstants.CallNoteContentFolderName);
            _fileName = "whisper.json";

            _userName            = "******";
            _notExistingUserName = "******";

            _callNote1 = new CallNote
            {
                NoteTime       = DateTime.ParseExact("02/02/1988 21:14:14", Constants.DateTimeFormat, null),
                NoteContent    = "CallNote1",
                ChildCallNotes = new List <ChildCallNote>
                {
                    new ChildCallNote
                    {
                        NoteTime    = DateTime.ParseExact("02/02/1988 23:14:14", Constants.DateTimeFormat, null),
                        NoteContent = "CallNote12"
                    },
                    new ChildCallNote
                    {
                        NoteTime    = DateTime.ParseExact("02/02/1988 23:34:14", Constants.DateTimeFormat, null),
                        NoteContent = "CallNote123"
                    }
                }
            };

            _callNote2 = new CallNote
            {
                NoteTime       = DateTime.ParseExact("02/02/2011 21:14:14", Constants.DateTimeFormat, null),
                NoteContent    = "CallNote2",
                ChildCallNotes = null
            };

            _childCallNote1 = new ChildCallNote
            {
                NoteTime    = DateTime.ParseExact("02/02/1977 23:14:14", Constants.DateTimeFormat, null),
                NoteContent = "ChildNote12"
            };

            _childCallNote2 = new ChildCallNote
            {
                NoteTime    = DateTime.ParseExact("02/02/1976 23:22:14", Constants.DateTimeFormat, null),
                NoteContent = "ChildNote22"
            };

            _callNotes = new List <CallNote>
            {
                new CallNote
                {
                    NoteTime       = DateTime.ParseExact("02/02/1999 21:14:14", Constants.DateTimeFormat, null),
                    NoteContent    = "Note1",
                    ChildCallNotes = new List <ChildCallNote>
                    {
                        new ChildCallNote
                        {
                            NoteTime    = DateTime.ParseExact("02/02/1999 23:14:14", Constants.DateTimeFormat, null),
                            NoteContent = "Note12"
                        },
                        new ChildCallNote
                        {
                            NoteTime    = DateTime.ParseExact("02/02/1999 23:34:14", Constants.DateTimeFormat, null),
                            NoteContent = "Note123"
                        }
                    }
                },
                new CallNote
                {
                    NoteTime       = DateTime.ParseExact("02/02/2000 21:14:14", Constants.DateTimeFormat, null),
                    NoteContent    = "Note2",
                    ChildCallNotes = new List <ChildCallNote>
                    {
                        new ChildCallNote
                        {
                            NoteTime    = DateTime.ParseExact("02/02/2000 23:14:14", Constants.DateTimeFormat, null),
                            NoteContent = "Note22"
                        },
                        new ChildCallNote
                        {
                            NoteTime    = DateTime.ParseExact("02/02/2000 23:34:14", Constants.DateTimeFormat, null),
                            NoteContent = "Note223"
                        }
                    }
                }
            };

            _jObject = JObject.Parse(@"{
                                          'Customers': [
                                            {
                                              'Id': 1,
                                              'UserName': '******',
                                              'FirstName': 'sdfdsf',
                                              'LastName': 'sdfsdfsd',
                                              'PhoneNumber': '0444444444',
                                              'DateOfBirth': '09/11/2016',
                                              'CallNoteName': 'whisper.json'
                                            },
                                            {
                                              'Id': 2,
                                              'UserName': '******',
                                              'FirstName': 'AAA',
                                              'LastName': 'BBBCD',
                                              'PhoneNumber': '0444321321',
                                              'DateOfBirth': '01/01/2000',
                                              'CallNoteName': 'wing.json'
                                            },
                                            {
                                              'Id': 3,
                                              'UserName': '******',
                                              'FirstName': 'CC',
                                              'LastName': 'BC',
                                              'PhoneNumber': '0422159753',
                                              'DateOfBirth': '01/01/1995',
                                              'CallNoteName': 'water.json'
                                            }
                                          ]
                                        }");

            _callNotesService = new CallNotesService(_customersFilePath, _customersFileName);

            Directory.CreateDirectory(_filePath);

            File.Delete(Path.Combine(_customersFilePath, _customersFileName));
            // Create customers file for the use of CallNotesService
            CreateAndWriteCustomersFile();

            // Delete current call note json file if exist
            File.Delete(Path.Combine(_filePath, _fileName));

            // Write the call notes to file before doing next operation
            WriteCallNotesToFile(_filePath, _fileName, _callNotes);
        }
        public void WriteChildCallNote(string userName, ChildCallNote childCallNote)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug(
                    "[CallNotesService::WriteChildCallNotes] Starting writing ChildCallNote. UserName: {}, ChildCallNote: {}",
                    userName, childCallNote);
            }

            try
            {
                if (userName == null)
                {
                    throw new ArgumentNullException(nameof(userName), "User Name can not be null");
                }

                if (childCallNote == null)
                {
                    throw new ArgumentNullException(nameof(childCallNote), "childCallNote can not be null");
                }

                var customerEntity = _customersRepository.SelectByUserName(userName);
                if (customerEntity == null)
                {
                    throw new BusinessLogicException(
                              $"[CallNotesService::WriteChildCallNotes] Can not find this User by userName. UserName: {userName}");
                }

                var fileName  = customerEntity.CallNoteName;
                var callNotes = CallNoteFileHelper.ReadCallNotes(_callNotesFilePath, fileName);

                if (callNotes.Count == 0)
                {
                    throw new ArgumentException("Can not add childCallNote without CallNote.", nameof(childCallNote));
                }

                var lastCallNote   = callNotes[callNotes.Count - 1];
                var childCallNotes = lastCallNote.ChildCallNotes ?? new List <ChildCallNote>();
                childCallNotes.Add(childCallNote);

                CallNoteFileHelper.WriteCallNotes(_callNotesFilePath, fileName, callNotes);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CallNotesService::WriteChildCallNotes] Writing ChildCallNote successfully. UserName: {}, ChildCallNote: {}",
                        userName, childCallNote);
                }
            }
            catch (FileNotFoundException e)
            {
                _logger.Error(e, "[CallNotesService::WriteChildCallNotes] Can not find file. UserName: {}",
                              userName);
                throw new FileNotFoundException("[CallNotesService::WriteChildCallNotes] Can not find file. UserName: {}", e);
            }
            catch (Exception e)
            {
                _logger.Error(e,
                              "[CallNotesService::WriteChildCallNotes] Writing ChildCallNote failed. UserName: {}, ChildCallNote: {}",
                              userName, childCallNote);
                throw new BusinessLogicException(
                          "[CallNotesService::WriteChildCallNotes] Writing ChildCallNote failed.", e);
            }
        }