public async Task <JsonResult> FetchData(ReqData req)
        {
            var appModule = await _appModuleRepository.Get(req.AppId);

            var docTypeService = new DocumentTypeServices(_appModuleRepository);

            ObjectId documentTypeId;

            ObjectId.TryParse(req.DocumentTypeId, out documentTypeId);

            var documentName = await docTypeService.FindDocumentTypeName(req.AppId, documentTypeId);

            var rootDocumentName = documentName;
            var org = await _organisationRepository.Get(appModule.OrganisationId);

            var dataService = new DataService(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString, org.Id.ToString(), documentName);
            //var data = await dataService.Get(dataId, "");

            BsonDocument data;

            if (!string.IsNullOrEmpty(req.SubDocumentTypeId))
            {
                ObjectId subDocumentTypeId;
                ObjectId.TryParse(req.SubDocumentTypeId, out subDocumentTypeId);
                documentName = await docTypeService.FindDocumentTypeName(req.AppId, subDocumentTypeId);

                var subDocumentHierarchy = await docTypeService.FindSubDocumentHierarchy(req.AppId, documentTypeId, documentName, rootDocumentName);

                data = await dataService.Get(req.DataId, subDocumentHierarchy);
            }
            else
            {
                data = await dataService.Get(req.DataId);
            }

            var jsonWriterSettings = new JsonWriterSettings {
                OutputMode = JsonOutputMode.Strict
            };

            var result = new JsonGenericResult
            {
                IsSuccess = true,
                Result    = data.ToJson(jsonWriterSettings)
            };

            return(Json(result));
        }
        public async Task <JsonResult> Insert(ReqAddData req)
        {
            try
            {
                if (!string.IsNullOrEmpty(req.appId))
                {
                    var appModule = await _appModuleRepository.Get(req.appId);

                    if (appModule != null)
                    {
                        if (appModule.Forms == null)
                        {
                            appModule.Forms = new List <ModuleForm>();
                        }

                        ObjectId formId;
                        if (ObjectId.TryParse(req.foreignId, out formId))
                        {
                            var form   = appModule.Forms.FirstOrDefault(n => n.Id == formId);
                            var result = new JsonGenericResult
                            {
                                IsSuccess = true,
                                Result    = form
                            };

                            var org = await _organisationRepository.Get(appModule.OrganisationId);

                            var docTypeService = new DocumentTypeServices(_appModuleRepository);
                            var documentName   = await docTypeService.FindDocumentTypeName(req.appId, form.DocumentTypeId);

                            var parentDocumentName = documentName == req.ParentDocumentName ? "" : req.ParentDocumentName;


                            var dataService = new DataService(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString, org.Id.ToString(), documentName);
                            var serializer  = new JavaScriptSerializer();

                            if (form.SubDocumentTypeId != null && form.SubDocumentTypeId != ObjectId.Empty)
                            {
                                var subDocumentName = await docTypeService.FindDocumentTypeName(req.appId, form.SubDocumentTypeId.Value);

                                var rootDocumentName = await docTypeService.FindDocumentTypeName(req.appId, form.DocumentTypeId);

                                var parentHeirarchy = await docTypeService.FindSubDocumentHierarchy(req.appId, form.DocumentTypeId, subDocumentName, rootDocumentName);

                                parentHeirarchy = parentHeirarchy.Replace(subDocumentName, "");
                                if (parentHeirarchy.Length > 1)
                                {
                                    if (parentHeirarchy.Substring(parentHeirarchy.Length - 1, 1) == ".")
                                    {
                                        parentHeirarchy = parentHeirarchy.Substring(0, parentHeirarchy.Length - 1);
                                    }
                                }

                                //await dataService.Add(req.data, req.RootDataId, subDocumentName, parentDocumentName);
                                await dataService.Add(req.data, req.RootDataId, subDocumentName, parentHeirarchy);
                            }
                            else
                            {
                                dataService.Add(req.data);
                            }

                            return(Json(result));
                        }
                    }
                }
                var ErrorResult = new JsonGenericResult
                {
                    IsSuccess = false,
                    Message   = "No app selected."
                };
                return(Json(ErrorResult));
            }
            catch (Exception ex)
            {
                return(Json(new JsonGenericResult
                {
                    IsSuccess = false,
                    Message = ex.Message
                }));
            }
        }