public async Task <long> AddOrganizationAsync(User user, AddOrgFormModel orgForm)
        {
            try
            {
                Organization parent = null;
                if (orgForm.ParentId != null)
                {
                    parent = _context.Organizations.FirstOrDefault(c => c.ParentId == orgForm.ParentId);
                }
                var isDuplicateOrg = _context.Organizations.Any(c => c.OrganizationName == orgForm.OrganizationName);
                if (!isDuplicateOrg)
                {
                    var org = new Organization()
                    {
                        Description      = orgForm.Description,
                        OrganizationName = orgForm.OrganizationName,
                        ParentId         = orgForm.ParentId
                    };
                    await _context.Organizations.AddAsync(org);

                    await _context.SaveChangesAsync();

                    return(org.Id);
                }
                throw new ExperienceManagementGlobalException(OrganizationServiceErrors.AddDuplicateOrganizationError);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(OrganizationServiceErrors.AddOrganizationError, ex);
            }
        }
        public async Task <long> AddEquipmentAsync(User user, AddEquipmentFormModel equipmentForm)
        {
            try
            {
                Equipment parent = null;
                if (equipmentForm.ParentId != null)
                {
                    parent = _context.Equipments.FirstOrDefault(c => c.ParentId == equipmentForm.ParentId);
                }
                var isDuplicateEquipment = _context.Equipments.Any(c => c.EquipmentName == equipmentForm.EquipmentName);
                if (!isDuplicateEquipment)
                {
                    var org = new Equipment()
                    {
                        Description   = equipmentForm.Description,
                        EquipmentName = equipmentForm.EquipmentName,
                        ParentId      = equipmentForm.ParentId
                    };
                    await _context.Equipments.AddAsync(org);

                    await _context.SaveChangesAsync();

                    return(org.Id);
                }
                throw new ExperienceManagementGlobalException(EquipmentServiceErrors.AddDuplicateEquipmentError);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(EquipmentServiceErrors.AddEquipmentError, ex);
            }
        }
Example #3
0
        public async Task DeleteExpiredTokensAsync()
        {
            var now = DateTimeOffset.UtcNow;
            await _tokens.Where(x => x.RefreshTokenExpiresDateTime < now)
            .ForEachAsync(userToken =>
            {
                _tokens.Remove(userToken);
            });

            await _uow.SaveChangesAsync();
        }
Example #4
0
        public async Task <Guid> UploadAsync(User user, IFormFile file)
        {
            try
            {
                if (string.IsNullOrEmpty(file.FileName) || file.Length == 0)
                {
                    throw new ExperienceManagementGlobalException(UploadServiceErrors.UploadFileValidError);
                }
                var extension = file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
                var fileName  = Guid.NewGuid().ToString() + "." + extension;
                var path      = Path.Combine(_siteSettings.Value.UserAttachedFile.PhysicalPath, fileName);

                using (var bits = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(bits);
                }
                var uploadFile = new FileAddress()
                {
                    FilePath     = path,
                    FileType     = extension,
                    UserId       = user.Id,
                    FileSize     = file.Length,
                    CreationDate = DateTime.Now,
                };
                await _context.FileAddresses.AddAsync(uploadFile);

                await _context.SaveChangesAsync();

                return(uploadFile.FileId);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(UploadServiceErrors.UploadFileError, ex);
            }
        }
Example #5
0
        public async Task UpdateUserLastActivityDateAsync(Guid userId)
        {
            var user = await FindUserAsync(userId);

            if (user.LastLoggedIn != null)
            {
                var updateLastActivityDate = TimeSpan.FromMinutes(2);
                var currentUtc             = DateTimeOffset.UtcNow;
                var timeElapsed            = currentUtc.Subtract(user.LastLoggedIn.Value);
                if (timeElapsed < updateLastActivityDate)
                {
                    return;
                }
            }
            user.LastLoggedIn = DateTimeOffset.UtcNow;
            await _context.SaveChangesAsync();
        }
        public async Task <bool> ActivateIssueAsync(User user, ActivateIssueFormModel issueActivate)
        {
            try
            {
                var issue = await _context.Issues.FindAsync(issueActivate.IssueId);

                if (issue is null)
                {
                    throw new ExperienceManagementGlobalException(IssueServiceErrors.IssueNotFoundError);
                }
                issue.IsActive = issueActivate.IsActive;
                _context.Issues.Update(issue);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(IssueServiceErrors.ChangeStateIssueError, ex);
            }
        }
Example #7
0
        public async Task <long> AddGroupAsync(User user, AddGroupFormModel groupForm)
        {
            try
            {
                var sameGroup = await _context.GroupAuths.Where(c => c.Name == groupForm.Name).ToListAsync();

                if (sameGroup.Count > 0)
                {
                    throw new ExperienceManagementGlobalException(GroupServiceErrors.SameGroupExistError);
                }

                var group = new GroupAuth()
                {
                    Name        = groupForm.Name,
                    Description = groupForm.Description
                };
                _context.GroupAuths.Add(group);

                foreach (var gfRole in groupForm.RoleIds)
                {
                    var groupAuthRole = new GroupAuthRole()
                    {
                        RoleId      = gfRole,
                        GroupAuthId = group.Id
                    };
                    _context.GroupAuthRoles.Add(groupAuthRole);
                }
                await _context.SaveChangesAsync();

                return(group.Id);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(GroupServiceErrors.AddGroupError, ex);
            }
        }
Example #8
0
        public async Task <bool> AddOrUpdateReportStructureAsync(User user, UpdateReportStructureFormModel form)
        {
            try
            {
                var resp = await _context.ReportStructures.FirstOrDefaultAsync(c => c.Id == form.ReportStructureModelId || c.ReportId == form.ReportId);

                if (resp is null && form.ReportStructureModelId != 0)
                {
                    throw new ExperienceManagementGlobalException(ReportServiceErrors.ReportStructureNotFoundError);
                }
                if (resp != null)
                {
                    resp.Configuration       = form.Configuration;
                    resp.LastUpdatedDateTime = DateTime.Now;
                    resp.ReportId            = form.ReportId;
                    _context.ReportStructures.Update(resp);
                }
                else
                {
                    var report = new ReportStructure()
                    {
                        Configuration       = form.Configuration,
                        CreationDate        = DateTime.Now,
                        LastUpdatedDateTime = DateTime.Now,
                        ReportId            = form.ReportId
                    };
                    await _context.ReportStructures.AddAsync(report);
                }
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(ReportServiceErrors.UpdateReportStructureError, ex);
            }
        }